Associative arrays in HOC

When Python is the interpreter, what is a good
design for the interface to the basic NEURON
concepts.

Moderator: hines

Post Reply
vogdb
Posts: 37
Joined: Sun Aug 13, 2017 9:51 am

Associative arrays in HOC

Post by vogdb »

In Python we have dicts like this

Code: Select all

d = {'key1': 1, 'key2': 56}
Is there an analogue in HOC?
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Associative arrays in HOC

Post by ted »

Nope, hoc doesn't have dicts. You might be able to fake it with a pair of Lists.
vogdb
Posts: 37
Joined: Sun Aug 13, 2017 9:51 am

Re: Associative arrays in HOC

Post by vogdb »

There is an implementation of Dictionary in this post viewtopic.php?t=1438
vogdb
Posts: 37
Joined: Sun Aug 13, 2017 9:51 am

Re: Associative arrays in HOC

Post by vogdb »

Also here a couple of examples:

Code: Select all

begintemplate String
   public s
   strdef s
endtemplate String

begintemplate ObjDict
  public get, put, setKeys
  objref keyList, valueList, nil

  proc init() {
    keyList = new List()
    valueList = new List()
  }

  proc put() {localobj key
    key = new String()
    key.s = $s1
    keyList.append(key)
    valueList.append($o2)
  }

  obfunc get() {local i localobj key
    for i=0, keyList.count()-1 {
      if( strcmp( keyList.o(i).s, $s1 ) == 0 ) {
        return valueList.o(i)
      }
    }
    return nil
  }

endtemplate ObjDict

begintemplate NumDict
  public get, put, setKeys
  objref keyList, valueList

  proc init() {
    keyList = new List()
    valueList = new Vector()
  }

  proc put() {localobj key
    key = new String()
    key.s = $s1
    keyList.append(key)
    valueList.append($2)
  }

  func get() {local i localobj key
    for i=0, keyList.count()-1 {
      if( strcmp( keyList.o(i).s, $s1 ) == 0 ) {
        return valueList.get(i)
      }
    }
  }

endtemplate NumDict

objref od, nd, vv, vvv
od = new ObjDict()
vv = new Vector()
vv.insrt(0,6,3,4)
od.put("hello", vv)
vvv = od.get("hello")
print vvv.size()

nd = new NumDict()
nd.put("hello", 5)
print nd.get("hello")

ramcdougal
Posts: 267
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Associative arrays in HOC

Post by ramcdougal »

As long as Python is installed, you can use Python dictionaries from inside HOC:

Code: Select all

oc>objref py, d
oc>py = new PythonObject()
oc>d = py.dict()
oc>d.__setitem__("axon", 100)
	NULLobject 
oc>d.__setitem__("soma", 10)
	NULLobject 
oc>d.get("soma")
	10 
oc>d.get("axon")
	100 
ted
Site Admin
Posts: 6289
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Associative arrays in HOC

Post by ted »

As long as Python is installed, you can use Python dictionaries from inside HOC
This looks like the way to go--avoids trying to use hoc to replicate existing Python functionality.
Post Reply