428
|
1 /* XEmacs routines to deal with range tables.
|
|
2 Copyright (C) 1995 Sun Microsystems, Inc.
|
2421
|
3 Copyright (C) 1995, 2002, 2004 Ben Wing.
|
428
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
|
24 /* Written by Ben Wing, August 1995. */
|
|
25
|
|
26 #include <config.h>
|
|
27 #include "lisp.h"
|
|
28 #include "rangetab.h"
|
|
29
|
|
30 Lisp_Object Qrange_tablep;
|
|
31 Lisp_Object Qrange_table;
|
|
32
|
2421
|
33 Lisp_Object Qstart_closed_end_open;
|
|
34 Lisp_Object Qstart_open_end_open;
|
|
35 Lisp_Object Qstart_closed_end_closed;
|
|
36 Lisp_Object Qstart_open_end_closed;
|
|
37
|
428
|
38
|
|
39 /************************************************************************/
|
|
40 /* Range table object */
|
|
41 /************************************************************************/
|
|
42
|
2421
|
43 static enum range_table_type
|
|
44 range_table_symbol_to_type (Lisp_Object symbol)
|
|
45 {
|
|
46 if (NILP (symbol))
|
|
47 return RANGE_START_CLOSED_END_OPEN;
|
|
48
|
|
49 CHECK_SYMBOL (symbol);
|
|
50 if (EQ (symbol, Qstart_closed_end_open))
|
|
51 return RANGE_START_CLOSED_END_OPEN;
|
|
52 if (EQ (symbol, Qstart_closed_end_closed))
|
|
53 return RANGE_START_CLOSED_END_CLOSED;
|
|
54 if (EQ (symbol, Qstart_open_end_open))
|
|
55 return RANGE_START_OPEN_END_OPEN;
|
|
56 if (EQ (symbol, Qstart_open_end_closed))
|
|
57 return RANGE_START_OPEN_END_CLOSED;
|
|
58
|
|
59 invalid_constant ("Unknown range table type", symbol);
|
|
60 RETURN_NOT_REACHED (RANGE_START_CLOSED_END_OPEN);
|
|
61 }
|
|
62
|
|
63 static Lisp_Object
|
|
64 range_table_type_to_symbol (enum range_table_type type)
|
|
65 {
|
|
66 switch (type)
|
|
67 {
|
|
68 case RANGE_START_CLOSED_END_OPEN:
|
|
69 return Qstart_closed_end_open;
|
|
70 case RANGE_START_CLOSED_END_CLOSED:
|
|
71 return Qstart_closed_end_closed;
|
|
72 case RANGE_START_OPEN_END_OPEN:
|
|
73 return Qstart_open_end_open;
|
|
74 case RANGE_START_OPEN_END_CLOSED:
|
|
75 return Qstart_open_end_closed;
|
|
76 }
|
|
77
|
2500
|
78 ABORT ();
|
2421
|
79 return Qnil;
|
|
80 }
|
|
81
|
428
|
82 /* We use a sorted array of ranges.
|
|
83
|
|
84 #### We should be using the gap array stuff from extents.c. This
|
|
85 is not hard but just requires moving that stuff out of that file. */
|
|
86
|
|
87 static Lisp_Object
|
|
88 mark_range_table (Lisp_Object obj)
|
|
89 {
|
440
|
90 Lisp_Range_Table *rt = XRANGE_TABLE (obj);
|
428
|
91 int i;
|
|
92
|
|
93 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
94 mark_object (Dynarr_at (rt->entries, i).val);
|
|
95 return Qnil;
|
|
96 }
|
|
97
|
|
98 static void
|
2286
|
99 print_range_table (Lisp_Object obj, Lisp_Object printcharfun,
|
|
100 int UNUSED (escapeflag))
|
428
|
101 {
|
440
|
102 Lisp_Range_Table *rt = XRANGE_TABLE (obj);
|
428
|
103 int i;
|
|
104
|
2421
|
105 if (print_readably)
|
|
106 write_fmt_string_lisp (printcharfun, "#s(range-table type %s data (",
|
|
107 1, range_table_type_to_symbol (rt->type));
|
|
108 else
|
|
109 write_c_string (printcharfun, "#<range-table ");
|
428
|
110 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
111 {
|
|
112 struct range_table_entry *rte = Dynarr_atp (rt->entries, i);
|
2421
|
113 int so, ec;
|
428
|
114 if (i > 0)
|
826
|
115 write_c_string (printcharfun, " ");
|
2421
|
116 switch (rt->type)
|
|
117 {
|
|
118 case RANGE_START_CLOSED_END_OPEN: so = 0, ec = 0; break;
|
|
119 case RANGE_START_CLOSED_END_CLOSED: so = 0, ec = 1; break;
|
|
120 case RANGE_START_OPEN_END_OPEN: so = 1, ec = 0; break;
|
|
121 case RANGE_START_OPEN_END_CLOSED: so = 1; ec = 1; break;
|
2500
|
122 default: ABORT (); so = 0, ec = 0; break;
|
2421
|
123 }
|
|
124 write_fmt_string (printcharfun, "%c%ld %ld%c ",
|
|
125 print_readably ? '(' : so ? '(' : '[',
|
|
126 (long) (rte->first - so),
|
|
127 (long) (rte->last - ec),
|
|
128 print_readably ? ')' : ec ? ']' : ')'
|
|
129 );
|
428
|
130 print_internal (rte->val, printcharfun, 1);
|
|
131 }
|
2421
|
132 if (print_readably)
|
|
133 write_c_string (printcharfun, "))");
|
|
134 else
|
|
135 write_fmt_string (printcharfun, " 0x%x>", rt->header.uid);
|
428
|
136 }
|
|
137
|
|
138 static int
|
|
139 range_table_equal (Lisp_Object obj1, Lisp_Object obj2, int depth)
|
|
140 {
|
440
|
141 Lisp_Range_Table *rt1 = XRANGE_TABLE (obj1);
|
|
142 Lisp_Range_Table *rt2 = XRANGE_TABLE (obj2);
|
428
|
143 int i;
|
|
144
|
|
145 if (Dynarr_length (rt1->entries) != Dynarr_length (rt2->entries))
|
|
146 return 0;
|
|
147
|
|
148 for (i = 0; i < Dynarr_length (rt1->entries); i++)
|
|
149 {
|
|
150 struct range_table_entry *rte1 = Dynarr_atp (rt1->entries, i);
|
|
151 struct range_table_entry *rte2 = Dynarr_atp (rt2->entries, i);
|
|
152
|
|
153 if (rte1->first != rte2->first
|
|
154 || rte1->last != rte2->last
|
|
155 || !internal_equal (rte1->val, rte2->val, depth + 1))
|
|
156 return 0;
|
|
157 }
|
|
158
|
|
159 return 1;
|
|
160 }
|
|
161
|
2515
|
162 static Hashcode
|
428
|
163 range_table_entry_hash (struct range_table_entry *rte, int depth)
|
|
164 {
|
|
165 return HASH3 (rte->first, rte->last, internal_hash (rte->val, depth + 1));
|
|
166 }
|
|
167
|
2515
|
168 static Hashcode
|
428
|
169 range_table_hash (Lisp_Object obj, int depth)
|
|
170 {
|
440
|
171 Lisp_Range_Table *rt = XRANGE_TABLE (obj);
|
428
|
172 int i;
|
|
173 int size = Dynarr_length (rt->entries);
|
2515
|
174 Hashcode hash = size;
|
428
|
175
|
|
176 /* approach based on internal_array_hash(). */
|
|
177 if (size <= 5)
|
|
178 {
|
|
179 for (i = 0; i < size; i++)
|
|
180 hash = HASH2 (hash,
|
|
181 range_table_entry_hash (Dynarr_atp (rt->entries, i),
|
|
182 depth));
|
|
183 return hash;
|
|
184 }
|
|
185
|
|
186 /* just pick five elements scattered throughout the array.
|
|
187 A slightly better approach would be to offset by some
|
|
188 noise factor from the points chosen below. */
|
|
189 for (i = 0; i < 5; i++)
|
|
190 hash = HASH2 (hash, range_table_entry_hash (Dynarr_atp (rt->entries,
|
|
191 i*size/5),
|
|
192 depth));
|
|
193 return hash;
|
|
194 }
|
|
195
|
1204
|
196 static const struct memory_description rte_description_1[] = {
|
440
|
197 { XD_LISP_OBJECT, offsetof (range_table_entry, val) },
|
428
|
198 { XD_END }
|
|
199 };
|
|
200
|
1204
|
201 static const struct sized_memory_description rte_description = {
|
440
|
202 sizeof (range_table_entry),
|
428
|
203 rte_description_1
|
|
204 };
|
|
205
|
1204
|
206 static const struct memory_description rted_description_1[] = {
|
440
|
207 XD_DYNARR_DESC (range_table_entry_dynarr, &rte_description),
|
428
|
208 { XD_END }
|
|
209 };
|
|
210
|
1204
|
211 static const struct sized_memory_description rted_description = {
|
440
|
212 sizeof (range_table_entry_dynarr),
|
428
|
213 rted_description_1
|
|
214 };
|
|
215
|
1204
|
216 static const struct memory_description range_table_description[] = {
|
2367
|
217 { XD_BLOCK_PTR, offsetof (Lisp_Range_Table, entries), 1, &rted_description },
|
428
|
218 { XD_END }
|
|
219 };
|
|
220
|
934
|
221 DEFINE_LRECORD_IMPLEMENTATION ("range-table", range_table,
|
|
222 1, /*dumpable-flag*/
|
|
223 mark_range_table, print_range_table, 0,
|
|
224 range_table_equal, range_table_hash,
|
|
225 range_table_description,
|
|
226 Lisp_Range_Table);
|
428
|
227
|
|
228 /************************************************************************/
|
|
229 /* Range table operations */
|
|
230 /************************************************************************/
|
|
231
|
800
|
232 #ifdef ERROR_CHECK_STRUCTURES
|
428
|
233
|
|
234 static void
|
440
|
235 verify_range_table (Lisp_Range_Table *rt)
|
428
|
236 {
|
|
237 int i;
|
|
238
|
|
239 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
240 {
|
|
241 struct range_table_entry *rte = Dynarr_atp (rt->entries, i);
|
|
242 assert (rte->last >= rte->first);
|
|
243 if (i > 0)
|
2421
|
244 assert (Dynarr_at (rt->entries, i - 1).last <= rte->first);
|
428
|
245 }
|
|
246 }
|
|
247
|
|
248 #else
|
|
249
|
|
250 #define verify_range_table(rt)
|
|
251
|
|
252 #endif
|
|
253
|
|
254 /* Look up in a range table without the Dynarr wrapper.
|
|
255 Used also by the unified range table format. */
|
|
256
|
|
257 static Lisp_Object
|
|
258 get_range_table (EMACS_INT pos, int nentries, struct range_table_entry *tab,
|
|
259 Lisp_Object default_)
|
|
260 {
|
|
261 int left = 0, right = nentries;
|
|
262
|
|
263 /* binary search for the entry. Based on similar code in
|
|
264 extent_list_locate(). */
|
|
265 while (left != right)
|
|
266 {
|
|
267 /* RIGHT might not point to a valid entry (i.e. it's at the end
|
|
268 of the list), so NEWPOS must round down. */
|
647
|
269 int newpos = (left + right) >> 1;
|
428
|
270 struct range_table_entry *entry = tab + newpos;
|
2421
|
271 if (pos >= entry->last)
|
|
272 left = newpos + 1;
|
428
|
273 else if (pos < entry->first)
|
|
274 right = newpos;
|
|
275 else
|
|
276 return entry->val;
|
|
277 }
|
|
278
|
|
279 return default_;
|
|
280 }
|
|
281
|
|
282 DEFUN ("range-table-p", Frange_table_p, 1, 1, 0, /*
|
|
283 Return non-nil if OBJECT is a range table.
|
|
284 */
|
|
285 (object))
|
|
286 {
|
|
287 return RANGE_TABLEP (object) ? Qt : Qnil;
|
|
288 }
|
|
289
|
2421
|
290 DEFUN ("range-table-type", Frange_table_type, 1, 1, 0, /*
|
|
291 Return non-nil if OBJECT is a range table.
|
|
292 */
|
|
293 (range_table))
|
|
294 {
|
|
295 CHECK_RANGE_TABLE (range_table);
|
|
296 return range_table_type_to_symbol (XRANGE_TABLE (range_table)->type);
|
|
297 }
|
|
298
|
|
299 DEFUN ("make-range-table", Fmake_range_table, 0, 1, 0, /*
|
428
|
300 Return a new, empty range table.
|
|
301 You can manipulate it using `put-range-table', `get-range-table',
|
|
302 `remove-range-table', and `clear-range-table'.
|
2421
|
303 Range tables allow you to efficiently set values for ranges of integers.
|
|
304
|
|
305 TYPE is a symbol indicating how ranges are assumed to function at their
|
|
306 ends. It can be one of
|
|
307
|
|
308 SYMBOL RANGE-START RANGE-END
|
|
309 ------ ----------- ---------
|
|
310 `start-closed-end-open' (the default) closed open
|
|
311 `start-closed-end-closed' closed closed
|
|
312 `start-open-end-open' open open
|
|
313 `start-open-end-closed' open closed
|
|
314
|
|
315 A `closed' endpoint of a range means that the number at that end is included
|
|
316 in the range. For an `open' endpoint, the number would not be included.
|
|
317
|
|
318 For example, a closed-open range from 5 to 20 would be indicated as [5,
|
|
319 20) where a bracket indicates a closed end and a parenthesis an open end,
|
|
320 and would mean `all the numbers between 5 and 20', including 5 but not 20.
|
|
321 This seems a little strange at first but is in fact extremely common in
|
|
322 the outside world as well as in computers and makes things work sensibly.
|
|
323 For example, if I say "there are seven days between today and next week
|
|
324 today", I'm including today but not next week today; if I included both,
|
|
325 there would be eight days. Similarly, there are 15 (= 20 - 5) elements in
|
|
326 the range [5, 20), but 16 in the range [5, 20].
|
428
|
327 */
|
2421
|
328 (type))
|
428
|
329 {
|
440
|
330 Lisp_Range_Table *rt = alloc_lcrecord_type (Lisp_Range_Table,
|
|
331 &lrecord_range_table);
|
428
|
332 rt->entries = Dynarr_new (range_table_entry);
|
2421
|
333 rt->type = range_table_symbol_to_type (type);
|
793
|
334 return wrap_range_table (rt);
|
428
|
335 }
|
|
336
|
|
337 DEFUN ("copy-range-table", Fcopy_range_table, 1, 1, 0, /*
|
444
|
338 Return a new range table which is a copy of RANGE-TABLE.
|
|
339 It will contain the same values for the same ranges as RANGE-TABLE.
|
|
340 The values will not themselves be copied.
|
428
|
341 */
|
444
|
342 (range_table))
|
428
|
343 {
|
440
|
344 Lisp_Range_Table *rt, *rtnew;
|
428
|
345
|
444
|
346 CHECK_RANGE_TABLE (range_table);
|
|
347 rt = XRANGE_TABLE (range_table);
|
428
|
348
|
440
|
349 rtnew = alloc_lcrecord_type (Lisp_Range_Table, &lrecord_range_table);
|
428
|
350 rtnew->entries = Dynarr_new (range_table_entry);
|
2421
|
351 rtnew->type = rt->type;
|
428
|
352
|
|
353 Dynarr_add_many (rtnew->entries, Dynarr_atp (rt->entries, 0),
|
|
354 Dynarr_length (rt->entries));
|
793
|
355 return wrap_range_table (rtnew);
|
428
|
356 }
|
|
357
|
|
358 DEFUN ("get-range-table", Fget_range_table, 2, 3, 0, /*
|
444
|
359 Find value for position POS in RANGE-TABLE.
|
428
|
360 If there is no corresponding value, return DEFAULT (defaults to nil).
|
|
361 */
|
444
|
362 (pos, range_table, default_))
|
428
|
363 {
|
440
|
364 Lisp_Range_Table *rt;
|
428
|
365
|
444
|
366 CHECK_RANGE_TABLE (range_table);
|
|
367 rt = XRANGE_TABLE (range_table);
|
428
|
368
|
|
369 CHECK_INT_COERCE_CHAR (pos);
|
|
370
|
|
371 return get_range_table (XINT (pos), Dynarr_length (rt->entries),
|
|
372 Dynarr_atp (rt->entries, 0), default_);
|
|
373 }
|
|
374
|
|
375 void
|
|
376 put_range_table (Lisp_Object table, EMACS_INT first,
|
|
377 EMACS_INT last, Lisp_Object val)
|
|
378 {
|
|
379 int i;
|
|
380 int insert_me_here = -1;
|
440
|
381 Lisp_Range_Table *rt = XRANGE_TABLE (table);
|
428
|
382
|
2421
|
383 /* Fix up the numbers in accordance with the open/closedness to make
|
|
384 them behave like default open/closed. */
|
|
385
|
|
386 switch (rt->type)
|
|
387 {
|
|
388 case RANGE_START_CLOSED_END_OPEN: break;
|
|
389 case RANGE_START_CLOSED_END_CLOSED: last++; break;
|
|
390 case RANGE_START_OPEN_END_OPEN: first++; break;
|
|
391 case RANGE_START_OPEN_END_CLOSED: first++, last++; break;
|
|
392 }
|
|
393
|
|
394 if (first == last)
|
|
395 return;
|
|
396 if (first > last)
|
|
397 /* This will happen if originally first == last and both ends are
|
|
398 open. #### Should we signal an error? */
|
|
399 return;
|
|
400
|
428
|
401 /* Now insert in the proper place. This gets tricky because
|
|
402 we may be overlapping one or more existing ranges and need
|
|
403 to fix them up. */
|
|
404
|
|
405 /* First delete all sections of any existing ranges that overlap
|
|
406 the new range. */
|
|
407 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
408 {
|
|
409 struct range_table_entry *entry = Dynarr_atp (rt->entries, i);
|
|
410 /* We insert before the first range that begins at or after the
|
|
411 new range. */
|
|
412 if (entry->first >= first && insert_me_here < 0)
|
|
413 insert_me_here = i;
|
|
414 if (entry->last < first)
|
|
415 /* completely before the new range. */
|
|
416 continue;
|
|
417 if (entry->first > last)
|
|
418 /* completely after the new range. No more possibilities of
|
|
419 finding overlapping ranges. */
|
|
420 break;
|
2421
|
421 /* At this point the existing ENTRY overlaps or touches the new one. */
|
428
|
422 if (entry->first < first && entry->last <= last)
|
|
423 {
|
|
424 /* looks like:
|
|
425
|
2421
|
426 [ NEW )
|
|
427 [ EXISTING )
|
|
428
|
|
429 or
|
|
430
|
|
431 [ NEW )
|
|
432 [ EXISTING )
|
428
|
433
|
|
434 */
|
|
435 /* truncate the end off of it. */
|
2421
|
436 entry->last = first;
|
428
|
437 }
|
|
438 else if (entry->first < first && entry->last > last)
|
|
439 /* looks like:
|
|
440
|
2421
|
441 [ NEW )
|
|
442 [ EXISTING )
|
428
|
443
|
|
444 */
|
|
445 /* need to split this one in two. */
|
|
446 {
|
|
447 struct range_table_entry insert_me_too;
|
|
448
|
2421
|
449 insert_me_too.first = last;
|
428
|
450 insert_me_too.last = entry->last;
|
|
451 insert_me_too.val = entry->val;
|
2421
|
452 entry->last = first;
|
428
|
453 Dynarr_insert_many (rt->entries, &insert_me_too, 1, i + 1);
|
|
454 }
|
2421
|
455 else if (entry->last >= last)
|
428
|
456 {
|
|
457 /* looks like:
|
|
458
|
2421
|
459 [ NEW )
|
|
460 [ EXISTING )
|
|
461
|
|
462 or
|
|
463
|
|
464 [ NEW )
|
|
465 [ EXISTING )
|
428
|
466
|
|
467 */
|
|
468 /* truncate the start off of it. */
|
2421
|
469 entry->first = last;
|
428
|
470 }
|
|
471 else
|
|
472 {
|
|
473 /* existing is entirely within new. */
|
|
474 Dynarr_delete_many (rt->entries, i, 1);
|
|
475 i--; /* back up since everything shifted one to the left. */
|
|
476 }
|
|
477 }
|
|
478
|
|
479 /* Someone asked us to delete the range, not insert it. */
|
|
480 if (UNBOUNDP (val))
|
|
481 return;
|
|
482
|
|
483 /* Now insert the new entry, maybe at the end. */
|
|
484
|
|
485 if (insert_me_here < 0)
|
|
486 insert_me_here = i;
|
|
487
|
|
488 {
|
|
489 struct range_table_entry insert_me;
|
|
490
|
|
491 insert_me.first = first;
|
|
492 insert_me.last = last;
|
|
493 insert_me.val = val;
|
|
494
|
|
495 Dynarr_insert_many (rt->entries, &insert_me, 1, insert_me_here);
|
|
496 }
|
|
497
|
|
498 /* Now see if we can combine this entry with adjacent ones just
|
|
499 before or after. */
|
|
500
|
|
501 if (insert_me_here > 0)
|
|
502 {
|
|
503 struct range_table_entry *entry = Dynarr_atp (rt->entries,
|
|
504 insert_me_here - 1);
|
2421
|
505 if (EQ (val, entry->val) && entry->last == first)
|
428
|
506 {
|
|
507 entry->last = last;
|
|
508 Dynarr_delete_many (rt->entries, insert_me_here, 1);
|
|
509 insert_me_here--;
|
|
510 /* We have morphed into a larger range. Update our records
|
|
511 in case we also combine with the one after. */
|
|
512 first = entry->first;
|
|
513 }
|
|
514 }
|
|
515
|
|
516 if (insert_me_here < Dynarr_length (rt->entries) - 1)
|
|
517 {
|
|
518 struct range_table_entry *entry = Dynarr_atp (rt->entries,
|
|
519 insert_me_here + 1);
|
2421
|
520 if (EQ (val, entry->val) && entry->first == last)
|
428
|
521 {
|
|
522 entry->first = first;
|
|
523 Dynarr_delete_many (rt->entries, insert_me_here, 1);
|
|
524 }
|
|
525 }
|
|
526 }
|
|
527
|
|
528 DEFUN ("put-range-table", Fput_range_table, 4, 4, 0, /*
|
2421
|
529 Set the value for range START .. END to be VALUE in RANGE-TABLE.
|
428
|
530 */
|
444
|
531 (start, end, value, range_table))
|
428
|
532 {
|
|
533 EMACS_INT first, last;
|
|
534
|
444
|
535 CHECK_RANGE_TABLE (range_table);
|
428
|
536 CHECK_INT_COERCE_CHAR (start);
|
|
537 first = XINT (start);
|
|
538 CHECK_INT_COERCE_CHAR (end);
|
|
539 last = XINT (end);
|
|
540 if (first > last)
|
563
|
541 invalid_argument_2 ("start must be <= end", start, end);
|
428
|
542
|
444
|
543 put_range_table (range_table, first, last, value);
|
|
544 verify_range_table (XRANGE_TABLE (range_table));
|
428
|
545 return Qnil;
|
|
546 }
|
|
547
|
|
548 DEFUN ("remove-range-table", Fremove_range_table, 3, 3, 0, /*
|
2421
|
549 Remove the value for range START .. END in RANGE-TABLE.
|
428
|
550 */
|
444
|
551 (start, end, range_table))
|
428
|
552 {
|
444
|
553 return Fput_range_table (start, end, Qunbound, range_table);
|
428
|
554 }
|
|
555
|
|
556 DEFUN ("clear-range-table", Fclear_range_table, 1, 1, 0, /*
|
444
|
557 Flush RANGE-TABLE.
|
428
|
558 */
|
444
|
559 (range_table))
|
428
|
560 {
|
444
|
561 CHECK_RANGE_TABLE (range_table);
|
|
562 Dynarr_reset (XRANGE_TABLE (range_table)->entries);
|
428
|
563 return Qnil;
|
|
564 }
|
|
565
|
|
566 DEFUN ("map-range-table", Fmap_range_table, 2, 2, 0, /*
|
444
|
567 Map FUNCTION over entries in RANGE-TABLE, calling it with three args,
|
428
|
568 the beginning and end of the range and the corresponding value.
|
442
|
569
|
|
570 Results are guaranteed to be correct (i.e. each entry processed
|
|
571 exactly once) if FUNCTION modifies or deletes the current entry
|
444
|
572 \(i.e. passes the current range to `put-range-table' or
|
442
|
573 `remove-range-table'), but not otherwise.
|
428
|
574 */
|
444
|
575 (function, range_table))
|
428
|
576 {
|
442
|
577 Lisp_Range_Table *rt;
|
|
578 int i;
|
|
579
|
444
|
580 CHECK_RANGE_TABLE (range_table);
|
442
|
581 CHECK_FUNCTION (function);
|
|
582
|
444
|
583 rt = XRANGE_TABLE (range_table);
|
442
|
584
|
|
585 /* Do not "optimize" by pulling out the length computation below!
|
|
586 FUNCTION may have changed the table. */
|
|
587 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
588 {
|
|
589 struct range_table_entry *entry = Dynarr_atp (rt->entries, i);
|
|
590 EMACS_INT first, last;
|
|
591 Lisp_Object args[4];
|
|
592 int oldlen;
|
|
593
|
|
594 again:
|
|
595 first = entry->first;
|
|
596 last = entry->last;
|
|
597 oldlen = Dynarr_length (rt->entries);
|
|
598 args[0] = function;
|
|
599 args[1] = make_int (first);
|
|
600 args[2] = make_int (last);
|
|
601 args[3] = entry->val;
|
|
602 Ffuncall (countof (args), args);
|
|
603 /* Has FUNCTION removed the entry? */
|
|
604 if (oldlen > Dynarr_length (rt->entries)
|
|
605 && i < Dynarr_length (rt->entries)
|
|
606 && (first != entry->first || last != entry->last))
|
|
607 goto again;
|
|
608 }
|
|
609
|
428
|
610 return Qnil;
|
|
611 }
|
|
612
|
|
613
|
|
614 /************************************************************************/
|
|
615 /* Range table read syntax */
|
|
616 /************************************************************************/
|
|
617
|
|
618 static int
|
2421
|
619 rangetab_type_validate (Lisp_Object UNUSED (keyword), Lisp_Object value,
|
|
620 Error_Behavior UNUSED (errb))
|
|
621 {
|
|
622 /* #### should deal with ERRB */
|
|
623 range_table_symbol_to_type (value);
|
|
624 return 1;
|
|
625 }
|
|
626
|
|
627 static int
|
2286
|
628 rangetab_data_validate (Lisp_Object UNUSED (keyword), Lisp_Object value,
|
|
629 Error_Behavior UNUSED (errb))
|
428
|
630 {
|
2367
|
631 /* #### should deal with ERRB */
|
|
632 EXTERNAL_PROPERTY_LIST_LOOP_3 (range, data, value)
|
428
|
633 {
|
|
634 if (!INTP (range) && !CHARP (range)
|
|
635 && !(CONSP (range) && CONSP (XCDR (range))
|
|
636 && NILP (XCDR (XCDR (range)))
|
|
637 && (INTP (XCAR (range)) || CHARP (XCAR (range)))
|
|
638 && (INTP (XCAR (XCDR (range))) || CHARP (XCAR (XCDR (range))))))
|
563
|
639 sferror ("Invalid range format", range);
|
428
|
640 }
|
|
641
|
|
642 return 1;
|
|
643 }
|
|
644
|
|
645 static Lisp_Object
|
2421
|
646 rangetab_instantiate (Lisp_Object plist)
|
428
|
647 {
|
2425
|
648 Lisp_Object data = Qnil, type = Qnil, rangetab;
|
428
|
649
|
2421
|
650 PROPERTY_LIST_LOOP_3 (key, value, plist)
|
428
|
651 {
|
2421
|
652 if (EQ (key, Qtype)) type = value;
|
|
653 else if (EQ (key, Qdata)) data = value;
|
|
654 else
|
2500
|
655 ABORT ();
|
2421
|
656 }
|
|
657
|
2425
|
658 rangetab = Fmake_range_table (type);
|
428
|
659
|
2421
|
660 {
|
|
661 PROPERTY_LIST_LOOP_3 (range, val, data)
|
|
662 {
|
|
663 if (CONSP (range))
|
|
664 Fput_range_table (Fcar (range), Fcar (Fcdr (range)), val,
|
|
665 rangetab);
|
|
666 else
|
|
667 Fput_range_table (range, range, val, rangetab);
|
|
668 }
|
|
669 }
|
428
|
670
|
|
671 return rangetab;
|
|
672 }
|
|
673
|
|
674
|
|
675 /************************************************************************/
|
|
676 /* Unified range tables */
|
|
677 /************************************************************************/
|
|
678
|
|
679 /* A "unified range table" is a format for storing range tables
|
|
680 as contiguous blocks of memory. This is used by the regexp
|
|
681 code, which needs to use range tables to properly handle []
|
|
682 constructs in the presence of extended characters but wants to
|
|
683 store an entire compiled pattern as a contiguous block of memory.
|
|
684
|
|
685 Unified range tables are designed so that they can be placed
|
|
686 at an arbitrary (possibly mis-aligned) place in memory.
|
|
687 (Dealing with alignment is a pain in the ass.)
|
|
688
|
|
689 WARNING: No provisions for garbage collection are currently made.
|
|
690 This means that there must not be any Lisp objects in a unified
|
|
691 range table that need to be marked for garbage collection.
|
|
692 Good candidates for objects that can go into a range table are
|
|
693
|
|
694 -- numbers and characters (do not need to be marked)
|
|
695 -- nil, t (marked elsewhere)
|
|
696 -- charsets and coding systems (automatically marked because
|
|
697 they are in a marked list,
|
|
698 and can't be removed)
|
|
699
|
|
700 Good but slightly less so:
|
|
701
|
|
702 -- symbols (could be uninterned, but that is not likely)
|
|
703
|
|
704 Somewhat less good:
|
|
705
|
|
706 -- buffers, frames, devices (could get deleted)
|
|
707
|
|
708
|
|
709 It is expected that you work with range tables in the normal
|
|
710 format and then convert to unified format when you are done
|
|
711 making modifications. As such, no functions are provided
|
|
712 for modifying a unified range table. The only operations
|
|
713 you can do to unified range tables are
|
|
714
|
|
715 -- look up a value
|
|
716 -- retrieve all the ranges in an iterative fashion
|
|
717
|
|
718 */
|
|
719
|
|
720 /* The format of a unified range table is as follows:
|
|
721
|
|
722 -- The first byte contains the number of bytes to skip to find the
|
|
723 actual start of the table. This deals with alignment constraints,
|
|
724 since the table might want to go at any arbitrary place in memory.
|
|
725 -- The next three bytes contain the number of bytes to skip (from the
|
|
726 *first* byte) to find the stuff after the table. It's stored in
|
|
727 little-endian format because that's how God intended things. We don't
|
|
728 necessarily start the stuff at the very end of the table because
|
|
729 we want to have at least ALIGNOF (EMACS_INT) extra space in case
|
|
730 we have to move the range table around. (It appears that some
|
|
731 architectures don't maintain alignment when reallocing.)
|
|
732 -- At the prescribed offset is a struct unified_range_table, containing
|
|
733 some number of `struct range_table_entry' entries. */
|
|
734
|
|
735 struct unified_range_table
|
|
736 {
|
|
737 int nentries;
|
|
738 struct range_table_entry first;
|
|
739 };
|
|
740
|
|
741 /* Return size in bytes needed to store the data in a range table. */
|
|
742
|
|
743 int
|
|
744 unified_range_table_bytes_needed (Lisp_Object rangetab)
|
|
745 {
|
|
746 return (sizeof (struct range_table_entry) *
|
|
747 (Dynarr_length (XRANGE_TABLE (rangetab)->entries) - 1) +
|
|
748 sizeof (struct unified_range_table) +
|
|
749 /* ALIGNOF a struct may be too big. */
|
|
750 /* We have four bytes for the size numbers, and an extra
|
|
751 four or eight bytes for making sure we get the alignment
|
|
752 OK. */
|
|
753 ALIGNOF (EMACS_INT) + 4);
|
|
754 }
|
|
755
|
|
756 /* Convert a range table into unified format and store in DEST,
|
|
757 which must be able to hold the number of bytes returned by
|
|
758 range_table_bytes_needed(). */
|
|
759
|
|
760 void
|
|
761 unified_range_table_copy_data (Lisp_Object rangetab, void *dest)
|
|
762 {
|
|
763 /* We cast to the above structure rather than just casting to
|
|
764 char * and adding sizeof(int), because that will lead to
|
|
765 mis-aligned data on the Alpha machines. */
|
|
766 struct unified_range_table *un;
|
|
767 range_table_entry_dynarr *rted = XRANGE_TABLE (rangetab)->entries;
|
|
768 int total_needed = unified_range_table_bytes_needed (rangetab);
|
826
|
769 void *new_dest = ALIGN_PTR ((char *) dest + 4, EMACS_INT);
|
428
|
770
|
|
771 * (char *) dest = (char) ((char *) new_dest - (char *) dest);
|
|
772 * ((unsigned char *) dest + 1) = total_needed & 0xFF;
|
|
773 total_needed >>= 8;
|
|
774 * ((unsigned char *) dest + 2) = total_needed & 0xFF;
|
|
775 total_needed >>= 8;
|
|
776 * ((unsigned char *) dest + 3) = total_needed & 0xFF;
|
|
777 un = (struct unified_range_table *) new_dest;
|
|
778 un->nentries = Dynarr_length (rted);
|
|
779 memcpy (&un->first, Dynarr_atp (rted, 0),
|
|
780 sizeof (struct range_table_entry) * Dynarr_length (rted));
|
|
781 }
|
|
782
|
|
783 /* Return number of bytes actually used by a unified range table. */
|
|
784
|
|
785 int
|
|
786 unified_range_table_bytes_used (void *unrangetab)
|
|
787 {
|
|
788 return ((* ((unsigned char *) unrangetab + 1))
|
|
789 + ((* ((unsigned char *) unrangetab + 2)) << 8)
|
|
790 + ((* ((unsigned char *) unrangetab + 3)) << 16));
|
|
791 }
|
|
792
|
|
793 /* Make sure the table is aligned, and move it around if it's not. */
|
|
794 static void
|
|
795 align_the_damn_table (void *unrangetab)
|
|
796 {
|
|
797 void *cur_dest = (char *) unrangetab + * (char *) unrangetab;
|
826
|
798 if (cur_dest != ALIGN_PTR (cur_dest, EMACS_INT))
|
428
|
799 {
|
|
800 int count = (unified_range_table_bytes_used (unrangetab) - 4
|
|
801 - ALIGNOF (EMACS_INT));
|
|
802 /* Find the proper location, just like above. */
|
826
|
803 void *new_dest = ALIGN_PTR ((char *) unrangetab + 4, EMACS_INT);
|
428
|
804 /* memmove() works in the presence of overlapping data. */
|
|
805 memmove (new_dest, cur_dest, count);
|
|
806 * (char *) unrangetab = (char) ((char *) new_dest - (char *) unrangetab);
|
|
807 }
|
|
808 }
|
|
809
|
|
810 /* Look up a value in a unified range table. */
|
|
811
|
|
812 Lisp_Object
|
|
813 unified_range_table_lookup (void *unrangetab, EMACS_INT pos,
|
|
814 Lisp_Object default_)
|
|
815 {
|
|
816 void *new_dest;
|
|
817 struct unified_range_table *un;
|
|
818
|
|
819 align_the_damn_table (unrangetab);
|
|
820 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
821 un = (struct unified_range_table *) new_dest;
|
|
822
|
|
823 return get_range_table (pos, un->nentries, &un->first, default_);
|
|
824 }
|
|
825
|
|
826 /* Return number of entries in a unified range table. */
|
|
827
|
|
828 int
|
|
829 unified_range_table_nentries (void *unrangetab)
|
|
830 {
|
|
831 void *new_dest;
|
|
832 struct unified_range_table *un;
|
|
833
|
|
834 align_the_damn_table (unrangetab);
|
|
835 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
836 un = (struct unified_range_table *) new_dest;
|
|
837 return un->nentries;
|
|
838 }
|
|
839
|
|
840 /* Return the OFFSETth range (counting from 0) in UNRANGETAB. */
|
|
841 void
|
|
842 unified_range_table_get_range (void *unrangetab, int offset,
|
|
843 EMACS_INT *min, EMACS_INT *max,
|
|
844 Lisp_Object *val)
|
|
845 {
|
|
846 void *new_dest;
|
|
847 struct unified_range_table *un;
|
|
848 struct range_table_entry *tab;
|
|
849
|
|
850 align_the_damn_table (unrangetab);
|
|
851 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
852 un = (struct unified_range_table *) new_dest;
|
|
853
|
|
854 assert (offset >= 0 && offset < un->nentries);
|
|
855 tab = (&un->first) + offset;
|
|
856 *min = tab->first;
|
|
857 *max = tab->last;
|
|
858 *val = tab->val;
|
|
859 }
|
|
860
|
|
861
|
|
862 /************************************************************************/
|
|
863 /* Initialization */
|
|
864 /************************************************************************/
|
|
865
|
|
866 void
|
|
867 syms_of_rangetab (void)
|
|
868 {
|
442
|
869 INIT_LRECORD_IMPLEMENTATION (range_table);
|
|
870
|
563
|
871 DEFSYMBOL_MULTIWORD_PREDICATE (Qrange_tablep);
|
|
872 DEFSYMBOL (Qrange_table);
|
428
|
873
|
2421
|
874 DEFSYMBOL (Qstart_closed_end_open);
|
|
875 DEFSYMBOL (Qstart_open_end_open);
|
|
876 DEFSYMBOL (Qstart_closed_end_closed);
|
|
877 DEFSYMBOL (Qstart_open_end_closed);
|
|
878
|
428
|
879 DEFSUBR (Frange_table_p);
|
2421
|
880 DEFSUBR (Frange_table_type);
|
428
|
881 DEFSUBR (Fmake_range_table);
|
|
882 DEFSUBR (Fcopy_range_table);
|
|
883 DEFSUBR (Fget_range_table);
|
|
884 DEFSUBR (Fput_range_table);
|
|
885 DEFSUBR (Fremove_range_table);
|
|
886 DEFSUBR (Fclear_range_table);
|
|
887 DEFSUBR (Fmap_range_table);
|
|
888 }
|
|
889
|
|
890 void
|
|
891 structure_type_create_rangetab (void)
|
|
892 {
|
|
893 struct structure_type *st;
|
|
894
|
|
895 st = define_structure_type (Qrange_table, 0, rangetab_instantiate);
|
|
896
|
|
897 define_structure_type_keyword (st, Qdata, rangetab_data_validate);
|
2421
|
898 define_structure_type_keyword (st, Qtype, rangetab_type_validate);
|
428
|
899 }
|