Debian Codegraph @

reports/stats.ll

raw
1 /*
2 * @progname stats.ll
3 * @version 10.0
4 * @author Jim Eggert
5 * @category
6 * @output Text
7 * @description Compute statistics on dates, ages, counts in the DB.
8
9 This LifeLines report program computes mean statistics of various
10 quantities binned over other quantities. The quantities it knows
11 about are ages at and dates of birth, christening, first and last
12 marriage, first and last child's birth, death, burial, and today; the
13 number of children, siblings, and marriages; and sex, surname, first
14 name, soundex, and any simple GEDCOM tag. These can be combined
15 nearly arbitrarily and evaluated over the whole database, or
16 restricted to ancestors or descendants of a chosen individual or to
17 members of a predetermined set. Further restrictions on the
18 individuals included in the statistics can be based on any quantity
19 that the program knows about. The program will optionally print out
20 the names of all the individuals included in the statistics.
21
22 For example, you can produce statistics of
23 the age at death of as a function of birth year,
24 dage vs byear
25 the number of children of females named Smith as a
26 function of year of first marriage,
27 kids vs myear | sex = F & surname = Smith
28 the number of spouses for male vs female blacksmiths,
29 families vs sex | occu = blacksmith
30 the age at last childbirth as a function of place of marriage.
31 qage vs mplace
32 the names of all Joneses who lived to be greater than 80
33 unity vs unity | surname = Jones & dage > 80
34 All this without writing any programs of your own.
35
36 If a particular statistic for an individual is unavailable, and if the
37 global variable not_strict is nonzero (as it is in the distribution
38 version of this report, then certain guesses are allowed as to the
39 value of that statistic. So far, these guesses are few. Birth year
40 and month are guessed from baptismal date, and death year and month
41 are guessed from burial date.
42
43 The user is prompted for what quantity to plot vs what to bin over.
44 Each is to be given as a specification string of the form
45 e<time> or e<place> or <count> or <label>
46 where e is the event code, <time> and <place> are place codes,
47 <count> is the count codes, and <label> is the label code.
48 event codes
49 b birth (can get year or month from christening)
50 c christening (can get year or month from birth)
51 d death (can get year or month from burial)
52 e burial (entombment) (can get year or month from death)
53 m marriage (first)
54 n marriage (last)
55 p parturition or childbirth (first)
56 q parturition (last)
57 t today (most useful with age)
58 time codes
59 age (in years)
60 fage (father's age)
61 mage (mother's age)
62 year
63 month
64 day
65 place codes
66 place (tplace is always the empty string)
67 count codes
68 unity (always returns one, good for simple histograms or averages)
69 kids
70 families
71 siblings
72 siblingorder
73 label codes
74 sex
75 surname
76 firstname
77 soundex
78 <tag> (returns the value of the first GEDCOM node with this tag)
79 (a useful example is OCCU for occupation)
80
81 For example, to compute statistics of age at death vs year of first
82 marriage, enter dage then myear then the bin size for myear.
83 To compute statistics of the number of children versus occupation,
84 enter kids then occu.
85
86 The statistics can also be restricted according to any of the known
87 characteristics. The user is prompted for any constraints, to be entered
88 one at a time. For example, if you want to accumulate statistics only
89 if the person is female, enter the constraint
90 sex = F
91 or if you want to allow only people born after 1800
92 byear > 1800
93
94 The allowed equality symbols are =, !=, <, >, <=, and >=. For
95 strings, two additional symbols are allowed, contains and isin.
96 Multiple constraints are allowed, just enter return if you don't want
97 any more. Please note that the constraints are composed of three
98 parts: left hand side, inequality symbol, and right hand side. You
99 must use exactly one space between each pair of parts.
100
101 Case is not important. You can't compute averages of label codes,
102 as that would not be meaningful.
103
104 stats - a LifeLines database statistical extraction report program
105 by Jim Eggert ([email protected])
106 Version 1 (14 Dec 1992)
107 Version 2 (17 Dec 1992) added restrictions,
108 unity, general GEDCOM tag, e<place>, and today
109 Version 3 (20 Dec 1992) added sorting
110 Version 4 (30 Jan 1993) bugfix, modified find_bin to use requeue()
111 Requires LifeLines v2.3.3 or later
112 Version 5 (30 Jun 1993) changed bubblesort to listsort
113 listsort code by John Chandler ([email protected])
114 Version 6 (2 July 1993) added firstname, changed user interface
115 Version 7 (16 Mar 1995) changed listsort to quicksort,
116 modernized, fixed kids bug, added fage and mage
117 Requires LifeLines v3.0.1 or later
118 Version 8 (4 Sep 1995) added min and max, no printing of bogus 0s
119 Version 9 (29 Sep 1995) fixed minor bugs, added indiset, substrings
120 Version 10 (15 Jan 2000) now uses eqstr, fixed quicksort bug
121
122 */
123
124 global(ret_type) /* return type 0=none, 1=string, 2=integer */
125 global(minbin) /* maintained by find_bin */
126 global(maxbin) /* ditto */
127 global(deltabin) /* zero for binning over non-numeric types */
128 global(not_strict)/* 0=strict dates, 1=estimate dates */
129 global(accuracy) /* 10^digits after the decimal */
130 global(log) /* 0=don't, 1=do write an individual log */
131 global(rlist) /* list of restriction variable names */
132 global(tlist) /* list of restriction variable types */
133 global(clist) /* list of restriction inequality codes */
134 global(vlist) /* list of restriction values */
135 global(vtable) /* table of known return value types */
136 global(xvaltable) /* table of known bin strings */
137
138 proc main() {
139 call init_vtable() /* set up the table of return value types */
140 set(not_strict,1)
141 set(accuracy,100) /* compute and print to 0.01 */
142 list(ysums) /* bin sums */
143 list(ycounts) /* bin counts */
144 list(ymins) /* bin mins */
145 list(ymaxs) /* bin maxes */
146 list(xvals) /* bin values */
147 list(relist) /* contains restriction equations */
148 list(rlist) /* contains restriction LHSs */
149 list(tlist) /* contains restriction LHS types */
150 list(clist) /* contains restriction inequalities */
151 list(vlist) /* contains restriction RHSs */
152 list(ilist) /* index list for sorting */
153 list(restriction_tokens) /* contains restriction equation elements */
154 table(xvaltable) /* table of bin labels */
155 indiset(people)
156 set(sepline,concat("-------------------------------------",
157 "-------------------------------------\n"))
158
159 while (1) {
160 getstrmsg(ystring,"Collect statistics of ")
161 set(ystring,save(upper(ystring)))
162 if (ne(value_type(ystring),1)) { break() }
163 print("Can't do statistics on ") print(ystring) print("\n")
164 }
165 getstrmsg(xstring,"versus ")
166 set(xstring,save(upper(xstring)))
167 set(deltabin,0)
168 if (ne(value_type(xstring),1)) {
169 while (1) {
170 getintmsg(deltabin,"with bin width ")
171 if (deltabin) { break() }
172 print("Can't have zero bin width\n")
173 }
174 set(minbin,1000000)
175 set(maxbin,neg(minbin))
176 }
177 getintmsg(log,"logging people (0=no, 1=yes) ")
178 getintmsg(who,
179 "over set (0=all, 1=descendants, 2=ancestors, 3=indiset) ")
180 if (or(eq(who,1),eq(who,2))) {
181 getindimsg(of,"of ")
182 set(ofkey,save(key(of)))
183 }
184 elsif (eq(who,3)) { getindiset(people) }
185 while (1) {
186 getstrmsg(restriction_equation,"restricted by ")
187 if (eqstr(restriction_equation,"")) { break() }
188 while(not(empty(restriction_tokens)))
189 { set(junk,dequeue(restriction_tokens)) }
190 extracttokens(restriction_equation,restriction_tokens,nt," ")
191 if (nt) {
192 set(restriction,save(upper(dequeue(restriction_tokens))))
193 set(inequality,save(dequeue(restriction_tokens)))
194 if (inequality) {
195 set(type,value_type(restriction))
196 if (eq(type,1)) {
197 set(value,save(dequeue(restriction_tokens)))
198 }
199 else {
200 set(value,atoi(dequeue(restriction_tokens)))
201 }
202 enqueue(rlist,restriction)
203 enqueue(tlist,type)
204 enqueue(clist,inequality)
205 enqueue(vlist,value)
206 enqueue(relist,save(restriction_equation))
207 }
208 }
209 }
210 if (log) {
211 sepline
212 "Log of individuals and their values used in the statistics\n"
213 sepline
214 "key" col(8) "name" col(50) xstring col(65) ystring "\n"
215 sepline
216 }
217
218 if (eq(who,0)) {
219 forindi(person,pnum) {
220 call stat_person(person,xstring,ystring,
221 ycounts,ysums,ymins,ymaxs,xvals)
222 }
223 }
224 else {
225 if (ne(who,3)) {
226 addtoset(people,of,0)
227 if (eq(who,1)) { set(people,descendentset(people)) }
228 elsif (eq(who,2)) { set(people,ancestorset(people)) }
229 addtoset(people,of,0)
230 }
231 forindiset(people,person,pval,pnum) {
232 call stat_person(person,xstring,ystring,
233 ycounts,ysums,ymins,ymaxs,xvals)
234 }
235 }
236
237 set(sort_it,0)
238 if (eq(deltabin,0)) {
239 getintmsg(sort_it,"Sort the output? (0=no, 1=yes)")
240 }
241 if (sort_it) { call quicksort(xvals,ilist) }
242 else { forlist(ycounts,ycount,ynum) { enqueue(ilist,ynum) } }
243
244 if (log) { sepline }
245 sepline
246 "Statistics of " ystring " binned by " xstring
247 "\nfor "
248 if (eq(who,0)) { "all individuals" }
249 elsif (eq(who,1)) { "descendants of " ofkey " " name(indi(ofkey)) }
250 elsif (eq(who,2)) { "ancestors of " ofkey " " name(indi(ofkey)) }
251 elsif (eq(who,3)) { "selected set of individuals" }
252 "\nin database " database()
253 if (not(empty(relist))) {
254 " subject to"
255 while(not(empty(relist))) {
256 "\n"
257 dequeue(relist)
258 }
259 }
260 "\n"
261 sepline
262 col(6) xstring if (gt(deltabin,0)) { " range" }
263 col(30) "bin" col(38) "total" col(50) "min" col(60) "max"
264 col(70) "average\n"
265 "bin" col(6)
266 if (gt(deltabin,0)) { "from" col(18) "to" set(binx,minbin) }
267 else { "label" }
268 col(30) "count" col(38) ystring col(50) ystring col(60)
269 ystring col(70) ystring "\n"
270 sepline
271 set(bin,0)
272 set(allycount,0)
273 set(allysum,0)
274 set(allymin,999999)
275 set(allymax,neg(999999))
276 forlist(ilist,index,num) {
277 set(ycount,getel(ycounts,index))
278 set(ysum,getel(ysums,index))
279 set(ymin,getel(ymins,index))
280 set(ymax,getel(ymaxs,index))
281 incr(bin)
282 d(bin) col(6)
283 if (gt(deltabin,0)) {
284 d(binx)
285 set(binx,add(binx,deltabin))
286 col(18) d(binx)
287 }
288 else { getel(xvals,index) }
289 col(30) d(ycount) set(allycount,add(allycount,ycount))
290 col(38) dd(ysum,ycount) set(allysum,add(allysum,ysum))
291 col(50) dd(ymin,ycount) if (lt(ymin,allymin)) { set(allymin,ymin) }
292 col(60) dd(ymax,ycount) if (gt(ymax,allymax)) { set(allymax,ymax) }
293 col(70)
294 if (gt(ycount,0)) {
295 d(div(ysum,ycount)) "."
296 set(frac,div(mul(mod(ysum,ycount),accuracy),ycount))
297 call print_frac(frac)
298 }
299 else { "--" }
300 "\n"
301 }
302 sepline
303 "all" col(6)
304 if (gt(deltabin,0)) {
305 dd(minbin,allycount) col(18) dd(maxbin,allycount)
306 }
307 else { xstring }
308 col(30) d(allycount)
309 col(38) dd(allysum,allycount)
310 col(50) dd(allymin,allycount)
311 col(60) dd(allymax,allycount)
312 col(70)
313 if (gt(allycount,0)) {
314 d(div(allysum,allycount)) "."
315 set(frac,div(mul(mod(allysum,allycount),accuracy),allycount))
316 call print_frac(frac)
317 }
318 else { "--" }
319 "\n"
320 sepline
321 }
322
323 func dd(value,count) {
324 if (count) { return(d(value)) }
325 return("--")
326 }
327
328 proc init_vtable() {
329 table(vtable)
330 list(initials)
331 enqueue(initials,"B")
332 enqueue(initials,"C")
333 enqueue(initials,"M")
334 enqueue(initials,"N")
335 enqueue(initials,"P")
336 enqueue(initials,"Q")
337 enqueue(initials,"D")
338 enqueue(initials,"E")
339 enqueue(initials,"T")
340 list(numbers)
341 enqueue(numbers,"AGE")
342 enqueue(numbers,"FAGE")
343 enqueue(numbers,"MAGE")
344 enqueue(numbers,"YEAR")
345 enqueue(numbers,"MONTH")
346 enqueue(numbers,"DAY")
347 forlist(initials,initial,lnum) {
348 forlist(numbers,number,nnum) {
349 insert(vtable,save(concat(initial,number)),2)
350 }
351 insert(vtable,save(concat(initial,"PLACE")),1)
352 }
353 insert(vtable,"UNITY",2)
354 insert(vtable,"KIDS",2)
355 insert(vtable,"FAMILIES",2)
356 insert(vtable,"SIBLINGS",2)
357 insert(vtable,"SIBLINGORDER",2)
358 }
359
360 /* compute a value type from the value specification string */
361 func value_type(spec_string) {
362 /* print(spec_string," ",d(lookup(vtable,spec_string)),"\n") */
363 if (vt,lookup(vtable,spec_string)) { return(vt) }
364 return(1) /* SEX, SURNAME, FIRSTNAME, SOUNDEX, or GEDCOM tag */
365 }
366
367 /* get the specified value for a person */
368 func get_val(person,spec_string) {
369 list(namelist)
370 set(ret_type,0)
371 set(event,0)
372 set(initial,save(trim(spec_string,1)))
373 if (eqstr(initial,"B")) {
374 set(e,birth(person))
375 set(e1,baptism(person))
376 set(event,1)
377 }
378 elsif (eqstr(initial,"C")) {
379 set(e,baptism(person))
380 set(e1,birth(person))
381 set(event,1)
382 }
383 elsif (eqstr(initial,"D")) {
384 set(e,death(person))
385 set(e1,burial(person))
386 set(event,1)
387 }
388 elsif (eqstr(initial,"E")) {
389 set(e,burial(person))
390 set(e1,death(person))
391 set(event,1)
392 }
393 elsif (eqstr(initial,"M")) {
394 families(person,fam,spouse,fnum) {
395 set(e,marriage(fam))
396 set(event,1)
397 break()
398 }
399 }
400 elsif (eqstr(initial,"N")) {
401 families(person,fam,spouse,fnum) {
402 set(e,marriage(fam))
403 set(event,1)
404 }
405 }
406 elsif (eqstr(initial,"P")) {
407 families(person,fam,spouse,fnum) {
408 if (child,firstchild(fam)) {
409 set(e,birth(child))
410 set(e1,baptism(child))
411 set(event,1)
412 break()
413 }
414 }
415 }
416 elsif (eqstr(initial,"Q")) {
417 families(person,fam,spouse,fnum) {
418 if (child,lastchild(fam)) {
419 set(e,birth(child))
420 set(e1,baptism(child))
421 set(event,1)
422 }
423 }
424 }
425 elsif (eqstr(initial,"T")) {
426 set(e,gettoday())
427 set(event,1)
428 }
429 if (eq(event,1)) {
430 if (e) { extractdate(e,day,month,year) }
431 if (e1) { extractdate(e1,day1,month1,year1) }
432 if (eqstr(spec_string,concat(initial,"YEAR"))) {
433 if (year) { set(ret_type,2) return(year) }
434 if (and(not_strict,year1)) {
435 set(ret_type,2) return(year1)
436 }
437 return(0)
438 }
439 if (eqstr(spec_string,concat(initial,"MONTH"))) {
440 if (month) { set(ret_type,2) return(month) }
441 if (and(not_strict,month1)) {
442 set(ret_type,2) return(month1)
443 }
444 return(0)
445 }
446 if (eqstr(spec_string,concat(initial,"DAY"))) {
447 if (day) { set(ret_type,2) return(day) }
448 return(0)
449 }
450 if (eqstr(spec_string,concat(initial,"AGE"))) {
451 set(byear,0)
452 if (b,birth(person)) { extractdate(b,bday,bmonth,byear) }
453 if (and(not(byear),not_strict)) {
454 if (b,baptism(person)) { extractdate(b,bday,bmonth,byear) }
455 }
456 if (byear) {
457 if (year) {
458 set(ret_type,2) return(sub(year,byear))
459 }
460 if (and(not_strict,year1)) {
461 set(ret_type,2) return(sub(year1,byear))
462 }
463 }
464 return(0)
465 }
466 if (eqstr(spec_string,concat(initial,"FAGE"))) {
467 set(byear,0)
468 if(b,birth(father(person))) { extractdate(b,bday,bmonth,byear) }
469 if (and(not(byear),not_strict)) {
470 if(b,baptism(father(person))) {
471 extractdate(b,bday,bmonth,byear)
472 }
473 }
474 if (byear) {
475 if (year) {
476 set(ret_type,2) return(sub(year,byear))
477 }
478 if (and(not_strict,year1)) {
479 set(ret_type,2) return(sub(year1,byear))
480 }
481 }
482 return(0)
483 }
484 if (eqstr(spec_string,concat(initial,"MAGE"))) {
485 set(byear,0)
486 if(b,birth(mother(person))) { extractdate(b,bday,bmonth,byear) }
487 if (and(not(byear),not_strict)) {
488 if(b,baptism(mother(person))) {
489 extractdate(b,bday,bmonth,byear)
490 }
491 }
492 if (byear) {
493 if (year) {
494 set(ret_type,2) return(sub(year,byear))
495 }
496 if (and(not_strict,year1)) {
497 set(ret_type,2) return(sub(year1,byear))
498 }
499 }
500 return(0)
501 }
502 if (eqstr(spec_string,concat(initial,"PLACE"))) {
503 set(ret_val,save(place(e)))
504 if (and(not_strict,eqstr(ret_val,""))) {
505 set(ret_val,save(place(e1)))
506 }
507 set(ret_type,1)
508 return(ret_val)
509 }
510 }
511 if (eqstr(spec_string,"KIDS")) {
512 set(nkids,0)
513 families(person,fam,spouse,fnum) {
514 set(nkids,add(nkids,nchildren(fam)))
515 }
516 set(ret_type,2)
517 return(nkids)
518 }
519 if (eqstr(spec_string,"FAMILIES")) {
520 set(ret_type,2)
521 return(nfamilies(person))
522 }
523 if (eqstr(spec_string,"SIBLINGS")) {
524 if (fam,parents(person)) {
525 set(ret_type,2)
526 return(nchildren(fam))
527 }
528 return(0)
529 }
530 if (eqstr(spec_string,"SIBLINGORDER")) {
531 if (fam,parents(person)) {
532 set(ret_type,2)
533 set(cnum,1)
534 set(child,person)
535 while(child,prevsib(child)) { incr(cnum) }
536 return(cnum)
537 }
538 return(0)
539 }
540 if (eqstr(spec_string,"UNITY")) {
541 set(ret_type,2)
542 return(1)
543 }
544 /* The next four lines will work even if you comment them out.
545 Sex is a powerful force, I guess. */
546 if (eqstr(spec_string,"SEX")) {
547 set(ret_type,1)
548 return(save(sex(person)))
549 }
550 if (eqstr(spec_string,"SURNAME")) {
551 set(ret_type,1)
552 return(save(surname(person)))
553 }
554 if (eqstr(spec_string,"FIRSTNAME")) {
555 set(ret_type,1)
556 extractnames(inode(person), namelist, ncomp, sindx)
557 if( or( gt(sindx,1), gt(ncomp,sindx))) {
558 set(gindx,1) if(eq(sindx,1)) { set(gindx,2) }
559 return(save(getel(namelist, gindx)))
560 }
561 return("")
562 }
563 if (eqstr(spec_string,"SOUNDEX")) {
564 set(ret_type,1)
565 return(save(soundex(person)))
566 }
567 fornodes(inode(person),node) {
568 if (eqstr(tag(node),spec_string)) {
569 set(ret_type,1)
570 return(save(value(node)))
571 }
572 }
573 return(0)
574 }
575
576 func find_bin(xvalue,ycounts,ysums,ymins,ymaxs,xvals) {
577 if (gt(deltabin,0)) { /* numeric data type */
578 if (lt(maxbin,minbin)) { /* first time through */
579 set(minbin,sub(xvalue,mod(xvalue,deltabin)))
580 set(maxbin,add(minbin,deltabin))
581 enqueue(ycounts,0)
582 enqueue(ysums,0)
583 enqueue(ymins,999999)
584 enqueue(ymaxs,neg(999999))
585 return(1)
586 }
587 while (lt(xvalue,minbin)) {
588 requeue(ycounts,0)
589 requeue(ysums,0)
590 requeue(ymins,999999)
591 requeue(ymaxs,neg(999999))
592 set(minbin,sub(minbin,deltabin))
593 }
594 while (ge(xvalue,maxbin)) {
595 enqueue(ycounts,0)
596 enqueue(ysums,0)
597 enqueue(ymins,999999)
598 enqueue(ymaxs,neg(999999))
599 set(maxbin,add(maxbin,deltabin))
600 }
601 return(add(div(sub(xvalue,minbin),deltabin),1))
602 }
603 /* unsorted string data type */
604 if (r,lookup(xvaltable,xvalue)) { return(r) }
605 enqueue(xvals,xvalue)
606 enqueue(ycounts,0)
607 enqueue(ysums,0)
608 enqueue(ymins,999999)
609 enqueue(ymaxs,neg(999999))
610 set(r,length(xvals))
611 insert(xvaltable,xvalue,r)
612 return(r)
613 }
614
615 func filter_person(person) {
616 forlist(rlist,restriction,rnum) {
617 set(rtype,getel(tlist,rnum))
618 set(inequality,getel(clist,rnum))
619 set(cvalue,getel(vlist,rnum))
620 set(ret_val,get_val(person,restriction))
621 if (eq(ret_type,rtype)) {
622 if (eq(rtype,2)) { /* numeric */
623 if (eqstr(inequality,"=")) {
624 if (not(eq(ret_val,cvalue))) { return(0) }
625 }
626 elsif (eqstr(inequality,"!=")) {
627 if (not(ne(ret_val,cvalue))) { return(0) }
628 }
629 elsif (eqstr(inequality,">")) {
630 if (not(gt(ret_val,cvalue))) { return(0) }
631 }
632 elsif (eqstr(inequality,"<")) {
633 if (not(lt(ret_val,cvalue))) { return(0) }
634 }
635 elsif (eqstr(inequality,">=")) {
636 if (not(ge(ret_val,cvalue))) { return(0) }
637 }
638 elsif (eqstr(inequality,"<=")) {
639 if (not(le(ret_val,cvalue))) { return(0) }
640 }
641 else { return(0) } /* error condition */
642 }
643 else { /* string */
644 if (eqstr(inequality,"=")) {
645 if (not(eq(strcmp(ret_val,cvalue),0))) { return(0) }
646 }
647 elsif (eqstr(inequality,"!=")) {
648 if (not(ne(strcmp(ret_val,cvalue),0))) { return(0) }
649 }
650 elsif (eqstr(inequality,">")) {
651 if (not(gt(strcmp(ret_val,cvalue),0))) { return(0) }
652 }
653 elsif (eqstr(inequality,"<")) {
654 if (not(lt(strcmp(ret_val,cvalue),0))) { return(0) }
655 }
656 elsif (eqstr(inequality,">=")) {
657 if (not(ge(strcmp(ret_val,cvalue),0))) { return(0) }
658 }
659 elsif (eqstr(inequality,"<=")) {
660 if (not(le(strcmp(ret_val,cvalue),0))) { return(0) }
661 }
662 elsif (eqstr(inequality,"contains")) {
663 if (not(index(ret_val,cvalue,1))) { return(0) }
664 }
665 elsif (eqstr(inequality,"isin")) {
666 if (not(index(cvalue,ret_val,1))) { return(0) }
667 }
668 else { return(0) } /* error condition */
669 }
670 }
671 else { return(0) } /* error condition */
672 }
673 return(1)
674 }
675
676 proc stat_person(person,xstring,ystring,ycounts,ysums,ymins,ymaxs,xvals) {
677 if (filter_person(person)) {
678 set(value,get_val(person,ystring))
679 if (eq(ret_type,2)) {
680 set(ret_val,get_val(person,xstring))
681 if (ret_type) {
682 if (log) {
683 key(person) col(8) name(person) col(50)
684 if (eq(ret_type,1)) { ret_val } else { d(ret_val) }
685 col(65) d(value) "\n"
686 }
687 set(bin,find_bin(ret_val,ycounts,ysums,ymins,ymaxs,xvals))
688 setel(ycounts,bin,add(getel(ycounts,bin),1))
689 setel(ysums,bin,add(getel(ysums,bin),value))
690 if (lt(value,getel(ymins,bin))) { setel(ymins,bin,value) }
691 if (gt(value,getel(ymaxs,bin))) { setel(ymaxs,bin,value) }
692 }
693 }
694 }
695 }
696
697 proc print_frac(frac) {
698 set(check,div(accuracy,10))
699 while (gt(check,1)) {
700 if (lt(frac,check)) { "0" }
701 set(check,div(check,10))
702 }
703 d(frac)
704 }
705
706 func compare(astring,bstring) {
707 return(strcmp(astring,bstring))
708 }
709
710 /*
711 quicksort: Sort an input list by generating a permuted index list
712 Input: alist - list to be sorted
713 Output: ilist - list of index pointers into "alist" in sorted order
714 Needed: compare- external function of two arguments to return -1,0,+1
715 according to relative order of the two arguments
716 */
717 proc quicksort(alist,ilist) {
718 set(len,length(alist))
719 set(index,len)
720 while(index) {
721 setel(ilist,index,index)
722 decr(index)
723 }
724 if (ge(len,2)) { call qsort(alist,ilist,1,len) }
725 }
726
727 /* recursive core of quicksort */
728 proc qsort(alist,ilist,left,right) {
729 if(pcur,getpivot(alist,ilist,left,right)) {
730 set(pivot,getel(alist,getel(ilist,pcur)))
731 set(mid,partition(alist,ilist,left,right,pivot))
732 call qsort(alist,ilist,left,sub(mid,1))
733 call qsort(alist,ilist,mid,right)
734 }
735 }
736
737 /* partition around pivot */
738 func partition(alist,ilist,left,right,pivot) {
739 while(1) {
740 set(tmp,getel(ilist,left))
741 setel(ilist,left,getel(ilist,right))
742 setel(ilist,right,tmp)
743 while(lt(compare(getel(alist,getel(ilist,left)),pivot),0)) {
744 incr(left)
745 }
746 while(ge(compare(getel(alist,getel(ilist,right)),pivot),0)) {
747 decr(right)
748 }
749 if(gt(left,right)) { break() }
750 }
751 return(left)
752 }
753
754 /* choose pivot */
755 func getpivot(alist,ilist,left,right) {
756 set(pivot,getel(alist,getel(ilist,left)))
757 set(left0,left)
758 incr(left)
759 while(le(left,right)) {
760 set(rel,compare(getel(alist,getel(ilist,left)),pivot))
761 if (gt(rel,0)) { return(left) }
762 if (lt(rel,0)) { return(left0) }
763 incr(left)
764 }
765 return(0)
766 }