Mercurial > hg > octave-nkf > gnulib-hg
annotate lib/gl_array_oset.c @ 17587:344018b6e5d7
maint: update copyright
I ran 'make update-copyright'.
Signed-off-by: Eric Blake <eblake@redhat.com>
author | Eric Blake <eblake@redhat.com> |
---|---|
date | Wed, 01 Jan 2014 00:04:40 -0700 |
parents | e542fd46ad6f |
children | ab58d4870664 |
rev | line source |
---|---|
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
1 /* Ordered set data type implemented by an array. |
17587 | 2 Copyright (C) 2006-2007, 2009-2014 Free Software Foundation, Inc. |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
3 Written by Bruno Haible <bruno@clisp.org>, 2006. |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
4 |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8436
diff
changeset
|
5 This program is free software: you can redistribute it and/or modify |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
6 it under the terms of the GNU General Public License as published by |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8436
diff
changeset
|
7 the Free Software Foundation; either version 3 of the License, or |
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8436
diff
changeset
|
8 (at your option) any later version. |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
9 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
10 This program is distributed in the hope that it will be useful, |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
11 but WITHOUT ANY WARRANTY; without even the implied warranty of |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
13 GNU General Public License for more details. |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
14 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
15 You should have received a copy of the GNU General Public License |
9309
bbbbbf4cd1c5
Change copyright notice from GPLv2+ to GPLv3+.
Bruno Haible <bruno@clisp.org>
parents:
8436
diff
changeset
|
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
17 |
7304
1c4ed7637c24
Include <config.h> unconditionally.
Bruno Haible <bruno@clisp.org>
parents:
6976
diff
changeset
|
18 #include <config.h> |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
19 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
20 /* Specification. */ |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
21 #include "gl_array_oset.h" |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
22 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
23 #include <stdlib.h> |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
24 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
25 /* Checked size_t computations. */ |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
26 #include "xsize.h" |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
27 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
28 /* -------------------------- gl_oset_t Data Type -------------------------- */ |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
29 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
30 /* Concrete gl_oset_impl type, valid for this file only. */ |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
31 struct gl_oset_impl |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
32 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
33 struct gl_oset_impl_base base; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
34 /* An array of ALLOCATED elements, of which the first COUNT are used. |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
35 0 <= COUNT <= ALLOCATED. */ |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
36 const void **elements; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
37 size_t count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
38 size_t allocated; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
39 }; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
40 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
41 static gl_oset_t |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
42 gl_array_nx_create_empty (gl_oset_implementation_t implementation, |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
43 gl_setelement_compar_fn compar_fn, |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
44 gl_setelement_dispose_fn dispose_fn) |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
45 { |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
46 struct gl_oset_impl *set = |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
47 (struct gl_oset_impl *) malloc (sizeof (struct gl_oset_impl)); |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
48 |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
49 if (set == NULL) |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
50 return NULL; |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
51 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
52 set->base.vtable = implementation; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
53 set->base.compar_fn = compar_fn; |
8436
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
54 set->base.dispose_fn = dispose_fn; |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
55 set->elements = NULL; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
56 set->count = 0; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
57 set->allocated = 0; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
58 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
59 return set; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
60 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
61 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
62 static size_t |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
63 gl_array_size (gl_oset_t set) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
64 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
65 return set->count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
66 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
67 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
68 static size_t |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
69 gl_array_indexof (gl_oset_t set, const void *elt) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
70 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
71 size_t count = set->count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
72 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
73 if (count > 0) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
74 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
75 gl_setelement_compar_fn compar = set->base.compar_fn; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
76 size_t low = 0; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
77 size_t high = count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
78 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
79 /* At each loop iteration, low < high; for indices < low the values |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
80 are smaller than ELT; for indices >= high the values are greater |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
81 than ELT. So, if the element occurs in the list, it is at |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
82 low <= position < high. */ |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
83 do |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
84 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
85 size_t mid = low + (high - low) / 2; /* low <= mid < high */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
86 int cmp = (compar != NULL |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
87 ? compar (set->elements[mid], elt) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
88 : (set->elements[mid] > elt ? 1 : |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
89 set->elements[mid] < elt ? -1 : 0)); |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
90 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
91 if (cmp < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
92 low = mid + 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
93 else if (cmp > 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
94 high = mid; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
95 else /* cmp == 0 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
96 /* We have an element equal to ELT at index MID. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
97 return mid; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
98 } |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
99 while (low < high); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
100 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
101 return (size_t)(-1); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
102 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
103 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
104 static bool |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
105 gl_array_search (gl_oset_t set, const void *elt) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
106 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
107 return gl_array_indexof (set, elt) != (size_t)(-1); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
108 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
109 |
7403
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
110 static bool |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
111 gl_array_search_atleast (gl_oset_t set, |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
112 gl_setelement_threshold_fn threshold_fn, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
113 const void *threshold, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
114 const void **eltp) |
7403
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
115 { |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
116 size_t count = set->count; |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
117 |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
118 if (count > 0) |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
119 { |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
120 size_t low = 0; |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
121 size_t high = count; |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
122 |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
123 /* At each loop iteration, low < high; for indices < low the values are |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
124 smaller than THRESHOLD; for indices >= high the values are nonexistent. |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
125 So, if an element >= THRESHOLD occurs in the list, it is at |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
126 low <= position < high. */ |
7403
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
127 do |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
128 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
129 size_t mid = low + (high - low) / 2; /* low <= mid < high */ |
7403
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
130 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
131 if (! threshold_fn (set->elements[mid], threshold)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
132 low = mid + 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
133 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
134 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
135 /* We have an element >= THRESHOLD at index MID. But we need the |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
136 minimal such index. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
137 high = mid; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
138 /* At each loop iteration, low <= high and |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
139 compar (list->elements[high], value) >= 0, |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
140 and we know that the first occurrence of the element is at |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
141 low <= position <= high. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
142 while (low < high) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
143 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
144 size_t mid2 = low + (high - low) / 2; /* low <= mid2 < high */ |
7403
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
145 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
146 if (! threshold_fn (set->elements[mid2], threshold)) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
147 low = mid2 + 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
148 else |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
149 high = mid2; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
150 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
151 *eltp = set->elements[low]; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
152 return true; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
153 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
154 } |
7403
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
155 while (low < high); |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
156 } |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
157 return false; |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
158 } |
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
159 |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
160 /* Ensure that set->allocated > set->count. |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
161 Return 0 upon success, -1 upon out-of-memory. */ |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
162 static int |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
163 grow (gl_oset_t set) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
164 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
165 size_t new_allocated; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
166 size_t memory_size; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
167 const void **memory; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
168 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
169 new_allocated = xtimes (set->allocated, 2); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
170 new_allocated = xsum (new_allocated, 1); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
171 memory_size = xtimes (new_allocated, sizeof (const void *)); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
172 if (size_overflow_p (memory_size)) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
173 /* Overflow, would lead to out of memory. */ |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
174 return -1; |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
175 memory = (const void **) realloc (set->elements, memory_size); |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
176 if (memory == NULL) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
177 /* Out of memory. */ |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
178 return -1; |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
179 set->elements = memory; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
180 set->allocated = new_allocated; |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
181 return 0; |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
182 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
183 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
184 /* Add the given element ELT at the given position, |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
185 0 <= position <= gl_oset_size (set). |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
186 Return 1 upon success, -1 upon out-of-memory. */ |
17179
bbfb83985261
array-oset, linkedhash-list, rbtree-oset: no need for 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
187 static int |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
188 gl_array_nx_add_at (gl_oset_t set, size_t position, const void *elt) |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
189 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
190 size_t count = set->count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
191 const void **elements; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
192 size_t i; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
193 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
194 if (count == set->allocated) |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
195 if (grow (set) < 0) |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
196 return -1; |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
197 elements = set->elements; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
198 for (i = count; i > position; i--) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
199 elements[i] = elements[i - 1]; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
200 elements[position] = elt; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
201 set->count = count + 1; |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
202 return 1; |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
203 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
204 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
205 /* Remove the element at the given position, |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
206 0 <= position < gl_oset_size (set). */ |
17179
bbfb83985261
array-oset, linkedhash-list, rbtree-oset: no need for 'inline'
Paul Eggert <eggert@cs.ucla.edu>
parents:
16201
diff
changeset
|
207 static void |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
208 gl_array_remove_at (gl_oset_t set, size_t position) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
209 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
210 size_t count = set->count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
211 const void **elements; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
212 size_t i; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
213 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
214 elements = set->elements; |
8436
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
215 if (set->base.dispose_fn != NULL) |
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
216 set->base.dispose_fn (elements[position]); |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
217 for (i = position + 1; i < count; i++) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
218 elements[i - 1] = elements[i]; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
219 set->count = count - 1; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
220 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
221 |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
222 static int |
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
223 gl_array_nx_add (gl_oset_t set, const void *elt) |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
224 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
225 size_t count = set->count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
226 size_t low = 0; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
227 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
228 if (count > 0) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
229 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
230 gl_setelement_compar_fn compar = set->base.compar_fn; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
231 size_t high = count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
232 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
233 /* At each loop iteration, low < high; for indices < low the values |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
234 are smaller than ELT; for indices >= high the values are greater |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
235 than ELT. So, if the element occurs in the list, it is at |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
236 low <= position < high. */ |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
237 do |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
238 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
239 size_t mid = low + (high - low) / 2; /* low <= mid < high */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
240 int cmp = (compar != NULL |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
241 ? compar (set->elements[mid], elt) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
242 : (set->elements[mid] > elt ? 1 : |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
243 set->elements[mid] < elt ? -1 : 0)); |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
244 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
245 if (cmp < 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
246 low = mid + 1; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
247 else if (cmp > 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
248 high = mid; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
249 else /* cmp == 0 */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
250 return false; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
251 } |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
252 while (low < high); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
253 } |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
254 return gl_array_nx_add_at (set, low, elt); |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
255 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
256 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
257 static bool |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
258 gl_array_remove (gl_oset_t set, const void *elt) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
259 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
260 size_t index = gl_array_indexof (set, elt); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
261 if (index != (size_t)(-1)) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
262 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
263 gl_array_remove_at (set, index); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
264 return true; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
265 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
266 else |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
267 return false; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
268 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
269 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
270 static void |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
271 gl_array_free (gl_oset_t set) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
272 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
273 if (set->elements != NULL) |
8436
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
274 { |
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
275 if (set->base.dispose_fn != NULL) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
276 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
277 size_t count = set->count; |
8436
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
278 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
279 if (count > 0) |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
280 { |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
281 gl_setelement_dispose_fn dispose = set->base.dispose_fn; |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
282 const void **elements = set->elements; |
8436
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
283 |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
284 do |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
285 dispose (*elements++); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
286 while (--count > 0); |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
287 } |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
288 } |
8436
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
289 free (set->elements); |
36bbb949160c
Add an element disposal function.
Bruno Haible <bruno@clisp.org>
parents:
8422
diff
changeset
|
290 } |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
291 free (set); |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
292 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
293 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
294 /* --------------------- gl_oset_iterator_t Data Type --------------------- */ |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
295 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
296 static gl_oset_iterator_t |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
297 gl_array_iterator (gl_oset_t set) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
298 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
299 gl_oset_iterator_t result; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
300 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
301 result.vtable = set->base.vtable; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
302 result.set = set; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
303 result.count = set->count; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
304 result.p = set->elements + 0; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
305 result.q = set->elements + set->count; |
7352
3d5bd6899004
* gl_anylinked_list2.h [lint] (gl_linked_iterator)
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
parents:
7304
diff
changeset
|
306 #ifdef lint |
3d5bd6899004
* gl_anylinked_list2.h [lint] (gl_linked_iterator)
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
parents:
7304
diff
changeset
|
307 result.i = 0; |
3d5bd6899004
* gl_anylinked_list2.h [lint] (gl_linked_iterator)
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
parents:
7304
diff
changeset
|
308 result.j = 0; |
3d5bd6899004
* gl_anylinked_list2.h [lint] (gl_linked_iterator)
Ralf Wildenhues <Ralf.Wildenhues@gmx.de>
parents:
7304
diff
changeset
|
309 #endif |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
310 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
311 return result; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
312 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
313 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
314 static bool |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
315 gl_array_iterator_next (gl_oset_iterator_t *iterator, const void **eltp) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
316 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
317 gl_oset_t set = iterator->set; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
318 if (iterator->count != set->count) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
319 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
320 if (iterator->count != set->count + 1) |
12421
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
321 /* Concurrent modifications were done on the set. */ |
e8d2c6fc33ad
Use spaces for indentation, not tabs.
Bruno Haible <bruno@clisp.org>
parents:
9309
diff
changeset
|
322 abort (); |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
323 /* The last returned element was removed. */ |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
324 iterator->count--; |
8422
f968c473e8b7
Make pointer decrementing code ANSI C compliant.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
325 iterator->p = (const void **) iterator->p - 1; |
f968c473e8b7
Make pointer decrementing code ANSI C compliant.
Bruno Haible <bruno@clisp.org>
parents:
7603
diff
changeset
|
326 iterator->q = (const void **) iterator->q - 1; |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
327 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
328 if (iterator->p < iterator->q) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
329 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
330 const void **p = (const void **) iterator->p; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
331 *eltp = *p; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
332 iterator->p = p + 1; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
333 return true; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
334 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
335 else |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
336 return false; |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
337 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
338 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
339 static void |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
340 gl_array_iterator_free (gl_oset_iterator_t *iterator) |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
341 { |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
342 } |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
343 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
344 |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
345 const struct gl_oset_implementation gl_array_oset_implementation = |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
346 { |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
347 gl_array_nx_create_empty, |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
348 gl_array_size, |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
349 gl_array_search, |
7403
2fbd1f779c5f
Add a search_atleast operation.
Bruno Haible <bruno@clisp.org>
parents:
7401
diff
changeset
|
350 gl_array_search_atleast, |
12444
29d240cb21b2
Move the malloc checking from module 'oset' to new module 'xoset'.
Bruno Haible <bruno@clisp.org>
parents:
12421
diff
changeset
|
351 gl_array_nx_add, |
6976
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
352 gl_array_remove, |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
353 gl_array_free, |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
354 gl_array_iterator, |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
355 gl_array_iterator_next, |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
356 gl_array_iterator_free |
c77399b5682d
Ordered set data type implemented by an array.
Bruno Haible <bruno@clisp.org>
parents:
diff
changeset
|
357 }; |