comparison src/rangetab.c @ 5125:b5df3737028a ben-lisp-object

merge
author Ben Wing <ben@xemacs.org>
date Wed, 24 Feb 2010 01:58:04 -0600
parents e0db3c197671 0d4c9d0f6a8d
children a9c41067dd88
comparison
equal deleted inserted replaced
5124:623d57b7fbe8 5125:b5df3737028a
1 /* XEmacs routines to deal with range tables. 1 /* XEmacs routines to deal with range tables.
2 Copyright (C) 1995 Sun Microsystems, Inc. 2 Copyright (C) 1995 Sun Microsystems, Inc.
3 Copyright (C) 1995, 2002, 2004, 2005 Ben Wing. 3 Copyright (C) 1995, 2002, 2004, 2005, 2010 Ben Wing.
4 4
5 This file is part of XEmacs. 5 This file is part of XEmacs.
6 6
7 XEmacs is free software; you can redistribute it and/or modify it 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 8 under the terms of the GNU General Public License as published by the
105 105
106 if (print_readably) 106 if (print_readably)
107 write_fmt_string_lisp (printcharfun, "#s(range-table type %s data (", 107 write_fmt_string_lisp (printcharfun, "#s(range-table type %s data (",
108 1, range_table_type_to_symbol (rt->type)); 108 1, range_table_type_to_symbol (rt->type));
109 else 109 else
110 write_c_string (printcharfun, "#<range-table "); 110 write_ascstring (printcharfun, "#<range-table ");
111 for (i = 0; i < Dynarr_length (rt->entries); i++) 111 for (i = 0; i < Dynarr_length (rt->entries); i++)
112 { 112 {
113 struct range_table_entry *rte = Dynarr_atp (rt->entries, i); 113 struct range_table_entry *rte = Dynarr_atp (rt->entries, i);
114 int so, ec; 114 int so, ec;
115 if (i > 0) 115 if (i > 0)
116 write_c_string (printcharfun, " "); 116 write_ascstring (printcharfun, " ");
117 switch (rt->type) 117 switch (rt->type)
118 { 118 {
119 case RANGE_START_CLOSED_END_OPEN: so = 0, ec = 0; break; 119 case RANGE_START_CLOSED_END_OPEN: so = 0, ec = 0; break;
120 case RANGE_START_CLOSED_END_CLOSED: so = 0, ec = 1; 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; 121 case RANGE_START_OPEN_END_OPEN: so = 1, ec = 0; break;
129 print_readably ? ')' : ec ? ']' : ')' 129 print_readably ? ')' : ec ? ']' : ')'
130 ); 130 );
131 print_internal (rte->val, printcharfun, 1); 131 print_internal (rte->val, printcharfun, 1);
132 } 132 }
133 if (print_readably) 133 if (print_readably)
134 write_c_string (printcharfun, "))"); 134 write_ascstring (printcharfun, "))");
135 else 135 else
136 write_fmt_string (printcharfun, " 0x%x>", rt->header.uid); 136 write_fmt_string (printcharfun, " 0x%x>", rt->header.uid);
137 } 137 }
138 138
139 static int 139 static int
140 range_table_equal (Lisp_Object obj1, Lisp_Object obj2, int depth) 140 range_table_equal (Lisp_Object obj1, Lisp_Object obj2, int depth, int foldcase)
141 { 141 {
142 Lisp_Range_Table *rt1 = XRANGE_TABLE (obj1); 142 Lisp_Range_Table *rt1 = XRANGE_TABLE (obj1);
143 Lisp_Range_Table *rt2 = XRANGE_TABLE (obj2); 143 Lisp_Range_Table *rt2 = XRANGE_TABLE (obj2);
144 int i; 144 int i;
145 145
151 struct range_table_entry *rte1 = Dynarr_atp (rt1->entries, i); 151 struct range_table_entry *rte1 = Dynarr_atp (rt1->entries, i);
152 struct range_table_entry *rte2 = Dynarr_atp (rt2->entries, i); 152 struct range_table_entry *rte2 = Dynarr_atp (rt2->entries, i);
153 153
154 if (rte1->first != rte2->first 154 if (rte1->first != rte2->first
155 || rte1->last != rte2->last 155 || rte1->last != rte2->last
156 || !internal_equal (rte1->val, rte2->val, depth + 1)) 156 || !internal_equal_0 (rte1->val, rte2->val, depth + 1, foldcase))
157 return 0; 157 return 0;
158 } 158 }
159 159
160 return 1; 160 return 1;
161 } 161 }
354 obj = ALLOC_LISP_OBJECT (range_table); 354 obj = ALLOC_LISP_OBJECT (range_table);
355 rtnew = XRANGE_TABLE (obj); 355 rtnew = XRANGE_TABLE (obj);
356 rtnew->entries = Dynarr_new (range_table_entry); 356 rtnew->entries = Dynarr_new (range_table_entry);
357 rtnew->type = rt->type; 357 rtnew->type = rt->type;
358 358
359 Dynarr_add_many (rtnew->entries, Dynarr_atp (rt->entries, 0), 359 Dynarr_add_many (rtnew->entries, Dynarr_begin (rt->entries),
360 Dynarr_length (rt->entries)); 360 Dynarr_length (rt->entries));
361 return obj; 361 return obj;
362 } 362 }
363 363
364 DEFUN ("get-range-table", Fget_range_table, 2, 3, 0, /* 364 DEFUN ("get-range-table", Fget_range_table, 2, 3, 0, /*
373 rt = XRANGE_TABLE (range_table); 373 rt = XRANGE_TABLE (range_table);
374 374
375 CHECK_INT_COERCE_CHAR (pos); 375 CHECK_INT_COERCE_CHAR (pos);
376 376
377 return get_range_table (XINT (pos), Dynarr_length (rt->entries), 377 return get_range_table (XINT (pos), Dynarr_length (rt->entries),
378 Dynarr_atp (rt->entries, 0), default_); 378 Dynarr_begin (rt->entries), default_);
379 }
380
381 static void
382 external_to_internal_adjust_ends (enum range_table_type type,
383 EMACS_INT *first, EMACS_INT *last)
384 {
385 /* Fix up the numbers in accordance with the open/closedness to make
386 them behave like default open/closed. */
387 switch (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
396 static void
397 internal_to_external_adjust_ends (enum range_table_type type,
398 EMACS_INT *first, EMACS_INT *last)
399 {
400 /* Reverse the changes made in external_to_internal_adjust_ends().
401 */
402 switch (type)
403 {
404 case RANGE_START_CLOSED_END_OPEN: break;
405 case RANGE_START_CLOSED_END_CLOSED: (*last)--; break;
406 case RANGE_START_OPEN_END_OPEN: (*first)--; break;
407 case RANGE_START_OPEN_END_CLOSED: (*first)--, (*last)--; break;
408 }
379 } 409 }
380 410
381 void 411 void
382 put_range_table (Lisp_Object table, EMACS_INT first, 412 put_range_table (Lisp_Object table, EMACS_INT first,
383 EMACS_INT last, Lisp_Object val) 413 EMACS_INT last, Lisp_Object val)
384 { 414 {
385 int i; 415 int i;
386 int insert_me_here = -1; 416 int insert_me_here = -1;
387 Lisp_Range_Table *rt = XRANGE_TABLE (table); 417 Lisp_Range_Table *rt = XRANGE_TABLE (table);
388 418
389 /* Fix up the numbers in accordance with the open/closedness to make 419 external_to_internal_adjust_ends (rt->type, &first, &last);
390 them behave like default open/closed. */
391
392 switch (rt->type)
393 {
394 case RANGE_START_CLOSED_END_OPEN: break;
395 case RANGE_START_CLOSED_END_CLOSED: last++; break;
396 case RANGE_START_OPEN_END_OPEN: first++; break;
397 case RANGE_START_OPEN_END_CLOSED: first++, last++; break;
398 }
399
400 if (first == last) 420 if (first == last)
401 return; 421 return;
402 if (first > last) 422 if (first > last)
403 /* This will happen if originally first == last and both ends are 423 /* This will happen if originally first == last and both ends are
404 open. #### Should we signal an error? */ 424 open. #### Should we signal an error? */
605 args[0] = function; 625 args[0] = function;
606 /* Fix up the numbers in accordance with the open/closedness of the 626 /* Fix up the numbers in accordance with the open/closedness of the
607 table. */ 627 table. */
608 { 628 {
609 EMACS_INT premier = first, dernier = last; 629 EMACS_INT premier = first, dernier = last;
610 switch (rt->type) 630 internal_to_external_adjust_ends (rt->type, &premier, &dernier);
611 {
612 case RANGE_START_CLOSED_END_OPEN: break;
613 case RANGE_START_CLOSED_END_CLOSED: dernier--; break;
614 case RANGE_START_OPEN_END_OPEN: premier--; break;
615 case RANGE_START_OPEN_END_CLOSED: premier--, dernier--; break;
616 }
617 args[1] = make_int (premier); 631 args[1] = make_int (premier);
618 args[2] = make_int (dernier); 632 args[2] = make_int (dernier);
619 } 633 }
620 args[3] = entry->val; 634 args[3] = entry->val;
621 Ffuncall (countof (args), args); 635 Ffuncall (countof (args), args);
752 some number of `struct range_table_entry' entries. */ 766 some number of `struct range_table_entry' entries. */
753 767
754 struct unified_range_table 768 struct unified_range_table
755 { 769 {
756 int nentries; 770 int nentries;
771 enum range_table_type type;
757 struct range_table_entry first; 772 struct range_table_entry first;
758 }; 773 };
759 774
760 /* Return size in bytes needed to store the data in a range table. */ 775 /* Return size in bytes needed to store the data in a range table. */
761 776
793 * ((unsigned char *) dest + 2) = total_needed & 0xFF; 808 * ((unsigned char *) dest + 2) = total_needed & 0xFF;
794 total_needed >>= 8; 809 total_needed >>= 8;
795 * ((unsigned char *) dest + 3) = total_needed & 0xFF; 810 * ((unsigned char *) dest + 3) = total_needed & 0xFF;
796 un = (struct unified_range_table *) new_dest; 811 un = (struct unified_range_table *) new_dest;
797 un->nentries = Dynarr_length (rted); 812 un->nentries = Dynarr_length (rted);
798 memcpy (&un->first, Dynarr_atp (rted, 0), 813 un->type = XRANGE_TABLE (rangetab)->type;
814 memcpy (&un->first, Dynarr_begin (rted),
799 sizeof (struct range_table_entry) * Dynarr_length (rted)); 815 sizeof (struct range_table_entry) * Dynarr_length (rted));
800 } 816 }
801 817
802 /* Return number of bytes actually used by a unified range table. */ 818 /* Return number of bytes actually used by a unified range table. */
803 819
873 assert (offset >= 0 && offset < un->nentries); 889 assert (offset >= 0 && offset < un->nentries);
874 tab = (&un->first) + offset; 890 tab = (&un->first) + offset;
875 *min = tab->first; 891 *min = tab->first;
876 *max = tab->last; 892 *max = tab->last;
877 *val = tab->val; 893 *val = tab->val;
894
895 internal_to_external_adjust_ends (un->type, min, max);
878 } 896 }
879 897
880 898
881 /************************************************************************/ 899 /************************************************************************/
882 /* Initialization */ 900 /* Initialization */