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