union

Accession:49838
Union allows storage of a double, string or object <br/> It is useful as a localobj in an obfunc since it will permit returning anything <br/> It also makes it easy to pick up any kind of value out of a list, vector or array <br/> USAGE: XO=new Union(val) where val can be a number string or object <ul> <li> XO=new Union(list,index) will pick up a string or object from list <li> XO=new Union(vec,index) will pick up a number from vector <li> XO=new Union(&dbl,index) will pick up a number from double array </ul>
Tool Information (Click on a link to find other Tools with that property)
Tool Type: Utility;
Simulation Environment: NEURON;
\
union
readme.txt
mosinit.hoc
union.hoc
                            
// $Id: union.hoc,v 1.2 2005/01/29 18:23:10 billl Exp $
// Union allows storage of a double, string or object
// It is useful as a localobj in an obfunc since it will permit returning anything
// It also makes it easy to pick up any kind of value out of a list, vector or array

load_file("stdgui.hoc")

objref sfunc
sfunc = hoc_sf_

// template Union
// USAGE: XO=new Union(val) where val can be a number string or object
//        XO=new Union(list,index) will pick up a string or object from list
//        XO=new Union(vec,index)  will pick up a number from vector
//        XO=new Union(&dbl,index) will pick up a number from double array
begintemplate Union
public ty,x,s,o
strdef s
objref o
external sfunc,tstr
// 1arg -- store a num, string or obj
// 2arg -- pick up type out of list
proc init() { local ii
  x=0 ty=-1
  if (numarg() == 1) {
    ty = argtype(1)
    if (ty==0) { x=$1
    } else if (ty==2) { s=$s1 
    } else if (ty==1) { o=$o1 
    } else printf("Union ERR: argtype not recognized %d\n",ty)
  } else if (numarg()==2) {
    ii=$2
    if (argtype(1)==1) {
      if (isobj($o1,"Vector")) { 
        if (ii<0 || ii>=$o1.size) {
          printf("Union ERR: vec index (%d) out of bounds for %s\n",ii,$o1)
          return }
        ty=0 x=$o1.x[ii] o=$o1
      } else if (isobj($o1,"List")) {
        if (ii<0 || ii>=$o1.count) {
          printf("Union ERR: list index (%d) out of bounds for %s\n",ii,$o1)
          return }
        if (isobj($o1.object(ii),"String")) { // will handle String2
          ty=2 s=$o1.object(ii).s o=$o1.object(ii)
        } else { ty=1 o=$o1.object(ii) }
      }
    } else if (argtype(1)==3) {
      ty=0 x=$&1[ii] // could check - but no + bound checking possible
    }
  }
}

func isobj () {
  sprint(tstr,"%s",$o1)
  if (sfunc.substr(tstr,$s2)==0) return 1 else return 0
}
endtemplate Union

// testing
double x[10]
objref tmplist,vec,XO
tmplist=new List()
vec=new Vector()
vec.indgen(7,100,3)
for ii=0,5 tmplist.append(new String("hello"))
tmplist.insrt(3,vec)
// tmplist.insrt(3,new String2("a","b"))
for ii=0,9 x[ii]=1.3+ii

proc pr () { printf("TYPE: %d NUM:%5g STR: %-8s OBJ: %s\n",$o1.ty,$o1.x,$o1.s,$o1.o) }

// with 1 arg
{XO=new Union(5) pr(XO) }
{XO=new Union("hello") pr(XO) }
{XO=new Union(vec) pr(XO) }
// with 2 args
{XO=new Union(tmplist,3) pr(XO) }
{XO=new Union(tmplist,1) pr(XO) }
{XO=new Union(vec,7) pr(XO) }
{XO=new Union(&x,7) pr(XO) }