// groucho_gapbld.hoc
/*
*****************************this is one big comment ***************************
from            SUBROUTINE GROUCHO_gapbld (thisno, numcells, numgj,
     &       gjtable, allowedcomps, num_allowedcomps, display)
c       Construct a gap-junction network for groucho.f
$1 thisno double
$2 numcells = number of cells in population, e.g. number of tufted IB cells
$3 numgj = total number of gj to be formed in this population
// this matrix is returned: gjtable = table of gj's: each row is a gj.  
     Entries are: cell A,
c    compartment on cell A; cell B, compartment on cell B
$o4 c allowedcomps = a list of compartments where gj allowed to form
$5 num_allowedcomps = number of compartments in a cell on which a gj 
c    might form.
$6 display is an integer flag.  If display = 1, print gjtable

        INTEGER thisno, numcells, numgj, gjtable(numgj,4),
     &    num_allowedcomps, allowedcomps(num_allowedcomps)
        INTEGER i,j,k,l,m,n,o,p, ictr /0/
c ictr keeps track of how many gj have been "built"
        INTEGER display

        double precision seed, x(2), y(2)

Note: this function is for gap junctions that form between a cells that are
members of a population of a single cell type
*****************************this is one big comment ***************************
*/
objref gjtable, x, y, allowedcomps
obfunc groucho_gapbld() {localobj used
// see above note for arguments $1,$2,$3,$o4,$5
// print "arrived"
	thisno = $1
	numcells = $2
	numgj = $3
	allowedcomps = $o4
	num_allowedcomps = $5
	display = $6

	seed = new Vector()
	seed.append(137.e0)

	objref gjtable
	gjtable = new Matrix(numgj+1, 4+1) // fortran notation indicies start at 1

	ictr = 0
	k = 2
	not_unique = 0 // make global so not local in loops
        used = new Matrix(numcells+1, numcells+1, 2) // sparse

// 2
// print "starting loop"
	while (ictr < numgj) {
 //         print "ictr = ",ictr
	  not_unique = 1 // 1 is true, 0 is false
	    while (not_unique) {
		x = durand (seed, k, x)
// This defines a candidate cell pair
		y = durand (seed, k, y)
// This defines a candidate pair of compartments

		i = int ( x.x[0] * numcells ) + 1
		j = int ( x.x[1] * numcells ) + 1
//		print "i,j: ",i,", ",j
// no longer required		if (i.eq.0) i = 1
// no longer required		if (i.gt.numcells) i = numcells
// no longer required		if (j.eq.0) j = 1
// no longer required		if (j.gt.numcells) j = numcells

// Is the unordered cell pair (i,j) in the list so far?
// not necessary to be this efficient  if (ictr.eq.0) goto 1

 		not_unique = 0
 if (0) {
		for eL = 1, ictr {
//		  print "compare i,j with eL = ",eL, " : ",gjtable.x(eL,1),", ",gjtable.x(eL,3)
		  if ((gjtable.x(eL,1) == i) && (gjtable.x(eL,3) == j)) { not_unique = 1 }
 		  if ((gjtable.x(eL,1) == j) && (gjtable.x(eL,3) == i)) { not_unique = 1 }
		}
//		print " at end of loop not_unique = ",not_unique
  }else{
                if (used.getval(i, j) || used.getval(j, i)){
                        not_unique = 1
                }else{
                        used.x[i][j] = 1
                }
		if (one_tenth_ncell) {
			not_unique = 0
		}
  }
	    } // while replaces if (not_unique.eq.1) goto 2

// Proceed with construction
// 1
	  ictr = ictr + 1
          m = int ( y.x[0] * num_allowedcomps ) + 1
          n = int ( y.x[1] * num_allowedcomps ) + 1
//	print "assigning quantities: ", i, ", ", j, ", ", allowedcomps.x[m], ", ",allowedcomps.x[n]

         gjtable.x(ictr,1) = i
         gjtable.x(ictr,3) = j
         gjtable.x(ictr,2) = allowedcomps.x (m)
         gjtable.x(ictr,4) = allowedcomps.x (n)
	}
//            if (ictr.lt.numgj) goto 2

// Possibly print out gjtable when done.
       if ((display == 1) && (thisno == 0)) {
        print " GJTABLE "
        for i = 1, numgj {
        printf("%6d %6d %6d %6d",gjtable.x(i,1), gjtable.x(i,2), \
                gjtable.x(i,3), gjtable.x(i,4))
        }
       }
	return gjtable
}