|
|
1.1 root 1: #include <libc.h>
2: #include <fio.h>
3: #include <string.h>
4: #include "dbtypes.h"
5: extern "C" qsort(Tuple **, int, int, int (*)(Tuple **, Tuple **));
6:
7: /*
8: * add a tuple to a set
9: */
10: void
11: Set::add(Tuple *t)
12: {
13: register Tuple *tp;
14:
15: /*
16: * make sure it isn't already in the set
17: */
18: for(tp=first; tp; tp=tp->next)
19: if(tp==t)
20: return;
21:
22: /*
23: * add it
24: */
25: if(first==(Tuple *)0)
26: first = last = t;
27: else {
28: last->next = t;
29: last = t;
30: }
31: last->next = (Tuple *)0;
32: tuples++;
33: }
34:
35: /*
36: * output a set in it's linear form
37: */
38: int
39: Set::print(int fd)
40: {
41: Tuple *t;
42:
43: for(t=first; t; t=t->next){
44: if (Fprint(fd, "\t") < 0)
45: return -1;
46: if (t->print(fd) < 0)
47: return -1;
48: if (Fprint(fd, "\n") < 0)
49: return -1;;
50: }
51: return 0;
52: }
53:
54: /*
55: * sort a set of tuples on distance from the origin
56: */
57: int
58: comparetuples(Tuple **a, Tuple **b)
59: {
60: return (*a)->dist - (*b)->dist;
61: }
62: void
63: Set::sort(Tuple *origin)
64: {
65: static Tuple **array;
66: static int arraylen;
67: Tuple *t;
68: int i;
69:
70: /*
71: * Find distance from origin of all tuples in the set, and
72: * put them into an array for sorting.
73: */
74: if(tuples > arraylen){
75: if(array)
76: delete array;
77: array = new Tuple *[tuples];
78: arraylen = tuples;
79: }
80:
81: for(t=first, i=0; t; i++, t=t->next){
82: t->distance(origin);
83: array[i] = t;
84: }
85:
86: /*
87: * sort the easy way
88: */
89: qsort(array, tuples, sizeof(Tuple *), comparetuples);
90:
91: /*
92: * rethread
93: */
94: for(i=0; i<tuples-1; i++)
95: array[i]->next = array[i+1];
96: array[tuples-1]->next = (Tuple *)0;
97: first = array[0];
98: last = array[tuples-1];
99: }
100:
101: /*
102: * input a set from a file
103: */
104: Set::Set(int fd)
105: {
106: char *cp;
107: Tuple *t;
108:
109: tuples = 0;
110: first = last = (Tuple *)0;
111: while(cp=Frdline(fd)){
112: while(*cp==' ' || *cp=='\t')
113: cp++;
114: if(*cp==0 || *cp=='#')
115: continue;
116: t = new Tuple(cp, (Ordered *)0, (Tuple **)0);
117: add(t);
118: }
119: }
120:
121: /*
122: * Print the value of the first attribute matching one of the types.
123: */
124: void
125: Set::printvalue(int fd, char **types)
126: {
127: Tuple *t;
128:
129: for(; *types; types++)
130: for(t=first; t; t=t->next)
131: if(t->printvalue(fd, *types)==0)
132: return;
133: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.