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: 35
Joined: Sun Aug 13, 2017 9:51 am

Associative arrays in HOC

Post by vogdb » Fri Dec 13, 2019 5:36 pm

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: 5704
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Associative arrays in HOC

Post by ted » Mon Dec 16, 2019 11:30 am

Nope, hoc doesn't have dicts. You might be able to fake it with a pair of Lists.

vogdb
Posts: 35
Joined: Sun Aug 13, 2017 9:51 am

Re: Associative arrays in HOC

Post by vogdb » Wed Dec 18, 2019 5:52 am

There is an implementation of Dictionary in this post viewtopic.php?t=1438

vogdb
Posts: 35
Joined: Sun Aug 13, 2017 9:51 am

Re: Associative arrays in HOC

Post by vogdb » Wed Dec 18, 2019 7:20 am

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: 197
Joined: Fri Nov 28, 2008 3:38 pm
Location: Yale School of Public Health

Re: Associative arrays in HOC

Post by ramcdougal » Wed Jan 01, 2020 2:13 pm

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: 5704
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Associative arrays in HOC

Post by ted » Wed Jan 01, 2020 7:48 pm

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