1 | #include "madx.h" |
---|
2 | |
---|
3 | char* |
---|
4 | get_new_name(void) |
---|
5 | /* makes a new internal element or variable name */ |
---|
6 | { |
---|
7 | char name[NAME_L] = "__"; |
---|
8 | sprintf(&name[2], "%d", new_name_count++); |
---|
9 | strcat(name, "__"); |
---|
10 | return permbuff(name); |
---|
11 | } |
---|
12 | |
---|
13 | struct name_list* |
---|
14 | clone_name_list(struct name_list* p) |
---|
15 | { |
---|
16 | int i, l = p->curr > 0 ? p->curr : 1; |
---|
17 | char name[2*NAME_L]; |
---|
18 | struct name_list* clone; |
---|
19 | strcpy(name, p->name); strcat(name, "_clone"); |
---|
20 | clone = new_name_list(name, l); |
---|
21 | for (i = 0; i < p->curr; i++) clone->index[i] = p->index[i]; |
---|
22 | for (i = 0; i < p->curr; i++) clone->inform[i] = p->inform[i]; |
---|
23 | for (i = 0; i < p->curr; i++) clone->names[i] = p->names[i]; |
---|
24 | clone->curr = p->curr; |
---|
25 | return clone; |
---|
26 | } |
---|
27 | |
---|
28 | struct name_list* |
---|
29 | new_name_list(char* list_name, int length) |
---|
30 | { |
---|
31 | char rout_name[] = "new_name_list"; |
---|
32 | struct name_list* il = |
---|
33 | (struct name_list*) mycalloc(rout_name,1, sizeof(struct name_list)); |
---|
34 | strcpy(il->name, list_name); |
---|
35 | il->stamp = 123456; |
---|
36 | if (watch_flag) fprintf(debug_file, "creating ++> %s\n", il->name); |
---|
37 | il->names = (char**) mycalloc(rout_name,length, sizeof(char*)); |
---|
38 | il->index = (int*) mycalloc(rout_name,length, sizeof(int)); |
---|
39 | il->inform = (int*) mycalloc(rout_name,length, sizeof(int)); |
---|
40 | il->max = length; |
---|
41 | return il; |
---|
42 | } |
---|
43 | |
---|
44 | struct name_list* |
---|
45 | delete_name_list(struct name_list* l) |
---|
46 | { |
---|
47 | char rout_name[] = "delete_name_list"; |
---|
48 | if (l == NULL) return NULL; |
---|
49 | if (stamp_flag && l->stamp != 123456) |
---|
50 | fprintf(stamp_file, "d_n_l double delete --> %s\n", l->name); |
---|
51 | if (watch_flag) fprintf(debug_file, "deleting --> %s\n", l->name); |
---|
52 | if (l->index != NULL) myfree(rout_name, l->index); |
---|
53 | if (l->inform != NULL) myfree(rout_name, l->inform); |
---|
54 | if (l->names != NULL) myfree(rout_name, l->names); |
---|
55 | myfree(rout_name, l); |
---|
56 | return NULL; |
---|
57 | } |
---|
58 | |
---|
59 | struct vector_list* |
---|
60 | new_vector_list(int length) |
---|
61 | /* creates a name list and pointer list |
---|
62 | for double arrays with initial length "length". |
---|
63 | */ |
---|
64 | { |
---|
65 | char rout_name[] = "new_vector_list"; |
---|
66 | struct vector_list* vector = mycalloc(rout_name,1, sizeof(struct vector_list)); |
---|
67 | vector->max = length; |
---|
68 | vector->names = new_name_list("vector_list", length); |
---|
69 | vector->vectors = mycalloc(rout_name, length, sizeof(struct double_array*)); |
---|
70 | return vector; |
---|
71 | } |
---|
72 | |
---|
73 | struct vector_list* |
---|
74 | delete_vector_list(struct vector_list* vector) |
---|
75 | { |
---|
76 | char rout_name[] = "delete_vector_list"; |
---|
77 | int j; |
---|
78 | if (vector == NULL) return NULL; |
---|
79 | if (vector->names != NULL) |
---|
80 | { |
---|
81 | for (j = 0; j < vector->names->curr; j++) |
---|
82 | if (vector->vectors[j]) delete_double_array(vector->vectors[j]); |
---|
83 | delete_name_list(vector->names); |
---|
84 | } |
---|
85 | if (vector->vectors != NULL) myfree(rout_name, vector->vectors); |
---|
86 | myfree(rout_name, vector); |
---|
87 | return NULL; |
---|
88 | } |
---|
89 | |
---|
90 | void |
---|
91 | dump_name_list(struct name_list* nl) |
---|
92 | { |
---|
93 | int i; |
---|
94 | puts(" "); |
---|
95 | for (i = 0; i < nl->curr; i++) |
---|
96 | { |
---|
97 | fprintf(prt_file, v_format("%S %I\n"), |
---|
98 | nl->names[nl->index[i]], nl->inform[nl->index[i]]); |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | void |
---|
103 | copy_name_list(struct name_list* out, struct name_list* in) |
---|
104 | /* copies namelist in to namelist out */ |
---|
105 | { |
---|
106 | int i, l = in->curr > 0 ? in->curr : 1; |
---|
107 | while (out->max < l) grow_name_list(out); |
---|
108 | for (i = 0; i < in->curr; i++) out->index[i] = in->index[i]; |
---|
109 | for (i = 0; i < in->curr; i++) out->inform[i] = in->inform[i]; |
---|
110 | for (i = 0; i < in->curr; i++) out->names[i] = in->names[i]; |
---|
111 | out->curr = in->curr; |
---|
112 | } |
---|
113 | |
---|
114 | void |
---|
115 | grow_name_list(struct name_list* p) |
---|
116 | { |
---|
117 | char rout_name[] = "grow_name_list"; |
---|
118 | char** n_loc = p->names; |
---|
119 | int* l_ind = p->index; |
---|
120 | int* l_inf = p->inform; |
---|
121 | int j, new = 2*p->max; |
---|
122 | |
---|
123 | p->max = new; |
---|
124 | p->names = (char**) mycalloc(rout_name,new, sizeof(char*)); |
---|
125 | p->index = (int*) mycalloc(rout_name,new, sizeof(int)); |
---|
126 | p->inform = (int*) mycalloc(rout_name,new, sizeof(int)); |
---|
127 | for (j = 0; j < p->curr; j++) |
---|
128 | { |
---|
129 | p->names[j] = n_loc[j]; |
---|
130 | p->index[j] = l_ind[j]; |
---|
131 | p->inform[j] = l_inf[j]; |
---|
132 | } |
---|
133 | myfree(rout_name, n_loc); |
---|
134 | myfree(rout_name, l_ind); |
---|
135 | myfree(rout_name, l_inf); |
---|
136 | } |
---|
137 | |
---|
138 | void |
---|
139 | grow_vector_list(struct vector_list* p) |
---|
140 | { |
---|
141 | char rout_name[] = "grow_vector_list"; |
---|
142 | struct double_array** v_loc = p->vectors; |
---|
143 | int j, new = 2*p->max; |
---|
144 | |
---|
145 | p->max = new; |
---|
146 | p->vectors |
---|
147 | = (struct double_array**) mycalloc(rout_name,new, |
---|
148 | sizeof(struct double_array*)); |
---|
149 | for (j = 0; j < p->curr; j++) p->vectors[j] = v_loc[j]; |
---|
150 | myfree(rout_name, v_loc); |
---|
151 | } |
---|
152 | |
---|
153 | int |
---|
154 | add_to_name_list(char* name, int inf, struct name_list* vlist) |
---|
155 | /* adds name to alphabetic name list vlist */ |
---|
156 | /* inf is an integer kept with name */ |
---|
157 | { |
---|
158 | int j, num, low = 0, mid, high = vlist->curr - 1, pos = 0, ret; |
---|
159 | |
---|
160 | if (name == NULL) return -1; |
---|
161 | |
---|
162 | ret = name_list_pos(name, vlist); |
---|
163 | if ( ret < 0) |
---|
164 | { |
---|
165 | while (low <= high) |
---|
166 | { |
---|
167 | mid = (low + high) / 2; |
---|
168 | if ((num = strcmp(name, vlist->names[vlist->index[mid]])) < 0) |
---|
169 | { |
---|
170 | high = mid - 1; pos = mid; |
---|
171 | } |
---|
172 | else if (num > 0) { |
---|
173 | low = mid + 1; pos = low; |
---|
174 | } |
---|
175 | } |
---|
176 | ret = vlist->curr; |
---|
177 | if (vlist->curr == vlist->max) grow_name_list(vlist); |
---|
178 | for (j = vlist->curr; j > pos; j--) vlist->index[j] = vlist->index[j-1]; |
---|
179 | vlist->index[pos] = vlist->curr; |
---|
180 | vlist->inform[vlist->curr] = inf; |
---|
181 | vlist->names[vlist->curr++] = name; |
---|
182 | } |
---|
183 | else vlist->inform[ret] = inf; |
---|
184 | return ret; |
---|
185 | } |
---|
186 | |
---|
187 | int |
---|
188 | name_list_pos(const char* p, struct name_list* vlist) |
---|
189 | { |
---|
190 | int num, mid, low = 0, high = vlist->curr - 1; |
---|
191 | while (low <= high) |
---|
192 | { |
---|
193 | mid = (low + high) / 2; |
---|
194 | if ((num=strcmp(p, vlist->names[vlist->index[mid]])) < 0) high = mid - 1; |
---|
195 | else if ( num > 0) low = mid + 1; |
---|
196 | else return vlist->index[mid]; |
---|
197 | } |
---|
198 | return -1; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|