annotate lib/gl_sublist.c @ 7603:23f14c284219

Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
author Bruno Haible <bruno@clisp.org>
date Mon, 06 Nov 2006 13:03:10 +0000
parents 52a8ff5ade8a
children 238942284e2f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7420
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
1 /* Sequential list data type backed by another list.
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
2 Copyright (C) 2006 Free Software Foundation, Inc.
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
3 Written by Bruno Haible <bruno@clisp.org>, 2006.
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
4
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
5 This program is free software; you can redistribute it and/or modify
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
6 it under the terms of the GNU General Public License as published by
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
7 the Free Software Foundation; either version 2, or (at your option)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
8 any later version.
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
9
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
10 This program is distributed in the hope that it will be useful,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
13 GNU General Public License for more details.
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
14
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
15 You should have received a copy of the GNU General Public License
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
16 along with this program; if not, write to the Free Software Foundation,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
18
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
19 #include <config.h>
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
20
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
21 /* Specification. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
22 #include "gl_sublist.h"
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
23
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
24 #include <stdlib.h>
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
25
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
26 #include "xalloc.h"
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
27
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
28 #ifndef uintptr_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
29 # define uintptr_t unsigned long
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
30 #endif
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
31
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
32 /* -------------------------- gl_list_t Data Type -------------------------- */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
33
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
34 /* Concrete gl_list_impl type, valid for this file only. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
35 struct gl_list_impl
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
36 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
37 struct gl_list_impl_base base;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
38 /* Reference to the whole list. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
39 gl_list_t whole;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
40 /* Limits of the index range. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
41 size_t start;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
42 size_t end;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
43 };
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
44
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
45 /* struct gl_list_node_impl doesn't exist here. The pointers are actually
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
46 indices + 1. (We don't use the whole list's gl_list_node_t implementation,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
47 because gl_sublist_next_node and gl_sublist_previous_node would not be easy
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
48 to implement with this choice.) */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
49 #define INDEX_TO_NODE(index) (gl_list_node_t)(uintptr_t)(size_t)((index) + 1)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
50 #define NODE_TO_INDEX(node) ((uintptr_t)(node) - 1)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
51
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
52 static gl_list_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
53 gl_sublist_create_empty (gl_list_implementation_t implementation,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
54 gl_listelement_equals_fn equals_fn,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
55 gl_listelement_hashcode_fn hashcode_fn,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
56 bool allow_duplicates)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
57 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
58 /* Shouldn't be called. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
59 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
60 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
61
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
62 static gl_list_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
63 gl_sublist_create_fill (gl_list_implementation_t implementation,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
64 gl_listelement_equals_fn equals_fn,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
65 gl_listelement_hashcode_fn hashcode_fn,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
66 bool allow_duplicates,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
67 size_t count, const void **contents)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
68 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
69 /* Shouldn't be called. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
70 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
71 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
72
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
73 static size_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
74 gl_sublist_size (gl_list_t list)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
75 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
76 return list->end - list->start;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
77 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
78
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
79 static const void *
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
80 gl_sublist_node_value (gl_list_t list, gl_list_node_t node)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
81 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
82 uintptr_t index = NODE_TO_INDEX (node);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
83 if (!(index < list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
84 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
85 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
86 return gl_list_get_at (list->whole, list->start + index);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
87 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
88
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
89 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
90 gl_sublist_next_node (gl_list_t list, gl_list_node_t node)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
91 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
92 uintptr_t index = NODE_TO_INDEX (node);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
93 size_t count = list->end - list->start;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
94 if (!(index < count))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
95 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
96 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
97 index++;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
98 if (index < count)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
99 return INDEX_TO_NODE (index);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
100 else
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
101 return NULL;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
102 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
103
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
104 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
105 gl_sublist_previous_node (gl_list_t list, gl_list_node_t node)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
106 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
107 uintptr_t index = NODE_TO_INDEX (node);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
108 if (!(index < list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
109 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
110 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
111 if (index > 0)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
112 return INDEX_TO_NODE (index - 1);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
113 else
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
114 return NULL;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
115 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
116
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
117 static const void *
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
118 gl_sublist_get_at (gl_list_t list, size_t position)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
119 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
120 if (!(position < list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
121 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
122 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
123 return gl_list_get_at (list->whole, list->start + position);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
124 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
125
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
126 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
127 gl_sublist_set_at (gl_list_t list, size_t position, const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
128 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
129 if (!(position < list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
130 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
131 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
132 gl_list_set_at (list->whole, list->start + position, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
133 return INDEX_TO_NODE (position);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
134 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
135
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
136 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
137 gl_sublist_search_from_to (gl_list_t list, size_t start_index, size_t end_index,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
138 const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
139 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
140 if (!(start_index <= end_index && end_index <= list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
141 /* Invalid arguments. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
142 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
143 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
144 size_t index =
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
145 gl_list_indexof_from_to (list->whole,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
146 list->start + start_index,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
147 list->start + end_index,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
148 elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
149 if (index != (size_t)(-1))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
150 return INDEX_TO_NODE (index - list->start);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
151 else
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
152 return NULL;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
153 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
154 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
155
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
156 static size_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
157 gl_sublist_indexof_from_to (gl_list_t list,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
158 size_t start_index, size_t end_index,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
159 const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
160 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
161 if (!(start_index <= end_index && end_index <= list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
162 /* Invalid arguments. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
163 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
164 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
165 size_t index =
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
166 gl_list_indexof_from_to (list->whole,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
167 list->start + start_index,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
168 list->start + end_index,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
169 elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
170 if (index != (size_t)(-1))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
171 index -= list->start;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
172 return index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
173 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
174 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
175
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
176 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
177 gl_sublist_add_first (gl_list_t list, const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
178 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
179 gl_list_add_at (list->whole, list->start, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
180 list->end++;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
181 return INDEX_TO_NODE (0);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
182 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
183
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
184 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
185 gl_sublist_add_last (gl_list_t list, const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
186 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
187 gl_list_add_at (list->whole, list->end, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
188 list->end++;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
189 return INDEX_TO_NODE (list->end - list->start - 1);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
190 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
191
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
192 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
193 gl_sublist_add_before (gl_list_t list, gl_list_node_t node, const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
194 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
195 size_t position = NODE_TO_INDEX (node);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
196 if (!(position < list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
197 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
198 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
199 gl_list_add_at (list->whole, list->start + position, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
200 list->end++;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
201 return INDEX_TO_NODE (position);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
202 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
203
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
204 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
205 gl_sublist_add_after (gl_list_t list, gl_list_node_t node, const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
206 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
207 size_t position = NODE_TO_INDEX (node);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
208 if (!(position < list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
209 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
210 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
211 position++;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
212 gl_list_add_at (list->whole, list->start + position, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
213 list->end++;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
214 return INDEX_TO_NODE (position);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
215 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
216
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
217 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
218 gl_sublist_add_at (gl_list_t list, size_t position, const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
219 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
220 if (!(position <= list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
221 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
222 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
223 gl_list_add_at (list->whole, list->start + position, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
224 list->end++;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
225 return INDEX_TO_NODE (position);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
226 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
227
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
228 static bool
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
229 gl_sublist_remove_node (gl_list_t list, gl_list_node_t node)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
230 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
231 uintptr_t index = NODE_TO_INDEX (node);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
232 if (!(index < list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
233 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
234 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
235 return gl_list_remove_at (list->whole, list->start + index);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
236 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
237
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
238 static bool
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
239 gl_sublist_remove_at (gl_list_t list, size_t position)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
240 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
241 if (!(position < list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
242 /* Invalid argument. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
243 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
244 return gl_list_remove_at (list->whole, list->start + position);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
245 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
246
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
247 static bool
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
248 gl_sublist_remove (gl_list_t list, const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
249 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
250 size_t position =
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
251 gl_list_indexof_from_to (list->whole, list->start, list->end, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
252 if (position == (size_t)(-1))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
253 return false;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
254 else
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
255 return gl_list_remove_at (list->whole, position);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
256 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
257
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
258 static void
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
259 gl_sublist_list_free (gl_list_t list)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
260 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
261 free (list);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
262 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
263
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
264 /* --------------------- gl_list_iterator_t Data Type --------------------- */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
265
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
266 static gl_list_iterator_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
267 gl_sublist_iterator (gl_list_t list)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
268 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
269 return gl_list_iterator_from_to (list->whole, list->start, list->end);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
270 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
271
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
272 static gl_list_iterator_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
273 gl_sublist_iterator_from_to (gl_list_t list,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
274 size_t start_index, size_t end_index)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
275 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
276 if (!(start_index <= end_index && end_index <= list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
277 /* Invalid arguments. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
278 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
279 return gl_list_iterator_from_to (list->whole,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
280 list->start + start_index,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
281 list->start + end_index);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
282 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
283
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
284 static bool
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
285 gl_sublist_iterator_next (gl_list_iterator_t *iterator,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
286 const void **eltp, gl_list_node_t *nodep)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
287 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
288 /* Shouldn't be called. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
289 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
290 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
291
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
292 static void
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
293 gl_sublist_iterator_free (gl_list_iterator_t *iterator)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
294 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
295 /* Shouldn't be called. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
296 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
297 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
298
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
299 /* ---------------------- Sorted gl_list_t Data Type ---------------------- */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
300
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
301 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
302 gl_sublist_sortedlist_search (gl_list_t list,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
303 gl_listelement_compar_fn compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
304 const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
305 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
306 size_t index =
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
307 gl_sortedlist_indexof_from_to (list->whole, compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
308 list->start, list->end, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
309 if (index != (size_t)(-1))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
310 return INDEX_TO_NODE (index - list->start);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
311 else
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
312 return NULL;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
313 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
314
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
315 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
316 gl_sublist_sortedlist_search_from_to (gl_list_t list,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
317 gl_listelement_compar_fn compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
318 size_t low, size_t high,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
319 const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
320 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
321 size_t index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
322
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
323 if (!(low <= high && high <= list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
324 /* Invalid arguments. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
325 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
326
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
327 index =
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
328 gl_sortedlist_indexof_from_to (list->whole, compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
329 list->start + low, list->start + high, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
330 if (index != (size_t)(-1))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
331 return INDEX_TO_NODE (index - list->start);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
332 else
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
333 return NULL;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
334 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
335
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
336 static size_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
337 gl_sublist_sortedlist_indexof (gl_list_t list,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
338 gl_listelement_compar_fn compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
339 const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
340 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
341 size_t index =
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
342 gl_sortedlist_indexof_from_to (list->whole, compar, list->start, list->end,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
343 elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
344 if (index != (size_t)(-1))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
345 index -= list->start;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
346 return index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
347 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
348
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
349 static size_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
350 gl_sublist_sortedlist_indexof_from_to (gl_list_t list,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
351 gl_listelement_compar_fn compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
352 size_t low, size_t high,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
353 const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
354 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
355 size_t index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
356
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
357 if (!(low <= high && high <= list->end - list->start))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
358 /* Invalid arguments. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
359 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
360
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
361 index = gl_sortedlist_indexof_from_to (list->whole, compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
362 list->start + low, list->start + high,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
363 elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
364 if (index != (size_t)(-1))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
365 index -= list->start;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
366 return index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
367 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
368
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
369 static gl_list_node_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
370 gl_sublist_sortedlist_add (gl_list_t list,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
371 gl_listelement_compar_fn compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
372 const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
373 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
374 /* It's impossible to implement this method without risking to put the
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
375 whole list into unsorted order (namely, when the given ELT is smaller
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
376 or larger than all elements of the sublist). */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
377 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
378 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
379
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
380 static bool
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
381 gl_sublist_sortedlist_remove (gl_list_t list,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
382 gl_listelement_compar_fn compar,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
383 const void *elt)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
384 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
385 size_t index = gl_sublist_sortedlist_indexof (list, compar, elt);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
386 if (index == (size_t)(-1))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
387 return false;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
388 else
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
389 return gl_sublist_remove_at (list, index);
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
390 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
391
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
392
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
393 static const struct gl_list_implementation gl_sublist_list_implementation =
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
394 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
395 gl_sublist_create_empty,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
396 gl_sublist_create_fill,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
397 gl_sublist_size,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
398 gl_sublist_node_value,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
399 gl_sublist_next_node,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
400 gl_sublist_previous_node,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
401 gl_sublist_get_at,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
402 gl_sublist_set_at,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
403 gl_sublist_search_from_to,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
404 gl_sublist_indexof_from_to,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
405 gl_sublist_add_first,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
406 gl_sublist_add_last,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
407 gl_sublist_add_before,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
408 gl_sublist_add_after,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
409 gl_sublist_add_at,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
410 gl_sublist_remove_node,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
411 gl_sublist_remove_at,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
412 gl_sublist_remove,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
413 gl_sublist_list_free,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
414 gl_sublist_iterator,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
415 gl_sublist_iterator_from_to,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
416 gl_sublist_iterator_next,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
417 gl_sublist_iterator_free,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
418 gl_sublist_sortedlist_search,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
419 gl_sublist_sortedlist_search_from_to,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
420 gl_sublist_sortedlist_indexof,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
421 gl_sublist_sortedlist_indexof_from_to,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
422 gl_sublist_sortedlist_add,
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
423 gl_sublist_sortedlist_remove
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
424 };
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
425
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
426 gl_list_t
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
427 gl_sublist_create (gl_list_t whole_list, size_t start_index, size_t end_index)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
428 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
429 if (!(start_index <= end_index && end_index <= gl_list_size (whole_list)))
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
430 /* Invalid arguments. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
431 abort ();
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
432 {
7603
23f14c284219 Simplify xmalloc expressions. Add overflow check in xmalloc arguments.
Bruno Haible <bruno@clisp.org>
parents: 7420
diff changeset
433 struct gl_list_impl *list = XMALLOC (struct gl_list_impl);
7420
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
434
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
435 list->base.vtable = &gl_sublist_list_implementation;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
436 list->base.equals_fn = whole_list->base.equals_fn; /* actually unused */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
437 list->base.hashcode_fn = whole_list->base.hashcode_fn; /* actually unused */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
438 list->base.allow_duplicates = whole_list->base.allow_duplicates; /* unused */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
439 if (whole_list->base.vtable == &gl_sublist_list_implementation)
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
440 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
441 /* Optimization of a sublist of a sublist: Collapse the two
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
442 indirections into a single indirection. */
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
443 list->whole = whole_list->whole;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
444 list->start = whole_list->start + start_index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
445 list->end = whole_list->start + end_index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
446 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
447 else
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
448 {
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
449 list->whole = whole_list;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
450 list->start = start_index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
451 list->end = end_index;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
452 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
453
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
454 return list;
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
455 }
52a8ff5ade8a New module 'sublist'.
Bruno Haible <bruno@clisp.org>
parents:
diff changeset
456 }