File: celset.hoc

package info (click to toggle)
neuron 7.6.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 48,268 kB
  • sloc: cpp: 192,952; ansic: 145,860; python: 42,092; sh: 10,507; makefile: 6,816; yacc: 3,259; java: 995; lex: 457; csh: 108; pascal: 37; sed: 5
file content (845 lines) | stat: -rwxr-xr-x 19,402 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
begintemplate SNList // Like a Section List
public name, name_, list, differ, name_differ, add, del_all, ml, geo, geobox
public domainparms, is_subset
public export, sectionlist
objref list, this, ml, geo, geobox, domainparms, sectionlist
strdef name_, tstr
proc init() {
	name_ = $s1
	parm_ = .5
	list = new List()
	ml = new List()
	geo = new List()
	differ = 0
	name_differ = 0
}
func is_subset() { return 1 }
proc add() {
	if ($o1.sets.index(this) == -1) { // only add if not already in list
		list.append($o1)
		$o1.sets.append(this)
	}else{
		printf("Section already present in subset.\n")
	}
}
proc del_all() { local i, j
	for i=0, list.count-1 {
		j = list.object(i).sets.index(this)
		list.object(i).sets.remove(j)
	}
	list.remove_all()
}
proc name() {
	$s1 = name_
}

proc export() {local i
	sprint(tstr, "objref %s", name_)
//print tstr
	execute(tstr)
	sprint(tstr, "%s = new SectionList()", name_)
//print tstr
	execute(tstr)
	if (object_id(domainparms)) {
		sprint(tstr, "%s.sectionlist = %s", this, name_)
		execute(tstr)
	}
	for i=0, list.count-1 {
		list.object(i).name(tstr)
		sprint(tstr, "%s %s.append()", tstr, name_)
//print tstr
		execute(tstr)
	}
}

endtemplate SNList

// subset domain info display list item is either a subset or a 
// subset domain info object
begintemplate SDIDispListItem
public name, item
objref item
proc init() {
	item = $o1
}
proc name() {
	item.name($s1)
}
endtemplate SDIDispListItem

begintemplate SectionSubsets
public topol, hbox, g, update, snlist, save_data, showsel, bild, pr
public consist, esub, cexport, dpi, have_domainparm, xmlwrite
public domain_iter, delsdi, neuroml
objref g, this, snlist, tobj, tobj1, tobj2, bild, smap, imap
objref hb1, hb2, vb1, vb2, vb3, dk1, dk2
objref sdidisplist, nil, dpi
strdef tstr, tstr1
proc init() {
	sdidisp_working_ = 0
	parspecpage_=0
	showcel_active = 1
	esub = 0
	first = 1
	i=0
	sdidisplist = new List()
	snlist = new List()
	oldsel = -1
	bild = $o1
	snlist.append(bild.all)
	g = bild.gtopol
	panel()
	dk1.map()
	dk1.flip_to(0)
}

func have_domainparm() {local i
	for i = 0, snlist.count-1 {
		if (object_id(snlist.object(i).domainparms) != 0) {
			return 1
		}
	}
	return 0
}

iterator domain_iter() {local i, j
	for i=0, snlist.count-1 {
		if (object_id(snlist.object(i).domainparms)) {
			for j=0, snlist.object(i).domainparms.count-1 {
				$o1 = snlist.object(i).domainparms.object(j)
				iterator_statement
			}
		}
	}
}

proc cexport() {local i
	esub = $1
	if (bild.continuous == 0) {
		esub = 0
	}
	if (esub) {
		for i=0, snlist.count - 1 {
			snlist.object(i).export()
		}
	}
}

proc consist() {
	for i=0, snlist.count-1 {
		tobj = snlist.object(i).list
		tobj.remove_all()
	}
	for i=0, bild.topol.slist.count-1 {
		tobj = bild.topol.slist.object(i)
		for j=0, tobj.sets.count-1 {
			tobj.sets.object(j).list.append(tobj)
		}
	}
}
proc update() {local i, j
	showsel_active = 1
	consist()
	bild.topol.sel_all(0)
	snlist.select(oldsel)
	if (snlist.count > 0 && snlist.selected < 0) {
		snlist.select(0)
	}
	showsel1(snlist.selected)
	bild.topol.draw(g)
	name_differ()
	if (first) {
		first = 0
	}
	g.exec_menu("Crosshair")
	subselpage(parspecpage_)
	cexport(esub)
}

proc hints() {
	if (parspecpage_ == 0) {
xpanel("CellBuild Subsets hints")
xlabel("Usage Hints:")
xlabel("FIRST make a selection in the left Graph subwindow THEN press an action!")
xlabel("  That is, start by selecting some sections and then pressing")
xlabel("  the \"New SectionList\" button")
xlabel("")
xlabel("If the selection in this panel differs from the selected subset in the")
xlabel("  middle panel, that fact is indicated next to the subset name")
xlabel("")
xlabel("In the Graph panel:")
xlabel("  Add to selection by dragging mouse.")
xlabel("  Add to selection by holding down shift key while pressing mouse.")
xlabel("  Mouse press selects nearest section, not nearest name.")
xlabel("")
xlabel("If a section occurs in more than one section list then")
xlabel("  the order of the list of section lists matters! They should")
xlabel("  generally be ordered from general to specific. This determines")
xlabel("  the order of execution of the Geometry and Membrane specification.")
xlabel("")
xlabel("Biophysical parameters will later be specified as constants over a subset.")
xlabel("  Note: SubsetDomainIterators can be declared over the subset to allow")
xlabel("  inhomogenous parameters. (See \"Hints\" in  \"Parameterized Domain Page\".)")
xlabel("")
xpanel(0)
	}else{
xpanel("CellBuild Parameterized Domain Subsets hints")
xlabel("A \"new SubsetDomainIterator(subset)\" helps specify inhomogeneous")
xlabel("\"Biophysics\" parameters by specifying a position function, p,")
xlabel("which essentially carries out the idiom:")
xlabel("    forsec subset for (x, 0) { rangevar_suffix(x) = f(p(x)) }")
xlabel("(f will be specified in the \"Biophysics\" page).")
xlabel("The default position function is arc path distance from the")
		bild.topol.slist.object(0).name(tstr)
sprint(tstr, "root section which presently has the name, \"%s\".", tstr)
xlabel(tstr)
xlabel("")
xlabel("Note: For efficiency and flexibility the implementation of the")
xlabel("  position function is enclosed in a class with an iterator")
xlabel("  so that the domain iteration idiom is actually:")
xlabel("     for subset_x.loop(&x, &p) { rangevar_suffix(x) = f(p) }")
xlabel("  where: subset_x = new SubsetDomainIterator(subset)")
xlabel("  After the cell has been created this domain iterator may be used")
xlabel("  with any user defined body, e.g:")
xlabel("     for subset_x.loop() {")
xlabel("        printf(\"%s x=%g p=%g\\n\", secname(), subset_x.x, subset_x.p)")
xlabel("     }")
xlabel("")
//xbutton("More Hints", "hints3()")
xpanel(0)
	}
}

proc hints3() {
xpanel("CellBuild Parameterized Domain Type hints")
xlabel("")
xlabel("")
xlabel("")
xlabel("")
xlabel("")
xlabel("")
xlabel("")
xlabel("")
xlabel("")
xlabel("")
xpanel(0)
}

proc panel() {
	dk1 = new Deck()
	dk1.intercept(1)

	hb1 = new HBox(3)
	hb1.intercept(1)

	vb1 = new VBox(3)
	vb1.intercept(1)
	snlist.browser("",tstr, "list_label(hoc_ac_)")
	snlist.select_action("showsel1(hoc_ac_)")
	snlist.accept_action("showsel1(hoc_ac_)")
	xpanel("")
	xbutton("Hints", "hints()")
	xpanel()
	vb1.intercept(0)
	vb1.map()
	
	xpanel("")
	xlabel("First, select,")
	xmenu("Select")  
		xbutton("All", "sel_all(1)")
		xbutton("None", "sel_all(0)")   
		xbutton("Xor", "sel_xor(1)")  
		xmenu("Union", "op_menu(1)")
		xmenu("Intersection", "op_menu(2)")
		xmenu("Subtract", "op_menu(3)")
	xmenu()
	g.menu_tool("Select One", "sel1")
	g.menu_tool("Select Subtree", "sel2")
	g.menu_tool("Select Basename", "sel3")
	xlabel("then, act.")
	xbutton("New SectionList", "newslist()")
	xbutton("Selection->SecList", "sel2slist()")
	xbutton("Delete SecList", "delslist() oldsel = -1")
	xbutton("Change Name", "chslist()")
	xbutton("Move up", "move(-1)")
	xbutton("Move down", "move(1)")
	xbutton("Parameterized Domain Page", "subselpage(1)")
	xpanel()
	hb1.intercept(0)
	hb1.map()

	hb2 = new HBox(3)
	hb2.intercept(1)

	vb2 = new VBox(3)
	vb2.intercept(1)
	sdidisplist.browser("",tstr, "sdidisplist_label(hoc_ac_)")
	sdidisplist.select_action("sdidispsel1(hoc_ac_)")
	sdidisplist.accept_action("sdidispsel1(hoc_ac_)")
	xpanel("")
	xbutton("Hints", "hints()")
	xpanel()
	vb2.intercept(0)
	vb2.map()
	
	vb3 = new VBox(3)
	vb3.intercept(1)
	xpanel("")
	xlabel("Parameterized Domain Specification")
	xbutton("Return to Subset Selection Page", "subselpage(0)")
	xpanel()
	dk2 = new Deck()
	dk2.intercept(1)
	xpanel("")
	xbutton("Create a SubsetDomainIterator", "newsdi()")
	xpanel()
	xpanel("")
	xlabel("this is temporary")
	xpanel()
	dk2.intercept(0)
	dk2.map()
	vb3.intercept(0)
	vb3.map()

	hb2.intercept(0)
	hb2.map()
	dk1.intercept(0)
}

proc subselpage() {local i, j, i1, i2
	parspecpage_ = $1
	if ($1 == 1) {
		g.exec_menu("Crosshair")
		i1 = snlist.selected
		dk1.flip_to($1)
		doNotify()
		i2 = -1
		sdidisplist.remove_all()
		for i=0, snlist.count-1 {
			tobj = snlist.object(i)
			sdidisplist.append(new SDIDispListItem(tobj))
			if (object_id(tobj.domainparms)) for j=0, tobj.domainparms.count-1 {
				sdidisplist.append(new SDIDispListItem(tobj.domainparms.object(j)))
			}
			if (i1 == i) {
				i2 = sdidisplist.count-1
			}
		}
		sdidisplist.select(i2)
		if (i2 >= 0) {
			dpi = sdidisplist.object(i2).item
			if (dpi.is_subset) {
				dk2.flip_to(0)
				g.exec_menu("Crosshair")
				dpi = nil
			}else{
				dpi.flip(dk2)
				dpi.selih = nil
			}
		}else{
			dk2.flip_to(-1)
			g.exec_menu("Crosshair")
		}
	}else{
		dk1.flip_to($1)
		g.exec_menu("Crosshair")
	}
}

proc op_menu() {local i
	for i=0, snlist.count-1 {
		sprint(tstr, "opsel(%d, %d)", $1, i)
		xbutton(snlist.object(i).name_, tstr)
	}
}

proc opsel() {local i
	tobj = snlist.object($2)
	for i=0, tobj.list.count-1 {
		tobj1 = tobj.list.object(i)
		if ($1 == 1) { //union
			tobj1.selected = 1
		}else if ($1 == 2) { // intersection
			tobj1.selected += 1 // and look for the 2's below
		}else if ($1 == 3) { // difference
			tobj1.selected = 0
		}
	}
	if ($1 == 2) { // finish up the intersection
		tobj = bild.topol.slist
		for i=0, tobj.count-1 {
			tobj1 = tobj.object(i)
			if (tobj1.selected == 2) {
				tobj1.selected = 1
			}else{
				tobj1.selected = 0
			}
		}
	}
	bild.topol.draw(g)
	differs(snlist.selected)
}

proc move() {local i, j
	i = snlist.selected()
	bild.changed_ = 1
	bild.geom_cnt_ += 1
	if (i > -1) {
		j = i + $1
		if (j > -1 && j < snlist.count) {
			tobj1 = snlist.object(i)
			snlist.select(-1)
			snlist.remove(i)
			snlist.insrt(j,tobj1)
			snlist.select(j)
			oldsel = -1
			showsel1(j, g)
		}
	}
}

proc list_label() {local i
	i = $1
	if (snlist.object(i).name_differ) {
		sprint(tstr, "%s differs from selection", snlist.object(i).name_)
	}else{
		if (object_id(snlist.object(i).domainparms)) {
			sprint(tstr, "%s  (parameterized)", snlist.object(i).name_)
		}else{
			sprint(tstr, "%s", snlist.object(i).name_)
		}
	}
}

proc sdidisplist_label() {local i
	i = $1
	sdidisplist.object(i).name(tstr)
	if (sdidisplist.object(i).item.is_subset() == 0) {
		sprint(tstr, "   %s", tstr)
	}
}

proc sdidispsel1() {local i
	if (sdidisp_working_) { return }
	sdidisp_working_ = 1
	if ($1 >= 0) {
		tobj = sdidisplist.object($1).item
		if (tobj.is_subset()) {
			i = snlist.index(tobj)
			dk2.flip_to(0)
			g.exec_menu("Crosshair")
			showsel1(i)
		}else{
			dpi = tobj
			i = snlist.index(tobj.subset)
			showsel1(i)
			dpi.flip(dk2)
			dpi.selih = nil
		}
	}
	sdidisp_working_ = 0
}

proc add_domainparm() {local i
	i = snlist.index($o1)
	if (object_id($o1.domainparms) == 0) {
		$o1.domainparms = new List()
	}
	$o2 = new DomainParmInfo($o1, bild)
	$o1.domainparms.append($o2)
	tobj2 = $o1
	snlist.remove(i)
	$o1 = tobj2
	snlist.insrt(i, $o1)
}

proc newsdi() {local i
	i = sdidisplist.selected
	if (i >= 0) {
		tobj = sdidisplist.object(i).item
		add_domainparm(tobj, tobj1)
		sdidisplist.insrt(i+tobj.domainparms.count, new SDIDispListItem(tobj1))
		sdidisplist.select(-1)
		sdidisplist.select(i)
		showsel1(snlist.index(tobj))
	}
}
proc delsdi() {local i, iss
	i = sdidisplist.selected
	if (i >= 0) {
		tobj1 = sdidisplist.object(i).item
		sdidisplist.remove(i)
		tobj2 = tobj1.subset
		iss = snlist.index(tobj2)
		tobj2.domainparms.remove(tobj2.domainparms.index(tobj1))
		if (tobj2.domainparms.count == 0) {
			tobj2.domainparms= nil
			snlist.remove(iss)
			snlist.insrt(iss, tobj2)
		}
		subselpage(0)
	}
}

proc showsel1() {
	if (showcel_active) showsel($1, g)
}
proc showsel() { local i
	bild.topol.sel_all(0)
	if ($1 > -1) {
		tobj = snlist.object($1).list
		for i=0, tobj.count - 1 {
			tobj.object(i).selected = 1
		}
	}
	bild.topol.draw($o2)
	differs($1)
	oldsel = $1
}

proc sel1() {
	bild.topol.sel1($1,$2,$3,$4)
	bild.topol.draw(g)
	differs(snlist.selected)
}
proc sel2() {
	bild.topol.sel2($1,$2,$3,$4)
	bild.topol.draw(g)
	differs(snlist.selected)
}

proc sel3() {local i, dmin, imin, d, x
	if ($1 == 2) {
		bild.topol.sel3($1,$2,$3,$4)
		bild.topol.draw(g)
		differs(snlist.selected)
	}
}

proc sel_all() {
	bild.topol.sel_all($1)
	bild.topol.draw(g)
	differs(snlist.selected)
}

proc sel_xor() {
	bild.topol.sel_xor($1)
	bild.topol.draw(g)
	differs(snlist.selected)
}

func get_subname() {local in
	if (string_dialog($s1, $s2) == 0) {
		return 0
	}
	if (bild.topol.checkname($s2, bild.topol.slist, "Section", 1) == 0) {
		return 0
	}
	i = name_declared($s2)
	if (i != 0 && i != 2) {
		sprint($s2, "%s is already declared and is not an objref", $s2)
		continue_dialog($s2)
		return 0
	}
	return 1
}

proc newslist() {local i
	bild.changed_ = 1
	tstr = ""
	i = snlist.selected()
	if (i >= 0) {
		tstr = snlist.object(i).name_
	}
	if (get_subname("New SectionList name", tstr) == 0) {
		return
	}
	for i=0, snlist.count-1 {
		if (strcmp(tstr, snlist.object(i).name_) == 0) {
			sprint(tstr, "%s is already a SectionList name", tstr)
			continue_dialog(tstr)
			return
		}
	}			
	snlist.append(new SNList(tstr))
	snlist.select(snlist.count-1)
	name_differ()
	oldsel = snlist.count-1
	sel2slist()
}

proc chslist() { local i, j
	bild.changed_ = 1
	i = snlist.selected()
	if (i >= 0) {
		tstr = snlist.object(i).name_
		
		if (get_subname("Change SectionList name to", tstr) == 0) {
			return
		}

		for j=0, snlist.count-1 {
			if (i != j && strcmp(tstr, snlist.object(j).name_) == 0) {
				sprint(tstr, "%s is already a SectionList name", tstr)
				continue_dialog(tstr)
				return
			}
		}
		tobj1 = snlist.object(i)
		if (esub) {
			sprint(tstr1, "objref %s", tobj1.name_)
//print tstr1
			execute(tstr1)
			bild.mech_export()
		}
		tobj1.name_ = tstr
		snlist.select(-1)
		snlist.remove(i)
		snlist.insrt(i, tobj1)
		snlist.select(i)
		showsel1(i)
//		name_differ()
//		sel2slist()
	}
}

proc delslist() {local i
	bild.changed_ = 1
	bild.geom_cnt_ += 1
	i = snlist.selected()
	if (i >= 0) {
		snlist.object(i).del_all()
		if (esub) {
			sprint(tstr1, "objref %s", snlist.object(i).name_)
//print tstr1
			execute(tstr1)
			bild.mech_export()
		}
		snlist.select(-1)
		snlist.remove(i)
		bild.topol.sel_all(0)
	}
}

proc sel2slist() {local i, j
	bild.changed_ = 1
	bild.geom_cnt_ += 1
	i = snlist.selected
	if (i >= 0) {
		tobj = snlist.object(i)
		tobj.del_all()
		for j=0, bild.topol.slist.count-1 {
			tobj1 = bild.topol.slist.object(j)
			if (tobj1.selected) {
				tobj.add(tobj1)
			}
		}
		if (esub) {tobj.export() bild.mech_export()}
	}
	differs(snlist.selected)
}

proc differs() {local i, j, k, b
	i = $1
	b = 1
	if (i >= 0) {
		tobj = snlist.object(i).list
		k = 0
		for j = 0, tobj.count-1 {
			if (tobj.object(j).selected) {
				tobj.object(j).selected += 1
			}else{
				b = 0
			}
		}
		tobj = bild.topol.slist
		for j=0, tobj.count-1 {
			k = tobj.object(j).selected
			if (k == 1) {
				b = 0
			}else if (k == 2) {
				tobj.object(j).selected = 1
			}
		}
		snlist.object(i).differ = (b == 0)
		snlist.select(i)
	}
	name_differ()
}
proc name_differ() {local i, j
	j = snlist.selected
	for i=0, snlist.count-1 {
		tobj = snlist.object(i)
		if (i != j) {
			tobj.differ = 0
		}
//printf("differ=%d name_differ=%d\n", tobj.differ, tobj.name_differ)
		if (tobj.differ != tobj.name_differ) {
			tobj.name_differ = tobj.differ
			showcel_active = 0
			snlist.remove(i)
			snlist.insrt(i, tobj)
			snlist.select(j)
			showcel_active = 1
		}
	}
}

proc save_data() {local i, j
	update()
	sprint(tstr, "}\n{object_push(%s)}\n{first = 0}", $s2)
	$o1.save(tstr)
	for i=0,bild.topol.slist.count-1 {
		bild.topol.slist.object(i).selected = i
	}	
	for i=0, snlist.count-1 {
		tobj = snlist.object(i)
	    if (tobj != bild.all) {
		sprint(tstr, "{tobj = new SNList(\"%s\") snlist.append(tobj)}",tobj.name_)
		$o1.save(tstr)
		tobj = tobj.list
		sprint(tstr, "for i=0,%d tobj.add(bild.topol.slist.object(fscan()))",tobj.count-1)
		$o1.save(tstr)
		for j=0,tobj.count-1 {
			sprint(tstr, "%d", tobj.object(j).selected)
			$o1.save(tstr)
		}
		tobj = snlist.object(i)
	    }else{
		sprint(tstr, "{ tobj = snlist.object(%d)}", i)
		$o1.save(tstr)
	    }
		if (tobj.domainparms != nil) {
			for j=0, tobj.domainparms.count-1 {
				$o1.save("add_domainparm(tobj, tobj1)")
				tobj.domainparms.object(j).save_data($o1, "tobj1")
			}
		}
	}
	$o1.save("{consist()}\n{object_pop()}\n{")
}

proc neuroml() {local i, j  localobj s, sl, snl, cbs, cg
	s = new String()
	sl = bild.topol.slist
	for i = 0, $o1.cablegroups_.__len__() - 1 {
		cg = $o1.cablegroups_[i]
		if (strcmp(cg.name_, "all") == 0) { continue }
		snl = new SNList(cg.name_)
		snlist.append(snl)
		for j=0, cg.cable_indices_.__len__() - 1 {
			cbs = sl.object(cg.cable_indices_[j])
			snl.add(cbs)
		}
	}	
}

proc pr() {local i, j, nline, isss
	update()
	prdec($o1, "")
	$o1.printf("\nproc subsets() { local i")
	prdec($o1,"  ")
	$o1.printf("\n")
	nline = 0
	isss = 0
	for i = 0, snlist.count - 1 {
		tobj = snlist.object(i)
		$o1.printf("  %s = new SectionList()\n", tobj.name_)
		nline += 1
		if (nline%200 == 0) {
			isss += 1
$o1.printf("  subsets_%d()\n}\nproc subsets_%d(){\n", isss, isss)
		}
		pr_subset($o1, tobj, &nline, &isss)
		$o1.printf("\n")
	}
	$o1.printf("}\n")
}

proc prdec() {
	for i = 0, snlist.count - 1 {
		if (i%7 == 0) {
			$o1.printf("\n%sobjref %s", $s2, snlist.object(i).name_)
		}else{
			$o1.printf(", %s", snlist.object(i).name_)
		}
	}
}

proc pr_subset() { local i, j, ni1, ni2, id1, id2
	// To be a bit more compact in expressing
	// the SectionList in hoc, we use for loops for contiguous indices.
	// args are File and SNList
	// topol.validname() has already been called and so
	// topol.unique_name is a Vector
	// that contains the object indices of the names with largest id.
	// This vector thus gives us our order for the unique names.
	// Furthermore the unique_name_index field of each section object
	// gives the index into the unique_name Vector.

	// construct Vector index map for subset of form
	// uniquenameindex*1e6 + arayindex
	imap = new Vector($o2.list.count)
	for i=0, imap.size-1 {
		tobj1 = $o2.list.object(i)
		imap.x[i] = tobj1.unique_name_index*1e6 + tobj1.id
	}
	// index vector sort gives us easily findable contiguous sets of
	// indices
	smap = imap.sortindex()
	for i=0, smap.size-1 {
		j = imap.x[smap.x[i]]
		ni1 = int(j/1e6)
		id1 = j - ni1*1e6
		id2 = id1
		for i = i+1, smap.size-1 {
			j = imap.x[smap.x[i]]
			ni2 = int(j/1e6)
			if ( ni2 > ni1 || id2 != j - ni2*1e6 - 1) {
				break
			}
			id2 = j - ni2*1e6
		}
		i = i-1
		tobj1 = bild.topol.slist.object(bild.topol.unique_name.x[ni1])
		if (id2 > id1) {
			$o1.printf("    for i=%d, %d %s[i] %s.append()\n",\
				id1, id2, tobj1.name_, $o2.name_)
		}else if (id1 > 0){
			$o1.printf("    %s[%d] %s.append()\n",\
				tobj1.name_, id1, $o2.name_)
		}else{
			$o1.printf("    %s %s.append()\n",\
				tobj1.name_, $o2.name_)
		}
		$&3 += 1
		if ($&3%200 == 0) {
			$&4 += 1
$o1.printf("  subsets_%d()\n}\nproc subsets_%d(){\n", $&4, $&4)
		}
	}
}

proc xmlwrite() {local i, j
	update()
	$o1.printf("  <SetOfFeatures>\n")
	for i = 0, snlist.count - 1 {
		tobj = snlist.object(i)
		$o1.printf("    <Path name=\"%s\">\n", tobj.name_)
		$o1.printf("      <SetOfSegments>\n")
		xmlwrite_subset($o1, tobj)
		$o1.printf("      </SetOfSegments>\n")
		$o1.printf("    </Path>\n")
	}
	$o1.printf("  </SetOfFeatures>\n")
}

proc xmlwrite_subset() { local i
	for i=0, $o2.list.count-1 {
		tobj1 = $o2.list.object(i)
		$o1.printf("        <Segment id=\"%d\"/>\n", tobj1.id_)
	}
}

endtemplate SectionSubsets