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[] = {
|
2551
|
217 { XD_BLOCK_PTR, offsetof (Lisp_Range_Table, entries), 1,
|
|
218 { &rted_description } },
|
428
|
219 { XD_END }
|
|
220 };
|
|
221
|
934
|
222 DEFINE_LRECORD_IMPLEMENTATION ("range-table", range_table,
|
|
223 1, /*dumpable-flag*/
|
|
224 mark_range_table, print_range_table, 0,
|
|
225 range_table_equal, range_table_hash,
|
|
226 range_table_description,
|
|
227 Lisp_Range_Table);
|
428
|
228
|
|
229 /************************************************************************/
|
|
230 /* Range table operations */
|
|
231 /************************************************************************/
|
|
232
|
800
|
233 #ifdef ERROR_CHECK_STRUCTURES
|
428
|
234
|
|
235 static void
|
440
|
236 verify_range_table (Lisp_Range_Table *rt)
|
428
|
237 {
|
|
238 int i;
|
|
239
|
|
240 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
241 {
|
|
242 struct range_table_entry *rte = Dynarr_atp (rt->entries, i);
|
|
243 assert (rte->last >= rte->first);
|
|
244 if (i > 0)
|
2421
|
245 assert (Dynarr_at (rt->entries, i - 1).last <= rte->first);
|
428
|
246 }
|
|
247 }
|
|
248
|
|
249 #else
|
|
250
|
|
251 #define verify_range_table(rt)
|
|
252
|
|
253 #endif
|
|
254
|
|
255 /* Look up in a range table without the Dynarr wrapper.
|
|
256 Used also by the unified range table format. */
|
|
257
|
|
258 static Lisp_Object
|
|
259 get_range_table (EMACS_INT pos, int nentries, struct range_table_entry *tab,
|
|
260 Lisp_Object default_)
|
|
261 {
|
|
262 int left = 0, right = nentries;
|
|
263
|
|
264 /* binary search for the entry. Based on similar code in
|
|
265 extent_list_locate(). */
|
|
266 while (left != right)
|
|
267 {
|
|
268 /* RIGHT might not point to a valid entry (i.e. it's at the end
|
|
269 of the list), so NEWPOS must round down. */
|
647
|
270 int newpos = (left + right) >> 1;
|
428
|
271 struct range_table_entry *entry = tab + newpos;
|
2421
|
272 if (pos >= entry->last)
|
|
273 left = newpos + 1;
|
428
|
274 else if (pos < entry->first)
|
|
275 right = newpos;
|
|
276 else
|
|
277 return entry->val;
|
|
278 }
|
|
279
|
|
280 return default_;
|
|
281 }
|
|
282
|
|
283 DEFUN ("range-table-p", Frange_table_p, 1, 1, 0, /*
|
|
284 Return non-nil if OBJECT is a range table.
|
|
285 */
|
|
286 (object))
|
|
287 {
|
|
288 return RANGE_TABLEP (object) ? Qt : Qnil;
|
|
289 }
|
|
290
|
2421
|
291 DEFUN ("range-table-type", Frange_table_type, 1, 1, 0, /*
|
|
292 Return non-nil if OBJECT is a range table.
|
|
293 */
|
|
294 (range_table))
|
|
295 {
|
|
296 CHECK_RANGE_TABLE (range_table);
|
|
297 return range_table_type_to_symbol (XRANGE_TABLE (range_table)->type);
|
|
298 }
|
|
299
|
|
300 DEFUN ("make-range-table", Fmake_range_table, 0, 1, 0, /*
|
428
|
301 Return a new, empty range table.
|
|
302 You can manipulate it using `put-range-table', `get-range-table',
|
|
303 `remove-range-table', and `clear-range-table'.
|
2421
|
304 Range tables allow you to efficiently set values for ranges of integers.
|
|
305
|
|
306 TYPE is a symbol indicating how ranges are assumed to function at their
|
|
307 ends. It can be one of
|
|
308
|
|
309 SYMBOL RANGE-START RANGE-END
|
|
310 ------ ----------- ---------
|
|
311 `start-closed-end-open' (the default) closed open
|
|
312 `start-closed-end-closed' closed closed
|
|
313 `start-open-end-open' open open
|
|
314 `start-open-end-closed' open closed
|
|
315
|
|
316 A `closed' endpoint of a range means that the number at that end is included
|
|
317 in the range. For an `open' endpoint, the number would not be included.
|
|
318
|
|
319 For example, a closed-open range from 5 to 20 would be indicated as [5,
|
|
320 20) where a bracket indicates a closed end and a parenthesis an open end,
|
|
321 and would mean `all the numbers between 5 and 20', including 5 but not 20.
|
|
322 This seems a little strange at first but is in fact extremely common in
|
|
323 the outside world as well as in computers and makes things work sensibly.
|
|
324 For example, if I say "there are seven days between today and next week
|
|
325 today", I'm including today but not next week today; if I included both,
|
|
326 there would be eight days. Similarly, there are 15 (= 20 - 5) elements in
|
|
327 the range [5, 20), but 16 in the range [5, 20].
|
428
|
328 */
|
2421
|
329 (type))
|
428
|
330 {
|
440
|
331 Lisp_Range_Table *rt = alloc_lcrecord_type (Lisp_Range_Table,
|
|
332 &lrecord_range_table);
|
428
|
333 rt->entries = Dynarr_new (range_table_entry);
|
2421
|
334 rt->type = range_table_symbol_to_type (type);
|
793
|
335 return wrap_range_table (rt);
|
428
|
336 }
|
|
337
|
|
338 DEFUN ("copy-range-table", Fcopy_range_table, 1, 1, 0, /*
|
444
|
339 Return a new range table which is a copy of RANGE-TABLE.
|
|
340 It will contain the same values for the same ranges as RANGE-TABLE.
|
|
341 The values will not themselves be copied.
|
428
|
342 */
|
444
|
343 (range_table))
|
428
|
344 {
|
440
|
345 Lisp_Range_Table *rt, *rtnew;
|
428
|
346
|
444
|
347 CHECK_RANGE_TABLE (range_table);
|
|
348 rt = XRANGE_TABLE (range_table);
|
428
|
349
|
440
|
350 rtnew = alloc_lcrecord_type (Lisp_Range_Table, &lrecord_range_table);
|
428
|
351 rtnew->entries = Dynarr_new (range_table_entry);
|
2421
|
352 rtnew->type = rt->type;
|
428
|
353
|
|
354 Dynarr_add_many (rtnew->entries, Dynarr_atp (rt->entries, 0),
|
|
355 Dynarr_length (rt->entries));
|
793
|
356 return wrap_range_table (rtnew);
|
428
|
357 }
|
|
358
|
|
359 DEFUN ("get-range-table", Fget_range_table, 2, 3, 0, /*
|
444
|
360 Find value for position POS in RANGE-TABLE.
|
428
|
361 If there is no corresponding value, return DEFAULT (defaults to nil).
|
|
362 */
|
444
|
363 (pos, range_table, default_))
|
428
|
364 {
|
440
|
365 Lisp_Range_Table *rt;
|
428
|
366
|
444
|
367 CHECK_RANGE_TABLE (range_table);
|
|
368 rt = XRANGE_TABLE (range_table);
|
428
|
369
|
|
370 CHECK_INT_COERCE_CHAR (pos);
|
|
371
|
|
372 return get_range_table (XINT (pos), Dynarr_length (rt->entries),
|
|
373 Dynarr_atp (rt->entries, 0), default_);
|
|
374 }
|
|
375
|
|
376 void
|
|
377 put_range_table (Lisp_Object table, EMACS_INT first,
|
|
378 EMACS_INT last, Lisp_Object val)
|
|
379 {
|
|
380 int i;
|
|
381 int insert_me_here = -1;
|
440
|
382 Lisp_Range_Table *rt = XRANGE_TABLE (table);
|
428
|
383
|
2421
|
384 /* Fix up the numbers in accordance with the open/closedness to make
|
|
385 them behave like default open/closed. */
|
|
386
|
|
387 switch (rt->type)
|
|
388 {
|
|
389 case RANGE_START_CLOSED_END_OPEN: break;
|
|
390 case RANGE_START_CLOSED_END_CLOSED: last++; break;
|
|
391 case RANGE_START_OPEN_END_OPEN: first++; break;
|
|
392 case RANGE_START_OPEN_END_CLOSED: first++, last++; break;
|
|
393 }
|
|
394
|
|
395 if (first == last)
|
|
396 return;
|
|
397 if (first > last)
|
|
398 /* This will happen if originally first == last and both ends are
|
|
399 open. #### Should we signal an error? */
|
|
400 return;
|
|
401
|
428
|
402 /* Now insert in the proper place. This gets tricky because
|
|
403 we may be overlapping one or more existing ranges and need
|
|
404 to fix them up. */
|
|
405
|
|
406 /* First delete all sections of any existing ranges that overlap
|
|
407 the new range. */
|
|
408 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
409 {
|
|
410 struct range_table_entry *entry = Dynarr_atp (rt->entries, i);
|
|
411 /* We insert before the first range that begins at or after the
|
|
412 new range. */
|
|
413 if (entry->first >= first && insert_me_here < 0)
|
|
414 insert_me_here = i;
|
|
415 if (entry->last < first)
|
|
416 /* completely before the new range. */
|
|
417 continue;
|
|
418 if (entry->first > last)
|
|
419 /* completely after the new range. No more possibilities of
|
|
420 finding overlapping ranges. */
|
|
421 break;
|
2421
|
422 /* At this point the existing ENTRY overlaps or touches the new one. */
|
428
|
423 if (entry->first < first && entry->last <= last)
|
|
424 {
|
|
425 /* looks like:
|
|
426
|
2421
|
427 [ NEW )
|
|
428 [ EXISTING )
|
|
429
|
|
430 or
|
|
431
|
|
432 [ NEW )
|
|
433 [ EXISTING )
|
428
|
434
|
|
435 */
|
|
436 /* truncate the end off of it. */
|
2421
|
437 entry->last = first;
|
428
|
438 }
|
|
439 else if (entry->first < first && entry->last > last)
|
|
440 /* looks like:
|
|
441
|
2421
|
442 [ NEW )
|
|
443 [ EXISTING )
|
428
|
444
|
|
445 */
|
|
446 /* need to split this one in two. */
|
|
447 {
|
|
448 struct range_table_entry insert_me_too;
|
|
449
|
2421
|
450 insert_me_too.first = last;
|
428
|
451 insert_me_too.last = entry->last;
|
|
452 insert_me_too.val = entry->val;
|
2421
|
453 entry->last = first;
|
428
|
454 Dynarr_insert_many (rt->entries, &insert_me_too, 1, i + 1);
|
|
455 }
|
2421
|
456 else if (entry->last >= last)
|
428
|
457 {
|
|
458 /* looks like:
|
|
459
|
2421
|
460 [ NEW )
|
|
461 [ EXISTING )
|
|
462
|
|
463 or
|
|
464
|
|
465 [ NEW )
|
|
466 [ EXISTING )
|
428
|
467
|
|
468 */
|
|
469 /* truncate the start off of it. */
|
2421
|
470 entry->first = last;
|
428
|
471 }
|
|
472 else
|
|
473 {
|
|
474 /* existing is entirely within new. */
|
|
475 Dynarr_delete_many (rt->entries, i, 1);
|
|
476 i--; /* back up since everything shifted one to the left. */
|
|
477 }
|
|
478 }
|
|
479
|
|
480 /* Someone asked us to delete the range, not insert it. */
|
|
481 if (UNBOUNDP (val))
|
|
482 return;
|
|
483
|
|
484 /* Now insert the new entry, maybe at the end. */
|
|
485
|
|
486 if (insert_me_here < 0)
|
|
487 insert_me_here = i;
|
|
488
|
|
489 {
|
|
490 struct range_table_entry insert_me;
|
|
491
|
|
492 insert_me.first = first;
|
|
493 insert_me.last = last;
|
|
494 insert_me.val = val;
|
|
495
|
|
496 Dynarr_insert_many (rt->entries, &insert_me, 1, insert_me_here);
|
|
497 }
|
|
498
|
|
499 /* Now see if we can combine this entry with adjacent ones just
|
|
500 before or after. */
|
|
501
|
|
502 if (insert_me_here > 0)
|
|
503 {
|
|
504 struct range_table_entry *entry = Dynarr_atp (rt->entries,
|
|
505 insert_me_here - 1);
|
2421
|
506 if (EQ (val, entry->val) && entry->last == first)
|
428
|
507 {
|
|
508 entry->last = last;
|
|
509 Dynarr_delete_many (rt->entries, insert_me_here, 1);
|
|
510 insert_me_here--;
|
|
511 /* We have morphed into a larger range. Update our records
|
|
512 in case we also combine with the one after. */
|
|
513 first = entry->first;
|
|
514 }
|
|
515 }
|
|
516
|
|
517 if (insert_me_here < Dynarr_length (rt->entries) - 1)
|
|
518 {
|
|
519 struct range_table_entry *entry = Dynarr_atp (rt->entries,
|
|
520 insert_me_here + 1);
|
2421
|
521 if (EQ (val, entry->val) && entry->first == last)
|
428
|
522 {
|
|
523 entry->first = first;
|
|
524 Dynarr_delete_many (rt->entries, insert_me_here, 1);
|
|
525 }
|
|
526 }
|
|
527 }
|
|
528
|
|
529 DEFUN ("put-range-table", Fput_range_table, 4, 4, 0, /*
|
2421
|
530 Set the value for range START .. END to be VALUE in RANGE-TABLE.
|
428
|
531 */
|
444
|
532 (start, end, value, range_table))
|
428
|
533 {
|
|
534 EMACS_INT first, last;
|
|
535
|
444
|
536 CHECK_RANGE_TABLE (range_table);
|
428
|
537 CHECK_INT_COERCE_CHAR (start);
|
|
538 first = XINT (start);
|
|
539 CHECK_INT_COERCE_CHAR (end);
|
|
540 last = XINT (end);
|
|
541 if (first > last)
|
563
|
542 invalid_argument_2 ("start must be <= end", start, end);
|
428
|
543
|
444
|
544 put_range_table (range_table, first, last, value);
|
|
545 verify_range_table (XRANGE_TABLE (range_table));
|
428
|
546 return Qnil;
|
|
547 }
|
|
548
|
|
549 DEFUN ("remove-range-table", Fremove_range_table, 3, 3, 0, /*
|
2421
|
550 Remove the value for range START .. END in RANGE-TABLE.
|
428
|
551 */
|
444
|
552 (start, end, range_table))
|
428
|
553 {
|
444
|
554 return Fput_range_table (start, end, Qunbound, range_table);
|
428
|
555 }
|
|
556
|
|
557 DEFUN ("clear-range-table", Fclear_range_table, 1, 1, 0, /*
|
444
|
558 Flush RANGE-TABLE.
|
428
|
559 */
|
444
|
560 (range_table))
|
428
|
561 {
|
444
|
562 CHECK_RANGE_TABLE (range_table);
|
|
563 Dynarr_reset (XRANGE_TABLE (range_table)->entries);
|
428
|
564 return Qnil;
|
|
565 }
|
|
566
|
|
567 DEFUN ("map-range-table", Fmap_range_table, 2, 2, 0, /*
|
444
|
568 Map FUNCTION over entries in RANGE-TABLE, calling it with three args,
|
428
|
569 the beginning and end of the range and the corresponding value.
|
442
|
570
|
|
571 Results are guaranteed to be correct (i.e. each entry processed
|
|
572 exactly once) if FUNCTION modifies or deletes the current entry
|
444
|
573 \(i.e. passes the current range to `put-range-table' or
|
442
|
574 `remove-range-table'), but not otherwise.
|
428
|
575 */
|
444
|
576 (function, range_table))
|
428
|
577 {
|
442
|
578 Lisp_Range_Table *rt;
|
|
579 int i;
|
|
580
|
444
|
581 CHECK_RANGE_TABLE (range_table);
|
442
|
582 CHECK_FUNCTION (function);
|
|
583
|
444
|
584 rt = XRANGE_TABLE (range_table);
|
442
|
585
|
|
586 /* Do not "optimize" by pulling out the length computation below!
|
|
587 FUNCTION may have changed the table. */
|
|
588 for (i = 0; i < Dynarr_length (rt->entries); i++)
|
|
589 {
|
|
590 struct range_table_entry *entry = Dynarr_atp (rt->entries, i);
|
|
591 EMACS_INT first, last;
|
|
592 Lisp_Object args[4];
|
|
593 int oldlen;
|
|
594
|
|
595 again:
|
|
596 first = entry->first;
|
|
597 last = entry->last;
|
|
598 oldlen = Dynarr_length (rt->entries);
|
|
599 args[0] = function;
|
|
600 args[1] = make_int (first);
|
|
601 args[2] = make_int (last);
|
|
602 args[3] = entry->val;
|
|
603 Ffuncall (countof (args), args);
|
|
604 /* Has FUNCTION removed the entry? */
|
|
605 if (oldlen > Dynarr_length (rt->entries)
|
|
606 && i < Dynarr_length (rt->entries)
|
|
607 && (first != entry->first || last != entry->last))
|
|
608 goto again;
|
|
609 }
|
|
610
|
428
|
611 return Qnil;
|
|
612 }
|
|
613
|
|
614
|
|
615 /************************************************************************/
|
|
616 /* Range table read syntax */
|
|
617 /************************************************************************/
|
|
618
|
|
619 static int
|
2421
|
620 rangetab_type_validate (Lisp_Object UNUSED (keyword), Lisp_Object value,
|
|
621 Error_Behavior UNUSED (errb))
|
|
622 {
|
|
623 /* #### should deal with ERRB */
|
|
624 range_table_symbol_to_type (value);
|
|
625 return 1;
|
|
626 }
|
|
627
|
|
628 static int
|
2286
|
629 rangetab_data_validate (Lisp_Object UNUSED (keyword), Lisp_Object value,
|
|
630 Error_Behavior UNUSED (errb))
|
428
|
631 {
|
2367
|
632 /* #### should deal with ERRB */
|
|
633 EXTERNAL_PROPERTY_LIST_LOOP_3 (range, data, value)
|
428
|
634 {
|
|
635 if (!INTP (range) && !CHARP (range)
|
|
636 && !(CONSP (range) && CONSP (XCDR (range))
|
|
637 && NILP (XCDR (XCDR (range)))
|
|
638 && (INTP (XCAR (range)) || CHARP (XCAR (range)))
|
|
639 && (INTP (XCAR (XCDR (range))) || CHARP (XCAR (XCDR (range))))))
|
563
|
640 sferror ("Invalid range format", range);
|
428
|
641 }
|
|
642
|
|
643 return 1;
|
|
644 }
|
|
645
|
|
646 static Lisp_Object
|
2421
|
647 rangetab_instantiate (Lisp_Object plist)
|
428
|
648 {
|
2425
|
649 Lisp_Object data = Qnil, type = Qnil, rangetab;
|
428
|
650
|
2421
|
651 PROPERTY_LIST_LOOP_3 (key, value, plist)
|
428
|
652 {
|
2421
|
653 if (EQ (key, Qtype)) type = value;
|
|
654 else if (EQ (key, Qdata)) data = value;
|
|
655 else
|
2500
|
656 ABORT ();
|
2421
|
657 }
|
|
658
|
2425
|
659 rangetab = Fmake_range_table (type);
|
428
|
660
|
2421
|
661 {
|
|
662 PROPERTY_LIST_LOOP_3 (range, val, data)
|
|
663 {
|
|
664 if (CONSP (range))
|
|
665 Fput_range_table (Fcar (range), Fcar (Fcdr (range)), val,
|
|
666 rangetab);
|
|
667 else
|
|
668 Fput_range_table (range, range, val, rangetab);
|
|
669 }
|
|
670 }
|
428
|
671
|
|
672 return rangetab;
|
|
673 }
|
|
674
|
|
675
|
|
676 /************************************************************************/
|
|
677 /* Unified range tables */
|
|
678 /************************************************************************/
|
|
679
|
|
680 /* A "unified range table" is a format for storing range tables
|
|
681 as contiguous blocks of memory. This is used by the regexp
|
|
682 code, which needs to use range tables to properly handle []
|
|
683 constructs in the presence of extended characters but wants to
|
|
684 store an entire compiled pattern as a contiguous block of memory.
|
|
685
|
|
686 Unified range tables are designed so that they can be placed
|
|
687 at an arbitrary (possibly mis-aligned) place in memory.
|
|
688 (Dealing with alignment is a pain in the ass.)
|
|
689
|
|
690 WARNING: No provisions for garbage collection are currently made.
|
|
691 This means that there must not be any Lisp objects in a unified
|
|
692 range table that need to be marked for garbage collection.
|
|
693 Good candidates for objects that can go into a range table are
|
|
694
|
|
695 -- numbers and characters (do not need to be marked)
|
|
696 -- nil, t (marked elsewhere)
|
|
697 -- charsets and coding systems (automatically marked because
|
|
698 they are in a marked list,
|
|
699 and can't be removed)
|
|
700
|
|
701 Good but slightly less so:
|
|
702
|
|
703 -- symbols (could be uninterned, but that is not likely)
|
|
704
|
|
705 Somewhat less good:
|
|
706
|
|
707 -- buffers, frames, devices (could get deleted)
|
|
708
|
|
709
|
|
710 It is expected that you work with range tables in the normal
|
|
711 format and then convert to unified format when you are done
|
|
712 making modifications. As such, no functions are provided
|
|
713 for modifying a unified range table. The only operations
|
|
714 you can do to unified range tables are
|
|
715
|
|
716 -- look up a value
|
|
717 -- retrieve all the ranges in an iterative fashion
|
|
718
|
|
719 */
|
|
720
|
|
721 /* The format of a unified range table is as follows:
|
|
722
|
|
723 -- The first byte contains the number of bytes to skip to find the
|
|
724 actual start of the table. This deals with alignment constraints,
|
|
725 since the table might want to go at any arbitrary place in memory.
|
|
726 -- The next three bytes contain the number of bytes to skip (from the
|
|
727 *first* byte) to find the stuff after the table. It's stored in
|
|
728 little-endian format because that's how God intended things. We don't
|
|
729 necessarily start the stuff at the very end of the table because
|
|
730 we want to have at least ALIGNOF (EMACS_INT) extra space in case
|
|
731 we have to move the range table around. (It appears that some
|
|
732 architectures don't maintain alignment when reallocing.)
|
|
733 -- At the prescribed offset is a struct unified_range_table, containing
|
|
734 some number of `struct range_table_entry' entries. */
|
|
735
|
|
736 struct unified_range_table
|
|
737 {
|
|
738 int nentries;
|
|
739 struct range_table_entry first;
|
|
740 };
|
|
741
|
|
742 /* Return size in bytes needed to store the data in a range table. */
|
|
743
|
|
744 int
|
|
745 unified_range_table_bytes_needed (Lisp_Object rangetab)
|
|
746 {
|
|
747 return (sizeof (struct range_table_entry) *
|
|
748 (Dynarr_length (XRANGE_TABLE (rangetab)->entries) - 1) +
|
|
749 sizeof (struct unified_range_table) +
|
|
750 /* ALIGNOF a struct may be too big. */
|
|
751 /* We have four bytes for the size numbers, and an extra
|
|
752 four or eight bytes for making sure we get the alignment
|
|
753 OK. */
|
|
754 ALIGNOF (EMACS_INT) + 4);
|
|
755 }
|
|
756
|
|
757 /* Convert a range table into unified format and store in DEST,
|
|
758 which must be able to hold the number of bytes returned by
|
|
759 range_table_bytes_needed(). */
|
|
760
|
|
761 void
|
|
762 unified_range_table_copy_data (Lisp_Object rangetab, void *dest)
|
|
763 {
|
|
764 /* We cast to the above structure rather than just casting to
|
|
765 char * and adding sizeof(int), because that will lead to
|
|
766 mis-aligned data on the Alpha machines. */
|
|
767 struct unified_range_table *un;
|
|
768 range_table_entry_dynarr *rted = XRANGE_TABLE (rangetab)->entries;
|
|
769 int total_needed = unified_range_table_bytes_needed (rangetab);
|
826
|
770 void *new_dest = ALIGN_PTR ((char *) dest + 4, EMACS_INT);
|
428
|
771
|
|
772 * (char *) dest = (char) ((char *) new_dest - (char *) dest);
|
|
773 * ((unsigned char *) dest + 1) = total_needed & 0xFF;
|
|
774 total_needed >>= 8;
|
|
775 * ((unsigned char *) dest + 2) = total_needed & 0xFF;
|
|
776 total_needed >>= 8;
|
|
777 * ((unsigned char *) dest + 3) = total_needed & 0xFF;
|
|
778 un = (struct unified_range_table *) new_dest;
|
|
779 un->nentries = Dynarr_length (rted);
|
|
780 memcpy (&un->first, Dynarr_atp (rted, 0),
|
|
781 sizeof (struct range_table_entry) * Dynarr_length (rted));
|
|
782 }
|
|
783
|
|
784 /* Return number of bytes actually used by a unified range table. */
|
|
785
|
|
786 int
|
|
787 unified_range_table_bytes_used (void *unrangetab)
|
|
788 {
|
|
789 return ((* ((unsigned char *) unrangetab + 1))
|
|
790 + ((* ((unsigned char *) unrangetab + 2)) << 8)
|
|
791 + ((* ((unsigned char *) unrangetab + 3)) << 16));
|
|
792 }
|
|
793
|
|
794 /* Make sure the table is aligned, and move it around if it's not. */
|
|
795 static void
|
|
796 align_the_damn_table (void *unrangetab)
|
|
797 {
|
|
798 void *cur_dest = (char *) unrangetab + * (char *) unrangetab;
|
826
|
799 if (cur_dest != ALIGN_PTR (cur_dest, EMACS_INT))
|
428
|
800 {
|
|
801 int count = (unified_range_table_bytes_used (unrangetab) - 4
|
|
802 - ALIGNOF (EMACS_INT));
|
|
803 /* Find the proper location, just like above. */
|
826
|
804 void *new_dest = ALIGN_PTR ((char *) unrangetab + 4, EMACS_INT);
|
428
|
805 /* memmove() works in the presence of overlapping data. */
|
|
806 memmove (new_dest, cur_dest, count);
|
|
807 * (char *) unrangetab = (char) ((char *) new_dest - (char *) unrangetab);
|
|
808 }
|
|
809 }
|
|
810
|
|
811 /* Look up a value in a unified range table. */
|
|
812
|
|
813 Lisp_Object
|
|
814 unified_range_table_lookup (void *unrangetab, EMACS_INT pos,
|
|
815 Lisp_Object default_)
|
|
816 {
|
|
817 void *new_dest;
|
|
818 struct unified_range_table *un;
|
|
819
|
|
820 align_the_damn_table (unrangetab);
|
|
821 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
822 un = (struct unified_range_table *) new_dest;
|
|
823
|
|
824 return get_range_table (pos, un->nentries, &un->first, default_);
|
|
825 }
|
|
826
|
|
827 /* Return number of entries in a unified range table. */
|
|
828
|
|
829 int
|
|
830 unified_range_table_nentries (void *unrangetab)
|
|
831 {
|
|
832 void *new_dest;
|
|
833 struct unified_range_table *un;
|
|
834
|
|
835 align_the_damn_table (unrangetab);
|
|
836 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
837 un = (struct unified_range_table *) new_dest;
|
|
838 return un->nentries;
|
|
839 }
|
|
840
|
|
841 /* Return the OFFSETth range (counting from 0) in UNRANGETAB. */
|
|
842 void
|
|
843 unified_range_table_get_range (void *unrangetab, int offset,
|
|
844 EMACS_INT *min, EMACS_INT *max,
|
|
845 Lisp_Object *val)
|
|
846 {
|
|
847 void *new_dest;
|
|
848 struct unified_range_table *un;
|
|
849 struct range_table_entry *tab;
|
|
850
|
|
851 align_the_damn_table (unrangetab);
|
|
852 new_dest = (char *) unrangetab + * (char *) unrangetab;
|
|
853 un = (struct unified_range_table *) new_dest;
|
|
854
|
|
855 assert (offset >= 0 && offset < un->nentries);
|
|
856 tab = (&un->first) + offset;
|
|
857 *min = tab->first;
|
|
858 *max = tab->last;
|
|
859 *val = tab->val;
|
|
860 }
|
|
861
|
|
862
|
|
863 /************************************************************************/
|
|
864 /* Initialization */
|
|
865 /************************************************************************/
|
|
866
|
|
867 void
|
|
868 syms_of_rangetab (void)
|
|
869 {
|
442
|
870 INIT_LRECORD_IMPLEMENTATION (range_table);
|
|
871
|
563
|
872 DEFSYMBOL_MULTIWORD_PREDICATE (Qrange_tablep);
|
|
873 DEFSYMBOL (Qrange_table);
|
428
|
874
|
2421
|
875 DEFSYMBOL (Qstart_closed_end_open);
|
|
876 DEFSYMBOL (Qstart_open_end_open);
|
|
877 DEFSYMBOL (Qstart_closed_end_closed);
|
|
878 DEFSYMBOL (Qstart_open_end_closed);
|
|
879
|
428
|
880 DEFSUBR (Frange_table_p);
|
2421
|
881 DEFSUBR (Frange_table_type);
|
428
|
882 DEFSUBR (Fmake_range_table);
|
|
883 DEFSUBR (Fcopy_range_table);
|
|
884 DEFSUBR (Fget_range_table);
|
|
885 DEFSUBR (Fput_range_table);
|
|
886 DEFSUBR (Fremove_range_table);
|
|
887 DEFSUBR (Fclear_range_table);
|
|
888 DEFSUBR (Fmap_range_table);
|
|
889 }
|
|
890
|
|
891 void
|
|
892 structure_type_create_rangetab (void)
|
|
893 {
|
|
894 struct structure_type *st;
|
|
895
|
|
896 st = define_structure_type (Qrange_table, 0, rangetab_instantiate);
|
|
897
|
|
898 define_structure_type_keyword (st, Qdata, rangetab_data_validate);
|
2421
|
899 define_structure_type_keyword (st, Qtype, rangetab_type_validate);
|
428
|
900 }
|