comparison src/intervals.c @ 39851:a535a2e3b5c4

(traverse_intervals): Use less stack space. (traverse_intervals_noorder): New function. (search_for_interval, count_intervals): Use it.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Fri, 12 Oct 2001 21:53:44 +0000
parents 0b986bb45526
children a1e434124570
comparison
equal deleted inserted replaced
39850:80b844540f64 39851:a535a2e3b5c4
186 return 1; 186 return 1;
187 } 187 }
188 188
189 189
190 /* Traverse an interval tree TREE, performing FUNCTION on each node. 190 /* Traverse an interval tree TREE, performing FUNCTION on each node.
191 No guarantee is made about the order of traversal.
192 Pass FUNCTION two args: an interval, and ARG. */
193
194 void
195 traverse_intervals_noorder (tree, function, arg)
196 INTERVAL tree;
197 void (* function) P_ ((INTERVAL, Lisp_Object));
198 Lisp_Object arg;
199 {
200 /* Minimize stack usage. */
201 while (!NULL_INTERVAL_P (tree))
202 {
203 (*function) (tree, arg);
204 if (NULL_INTERVAL_P (tree->right))
205 tree = tree->left;
206 else
207 {
208 traverse_intervals_noorder (tree->left, function, arg);
209 tree = tree->right;
210 }
211 }
212 }
213
214 /* Traverse an interval tree TREE, performing FUNCTION on each node.
191 Pass FUNCTION two args: an interval, and ARG. */ 215 Pass FUNCTION two args: an interval, and ARG. */
192 216
193 void 217 void
194 traverse_intervals (tree, position, depth, function, arg) 218 traverse_intervals (tree, position, depth, function, arg)
195 INTERVAL tree; 219 INTERVAL tree;
196 int position, depth; 220 int position, depth;
197 void (* function) P_ ((INTERVAL, Lisp_Object)); 221 void (* function) P_ ((INTERVAL, Lisp_Object));
198 Lisp_Object arg; 222 Lisp_Object arg;
199 { 223 {
200 if (NULL_INTERVAL_P (tree)) 224 while (!NULL_INTERVAL_P (tree))
201 return; 225 {
202 226 traverse_intervals (tree->left, position, depth + 1, function, arg);
203 traverse_intervals (tree->left, position, depth + 1, function, arg); 227 position += LEFT_TOTAL_LENGTH (tree);
204 position += LEFT_TOTAL_LENGTH (tree); 228 tree->position = position;
205 tree->position = position; 229 (*function) (tree, arg);
206 (*function) (tree, arg); 230 position += LENGTH (tree); tree = tree->right; depth++;
207 position += LENGTH (tree); 231 }
208 traverse_intervals (tree->right, position, depth + 1, function, arg);
209 } 232 }
210 233
211 #if 0 234 #if 0
212 235
213 static int icount; 236 static int icount;
234 register INTERVAL i, tree; 257 register INTERVAL i, tree;
235 { 258 {
236 icount = 0; 259 icount = 0;
237 search_interval = i; 260 search_interval = i;
238 found_interval = NULL_INTERVAL; 261 found_interval = NULL_INTERVAL;
239 traverse_intervals (tree, 1, 0, &check_for_interval, Qnil); 262 traverse_intervals_noorder (tree, &check_for_interval, Qnil);
240 return found_interval; 263 return found_interval;
241 } 264 }
242 265
243 static void 266 static void
244 inc_interval_count (i) 267 inc_interval_count (i)
256 register INTERVAL i; 279 register INTERVAL i;
257 { 280 {
258 icount = 0; 281 icount = 0;
259 idepth = 0; 282 idepth = 0;
260 zero_length = 0; 283 zero_length = 0;
261 traverse_intervals (i, 1, 0, &inc_interval_count, Qnil); 284 traverse_intervals_noorder (i, &inc_interval_count, Qnil);
262 285
263 return icount; 286 return icount;
264 } 287 }
265 288
266 static INTERVAL 289 static INTERVAL
283 B => A 306 B => A
284 / \ / \ 307 / \ / \
285 c c 308 c c
286 */ 309 */
287 310
288 static INTERVAL 311 static INLINE INTERVAL
289 rotate_right (interval) 312 rotate_right (interval)
290 INTERVAL interval; 313 INTERVAL interval;
291 { 314 {
292 INTERVAL i; 315 INTERVAL i;
293 INTERVAL B = interval->left; 316 INTERVAL B = interval->left;
329 B => A 352 B => A
330 / \ / \ 353 / \ / \
331 c c 354 c c
332 */ 355 */
333 356
334 static INTERVAL 357 static INLINE INTERVAL
335 rotate_left (interval) 358 rotate_left (interval)
336 INTERVAL interval; 359 INTERVAL interval;
337 { 360 {
338 INTERVAL i; 361 INTERVAL i;
339 INTERVAL B = interval->right; 362 INTERVAL B = interval->right;