• R/O
  • SSH
  • HTTPS

cstl: Commit


Commit MetaInfo

Revision43 (tree)
Time2007-03-25 03:11:09
Authorkatono

Log Message

0.2.1をリリース。

Change Summary

Incremental Difference

--- tags/rel-0.2.1/test/vector_test.c (nonexistent)
+++ tags/rel-0.2.1/test/vector_test.c (revision 43)
@@ -0,0 +1,598 @@
1+#include "test.h"
2+
3+
4+VECTOR_IMPLEMENT(UCharVector, unsigned char)
5+VECTOR_IMPLEMENT(IntVector, int)
6+
7+static UCharVector *uv;
8+static IntVector *iv;
9+
10+#define SIZE 15
11+
12+
13+static unsigned char hoge[256];
14+static int piyo[256];
15+
16+void vector_init_hoge(void)
17+{
18+ int i;
19+ for (i = 0; i < 256; i++) {
20+ hoge[i] = i;
21+ }
22+}
23+
24+void vector_init_piyo(void)
25+{
26+ int i;
27+ for (i = 0; i < 256; i++) {
28+ piyo[i] = i;
29+ }
30+}
31+
32+
33+void VectorTest_test_1_1(void)
34+{
35+ size_t a;
36+ printf("***** test_1_1 *****\n");
37+ uv = UCharVector_new(SIZE);
38+ vector_init_hoge();
39+ /* 初期状態 */
40+ assert(UCharVector_capacity(uv) == SIZE);
41+ assert(UCharVector_size(uv) == 0);
42+ assert(UCharVector_empty(uv));
43+ /* insert_array */
44+ UCharVector_clear(uv);
45+ assert(UCharVector_insert_array(uv, 0, hoge, SIZE));
46+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
47+ assert(UCharVector_capacity(uv) == SIZE);
48+ assert(UCharVector_size(uv) == SIZE);
49+ assert(!UCharVector_empty(uv));
50+ /* clear */
51+ UCharVector_clear(uv);
52+ assert(UCharVector_size(uv) == 0);
53+ assert(UCharVector_empty(uv));
54+ /* insert_array */
55+ UCharVector_clear(uv);
56+ assert(UCharVector_insert_array(uv, 0, &hoge[1], 255));
57+ assert(memcmp(UCharVector_at(uv, 0), &hoge[1], 255) == 0);
58+ a = UCharVector_capacity(uv);
59+ assert(a > SIZE);
60+ assert(UCharVector_size(uv) == 255);
61+ assert(!UCharVector_empty(uv));
62+ /* clear */
63+ UCharVector_clear(uv);
64+ assert(UCharVector_size(uv) == 0);
65+ assert(UCharVector_empty(uv));
66+ /* insert_array */
67+ UCharVector_clear(uv);
68+ assert(UCharVector_insert_array(uv, 0, hoge, SIZE));
69+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
70+ assert(UCharVector_capacity(uv) == a);
71+ assert(UCharVector_size(uv) == SIZE);
72+ assert(!UCharVector_empty(uv));
73+ /* clear */
74+ UCharVector_clear(uv);
75+ assert(UCharVector_size(uv) == 0);
76+ assert(UCharVector_empty(uv));
77+ /* reserve */
78+ assert(UCharVector_reserve(uv, 1024));
79+ assert(UCharVector_capacity(uv) == 1024);
80+ assert(UCharVector_size(uv) == 0);
81+ assert(UCharVector_empty(uv));
82+
83+ assert(UCharVector_reserve(uv, 256));
84+ assert(UCharVector_capacity(uv) == 1024);
85+ assert(UCharVector_size(uv) == 0);
86+ assert(UCharVector_empty(uv));
87+
88+ UCharVector_delete(uv);
89+}
90+
91+void VectorTest_test_1_2(void)
92+{
93+ int i;
94+ size_t a;
95+ printf("***** test_1_2 *****\n");
96+ uv = UCharVector_new(SIZE);
97+ vector_init_hoge();
98+ /* push_back */
99+ for (i = 0; i < SIZE; i++) {
100+ assert(UCharVector_push_back(uv, hoge[i]));
101+ assert(UCharVector_size(uv) == i+1);
102+ assert(UCharVector_front(uv) == hoge[0]);
103+ assert(UCharVector_back(uv) == hoge[i]);
104+ }
105+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
106+ assert(UCharVector_capacity(uv) == SIZE);
107+ assert(UCharVector_size(uv) == SIZE);
108+ assert(!UCharVector_empty(uv));
109+ for (i = i; i < 256; i++) {
110+ assert(UCharVector_push_back(uv, hoge[i]));
111+ assert(UCharVector_size(uv) == i+1);
112+ assert(UCharVector_front(uv) == hoge[0]);
113+ assert(UCharVector_back(uv) == hoge[i]);
114+ }
115+ assert(memcmp(UCharVector_at(uv, 0), hoge, 256) == 0);
116+ a = UCharVector_capacity(uv);
117+ assert(a > SIZE);
118+ assert(UCharVector_size(uv) == 256);
119+ assert(!UCharVector_empty(uv));
120+ /* front, back */
121+ assert(UCharVector_front(uv) == hoge[0]);
122+ assert(UCharVector_back(uv) == hoge[255]);
123+ /* pop_back */
124+ for (i = 255; i >= 0; i--) {
125+ assert(UCharVector_front(uv) == hoge[0]);
126+ assert(UCharVector_back(uv) == hoge[i]);
127+ assert(UCharVector_pop_back(uv) == hoge[i]);
128+ assert(UCharVector_size(uv) == i);
129+ }
130+ assert(UCharVector_size(uv) == 0);
131+ assert(UCharVector_empty(uv));
132+ assert(UCharVector_capacity(uv) == a);
133+
134+ UCharVector_delete(uv);
135+}
136+
137+void VectorTest_test_1_3(void)
138+{
139+ int i;
140+ printf("***** test_1_3 *****\n");
141+ uv = UCharVector_new(SIZE);
142+ vector_init_hoge();
143+ /* insert_array */
144+ UCharVector_clear(uv);
145+ assert(UCharVector_insert_array(uv, 0, hoge, SIZE));
146+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
147+ assert(UCharVector_capacity(uv) == SIZE);
148+ assert(UCharVector_size(uv) == SIZE);
149+ assert(!UCharVector_empty(uv));
150+ /* at */
151+ for (i = 0; i < SIZE; i++) {
152+ assert(*UCharVector_at(uv, i) == hoge[i]);
153+ *UCharVector_at(uv, i) = hoge[i]+1;
154+ assert(*UCharVector_at(uv, i) == hoge[i]+1);
155+ }
156+ assert(UCharVector_capacity(uv) == SIZE);
157+ assert(UCharVector_size(uv) == SIZE);
158+ assert(!UCharVector_empty(uv));
159+
160+ UCharVector_delete(uv);
161+}
162+
163+void VectorTest_test_1_4(void)
164+{
165+ int i;
166+ unsigned char buf[256];
167+ printf("***** test_1_4 *****\n");
168+ uv = UCharVector_new(SIZE);
169+ /* insert */
170+ for (i = 0; i < SIZE; i++) {
171+ assert(UCharVector_insert(uv, 0, hoge[i]));
172+ buf[i] = SIZE - i - 1;
173+ }
174+ assert(memcmp(UCharVector_at(uv, 0), buf, SIZE) == 0);
175+ assert(UCharVector_capacity(uv) == SIZE);
176+ assert(UCharVector_size(uv) == SIZE);
177+ assert(!UCharVector_empty(uv));
178+ for (i = SIZE; i < 256; i++) {
179+ assert(UCharVector_insert(uv, UCharVector_size(uv), hoge[i]));
180+ buf[i] = i;
181+ assert(UCharVector_capacity(uv) > SIZE);
182+ }
183+ assert(memcmp(&UCharVector_at(uv, 0)[SIZE], &buf[SIZE], 256-SIZE) == 0);
184+ assert(UCharVector_size(uv) == 256);
185+ assert(UCharVector_insert(uv, SIZE, 0));
186+ assert(memcmp(UCharVector_at(uv, 0), buf, SIZE) == 0);
187+ assert(UCharVector_at(uv, 0)[SIZE] == 0);
188+ assert(memcmp(&UCharVector_at(uv, 0)[SIZE+1], &buf[SIZE], 256-SIZE) == 0);
189+ assert(UCharVector_size(uv) == 257);
190+ /* erase */
191+ UCharVector_erase(uv, SIZE, 1);
192+ assert(memcmp(UCharVector_at(uv, 0), buf, SIZE) == 0);
193+ assert(memcmp(&UCharVector_at(uv, 0)[SIZE], &buf[SIZE], 256-SIZE) == 0);
194+ for (i = SIZE; i < 256; i++) {
195+ UCharVector_erase(uv, UCharVector_size(uv)-1, 1);
196+ }
197+ assert(memcmp(UCharVector_at(uv, 0), buf, SIZE) == 0);
198+ assert(UCharVector_size(uv) == SIZE);
199+ for (i = 0; i < SIZE; i++) {
200+ assert(memcmp(UCharVector_at(uv, 0), &buf[i], SIZE-i) == 0);
201+ UCharVector_erase(uv, 0, 1);
202+ assert(UCharVector_size(uv) == SIZE-i-1);
203+ }
204+ assert(UCharVector_size(uv) == 0);
205+
206+ UCharVector_delete(uv);
207+}
208+
209+void VectorTest_test_1_5(void)
210+{
211+ printf("***** test_1_5 *****\n");
212+ uv = UCharVector_new(SIZE);
213+ vector_init_hoge();
214+ /* insert_array */
215+ assert(UCharVector_insert_array(uv, 0, hoge, SIZE));
216+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
217+ assert(UCharVector_capacity(uv) == SIZE);
218+ assert(UCharVector_size(uv) == SIZE);
219+
220+ assert(UCharVector_insert_array(uv, UCharVector_size(uv), &hoge[SIZE], 256-SIZE));
221+ assert(memcmp(UCharVector_at(uv, 0), hoge, 256) == 0);
222+ assert(UCharVector_capacity(uv) > SIZE);
223+ assert(UCharVector_size(uv) == 256);
224+
225+ assert(UCharVector_insert_array(uv, SIZE, hoge, SIZE));
226+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
227+ assert(memcmp(&UCharVector_at(uv, 0)[SIZE], hoge, 256) == 0);
228+ assert(UCharVector_size(uv) == 256+SIZE);
229+
230+ assert(UCharVector_insert_array(uv, 0, hoge, 1));
231+ assert(*UCharVector_at(uv, 0) == hoge[0]);
232+ assert(memcmp(&UCharVector_at(uv, 0)[1], hoge, SIZE) == 0);
233+ assert(memcmp(&UCharVector_at(uv, 0)[SIZE+1], hoge, 256) == 0);
234+ assert(UCharVector_size(uv) == 257+SIZE);
235+
236+ assert(UCharVector_insert_array(uv, 0, hoge, 0));
237+ assert(*UCharVector_at(uv, 0) == hoge[0]);
238+ assert(memcmp(&UCharVector_at(uv, 0)[1], hoge, SIZE) == 0);
239+ assert(memcmp(&UCharVector_at(uv, 0)[SIZE+1], hoge, 256) == 0);
240+ assert(UCharVector_size(uv) == 257+SIZE);
241+ /* erase */
242+ UCharVector_erase(uv, 0, 1);
243+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
244+ assert(memcmp(&UCharVector_at(uv, 0)[SIZE], hoge, 256) == 0);
245+ assert(UCharVector_size(uv) == 256+SIZE);
246+
247+ UCharVector_erase(uv, SIZE, SIZE);
248+ assert(memcmp(UCharVector_at(uv, 0), hoge, 256) == 0);
249+ assert(UCharVector_size(uv) == 256);
250+
251+ UCharVector_erase(uv, SIZE, 256-SIZE);
252+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
253+ assert(UCharVector_size(uv) == SIZE);
254+
255+ UCharVector_erase(uv, 0, SIZE);
256+ assert(UCharVector_size(uv) == 0);
257+
258+ UCharVector_delete(uv);
259+}
260+
261+void VectorTest_test_1_6(void)
262+{
263+ UCharVector *x;
264+ size_t i;
265+ unsigned char b[] = {1, 2, 4, 43, 2, 54, 1, 0, 2, 24};
266+ printf("***** test_1_6 *****\n");
267+ uv = UCharVector_new(SIZE);
268+ vector_init_hoge();
269+
270+ UCharVector_clear(uv);
271+ assert(UCharVector_insert_array(uv, 0, hoge, SIZE));
272+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
273+ assert(UCharVector_size(uv) == SIZE);
274+ assert(UCharVector_capacity(uv) == SIZE);
275+ /* resize */
276+ assert(UCharVector_resize(uv, SIZE + 10, 255));
277+ assert(UCharVector_size(uv) == SIZE + 10);
278+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
279+ assert(*UCharVector_at(uv, SIZE) == 255);
280+ assert(*UCharVector_at(uv, SIZE + 9) == 255);
281+ assert(UCharVector_capacity(uv) > SIZE);
282+
283+ assert(UCharVector_resize(uv, SIZE - 10, 255));
284+ assert(UCharVector_size(uv) == SIZE - 10);
285+
286+ assert(UCharVector_resize(uv, 0, 255));
287+ assert(UCharVector_empty(uv));
288+ /* shrink */
289+ assert(UCharVector_resize(uv, 1000, 255));
290+ assert(UCharVector_capacity(uv) == 1000);
291+ assert(UCharVector_size(uv) == 1000);
292+ assert(UCharVector_resize(uv, 100, 255));
293+ assert(UCharVector_capacity(uv) == 1000);
294+ assert(UCharVector_size(uv) == 100);
295+ UCharVector_shrink(uv, 500);
296+ assert(UCharVector_capacity(uv) == 500);
297+ assert(UCharVector_size(uv) == 100);
298+ UCharVector_shrink(uv, 0);
299+ assert(UCharVector_capacity(uv) == 100);
300+ assert(UCharVector_size(uv) == 100);
301+ assert(UCharVector_resize(uv, 10, 255));
302+ UCharVector_shrink(uv, 500);
303+ assert(UCharVector_capacity(uv) == 100);
304+ assert(UCharVector_size(uv) == 10);
305+ UCharVector_shrink(uv, 0);
306+ assert(UCharVector_capacity(uv) == 10);
307+ assert(UCharVector_size(uv) == 10);
308+ /* assign */
309+ x = UCharVector_new(SIZE);
310+ assert(UCharVector_assign(x, uv, 0, UCharVector_size(uv)));
311+ assert(UCharVector_size(x) == 10);
312+ for (i = 0; i < UCharVector_size(uv); i++) {
313+ assert(*UCharVector_at(x, i) == *UCharVector_at(uv, i));
314+ }
315+ assert(UCharVector_assign(x, x, 0, UCharVector_size(x)));
316+ assert(UCharVector_size(uv) == UCharVector_size(x));
317+ for (i = 0; i < UCharVector_size(uv); i++) {
318+ assert(*UCharVector_at(x, i) == *UCharVector_at(uv, i));
319+ }
320+ assert(UCharVector_assign(x, x, 1, 2));
321+ assert(UCharVector_size(x) == 2);
322+ assert(*UCharVector_at(x, 0) == *UCharVector_at(uv, 1));
323+ assert(*UCharVector_at(x, 1) == *UCharVector_at(uv, 2));
324+ assert(UCharVector_assign(x, x, 0, 0));
325+ assert(UCharVector_size(x) == 0);
326+ /* swap */
327+ UCharVector_clear(uv);
328+ UCharVector_shrink(uv, 0);
329+ UCharVector_clear(uv);
330+ assert(UCharVector_insert_array(uv, 0, hoge, SIZE));
331+ assert(memcmp(UCharVector_at(uv, 0), hoge, SIZE) == 0);
332+ assert(UCharVector_size(uv) == SIZE);
333+ assert(UCharVector_capacity(uv) == SIZE);
334+ UCharVector_clear(x);
335+ UCharVector_shrink(x, 0);
336+ UCharVector_clear(x);
337+ assert(UCharVector_insert_array(x, 0, b, sizeof b));
338+ assert(memcmp(UCharVector_at(x, 0), b, sizeof b) == 0);
339+ assert(UCharVector_size(x) == sizeof b);
340+ assert(UCharVector_capacity(x) == sizeof b);
341+
342+ UCharVector_swap(uv, x);
343+
344+ assert(memcmp(UCharVector_at(uv, 0), b, sizeof b) == 0);
345+ assert(UCharVector_size(uv) == sizeof b);
346+ assert(UCharVector_capacity(uv) == sizeof b);
347+ assert(memcmp(UCharVector_at(x, 0), hoge, SIZE) == 0);
348+ assert(UCharVector_size(x) == SIZE);
349+ assert(UCharVector_capacity(x) == SIZE);
350+
351+
352+
353+ UCharVector_delete(uv);
354+ UCharVector_delete(x);
355+}
356+
357+
358+void VectorTest_test_2_1(void)
359+{
360+ size_t a;
361+ printf("***** test_2_1 *****\n");
362+ iv = IntVector_new(SIZE);
363+ vector_init_piyo();
364+ /* 初期状態 */
365+ assert(IntVector_capacity(iv) == SIZE);
366+ assert(IntVector_size(iv) == 0);
367+ assert(IntVector_empty(iv));
368+ /* insert_array */
369+ IntVector_clear(iv);
370+ assert(IntVector_insert_array(iv, 0, piyo, SIZE));
371+ assert(memcmp(IntVector_at(iv, 0), piyo, SIZE) == 0);
372+ assert(IntVector_capacity(iv) == SIZE);
373+ assert(IntVector_size(iv) == SIZE);
374+ assert(!IntVector_empty(iv));
375+ /* clear */
376+ IntVector_clear(iv);
377+ assert(IntVector_size(iv) == 0);
378+ assert(IntVector_empty(iv));
379+ /* insert_array */
380+ IntVector_clear(iv);
381+ assert(IntVector_insert_array(iv, 0, &piyo[1], 255));
382+ assert(memcmp(IntVector_at(iv, 0), &piyo[1], 255) == 0);
383+ a = IntVector_capacity(iv);
384+ assert(a > SIZE);
385+ assert(IntVector_size(iv) == 255);
386+ assert(!IntVector_empty(iv));
387+ /* clear */
388+ IntVector_clear(iv);
389+ assert(IntVector_size(iv) == 0);
390+ assert(IntVector_empty(iv));
391+ /* insert_array */
392+ IntVector_clear(iv);
393+ assert(IntVector_insert_array(iv, 0, piyo, SIZE));
394+ assert(memcmp(IntVector_at(iv, 0), piyo, SIZE) == 0);
395+ assert(IntVector_capacity(iv) == a);
396+ assert(IntVector_size(iv) == SIZE);
397+ assert(!IntVector_empty(iv));
398+ /* clear */
399+ IntVector_clear(iv);
400+ assert(IntVector_size(iv) == 0);
401+ assert(IntVector_empty(iv));
402+ /* reserve */
403+ assert(IntVector_reserve(iv, 1024));
404+ assert(IntVector_capacity(iv) == 1024);
405+ assert(IntVector_size(iv) == 0);
406+ assert(IntVector_empty(iv));
407+
408+ assert(IntVector_reserve(iv, 256));
409+ assert(IntVector_capacity(iv) == 1024);
410+ assert(IntVector_size(iv) == 0);
411+ assert(IntVector_empty(iv));
412+
413+ IntVector_delete(iv);
414+}
415+
416+void VectorTest_test_2_2(void)
417+{
418+ int i;
419+ size_t a;
420+ printf("***** test_2_2 *****\n");
421+ iv = IntVector_new(SIZE);
422+ vector_init_piyo();
423+ /* push_back */
424+ for (i = 0; i < SIZE; i++) {
425+ assert(IntVector_push_back(iv, piyo[i]));
426+ assert(IntVector_size(iv) == i+1);
427+ assert(IntVector_front(iv) == piyo[0]);
428+ assert(IntVector_back(iv) == piyo[i]);
429+ }
430+ assert(memcmp(IntVector_at(iv, 0), piyo, SIZE) == 0);
431+ assert(IntVector_capacity(iv) == SIZE);
432+ assert(IntVector_size(iv) == SIZE);
433+ assert(!IntVector_empty(iv));
434+ for (i = i; i < 256; i++) {
435+ assert(IntVector_push_back(iv, piyo[i]));
436+ assert(IntVector_size(iv) == i+1);
437+ assert(IntVector_front(iv) == piyo[0]);
438+ assert(IntVector_back(iv) == piyo[i]);
439+ }
440+ assert(memcmp(IntVector_at(iv, 0), piyo, 256) == 0);
441+ a = IntVector_capacity(iv);
442+ assert(a > SIZE);
443+ assert(IntVector_size(iv) == 256);
444+ assert(!IntVector_empty(iv));
445+ /* front, back */
446+ assert(IntVector_front(iv) == piyo[0]);
447+ assert(IntVector_back(iv) == piyo[255]);
448+ /* pop_back */
449+ for (i = 255; i >= 0; i--) {
450+ assert(IntVector_front(iv) == piyo[0]);
451+ assert(IntVector_back(iv) == piyo[i]);
452+ assert(IntVector_pop_back(iv) == piyo[i]);
453+ assert(IntVector_size(iv) == i);
454+ }
455+ assert(IntVector_size(iv) == 0);
456+ assert(IntVector_empty(iv));
457+ assert(IntVector_capacity(iv) == a);
458+
459+ IntVector_delete(iv);
460+}
461+
462+void VectorTest_test_2_3(void)
463+{
464+ int i;
465+ printf("***** test_2_3 *****\n");
466+ iv = IntVector_new(SIZE);
467+ vector_init_piyo();
468+ /* insert_array */
469+ IntVector_clear(iv);
470+ assert(IntVector_insert_array(iv, 0, piyo, SIZE));
471+ assert(memcmp(IntVector_at(iv, 0), piyo, SIZE) == 0);
472+ assert(IntVector_capacity(iv) == SIZE);
473+ assert(IntVector_size(iv) == SIZE);
474+ assert(!IntVector_empty(iv));
475+ /* at */
476+ for (i = 0; i < SIZE; i++) {
477+ assert(*IntVector_at(iv, i) == piyo[i]);
478+ *IntVector_at(iv, i) = piyo[i]+1;
479+ assert(*IntVector_at(iv, i) == piyo[i]+1);
480+ }
481+ assert(IntVector_capacity(iv) == SIZE);
482+ assert(IntVector_size(iv) == SIZE);
483+ assert(!IntVector_empty(iv));
484+
485+ IntVector_delete(iv);
486+}
487+
488+void VectorTest_test_2_4(void)
489+{
490+ int i;
491+ int buf[256];
492+ printf("***** test_2_4 *****\n");
493+ iv = IntVector_new(SIZE);
494+ /* insert */
495+ for (i = 0; i < SIZE; i++) {
496+ assert(IntVector_insert(iv, 0, piyo[i]));
497+ buf[i] = SIZE - i - 1;
498+ }
499+ assert(memcmp(IntVector_at(iv, 0), buf, SIZE) == 0);
500+ assert(IntVector_capacity(iv) == SIZE);
501+ assert(IntVector_size(iv) == SIZE);
502+ assert(!IntVector_empty(iv));
503+ for (i = SIZE; i < 256; i++) {
504+ assert(IntVector_insert(iv, IntVector_size(iv), piyo[i]));
505+ buf[i] = i;
506+ assert(IntVector_capacity(iv) > SIZE);
507+ }
508+ assert(memcmp(&IntVector_at(iv, 0)[SIZE], &buf[SIZE], 256-SIZE) == 0);
509+ assert(IntVector_size(iv) == 256);
510+ assert(IntVector_insert(iv, SIZE, 0));
511+ assert(memcmp(IntVector_at(iv, 0), buf, SIZE) == 0);
512+ assert(IntVector_at(iv, 0)[SIZE] == 0);
513+ assert(memcmp(&IntVector_at(iv, 0)[SIZE+1], &buf[SIZE], 256-SIZE) == 0);
514+ assert(IntVector_size(iv) == 257);
515+ /* erase */
516+ IntVector_erase(iv, SIZE, 1);
517+ assert(memcmp(IntVector_at(iv, 0), buf, SIZE) == 0);
518+ assert(memcmp(&IntVector_at(iv, 0)[SIZE], &buf[SIZE], 256-SIZE) == 0);
519+ for (i = SIZE; i < 256; i++) {
520+ IntVector_erase(iv, IntVector_size(iv)-1, 1);
521+ }
522+ assert(memcmp(IntVector_at(iv, 0), buf, SIZE) == 0);
523+ assert(IntVector_size(iv) == SIZE);
524+ for (i = 0; i < SIZE; i++) {
525+ assert(memcmp(IntVector_at(iv, 0), &buf[i], SIZE-i) == 0);
526+ IntVector_erase(iv, 0, 1);
527+ assert(IntVector_size(iv) == SIZE-i-1);
528+ }
529+ assert(IntVector_size(iv) == 0);
530+
531+ IntVector_delete(iv);
532+}
533+
534+void VectorTest_test_2_5(void)
535+{
536+ printf("***** test_2_5 *****\n");
537+ iv = IntVector_new(SIZE);
538+ vector_init_piyo();
539+ /* insert_array */
540+ assert(IntVector_insert_array(iv, 0, piyo, SIZE));
541+ assert(memcmp(IntVector_at(iv, 0), piyo, SIZE) == 0);
542+ assert(IntVector_capacity(iv) == SIZE);
543+ assert(IntVector_size(iv) == SIZE);
544+
545+ assert(IntVector_insert_array(iv, IntVector_size(iv), &piyo[SIZE], 256-SIZE));
546+ assert(memcmp(IntVector_at(iv, 0), piyo, 256) == 0);
547+ assert(IntVector_capacity(iv) > SIZE);
548+ assert(IntVector_size(iv) == 256);
549+
550+ assert(IntVector_insert_array(iv, SIZE, piyo, SIZE));
551+ assert(memcmp(IntVector_at(iv, 0), piyo, SIZE) == 0);
552+ assert(memcmp(&IntVector_at(iv, 0)[SIZE], piyo, 256) == 0);
553+ assert(IntVector_size(iv) == 256+SIZE);
554+
555+ assert(IntVector_insert_array(iv, 0, piyo, 1));
556+ assert(*IntVector_at(iv, 0) == piyo[0]);
557+ assert(memcmp(&IntVector_at(iv, 0)[1], piyo, SIZE) == 0);
558+ assert(memcmp(&IntVector_at(iv, 0)[SIZE+1], piyo, 256) == 0);
559+ assert(IntVector_size(iv) == 257+SIZE);
560+ /* erase */
561+ IntVector_erase(iv, 0, 1);
562+ assert(memcmp(IntVector_at(iv, 0), piyo, SIZE) == 0);
563+ assert(memcmp(&IntVector_at(iv, 0)[SIZE], piyo, 256) == 0);
564+ assert(IntVector_size(iv) == 256+SIZE);
565+
566+ IntVector_erase(iv, SIZE, SIZE);
567+ assert(memcmp(IntVector_at(iv, 0), piyo, 256) == 0);
568+ assert(IntVector_size(iv) == 256);
569+
570+ IntVector_erase(iv, SIZE, 256-SIZE);
571+ assert(memcmp(IntVector_at(iv, 0), piyo, SIZE) == 0);
572+ assert(IntVector_size(iv) == SIZE);
573+
574+ IntVector_erase(iv, 0, SIZE);
575+ assert(IntVector_size(iv) == 0);
576+
577+ IntVector_delete(iv);
578+}
579+
580+
581+
582+
583+void VectorTest_run(void)
584+{
585+ printf("\n===== vector test =====\n");
586+ VectorTest_test_1_1();
587+ VectorTest_test_1_2();
588+ VectorTest_test_1_3();
589+ VectorTest_test_1_4();
590+ VectorTest_test_1_5();
591+ VectorTest_test_1_6();
592+ VectorTest_test_2_1();
593+ VectorTest_test_2_2();
594+ VectorTest_test_2_3();
595+ VectorTest_test_2_4();
596+ VectorTest_test_2_5();
597+}
598+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/deque_test.c (nonexistent)
+++ tags/rel-0.2.1/test/deque_test.c (revision 43)
@@ -0,0 +1,929 @@
1+#include "test.h"
2+
3+
4+
5+DEQUE_IMPLEMENT(UCharDeque, unsigned char)
6+DEQUE_IMPLEMENT(IntDeque, int)
7+
8+static UCharDeque *ud;
9+static IntDeque *id;
10+
11+#define MAX 255
12+
13+static unsigned char hoge[MAX];
14+static int piyo[MAX];
15+
16+void deque_init_hoge(void)
17+{
18+ int i;
19+ for (i = 0; i < MAX; i++) {
20+ hoge[i] = i;
21+ }
22+}
23+
24+void deque_init_piyo(void)
25+{
26+ int i;
27+ for (i = 0; i < MAX; i++) {
28+ piyo[i] = i;
29+ }
30+}
31+
32+int ud_cmp(UCharDeque *x, size_t idx, unsigned char *buf, size_t n)
33+{
34+ int i, j;
35+ for (i = idx, j = 0; i < idx + n; i++, j++) {
36+/* printf("%d, %d\n", *UCharDeque_at(x, i), buf[j]);*/
37+ if (*UCharDeque_at(x, i) != buf[j]) return 1;
38+ }
39+ return 0;
40+}
41+
42+int id_cmp(IntDeque *x, size_t idx, int *buf, size_t n)
43+{
44+ int i, j;
45+ for (i = idx, j = 0; i < idx + n; i++, j++) {
46+/* printf("%d, %d\n", *IntDeque_at(x, i), buf[j]);*/
47+ if (*IntDeque_at(x, i) != buf[j]) return 1;
48+ }
49+ return 0;
50+}
51+
52+
53+void DequeTest_test_1_1(void)
54+{
55+ int i, j;
56+ printf("***** test_1_1 *****\n");
57+ ud = UCharDeque_new(MAX);
58+ /* 初期状態 */
59+ assert(UCharDeque_max_size(ud) == MAX);
60+ assert(UCharDeque_size(ud) == 0);
61+ assert(UCharDeque_empty(ud));
62+ assert(!UCharDeque_full(ud));
63+ for (j = 0; j < MAX+1; j++) {
64+ ud->begin = ud->end = j;
65+ assert(UCharDeque_size(ud) == 0);
66+ /* insert_array */
67+ UCharDeque_clear(ud);
68+ assert(UCharDeque_insert_array(ud, 0, hoge, MAX));
69+ assert(ud_cmp(ud, 0, hoge, MAX) == 0);
70+ assert(UCharDeque_size(ud) == MAX);
71+ assert(!UCharDeque_empty(ud));
72+ assert(UCharDeque_full(ud));
73+ /* at */
74+ for (i = 0; i < UCharDeque_size(ud); i++) {
75+ assert(*UCharDeque_at(ud, i) == hoge[i]);
76+ *UCharDeque_at(ud, i) = hoge[MAX - i -1];
77+ assert(*UCharDeque_at(ud, i) == hoge[MAX - i -1]);
78+ }
79+ /* clear */
80+ UCharDeque_clear(ud);
81+ assert(UCharDeque_size(ud) == 0);
82+ assert(UCharDeque_empty(ud));
83+ assert(!UCharDeque_full(ud));
84+ /* insert_array */
85+ UCharDeque_clear(ud);
86+ assert(UCharDeque_insert_array(ud, 0, hoge, MAX/2));
87+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
88+ assert(UCharDeque_size(ud) == MAX/2);
89+ assert(!UCharDeque_empty(ud));
90+ assert(!UCharDeque_full(ud));
91+ /* clear */
92+ UCharDeque_clear(ud);
93+ assert(UCharDeque_size(ud) == 0);
94+ }
95+
96+ UCharDeque_delete(ud);
97+}
98+
99+void DequeTest_test_1_2(void)
100+{
101+ int i, j;
102+ size_t size;
103+ printf("***** test_1_2 *****\n");
104+ ud = UCharDeque_new(MAX);
105+ for (j = 0; j < MAX+1; j++) {
106+ ud->begin = ud->end = j;
107+ assert(UCharDeque_size(ud) == 0);
108+ /* push_back */
109+ /* MAXまで */
110+ for (i = 0; i < MAX; i++) {
111+ size = UCharDeque_size(ud);
112+ assert(UCharDeque_push_back(ud, hoge[i]));
113+ assert(UCharDeque_size(ud) == size + 1);
114+ }
115+ assert(ud_cmp(ud, 0, hoge, MAX) == 0);
116+ assert(UCharDeque_size(ud) == MAX);
117+ assert(!UCharDeque_empty(ud));
118+ assert(UCharDeque_full(ud));
119+ /* FULLで失敗 */
120+ assert(!UCharDeque_push_back(ud, hoge[0]));
121+ assert(UCharDeque_size(ud) == MAX);
122+ assert(!UCharDeque_empty(ud));
123+ assert(UCharDeque_full(ud));
124+ /* front */
125+ assert(UCharDeque_front(ud) == hoge[0]);
126+ /* back */
127+ assert(UCharDeque_back(ud) == hoge[MAX-1]);
128+ /* pop_front */
129+ size = UCharDeque_size(ud);
130+ assert(UCharDeque_pop_front(ud) == hoge[0]);
131+ assert(UCharDeque_size(ud) == size - 1);
132+ /* push_back */
133+ size = UCharDeque_size(ud);
134+ assert(UCharDeque_push_back(ud, hoge[0]));
135+ assert(UCharDeque_size(ud) == size + 1);
136+ assert(ud_cmp(ud, 0, &hoge[1], MAX-1) == 0);
137+ assert(ud_cmp(ud, MAX-1, hoge, 1) == 0);
138+ assert(UCharDeque_size(ud) == MAX);
139+ assert(!UCharDeque_empty(ud));
140+ assert(UCharDeque_full(ud));
141+ /* pop_front */
142+ for (i = 0; i < MAX; i++) {
143+ size = UCharDeque_size(ud);
144+ if (i < MAX-1) {
145+ assert(UCharDeque_pop_front(ud) == hoge[i+1]);
146+ } else {
147+ assert(UCharDeque_pop_front(ud) == hoge[0]);
148+ }
149+ assert(UCharDeque_size(ud) == size - 1);
150+ }
151+ assert(UCharDeque_size(ud) == 0);
152+ assert(UCharDeque_empty(ud));
153+ assert(!UCharDeque_full(ud));
154+ }
155+
156+ UCharDeque_delete(ud);
157+}
158+
159+void DequeTest_test_1_3(void)
160+{
161+ int i, j;
162+ size_t size;
163+ printf("***** test_1_3 *****\n");
164+ ud = UCharDeque_new(MAX);
165+ for (j = 0; j < MAX+1; j++) {
166+ ud->begin = ud->end = j;
167+ assert(UCharDeque_size(ud) == 0);
168+ /* push_front */
169+ /* MAXまで */
170+ for (i = 0; i < MAX; i++) {
171+ size = UCharDeque_size(ud);
172+ assert(UCharDeque_push_front(ud, hoge[MAX-i-1]));
173+ assert(UCharDeque_size(ud) == size + 1);
174+ }
175+ assert(ud_cmp(ud, 0, hoge, MAX) == 0);
176+ assert(UCharDeque_size(ud) == MAX);
177+ assert(!UCharDeque_empty(ud));
178+ assert(UCharDeque_full(ud));
179+ /* FULLで失敗 */
180+ assert(!UCharDeque_push_front(ud, hoge[0]));
181+ assert(UCharDeque_size(ud) == MAX);
182+ assert(!UCharDeque_empty(ud));
183+ assert(UCharDeque_full(ud));
184+ /* front */
185+ assert(UCharDeque_front(ud) == hoge[0]);
186+ /* back */
187+ assert(UCharDeque_back(ud) == hoge[MAX-1]);
188+ /* pop_back */
189+ size = UCharDeque_size(ud);
190+ assert(UCharDeque_pop_back(ud) == hoge[MAX-1]);
191+ assert(UCharDeque_pop_back(ud) == hoge[MAX-2]);
192+ assert(UCharDeque_size(ud) == size - 2);
193+ /* push_front */
194+ size = UCharDeque_size(ud);
195+ assert(UCharDeque_push_front(ud, hoge[MAX-1]));
196+ assert(UCharDeque_push_front(ud, hoge[MAX-2]));
197+ assert(UCharDeque_size(ud) == size + 2);
198+ assert(ud_cmp(ud, 0, &hoge[MAX-2], 2) == 0);
199+ assert(ud_cmp(ud, 2, hoge, MAX-2) == 0);
200+ assert(UCharDeque_size(ud) == MAX);
201+ assert(!UCharDeque_empty(ud));
202+ assert(UCharDeque_full(ud));
203+ /* pop_back */
204+ for (i = MAX-3; i >= 0; i--) {
205+ size = UCharDeque_size(ud);
206+ assert(UCharDeque_pop_back(ud) == hoge[i]);
207+ assert(UCharDeque_size(ud) == size - 1);
208+ }
209+ assert(UCharDeque_pop_back(ud) == hoge[MAX-1]);
210+ assert(UCharDeque_pop_back(ud) == hoge[MAX-2]);
211+
212+ assert(UCharDeque_size(ud) == 0);
213+ assert(UCharDeque_empty(ud));
214+ assert(!UCharDeque_full(ud));
215+ }
216+
217+ UCharDeque_delete(ud);
218+}
219+
220+void DequeTest_test_1_4(void)
221+{
222+ int i, j;
223+ printf("***** test_1_4 *****\n");
224+ ud = UCharDeque_new(MAX);
225+ for (j = 0; j < MAX+1; j++) {
226+ ud->begin = ud->end = j;
227+ assert(UCharDeque_size(ud) == 0);
228+ /* insert */
229+ /* 空状態 */
230+ for (i = 0; i < MAX/2; i++) {
231+ assert(ud_cmp(ud, 0, hoge, i) == 0);
232+ assert(UCharDeque_insert(ud, i, hoge[i]));
233+ }
234+ /* 先頭・前寄り */
235+ assert(UCharDeque_insert(ud, 0, hoge[0]));
236+ assert(UCharDeque_insert(ud, 1, hoge[1]));
237+ assert(UCharDeque_insert(ud, 2, hoge[2]));
238+ assert(ud_cmp(ud, 0, hoge, 3) == 0);
239+ assert(ud_cmp(ud, 3, hoge, MAX/2) == 0);
240+ assert(UCharDeque_size(ud) == MAX/2 + 3);
241+
242+ /* erase */
243+ /* 先頭・前寄り */
244+ UCharDeque_erase(ud, 2, 1);
245+ assert(ud_cmp(ud, 0, hoge, 2) == 0);
246+ assert(ud_cmp(ud, 2, hoge, MAX/2) == 0);
247+ assert(UCharDeque_size(ud) == MAX/2 + 2);
248+ UCharDeque_erase(ud, 1, 1);
249+ assert(ud_cmp(ud, 0, hoge, 1) == 0);
250+ assert(ud_cmp(ud, 1, hoge, MAX/2) == 0);
251+ assert(UCharDeque_size(ud) == MAX/2 + 1);
252+ UCharDeque_erase(ud, 0, 1);
253+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
254+ assert(UCharDeque_size(ud) == MAX/2);
255+
256+ /* insert */
257+ /* 末尾・後ろ寄り */
258+ assert(UCharDeque_insert(ud, MAX/2, hoge[0]));
259+ assert(UCharDeque_insert(ud, MAX/2, hoge[1]));
260+ assert(UCharDeque_insert(ud, MAX/2, hoge[2]));
261+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
262+ assert(*UCharDeque_at(ud, MAX/2 + 0) == hoge[2]);
263+ assert(*UCharDeque_at(ud, MAX/2 + 1) == hoge[1]);
264+ assert(*UCharDeque_at(ud, MAX/2 + 2) == hoge[0]);
265+
266+ /* erase */
267+ /* 末尾・後ろ寄り */
268+ UCharDeque_erase(ud, MAX/2, 1);
269+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
270+ assert(*UCharDeque_at(ud, MAX/2 + 0) == hoge[1]);
271+ assert(*UCharDeque_at(ud, MAX/2 + 1) == hoge[0]);
272+ assert(UCharDeque_size(ud) == MAX/2 + 2);
273+ UCharDeque_erase(ud, MAX/2, 1);
274+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
275+ assert(*UCharDeque_at(ud, MAX/2 + 0) == hoge[0]);
276+ assert(UCharDeque_size(ud) == MAX/2 + 1);
277+ UCharDeque_erase(ud, MAX/2, 1);
278+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
279+ assert(UCharDeque_size(ud) == MAX/2);
280+
281+ /* 先頭から全削除 */
282+ for (i = 0; i < MAX/2; i++) {
283+ assert(ud_cmp(ud, 0, &hoge[i], MAX/2 - i) == 0);
284+ UCharDeque_erase(ud, 0, 1);
285+ }
286+ assert(UCharDeque_size(ud) == 0);
287+
288+ for (i = 0; i < MAX/2; i++) {
289+ assert(ud_cmp(ud, 0, hoge, i) == 0);
290+ assert(UCharDeque_insert(ud, i, hoge[i]));
291+ }
292+ assert(UCharDeque_size(ud) == MAX/2);
293+
294+ /* 末尾から全削除 */
295+ for (i = 0; i < MAX/2; i++) {
296+ assert(ud_cmp(ud, 0, hoge, MAX/2 - i) == 0);
297+ UCharDeque_erase(ud, UCharDeque_size(ud) -1, 1);
298+ }
299+ assert(UCharDeque_size(ud) == 0);
300+
301+ }
302+
303+ UCharDeque_delete(ud);
304+}
305+
306+void DequeTest_test_1_5(void)
307+{
308+ int i;
309+ printf("***** test_1_5 *****\n");
310+ ud = UCharDeque_new(MAX);
311+ for (i = 0; i < MAX+1; i++) {
312+ ud->begin = ud->end = i;
313+ assert(UCharDeque_size(ud) == 0);
314+ /* insert_array */
315+ /* 空状態 */
316+ assert(UCharDeque_insert_array(ud, 0, hoge, MAX/2));
317+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
318+ assert(UCharDeque_size(ud) == MAX/2);
319+
320+ /* insert_array */
321+ /* 先頭 */
322+ assert(UCharDeque_insert_array(ud, 0, hoge, 3));
323+ assert(ud_cmp(ud, 0, hoge, 3) == 0);
324+ assert(ud_cmp(ud, 3, hoge, MAX/2) == 0);
325+ assert(UCharDeque_size(ud) == MAX/2 + 3);
326+ /* erase */
327+ UCharDeque_erase(ud, 0, 3);
328+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
329+ assert(UCharDeque_size(ud) == MAX/2);
330+
331+ /* insert_array */
332+ /* 前寄り */
333+ assert(UCharDeque_insert_array(ud, 2, hoge, 3));
334+ assert(ud_cmp(ud, 0, hoge, 2) == 0);
335+ assert(ud_cmp(ud, 2, hoge, 3) == 0);
336+ assert(ud_cmp(ud, 5, &hoge[2], MAX/2 -2) == 0);
337+ assert(UCharDeque_size(ud) == MAX/2 + 3);
338+ /* 0個指定 */
339+ assert(UCharDeque_insert_array(ud, 1, &hoge[MAX/2], 0));
340+ assert(ud_cmp(ud, 0, hoge, 2) == 0);
341+ assert(ud_cmp(ud, 2, hoge, 3) == 0);
342+ assert(ud_cmp(ud, 5, &hoge[2], MAX/2 -2) == 0);
343+ assert(UCharDeque_size(ud) == MAX/2 + 3);
344+ /* 1個指定 */
345+ assert(UCharDeque_insert_array(ud, 1, &hoge[MAX-1], 1));
346+ assert(ud_cmp(ud, 0, &hoge[0], 1) == 0);
347+ assert(ud_cmp(ud, 1, &hoge[MAX-1], 1) == 0);
348+ assert(ud_cmp(ud, 2, &hoge[1], 1) == 0);
349+ assert(ud_cmp(ud, 3, &hoge[0], 3) == 0);
350+ assert(ud_cmp(ud, 6, &hoge[2], MAX/2 -2) == 0);
351+ assert(UCharDeque_size(ud) == MAX/2 + 4);
352+ /* erase */
353+ /* 0個指定 */
354+ UCharDeque_erase(ud, 1, 0);
355+ assert(ud_cmp(ud, 0, &hoge[0], 1) == 0);
356+ assert(ud_cmp(ud, 1, &hoge[MAX-1], 1) == 0);
357+ assert(ud_cmp(ud, 2, &hoge[1], 1) == 0);
358+ assert(ud_cmp(ud, 3, &hoge[0], 3) == 0);
359+ assert(ud_cmp(ud, 6, &hoge[2], MAX/2 -2) == 0);
360+ assert(UCharDeque_size(ud) == MAX/2 + 4);
361+ /* 1個指定 */
362+ UCharDeque_erase(ud, 1, 1);
363+ assert(ud_cmp(ud, 0, hoge, 2) == 0);
364+ assert(ud_cmp(ud, 2, hoge, 3) == 0);
365+ assert(ud_cmp(ud, 5, &hoge[2], MAX/2 -2) == 0);
366+ assert(UCharDeque_size(ud) == MAX/2 + 3);
367+ /* 前寄り */
368+ UCharDeque_erase(ud, 2, 3);
369+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
370+ assert(UCharDeque_size(ud) == MAX/2);
371+
372+ /* insert_array */
373+ /* 末尾 */
374+ assert(UCharDeque_insert_array(ud, UCharDeque_size(ud), hoge, 3));
375+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
376+ assert(ud_cmp(ud, MAX/2, hoge, 3) == 0);
377+ assert(UCharDeque_size(ud) == MAX/2 + 3);
378+ /* erase */
379+ UCharDeque_erase(ud, UCharDeque_size(ud) - 3, 3);
380+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
381+ assert(UCharDeque_size(ud) == MAX/2);
382+
383+ /* insert_array */
384+ /* 後ろ寄り */
385+ assert(UCharDeque_insert_array(ud, UCharDeque_size(ud) - 2, hoge, 3));
386+ assert(ud_cmp(ud, 0, hoge, MAX/2 -2) == 0);
387+ assert(ud_cmp(ud, MAX/2 -2, hoge, 3) == 0);
388+ assert(ud_cmp(ud, MAX/2 +1, &hoge[MAX/2 -2], 2) == 0);
389+ assert(UCharDeque_size(ud) == MAX/2 + 3);
390+ /* 0個指定 */
391+ assert(UCharDeque_insert_array(ud, UCharDeque_size(ud) - 1, &hoge[MAX-1], 0));
392+ assert(ud_cmp(ud, 0, hoge, MAX/2 -2) == 0);
393+ assert(ud_cmp(ud, MAX/2 -2, hoge, 3) == 0);
394+ assert(ud_cmp(ud, MAX/2 +1, &hoge[MAX/2 -2], 2) == 0);
395+ assert(UCharDeque_size(ud) == MAX/2 + 3);
396+ /* 1個指定 */
397+ assert(UCharDeque_insert_array(ud, UCharDeque_size(ud) - 1, &hoge[MAX-1], 1));
398+ assert(ud_cmp(ud, 0, hoge, MAX/2 -2) == 0);
399+ assert(ud_cmp(ud, MAX/2 -2, hoge, 3) == 0);
400+ assert(ud_cmp(ud, MAX/2 +1, &hoge[MAX/2 -2], 1) == 0);
401+ assert(ud_cmp(ud, MAX/2 +2, &hoge[MAX-1], 1) == 0);
402+ assert(ud_cmp(ud, MAX/2 +3, &hoge[MAX/2 -1], 1) == 0);
403+ assert(UCharDeque_size(ud) == MAX/2 + 4);
404+ /* erase */
405+ /* 0個指定 */
406+ UCharDeque_erase(ud, UCharDeque_size(ud) - 2, 0);
407+ assert(ud_cmp(ud, 0, hoge, MAX/2 -2) == 0);
408+ assert(ud_cmp(ud, MAX/2 -2, hoge, 3) == 0);
409+ assert(ud_cmp(ud, MAX/2 +1, &hoge[MAX/2 -2], 1) == 0);
410+ assert(ud_cmp(ud, MAX/2 +2, &hoge[MAX-1], 1) == 0);
411+ assert(ud_cmp(ud, MAX/2 +3, &hoge[MAX/2 -1], 1) == 0);
412+ assert(UCharDeque_size(ud) == MAX/2 + 4);
413+ /* 1個指定 */
414+ UCharDeque_erase(ud, UCharDeque_size(ud) - 2, 1);
415+ assert(ud_cmp(ud, 0, hoge, MAX/2 -2) == 0);
416+ assert(ud_cmp(ud, MAX/2 -2, hoge, 3) == 0);
417+ assert(ud_cmp(ud, MAX/2 +1, &hoge[MAX/2 -2], 2) == 0);
418+ assert(UCharDeque_size(ud) == MAX/2 + 3);
419+ /* 後ろ寄り */
420+ UCharDeque_erase(ud, UCharDeque_size(ud) - 5, 3);
421+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
422+ assert(UCharDeque_size(ud) == MAX/2);
423+
424+ /* MAX以上で失敗 */
425+ assert(!UCharDeque_insert_array(ud, 0, hoge, MAX - MAX/2 +1));
426+ assert(ud_cmp(ud, 0, hoge, MAX/2) == 0);
427+ assert(UCharDeque_size(ud) == MAX/2);
428+ /* MAXになるまで */
429+ assert(UCharDeque_insert_array(ud, 0, hoge, MAX - MAX/2));
430+ assert(UCharDeque_size(ud) == MAX);
431+ assert(UCharDeque_full(ud));
432+ /* 先頭から全削除 */
433+ UCharDeque_erase(ud, 0, UCharDeque_size(ud));
434+ assert(UCharDeque_size(ud) == 0);
435+ }
436+
437+ UCharDeque_delete(ud);
438+}
439+
440+void DequeTest_test_1_6(void)
441+{
442+ int i;
443+ UCharDeque *x;
444+ unsigned char b[] = {1, 2, 4, 43, 2, 54, 1, 0, 2, 24};
445+ printf("***** test_1_6 *****\n");
446+ ud = UCharDeque_new(MAX);
447+ for (i = 0; i < MAX; i++) {
448+ assert(UCharDeque_push_back(ud, hoge[i]));
449+ }
450+ /* assign */
451+ assert(UCharDeque_size(ud) == MAX);
452+ x = UCharDeque_new(MAX);
453+ assert(UCharDeque_assign(x, ud, 0, UCharDeque_size(ud)));
454+ assert(UCharDeque_size(ud) == UCharDeque_size(x));
455+ assert(UCharDeque_max_size(ud) == UCharDeque_max_size(x));
456+ for (i = 0; i < UCharDeque_size(ud); i++) {
457+ assert(*UCharDeque_at(x, i) == *UCharDeque_at(ud, i));
458+ }
459+ assert(UCharDeque_assign(x, x, 0, UCharDeque_size(x)));
460+ assert(UCharDeque_size(ud) == UCharDeque_size(x));
461+ for (i = 0; i < UCharDeque_size(ud); i++) {
462+ assert(*UCharDeque_at(x, i) == *UCharDeque_at(ud, i));
463+ }
464+ assert(UCharDeque_assign(x, x, 1, 2));
465+ assert(UCharDeque_size(x) == 2);
466+ assert(*UCharDeque_at(x, 0) == *UCharDeque_at(ud, 1));
467+ assert(*UCharDeque_at(x, 1) == *UCharDeque_at(ud, 2));
468+ assert(UCharDeque_assign(x, x, 0, 0));
469+ assert(UCharDeque_size(x) == 0);
470+ /* resize */
471+ UCharDeque_clear(ud);
472+ assert(UCharDeque_resize(ud, 64, 100));
473+ assert(UCharDeque_size(ud) == 64);
474+ for (i = 0; i < UCharDeque_size(ud); i++) {
475+ assert(*UCharDeque_at(ud, i) == 100);
476+ }
477+ assert(UCharDeque_resize(ud, 16, 99));
478+ assert(UCharDeque_size(ud) == 16);
479+ for (i = 0; i < UCharDeque_size(ud); i++) {
480+ assert(*UCharDeque_at(ud, i) == 100);
481+ }
482+ assert(UCharDeque_resize(ud, 32, 99));
483+ for (i = 0; i < UCharDeque_size(ud); i++) {
484+ if (i < 16) {
485+ assert(*UCharDeque_at(ud, i) == 100);
486+ } else {
487+ assert(*UCharDeque_at(ud, i) == 99);
488+ }
489+ }
490+ assert(UCharDeque_size(ud) == 32);
491+ assert(UCharDeque_resize(ud, MAX+1, 99) == 0);
492+ assert(UCharDeque_resize(ud, 0, 100));
493+ assert(UCharDeque_size(ud) == 0);
494+ /* swap */
495+ UCharDeque_clear(ud);
496+ assert(UCharDeque_insert_array(ud, 0, hoge, MAX));
497+ assert(ud_cmp(ud, 0, hoge, MAX) == 0);
498+ assert(UCharDeque_size(ud) == MAX);
499+ UCharDeque_clear(x);
500+ assert(UCharDeque_insert_array(x, 0, b, sizeof b));
501+ assert(ud_cmp(x, 0, b, sizeof b) == 0);
502+ assert(UCharDeque_size(x) == sizeof b);
503+
504+ UCharDeque_swap(ud, x);
505+
506+ assert(ud_cmp(ud, 0, b, sizeof b) == 0);
507+ assert(UCharDeque_size(ud) == sizeof b);
508+ assert(ud_cmp(x, 0, hoge, MAX) == 0);
509+ assert(UCharDeque_size(x) == MAX);
510+
511+
512+ UCharDeque_delete(ud);
513+ UCharDeque_delete(x);
514+}
515+
516+
517+
518+void DequeTest_test_2_1(void)
519+{
520+ int i, j;
521+ printf("***** test_2_1 *****\n");
522+ id = IntDeque_new(MAX);
523+ /* 初期状態 */
524+ assert(IntDeque_max_size(id) == MAX);
525+ assert(IntDeque_size(id) == 0);
526+ assert(IntDeque_empty(id));
527+ assert(!IntDeque_full(id));
528+ for (j = 0; j < MAX+1; j++) {
529+ id->begin = id->end = j;
530+ assert(IntDeque_size(id) == 0);
531+ /* insert_array */
532+ IntDeque_clear(id);
533+ assert(IntDeque_insert_array(id, 0, piyo, MAX));
534+ assert(id_cmp(id, 0, piyo, MAX) == 0);
535+ assert(IntDeque_size(id) == MAX);
536+ assert(!IntDeque_empty(id));
537+ assert(IntDeque_full(id));
538+ /* at */
539+ for (i = 0; i < IntDeque_size(id); i++) {
540+ assert(*IntDeque_at(id, i) == hoge[i]);
541+ *IntDeque_at(id, i) = hoge[MAX - i -1];
542+ assert(*IntDeque_at(id, i) == hoge[MAX - i -1]);
543+ }
544+ /* clear */
545+ IntDeque_clear(id);
546+ assert(IntDeque_size(id) == 0);
547+ assert(IntDeque_empty(id));
548+ assert(!IntDeque_full(id));
549+ /* insert_array */
550+ IntDeque_clear(id);
551+ assert(IntDeque_insert_array(id, 0, piyo, MAX/2));
552+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
553+ assert(IntDeque_size(id) == MAX/2);
554+ assert(!IntDeque_empty(id));
555+ assert(!IntDeque_full(id));
556+ /* clear */
557+ IntDeque_clear(id);
558+ assert(IntDeque_size(id) == 0);
559+ }
560+
561+ IntDeque_delete(id);
562+}
563+
564+void DequeTest_test_2_2(void)
565+{
566+ int i, j;
567+ size_t size;
568+ printf("***** test_2_2 *****\n");
569+ id = IntDeque_new(MAX);
570+ for (j = 0; j < MAX+1; j++) {
571+ id->begin = id->end = j;
572+ assert(IntDeque_size(id) == 0);
573+ /* push_back */
574+ /* MAXまで */
575+ for (i = 0; i < MAX; i++) {
576+ size = IntDeque_size(id);
577+ assert(IntDeque_push_back(id, piyo[i]));
578+ assert(IntDeque_size(id) == size + 1);
579+ }
580+ assert(id_cmp(id, 0, piyo, MAX) == 0);
581+ assert(IntDeque_size(id) == MAX);
582+ assert(!IntDeque_empty(id));
583+ assert(IntDeque_full(id));
584+ /* FULLで失敗 */
585+ assert(!IntDeque_push_back(id, piyo[0]));
586+ assert(IntDeque_size(id) == MAX);
587+ assert(!IntDeque_empty(id));
588+ assert(IntDeque_full(id));
589+ /* front */
590+ assert(IntDeque_front(id) == piyo[0]);
591+ /* back */
592+ assert(IntDeque_back(id) == piyo[MAX-1]);
593+ /* pop_front */
594+ size = IntDeque_size(id);
595+ assert(IntDeque_pop_front(id) == piyo[0]);
596+ assert(IntDeque_size(id) == size - 1);
597+ /* push_back */
598+ size = IntDeque_size(id);
599+ assert(IntDeque_push_back(id, piyo[0]));
600+ assert(IntDeque_size(id) == size + 1);
601+ assert(id_cmp(id, 0, &piyo[1], MAX-1) == 0);
602+ assert(id_cmp(id, MAX-1, piyo, 1) == 0);
603+ assert(IntDeque_size(id) == MAX);
604+ assert(!IntDeque_empty(id));
605+ assert(IntDeque_full(id));
606+ /* pop_front */
607+ for (i = 0; i < MAX; i++) {
608+ size = IntDeque_size(id);
609+ if (i < MAX-1) {
610+ assert(IntDeque_pop_front(id) == piyo[i+1]);
611+ } else {
612+ assert(IntDeque_pop_front(id) == piyo[0]);
613+ }
614+ assert(IntDeque_size(id) == size - 1);
615+ }
616+ assert(IntDeque_size(id) == 0);
617+ assert(IntDeque_empty(id));
618+ assert(!IntDeque_full(id));
619+ }
620+
621+ IntDeque_delete(id);
622+}
623+
624+void DequeTest_test_2_3(void)
625+{
626+ int i, j;
627+ size_t size;
628+ printf("***** test_2_3 *****\n");
629+ id = IntDeque_new(MAX);
630+ for (j = 0; j < MAX+1; j++) {
631+ id->begin = id->end = j;
632+ assert(IntDeque_size(id) == 0);
633+ /* push_front */
634+ /* MAXまで */
635+ for (i = 0; i < MAX; i++) {
636+ size = IntDeque_size(id);
637+ assert(IntDeque_push_front(id, piyo[MAX-i-1]));
638+ assert(IntDeque_size(id) == size + 1);
639+ }
640+ assert(id_cmp(id, 0, piyo, MAX) == 0);
641+ assert(IntDeque_size(id) == MAX);
642+ assert(!IntDeque_empty(id));
643+ assert(IntDeque_full(id));
644+ /* FULLで失敗 */
645+ assert(!IntDeque_push_front(id, piyo[0]));
646+ assert(IntDeque_size(id) == MAX);
647+ assert(!IntDeque_empty(id));
648+ assert(IntDeque_full(id));
649+ /* front */
650+ assert(IntDeque_front(id) == piyo[0]);
651+ /* back */
652+ assert(IntDeque_back(id) == piyo[MAX-1]);
653+ /* pop_back */
654+ size = IntDeque_size(id);
655+ assert(IntDeque_pop_back(id) == piyo[MAX-1]);
656+ assert(IntDeque_pop_back(id) == piyo[MAX-2]);
657+ assert(IntDeque_size(id) == size - 2);
658+ /* push_front */
659+ size = IntDeque_size(id);
660+ assert(IntDeque_push_front(id, piyo[MAX-1]));
661+ assert(IntDeque_push_front(id, piyo[MAX-2]));
662+ assert(IntDeque_size(id) == size + 2);
663+ assert(id_cmp(id, 0, &piyo[MAX-2], 2) == 0);
664+ assert(id_cmp(id, 2, piyo, MAX-2) == 0);
665+ assert(IntDeque_size(id) == MAX);
666+ assert(!IntDeque_empty(id));
667+ assert(IntDeque_full(id));
668+ /* pop_back */
669+ for (i = MAX-3; i >= 0; i--) {
670+ size = IntDeque_size(id);
671+ assert(IntDeque_pop_back(id) == piyo[i]);
672+ assert(IntDeque_size(id) == size - 1);
673+ }
674+ assert(IntDeque_pop_back(id) == piyo[MAX-1]);
675+ assert(IntDeque_pop_back(id) == piyo[MAX-2]);
676+
677+ assert(IntDeque_size(id) == 0);
678+ assert(IntDeque_empty(id));
679+ assert(!IntDeque_full(id));
680+ }
681+
682+ IntDeque_delete(id);
683+}
684+
685+void DequeTest_test_2_4(void)
686+{
687+ int i, j;
688+ printf("***** test_2_4 *****\n");
689+ id = IntDeque_new(MAX);
690+ for (j = 0; j < MAX+1; j++) {
691+ id->begin = id->end = j;
692+ assert(IntDeque_size(id) == 0);
693+ /* insert */
694+ /* 空状態 */
695+ for (i = 0; i < MAX/2; i++) {
696+ assert(id_cmp(id, 0, piyo, i) == 0);
697+ assert(IntDeque_insert(id, i, piyo[i]));
698+ }
699+ /* 先頭・前寄り */
700+ assert(IntDeque_insert(id, 0, piyo[0]));
701+ assert(IntDeque_insert(id, 1, piyo[1]));
702+ assert(IntDeque_insert(id, 2, piyo[2]));
703+ assert(id_cmp(id, 0, piyo, 3) == 0);
704+ assert(id_cmp(id, 3, piyo, MAX/2) == 0);
705+ assert(IntDeque_size(id) == MAX/2 + 3);
706+
707+ /* erase */
708+ /* 先頭・前寄り */
709+ IntDeque_erase(id, 2, 1);
710+ assert(id_cmp(id, 0, piyo, 2) == 0);
711+ assert(id_cmp(id, 2, piyo, MAX/2) == 0);
712+ assert(IntDeque_size(id) == MAX/2 + 2);
713+ IntDeque_erase(id, 1, 1);
714+ assert(id_cmp(id, 0, piyo, 1) == 0);
715+ assert(id_cmp(id, 1, piyo, MAX/2) == 0);
716+ assert(IntDeque_size(id) == MAX/2 + 1);
717+ IntDeque_erase(id, 0, 1);
718+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
719+ assert(IntDeque_size(id) == MAX/2);
720+
721+ /* insert */
722+ /* 末尾・後ろ寄り */
723+ assert(IntDeque_insert(id, MAX/2, piyo[0]));
724+ assert(IntDeque_insert(id, MAX/2, piyo[1]));
725+ assert(IntDeque_insert(id, MAX/2, piyo[2]));
726+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
727+ assert(*IntDeque_at(id, MAX/2 + 0) == piyo[2]);
728+ assert(*IntDeque_at(id, MAX/2 + 1) == piyo[1]);
729+ assert(*IntDeque_at(id, MAX/2 + 2) == piyo[0]);
730+
731+ /* erase */
732+ /* 末尾・後ろ寄り */
733+ IntDeque_erase(id, MAX/2, 1);
734+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
735+ assert(*IntDeque_at(id, MAX/2 + 0) == piyo[1]);
736+ assert(*IntDeque_at(id, MAX/2 + 1) == piyo[0]);
737+ assert(IntDeque_size(id) == MAX/2 + 2);
738+ IntDeque_erase(id, MAX/2, 1);
739+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
740+ assert(*IntDeque_at(id, MAX/2 + 0) == piyo[0]);
741+ assert(IntDeque_size(id) == MAX/2 + 1);
742+ IntDeque_erase(id, MAX/2, 1);
743+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
744+ assert(IntDeque_size(id) == MAX/2);
745+
746+ /* 先頭から全削除 */
747+ for (i = 0; i < MAX/2; i++) {
748+ assert(id_cmp(id, 0, &piyo[i], MAX/2 - i) == 0);
749+ IntDeque_erase(id, 0, 1);
750+ }
751+ assert(IntDeque_size(id) == 0);
752+
753+ for (i = 0; i < MAX/2; i++) {
754+ assert(id_cmp(id, 0, piyo, i) == 0);
755+ assert(IntDeque_insert(id, i, piyo[i]));
756+ }
757+ assert(IntDeque_size(id) == MAX/2);
758+
759+ /* 末尾から全削除 */
760+ for (i = 0; i < MAX/2; i++) {
761+ assert(id_cmp(id, 0, piyo, MAX/2 - i) == 0);
762+ IntDeque_erase(id, IntDeque_size(id) -1, 1);
763+ }
764+ assert(IntDeque_size(id) == 0);
765+
766+ }
767+
768+ IntDeque_delete(id);
769+}
770+
771+void DequeTest_test_2_5(void)
772+{
773+ int i;
774+ printf("***** test_2_5 *****\n");
775+ id = IntDeque_new(MAX);
776+ for (i = 0; i < MAX+1; i++) {
777+ id->begin = id->end = i;
778+ assert(IntDeque_size(id) == 0);
779+ /* insert_array */
780+ /* 空状態 */
781+ assert(IntDeque_insert_array(id, 0, piyo, MAX/2));
782+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
783+ assert(IntDeque_size(id) == MAX/2);
784+
785+ /* insert_array */
786+ /* 先頭 */
787+ assert(IntDeque_insert_array(id, 0, piyo, 3));
788+ assert(id_cmp(id, 0, piyo, 3) == 0);
789+ assert(id_cmp(id, 3, piyo, MAX/2) == 0);
790+ assert(IntDeque_size(id) == MAX/2 + 3);
791+ /* erase */
792+ IntDeque_erase(id, 0, 3);
793+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
794+ assert(IntDeque_size(id) == MAX/2);
795+
796+ /* insert_array */
797+ /* 前寄り */
798+ assert(IntDeque_insert_array(id, 2, piyo, 3));
799+ assert(id_cmp(id, 0, piyo, 2) == 0);
800+ assert(id_cmp(id, 2, piyo, 3) == 0);
801+ assert(id_cmp(id, 5, &piyo[2], MAX/2 -2) == 0);
802+ assert(IntDeque_size(id) == MAX/2 + 3);
803+ /* 0個指定 */
804+ assert(IntDeque_insert_array(id, 1, &piyo[MAX/2], 0));
805+ assert(id_cmp(id, 0, piyo, 2) == 0);
806+ assert(id_cmp(id, 2, piyo, 3) == 0);
807+ assert(id_cmp(id, 5, &piyo[2], MAX/2 -2) == 0);
808+ assert(IntDeque_size(id) == MAX/2 + 3);
809+ /* 1個指定 */
810+ assert(IntDeque_insert_array(id, 1, &piyo[MAX-1], 1));
811+ assert(id_cmp(id, 0, &piyo[0], 1) == 0);
812+ assert(id_cmp(id, 1, &piyo[MAX-1], 1) == 0);
813+ assert(id_cmp(id, 2, &piyo[1], 1) == 0);
814+ assert(id_cmp(id, 3, &piyo[0], 3) == 0);
815+ assert(id_cmp(id, 6, &piyo[2], MAX/2 -2) == 0);
816+ assert(IntDeque_size(id) == MAX/2 + 4);
817+ /* erase */
818+ /* 0個指定 */
819+ IntDeque_erase(id, 1, 0);
820+ assert(id_cmp(id, 0, &piyo[0], 1) == 0);
821+ assert(id_cmp(id, 1, &piyo[MAX-1], 1) == 0);
822+ assert(id_cmp(id, 2, &piyo[1], 1) == 0);
823+ assert(id_cmp(id, 3, &piyo[0], 3) == 0);
824+ assert(id_cmp(id, 6, &piyo[2], MAX/2 -2) == 0);
825+ assert(IntDeque_size(id) == MAX/2 + 4);
826+ /* 1個指定 */
827+ IntDeque_erase(id, 1, 1);
828+ assert(id_cmp(id, 0, piyo, 2) == 0);
829+ assert(id_cmp(id, 2, piyo, 3) == 0);
830+ assert(id_cmp(id, 5, &piyo[2], MAX/2 -2) == 0);
831+ assert(IntDeque_size(id) == MAX/2 + 3);
832+ /* 前寄り */
833+ IntDeque_erase(id, 2, 3);
834+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
835+ assert(IntDeque_size(id) == MAX/2);
836+
837+ /* insert_array */
838+ /* 末尾 */
839+ assert(IntDeque_insert_array(id, IntDeque_size(id), piyo, 3));
840+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
841+ assert(id_cmp(id, MAX/2, piyo, 3) == 0);
842+ assert(IntDeque_size(id) == MAX/2 + 3);
843+ /* erase */
844+ IntDeque_erase(id, IntDeque_size(id) - 3, 3);
845+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
846+ assert(IntDeque_size(id) == MAX/2);
847+
848+ /* insert_array */
849+ /* 後ろ寄り */
850+ assert(IntDeque_insert_array(id, IntDeque_size(id) - 2, piyo, 3));
851+ assert(id_cmp(id, 0, piyo, MAX/2 -2) == 0);
852+ assert(id_cmp(id, MAX/2 -2, piyo, 3) == 0);
853+ assert(id_cmp(id, MAX/2 +1, &piyo[MAX/2 -2], 2) == 0);
854+ assert(IntDeque_size(id) == MAX/2 + 3);
855+ /* 0個指定 */
856+ assert(IntDeque_insert_array(id, IntDeque_size(id) - 1, &piyo[MAX-1], 0));
857+ assert(id_cmp(id, 0, piyo, MAX/2 -2) == 0);
858+ assert(id_cmp(id, MAX/2 -2, piyo, 3) == 0);
859+ assert(id_cmp(id, MAX/2 +1, &piyo[MAX/2 -2], 2) == 0);
860+ assert(IntDeque_size(id) == MAX/2 + 3);
861+ /* 1個指定 */
862+ assert(IntDeque_insert_array(id, IntDeque_size(id) - 1, &piyo[MAX-1], 1));
863+ assert(id_cmp(id, 0, piyo, MAX/2 -2) == 0);
864+ assert(id_cmp(id, MAX/2 -2, piyo, 3) == 0);
865+ assert(id_cmp(id, MAX/2 +1, &piyo[MAX/2 -2], 1) == 0);
866+ assert(id_cmp(id, MAX/2 +2, &piyo[MAX-1], 1) == 0);
867+ assert(id_cmp(id, MAX/2 +3, &piyo[MAX/2 -1], 1) == 0);
868+ assert(IntDeque_size(id) == MAX/2 + 4);
869+ /* erase */
870+ /* 0個指定 */
871+ IntDeque_erase(id, IntDeque_size(id) - 2, 0);
872+ assert(id_cmp(id, 0, piyo, MAX/2 -2) == 0);
873+ assert(id_cmp(id, MAX/2 -2, piyo, 3) == 0);
874+ assert(id_cmp(id, MAX/2 +1, &piyo[MAX/2 -2], 1) == 0);
875+ assert(id_cmp(id, MAX/2 +2, &piyo[MAX-1], 1) == 0);
876+ assert(id_cmp(id, MAX/2 +3, &piyo[MAX/2 -1], 1) == 0);
877+ assert(IntDeque_size(id) == MAX/2 + 4);
878+ /* 1個指定 */
879+ IntDeque_erase(id, IntDeque_size(id) - 2, 1);
880+ assert(id_cmp(id, 0, piyo, MAX/2 -2) == 0);
881+ assert(id_cmp(id, MAX/2 -2, piyo, 3) == 0);
882+ assert(id_cmp(id, MAX/2 +1, &piyo[MAX/2 -2], 2) == 0);
883+ assert(IntDeque_size(id) == MAX/2 + 3);
884+ /* 後ろ寄り */
885+ IntDeque_erase(id, IntDeque_size(id) - 5, 3);
886+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
887+ assert(IntDeque_size(id) == MAX/2);
888+
889+ /* MAX以上で失敗 */
890+ assert(!IntDeque_insert_array(id, 0, piyo, MAX - MAX/2 +1));
891+ assert(id_cmp(id, 0, piyo, MAX/2) == 0);
892+ assert(IntDeque_size(id) == MAX/2);
893+ /* MAXになるまで */
894+ assert(IntDeque_insert_array(id, 0, piyo, MAX - MAX/2));
895+ assert(IntDeque_size(id) == MAX);
896+ assert(IntDeque_full(id));
897+ /* 先頭から全削除 */
898+ IntDeque_erase(id, 0, IntDeque_size(id));
899+ assert(IntDeque_size(id) == 0);
900+ }
901+
902+ IntDeque_delete(id);
903+}
904+
905+
906+
907+
908+
909+
910+void DequeTest_run(void)
911+{
912+ printf("\n===== deque test =====\n");
913+ deque_init_hoge();
914+ deque_init_piyo();
915+
916+ DequeTest_test_1_1();
917+ DequeTest_test_1_2();
918+ DequeTest_test_1_3();
919+ DequeTest_test_1_4();
920+ DequeTest_test_1_5();
921+ DequeTest_test_1_6();
922+
923+ DequeTest_test_2_1();
924+ DequeTest_test_2_2();
925+ DequeTest_test_2_3();
926+ DequeTest_test_2_4();
927+ DequeTest_test_2_5();
928+}
929+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/list_test.c (nonexistent)
+++ tags/rel-0.2.1/test/list_test.c (revision 43)
@@ -0,0 +1,430 @@
1+#include "test.h"
2+
3+
4+LIST_IMPLEMENT(UCharList, unsigned char)
5+LIST_IMPLEMENT(IntList, int)
6+LIST_IMPLEMENT(StrList, char *)
7+LIST_IMPLEMENT(HogeList, Hoge)
8+
9+
10+static HogeList *hl;
11+
12+#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
13+
14+static Hoge hogetab[] = {
15+ {"000", 0},
16+ {"001", 1},
17+ {"002", 2},
18+ {"003", 3},
19+ {"004", 4},
20+ {"005", 5},
21+ {"006", 6},
22+ {"007", 7},
23+ {"008", 8},
24+ {"009", 9},
25+ {"010", 10},
26+ {"011", 11},
27+ {"012", 12},
28+ {"013", 13},
29+ {"014", 14},
30+ {"015", 15},
31+ {"016", 16},
32+ {"017", 17},
33+ {"018", 18},
34+};
35+
36+
37+
38+void ListTest_test_1_1(void)
39+{
40+ Hoge hoge;
41+ printf("***** test_1_1 *****\n");
42+ /* 初期状態 */
43+ assert(HogeList_size(hl) == 0);
44+ assert(HogeList_empty(hl));
45+ assert(HogeList_begin(hl) == HogeList_end(hl));
46+ assert(HogeList_rbegin(hl) == HogeList_rend(hl));
47+ /* clear */
48+ hoge = hogetab[0];
49+ assert(HogeList_push_back(hl, hoge));
50+ assert(HogeList_push_back(hl, hoge));
51+ assert(HogeList_push_back(hl, hoge));
52+ HogeList_clear(hl);
53+ assert(HogeList_size(hl) == 0);
54+ assert(HogeList_empty(hl));
55+}
56+
57+void ListTest_test_1_2(void)
58+{
59+ Hoge hoge;
60+ int i;
61+ printf("***** test_1_2 *****\n");
62+ /* push_back */
63+ for (i = 0; i < NELEMS(hogetab); i++) {
64+ assert(HogeList_push_back(hl, hogetab[i]));
65+ }
66+ assert(HogeList_size(hl) == NELEMS(hogetab));
67+ assert(!HogeList_empty(hl));
68+ /* pop_front */
69+ for (i = 0; i < NELEMS(hogetab); i++) {
70+ /* front */
71+ hoge = HogeList_front(hl);
72+ assert(!strcmp(hoge.key, hogetab[i].key));
73+ assert(hoge.value == hogetab[i].value);
74+ /* back */
75+ hoge = HogeList_back(hl);
76+ assert(!strcmp(hoge.key, hogetab[NELEMS(hogetab)-1].key));
77+ assert(hoge.value == hogetab[NELEMS(hogetab)-1].value);
78+ /* pop_front */
79+ hoge = HogeList_pop_front(hl);
80+ assert(!strcmp(hoge.key, hogetab[i].key));
81+ assert(hoge.value == hogetab[i].value);
82+ }
83+ assert(HogeList_size(hl) == 0);
84+ assert(HogeList_empty(hl));
85+ /* push_front */
86+ for (i = 0; i < NELEMS(hogetab); i++) {
87+ assert(HogeList_push_front(hl, hogetab[i]));
88+ }
89+ assert(HogeList_size(hl) == NELEMS(hogetab));
90+ assert(!HogeList_empty(hl));
91+ /* pop_back */
92+ for (i = 0; i < NELEMS(hogetab); i++) {
93+ /* front */
94+ hoge = HogeList_front(hl);
95+ assert(!strcmp(hoge.key, hogetab[NELEMS(hogetab)-1].key));
96+ assert(hoge.value == hogetab[NELEMS(hogetab)-1].value);
97+ /* back */
98+ hoge = HogeList_back(hl);
99+ assert(!strcmp(hoge.key, hogetab[i].key));
100+ assert(hoge.value == hogetab[i].value);
101+ /* pop_back */
102+ hoge = HogeList_pop_back(hl);
103+ assert(!strcmp(hoge.key, hogetab[i].key));
104+ assert(hoge.value == hogetab[i].value);
105+ }
106+ assert(HogeList_size(hl) == 0);
107+ assert(HogeList_empty(hl));
108+}
109+
110+void ListTest_test_1_3(void)
111+{
112+ Hoge hoge;
113+ int c = 0;
114+ printf("***** test_1_3 *****\n");
115+ /* 大量にpush_back */
116+ hoge = hogetab[0];
117+ while (c < 1000000 && HogeList_push_back(hl, hoge)) {
118+ c++;
119+ }
120+/* printf("size: %d, c: %d\n", HogeList_size(hl), c);*/
121+ assert(HogeList_size(hl) == c);
122+ assert(!HogeList_empty(hl));
123+ /* 大量にpush_front */
124+ HogeList_clear(hl);
125+ assert(HogeList_size(hl) == 0);
126+ assert(HogeList_empty(hl));
127+ c = 0;
128+ while (c < 1000000 && HogeList_push_front(hl, hoge)) {
129+ c++;
130+ }
131+/* printf("size: %d, c: %d\n", HogeList_size(hl), c);*/
132+ assert(HogeList_size(hl) == c);
133+ assert(!HogeList_empty(hl));
134+ HogeList_clear(hl);
135+ assert(HogeList_size(hl) == 0);
136+ assert(HogeList_empty(hl));
137+}
138+
139+void ListTest_test_1_4(void)
140+{
141+ Hoge hoge;
142+ int i;
143+ HogeListIterator pos;
144+ printf("***** test_1_4 *****\n");
145+ /* push_back */
146+ for (i = 0; i < NELEMS(hogetab); i++) {
147+ assert(HogeList_push_back(hl, hogetab[i]));
148+ }
149+ assert(HogeList_size(hl) == NELEMS(hogetab));
150+ assert(!HogeList_empty(hl));
151+ /* at */
152+ for (i = 0, pos = HogeList_begin(hl); pos != HogeList_end(hl); pos = HogeList_next(pos), i++) {
153+ hoge = *HogeList_at(pos);
154+ assert(!strcmp(hoge.key, hogetab[i].key));
155+ assert(hoge.value == hogetab[i].value);
156+ }
157+ assert(i == NELEMS(hogetab));
158+ for (i = 0, pos = HogeList_begin(hl); pos != HogeList_end(hl); pos = HogeList_next(pos), i++) {
159+ *HogeList_at(pos) = hogetab[0];
160+ }
161+ assert(i == NELEMS(hogetab));
162+ for (pos = HogeList_begin(hl); pos != HogeList_end(hl); pos = HogeList_next(pos)) {
163+ hoge = *HogeList_at(pos);
164+ assert(!strcmp(hoge.key, hogetab[0].key));
165+ assert(hoge.value == hogetab[0].value);
166+ }
167+ while (!HogeList_empty(hl)) {
168+ HogeList_pop_front(hl);
169+ }
170+ assert(HogeList_size(hl) == 0);
171+}
172+
173+void ListTest_test_1_5(void)
174+{
175+ Hoge hoge;
176+ int i;
177+ int flag = 0;
178+ HogeListIterator pos;
179+ printf("***** test_1_5 *****\n");
180+ /* beginの位置にinsert */
181+ for (i = 0; i < NELEMS(hogetab); i++) {
182+ assert(HogeList_insert(hl, HogeList_begin(hl), hogetab[i]) == HogeList_begin(hl));
183+ }
184+ HogeList_clear(hl);
185+ /* endの位置にinsert */
186+ for (i = 0; i < NELEMS(hogetab); i++) {
187+ assert(HogeList_insert(hl, HogeList_end(hl), hogetab[i]) == HogeList_rbegin(hl));
188+ }
189+ assert(HogeList_size(hl) == NELEMS(hogetab));
190+ assert(!HogeList_empty(hl));
191+ /* 真ん中の位置にinsert */
192+ for (i = 0, pos = HogeList_begin(hl); pos != HogeList_end(hl); pos = HogeList_next(pos), i++) {
193+ hoge = *HogeList_at(pos);
194+ assert(!strcmp(hoge.key, hogetab[i].key));
195+ assert(hoge.value == hogetab[i].value);
196+ if (i == NELEMS(hogetab)/2) {
197+ hoge = hogetab[0];
198+ assert(HogeList_insert(hl, pos, hoge) == HogeList_prev(pos));
199+ break;
200+ }
201+ }
202+ assert(HogeList_size(hl) == NELEMS(hogetab)+1);
203+ for (i = 0, pos = HogeList_begin(hl); pos != HogeList_end(hl); pos = HogeList_next(pos), i++) {
204+ hoge = *HogeList_at(pos);
205+ if (!flag && i == NELEMS(hogetab)/2) {
206+ assert(!strcmp(hoge.key, hogetab[0].key));
207+ assert(hoge.value == hogetab[0].value);
208+ flag = 1;
209+ i--;
210+ } else {
211+ assert(!strcmp(hoge.key, hogetab[i].key));
212+ assert(hoge.value == hogetab[i].value);
213+ }
214+ }
215+ HogeList_clear(hl);
216+}
217+
218+void ListTest_test_1_6(void)
219+{
220+ Hoge hoge;
221+ int i;
222+ HogeListIterator pos;
223+ size_t size;
224+ printf("***** test_1_6 *****\n");
225+ /* push_back */
226+ for (i = 0; i < NELEMS(hogetab); i++) {
227+ assert(HogeList_push_back(hl, hogetab[i]));
228+ }
229+ assert(HogeList_size(hl) == NELEMS(hogetab));
230+ assert(!HogeList_empty(hl));
231+ /* beginの位置の要素をerase */
232+ size = HogeList_size(hl);
233+ for (i = 0; i < NELEMS(hogetab); i++) {
234+ pos = HogeList_begin(hl);
235+ assert(HogeList_next(pos) == HogeList_erase(hl, pos));
236+ assert(HogeList_size(hl) == --size);
237+ }
238+ assert(HogeList_size(hl) == 0);
239+ assert(HogeList_empty(hl));
240+ /* push_back */
241+ for (i = 0; i < NELEMS(hogetab); i++) {
242+ assert(HogeList_push_back(hl, hogetab[i]));
243+ }
244+ assert(HogeList_size(hl) == NELEMS(hogetab));
245+ assert(!HogeList_empty(hl));
246+ /* endの前の位置の要素をerase */
247+ size = HogeList_size(hl);
248+ for (i = 0; i < NELEMS(hogetab); i++) {
249+ pos = HogeList_rbegin(hl);
250+ assert(HogeList_next(pos) == HogeList_erase(hl, pos));
251+ assert(HogeList_size(hl) == --size);
252+ }
253+ assert(HogeList_size(hl) == 0);
254+ assert(HogeList_empty(hl));
255+ /* push_back */
256+ for (i = 0; i < NELEMS(hogetab); i++) {
257+ assert(HogeList_push_back(hl, hogetab[i]));
258+ }
259+ assert(HogeList_size(hl) == NELEMS(hogetab));
260+ assert(!HogeList_empty(hl));
261+ /* 真ん中の位置の要素をerase */
262+ for (i = 0, pos = HogeList_begin(hl); pos != HogeList_end(hl); i++) {
263+ if (i == NELEMS(hogetab)/2) {
264+ pos = HogeList_erase(hl, pos);
265+ } else {
266+ pos = HogeList_next(pos);
267+ }
268+ }
269+ assert(HogeList_size(hl) == NELEMS(hogetab)-1);
270+ for (i = 0, pos = HogeList_begin(hl); pos != HogeList_end(hl); pos = HogeList_next(pos), i++) {
271+ hoge = *HogeList_at(pos);
272+ if (i >= NELEMS(hogetab)/2) {
273+ assert(!strcmp(hoge.key, hogetab[i+1].key));
274+ assert(hoge.value == hogetab[i+1].value);
275+ } else {
276+ assert(!strcmp(hoge.key, hogetab[i].key));
277+ assert(hoge.value == hogetab[i].value);
278+ }
279+ }
280+ /* erase_range */
281+ assert(!HogeList_empty(hl));
282+ assert(HogeList_end(hl) == HogeList_erase_range(hl, HogeList_begin(hl), HogeList_end(hl)));
283+ assert(HogeList_empty(hl));
284+}
285+
286+void ListTest_test_1_7(void)
287+{
288+ Hoge hoge;
289+ int i;
290+ HogeListIterator pos;
291+ printf("***** test_1_7 *****\n");
292+ /* push_back */
293+ for (i = 0; i < NELEMS(hogetab); i++) {
294+ assert(HogeList_push_back(hl, hogetab[i]));
295+ }
296+ assert(HogeList_size(hl) == NELEMS(hogetab));
297+ assert(!HogeList_empty(hl));
298+ /* rbegin/rend 逆順に走査 */
299+ for (i = 0, pos = HogeList_rbegin(hl); pos != HogeList_rend(hl); pos = HogeList_prev(pos), i++) {
300+ hoge = *HogeList_at(pos);
301+ assert(!strcmp(hoge.key, hogetab[NELEMS(hogetab)-1 - i].key));
302+ assert(hoge.value == hogetab[NELEMS(hogetab)-1 - i].value);
303+ }
304+ assert(i == NELEMS(hogetab));
305+ assert(HogeList_next(HogeList_rbegin(hl)) == HogeList_end(hl));
306+ assert(HogeList_prev(HogeList_begin(hl)) == HogeList_rend(hl));
307+ HogeList_clear(hl);
308+}
309+
310+
311+void ListTest_test_2_1(void)
312+{
313+ IntList *il = IntList_new();
314+ IntList *x;
315+ IntListIterator pos;
316+ IntListIterator pos2;
317+ int buf[32];
318+ int i;
319+ int b[] = {1, 2, 4, 43, 2, 54, 1, 0, 2, 24};
320+ printf("***** test_2_1 *****\n");
321+ for (i = 0; i < 32; i++) buf[i] = i;
322+ /* insert_array */
323+ assert(IntList_size(il) == 0);
324+ IntList_clear(il);
325+ assert(IntList_insert_array(il, IntList_begin(il), buf, 32));
326+ assert(IntList_size(il) == 32);
327+ for (pos = IntList_begin(il), i = 0; pos != IntList_end(il); pos = IntList_next(pos), i++) {
328+ assert(*IntList_at(pos) == i);
329+ }
330+ /* resize */
331+ assert(IntList_resize(il, 64, 100));
332+ assert(IntList_size(il) == 64);
333+ for (pos = IntList_begin(il), i = 0; pos != IntList_end(il); pos = IntList_next(pos), i++) {
334+ if (i < 32) {
335+ assert(*IntList_at(pos) == i);
336+ } else {
337+ assert(*IntList_at(pos) == 100);
338+ }
339+ }
340+ assert(IntList_resize(il, 16, 99));
341+ assert(IntList_size(il) == 16);
342+ for (pos = IntList_begin(il), i = 0; pos != IntList_end(il); pos = IntList_next(pos), i++) {
343+ assert(*IntList_at(pos) == i);
344+ }
345+ assert(IntList_resize(il, 0, 99));
346+ assert(IntList_size(il) == 0);
347+ assert(IntList_resize(il, 100, 99));
348+ /* assign */
349+ x = IntList_new();
350+ assert(IntList_assign(x, IntList_begin(il), IntList_end(il)));
351+ assert(IntList_size(x) == IntList_size(il));
352+ pos2 = IntList_begin(x);
353+ for (pos = IntList_begin(il); pos != IntList_end(il); pos = IntList_next(pos)) {
354+ assert(*IntList_at(pos) == *IntList_at(pos2));
355+ pos2 = IntList_next(pos2);
356+ }
357+ /* swap */
358+ IntList_clear(il);
359+ assert(IntList_insert_array(il, IntList_begin(il), buf, 32));
360+ IntList_clear(x);
361+ assert(IntList_insert_array(x, IntList_begin(x), b, sizeof b / sizeof b[0]));
362+ assert(IntList_size(il) == 32);
363+ for (pos = IntList_begin(il), i = 0; pos != IntList_end(il); pos = IntList_next(pos), i++) {
364+ assert(*IntList_at(pos) == i);
365+ }
366+ assert(IntList_size(x) == sizeof b / sizeof b[0]);
367+ for (pos = IntList_begin(x), i = 0; pos != IntList_end(x); pos = IntList_next(pos), i++) {
368+ assert(*IntList_at(pos) == b[i]);
369+ }
370+ IntList_swap(il, x);
371+ assert(IntList_size(x) == 32);
372+ printf("x:\n");
373+ for (pos = IntList_begin(x), i = 0; pos != IntList_end(x); pos = IntList_next(pos), i++) {
374+ assert(*IntList_at(pos) == i);
375+ printf("%2d, ", *IntList_at(pos));
376+ }
377+ printf("\n");
378+ printf("il:\n");
379+ assert(IntList_size(il) == sizeof b / sizeof b[0]);
380+ for (pos = IntList_begin(il), i = 0; pos != IntList_end(il); pos = IntList_next(pos), i++) {
381+ assert(*IntList_at(pos) == b[i]);
382+ printf("%2d, ", *IntList_at(pos));
383+ }
384+ printf("\n");
385+ /* splice */
386+ IntList_splice(x, IntList_begin(x), il, IntList_begin(il), IntList_end(il));
387+/* IntList_splice(x, IntList_begin(x), il, IntList_begin(il), IntList_begin(il));*/
388+/* IntList_splice(x, IntList_begin(x), il, IntList_begin(il), IntList_next(IntList_begin(il)));*/
389+/* IntList_splice(x, IntList_begin(x), x, IntList_next(IntList_begin(x)), IntList_end(x));*/
390+/* IntList_splice(x, IntList_end(x), x, IntList_next(IntList_begin(x)), IntList_end(x));*/
391+/* IntList_splice(x, IntList_rbegin(x), x, IntList_next(IntList_begin(x)), IntList_prev(IntList_rbegin(x)));*/
392+
393+ assert(IntList_size(il) == 0);
394+ assert(IntList_size(x) == 32 + sizeof b / sizeof b[0]);
395+ printf("x:\n");
396+ for (pos = IntList_begin(x), i = 0; pos != IntList_end(x); pos = IntList_next(pos), i++) {
397+ printf("%2d, ", *IntList_at(pos));
398+ }
399+ printf("\n");
400+ printf("il:\n");
401+ for (pos = IntList_begin(il), i = 0; pos != IntList_end(il); pos = IntList_next(pos), i++) {
402+ printf("%2d, ", *IntList_at(pos));
403+ }
404+ printf("\n");
405+
406+ IntList_delete(il);
407+ IntList_delete(x);
408+}
409+
410+
411+
412+void ListTest_run(void)
413+{
414+ printf("\n===== list test =====\n");
415+ hl = HogeList_new();
416+ assert(hl);
417+
418+ ListTest_test_1_1();
419+ ListTest_test_1_2();
420+ ListTest_test_1_3();
421+ ListTest_test_1_4();
422+ ListTest_test_1_5();
423+ ListTest_test_1_6();
424+ ListTest_test_1_7();
425+ ListTest_test_2_1();
426+
427+ HogeList_delete(hl);
428+}
429+
430+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/string_test.cpp (nonexistent)
+++ tags/rel-0.2.1/test/string_test.cpp (revision 43)
@@ -0,0 +1,526 @@
1+#include "../cstl/string.h"
2+#include <assert.h>
3+#include <stdio.h>
4+#include <string.h>
5+#include <string>
6+
7+#include "heap.h"
8+#ifdef MY_MALLOC
9+extern Heap heap;
10+#define malloc(s) Heap_alloc(&heap, s)
11+#define realloc(p, s) Heap_realloc(&heap, p, s)
12+#define free(p) Heap_free(&heap, p)
13+#endif
14+
15+STRING_INTERFACE(String, char)
16+STRING_IMPLEMENT(String, char)
17+
18+#define SIZE 16
19+
20+using namespace std;
21+
22+
23+void StringTest_test_1_1(void)
24+{
25+ String *x;
26+ string s;
27+ size_t i;
28+ printf("***** test_1_1 *****\n");
29+ /* 初期状態 */
30+ x = String_new(SIZE);
31+ assert(String_empty(x));
32+ assert(String_size(x) == 0);
33+ assert(String_capacity(x) == SIZE);
34+ /* c_str */
35+ String_assign(x, "abcdefghijklmn", 7);
36+// printf("%s\n", String_c_str(x));
37+ s = String_c_str(x);
38+ assert(strcmp(s.c_str(), String_c_str(x)) == 0);
39+ /* at */
40+ for (i = 0; i < String_length(x); i++) {
41+ assert(s.at(i) == *String_at(x, i));
42+ }
43+ /* clear */
44+ String_clear(x);
45+ assert(String_empty(x));
46+ assert(String_size(x) == 0);
47+
48+ String_delete(x);
49+}
50+
51+void StringTest_test_1_2(void)
52+{
53+ String *x;
54+ String *y;
55+ printf("***** test_1_2 *****\n");
56+ x = String_new(SIZE);
57+ /* capacity */
58+ /* reserve */
59+ String_reserve(x, 100);
60+ assert(String_size(x) == 0);
61+ assert(String_capacity(x) == 100);
62+ String_reserve(x, 99);
63+ assert(String_capacity(x) == 100);
64+ String_reserve(x, 101);
65+ assert(String_capacity(x) == 101);
66+ /* shrink */
67+ String_shrink(x, 10);
68+ assert(String_capacity(x) == 10);
69+ String_shrink(x, 11);
70+ assert(String_capacity(x) == 10);
71+ String_shrink(x, 9);
72+ assert(String_capacity(x) == 9);
73+ /* resize */
74+ String_resize(x, 10, 'a');
75+ assert(String_size(x) == 10);
76+ assert(strcmp("aaaaaaaaaa", String_c_str(x)) == 0);
77+ String_resize(x, 15, 'b');
78+ assert(String_size(x) == 15);
79+ assert(strcmp("aaaaaaaaaabbbbb", String_c_str(x)) == 0);
80+ String_resize(x, 5, 'b');
81+ assert(String_size(x) == 5);
82+ assert(strcmp("aaaaa", String_c_str(x)) == 0);
83+ String_resize(x, 0, 'b');
84+ assert(String_size(x) == 0);
85+ assert(strcmp("", String_c_str(x)) == 0);
86+ /* assign */
87+ String_assign(x, "abcdefghijklmnopqrstuvwxyz", NPOS);
88+ assert(String_size(x) == 26);
89+ assert(strcmp("abcdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
90+ /* erase */
91+ String_erase(x, 0, 3);
92+ assert(String_size(x) == 23);
93+ assert(strcmp("defghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
94+ String_erase(x, 0, 0);
95+ assert(String_size(x) == 23);
96+ assert(strcmp("defghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
97+ String_erase(x, 1, 1);
98+ assert(String_size(x) == 22);
99+ assert(strcmp("dfghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
100+ String_erase(x, 5, 10);
101+ assert(String_size(x) == 12);
102+ assert(strcmp("dfghituvwxyz", String_c_str(x)) == 0);
103+ String_erase(x, 11, 1);
104+ assert(String_size(x) == 11);
105+ assert(strcmp("dfghituvwxy", String_c_str(x)) == 0);
106+ String_erase(x, 5, NPOS);
107+ assert(String_size(x) == 5);
108+ assert(strcmp("dfghi", String_c_str(x)) == 0);
109+ String_erase(x, 0, NPOS);
110+ assert(String_size(x) == 0);
111+ assert(strcmp("", String_c_str(x)) == 0);
112+ /* compare */
113+ String_assign(x, "abcdefg", NPOS);
114+ y = String_new(SIZE);
115+ String_assign(y, "abcdefgh", NPOS);
116+ assert(String_compare(x, y) < 0);
117+ String_erase(y, String_size(y)-2, 2);
118+ assert(String_compare(x, y) > 0);
119+ String_erase(x, String_size(x)-1, 1);
120+ assert(String_compare(x, y) == 0);
121+ /* swap */
122+ String_assign(x, "abcdefg", NPOS);
123+ String_assign(y, "abcdefgh", NPOS);
124+ String_shrink(x, 0);
125+ String_shrink(y, 0);
126+ assert(strcmp("abcdefg", String_c_str(x)) == 0);
127+ assert(strcmp("abcdefgh", String_c_str(y)) == 0);
128+ assert(String_size(x) == 7);
129+ assert(String_size(y) == 8);
130+ String_swap(x, y);
131+ assert(strcmp("abcdefg", String_c_str(y)) == 0);
132+ assert(strcmp("abcdefgh", String_c_str(x)) == 0);
133+ assert(String_size(y) == 7);
134+ assert(String_size(x) == 8);
135+
136+ String_delete(x);
137+ String_delete(y);
138+}
139+
140+void StringTest_test_1_3(void)
141+{
142+ String *x;
143+ printf("***** test_1_3 *****\n");
144+ x = String_new(SIZE);
145+ /* assign */
146+ String_assign(x, "abcdefghijklmnopqrstuvwxyz", NPOS);
147+ assert(String_size(x) == 26);
148+ assert(strcmp("abcdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
149+ String_assign(x, String_c_str(x), NPOS);
150+ assert(String_size(x) == 26);
151+ assert(strcmp("abcdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
152+ String_assign(x, "abcdefghijklmnopqrstuvwxyz", 10);
153+ assert(String_size(x) == 10);
154+ assert(strcmp("abcdefghij", String_c_str(x)) == 0);
155+ String_assign(x, "abcdefghijklmnopqrstuvwxyz", 0);
156+ assert(String_size(x) == 0);
157+ assert(strcmp("", String_c_str(x)) == 0);
158+ String_assign(x, "", 0);
159+ assert(String_size(x) == 0);
160+ assert(strcmp("", String_c_str(x)) == 0);
161+ /* assign_c */
162+ String_assign_c(x, 10, 'a');
163+ assert(String_size(x) == 10);
164+ assert(strcmp("aaaaaaaaaa", String_c_str(x)) == 0);
165+ String_assign_c(x, 1, 'b');
166+ assert(String_size(x) == 1);
167+ assert(strcmp("b", String_c_str(x)) == 0);
168+ String_assign_c(x, 0, 'c');
169+ assert(String_size(x) == 0);
170+ assert(strcmp("", String_c_str(x)) == 0);
171+ /* append */
172+ String_append(x, "abcdef", NPOS);
173+ assert(String_size(x) == 6);
174+ assert(strcmp("abcdef", String_c_str(x)) == 0);
175+ String_append(x, "gh", NPOS);
176+ assert(String_size(x) == 8);
177+ assert(strcmp("abcdefgh", String_c_str(x)) == 0);
178+ String_append(x, "ijkl", 3);
179+ assert(String_size(x) == 11);
180+ assert(strcmp("abcdefghijk", String_c_str(x)) == 0);
181+ String_append(x, "l", 1);
182+ assert(String_size(x) == 12);
183+ assert(strcmp("abcdefghijkl", String_c_str(x)) == 0);
184+ String_append(x, "mno", 0);
185+ assert(String_size(x) == 12);
186+ assert(strcmp("abcdefghijkl", String_c_str(x)) == 0);
187+ String_append(x, "m", NPOS);
188+ assert(String_size(x) == 13);
189+ assert(strcmp("abcdefghijklm", String_c_str(x)) == 0);
190+ String_append(x, String_c_str(x) + 1, NPOS);
191+ assert(String_size(x) == 25);
192+ assert(strcmp("abcdefghijklmbcdefghijklm", String_c_str(x)) == 0);
193+ String_clear(x);
194+ assert(String_size(x) == 0);
195+ /* append_c */
196+ String_append_c(x, 5, 'a');
197+ assert(String_size(x) == 5);
198+ assert(strcmp("aaaaa", String_c_str(x)) == 0);
199+ String_append_c(x, 2, 'b');
200+ assert(String_size(x) == 7);
201+ assert(strcmp("aaaaabb", String_c_str(x)) == 0);
202+ String_append_c(x, 4, 'c');
203+ assert(String_size(x) == 11);
204+ assert(strcmp("aaaaabbcccc", String_c_str(x)) == 0);
205+ String_append_c(x, 0, 'd');
206+ assert(String_size(x) == 11);
207+ assert(strcmp("aaaaabbcccc", String_c_str(x)) == 0);
208+ /* push_back */
209+ String_push_back(x, 'd');
210+ String_push_back(x, 'e');
211+ String_push_back(x, 'f');
212+ assert(String_size(x) == 14);
213+ assert(strcmp("aaaaabbccccdef", String_c_str(x)) == 0);
214+ String_clear(x);
215+ assert(String_size(x) == 0);
216+ /* insert */
217+ String_insert(x, 0, "abcdef", NPOS);
218+ assert(String_size(x) == 6);
219+ assert(strcmp("abcdef", String_c_str(x)) == 0);
220+ String_insert(x, 0, "gh", NPOS);
221+ assert(String_size(x) == 8);
222+ assert(strcmp("ghabcdef", String_c_str(x)) == 0);
223+ String_insert(x, 1, "ijk", NPOS);
224+ assert(String_size(x) == 11);
225+ assert(strcmp("gijkhabcdef", String_c_str(x)) == 0);
226+ String_insert(x, String_size(x), "lmn", NPOS);
227+ assert(String_size(x) == 14);
228+ assert(strcmp("gijkhabcdeflmn", String_c_str(x)) == 0);
229+ String_insert(x, String_size(x)-1, "opqr", 3);
230+ assert(String_size(x) == 17);
231+ assert(strcmp("gijkhabcdeflmopqn", String_c_str(x)) == 0);
232+ String_insert(x, 5, "rst", 0);
233+ assert(String_size(x) == 17);
234+ assert(strcmp("gijkhabcdeflmopqn", String_c_str(x)) == 0);
235+ String_insert(x, 1, String_c_str(x), NPOS);
236+ assert(String_size(x) == 34);
237+ printf("%s\n", String_c_str(x));
238+ assert(strcmp("ggijkhabcdeflmopqnijkhabcdeflmopqn", String_c_str(x)) == 0);
239+ String_clear(x);
240+ assert(String_size(x) == 0);
241+ /* insert_c */
242+ String_insert_c(x, 0, 5, 'a');
243+ assert(String_size(x) == 5);
244+ assert(strcmp("aaaaa", String_c_str(x)) == 0);
245+ String_insert_c(x, 0, 3, 'b');
246+ assert(String_size(x) == 8);
247+ assert(strcmp("bbbaaaaa", String_c_str(x)) == 0);
248+ String_insert_c(x, 1, 2, 'c');
249+ assert(String_size(x) == 10);
250+ assert(strcmp("bccbbaaaaa", String_c_str(x)) == 0);
251+ String_insert_c(x, 2, 1, 'd');
252+ assert(String_size(x) == 11);
253+ assert(strcmp("bcdcbbaaaaa", String_c_str(x)) == 0);
254+ String_insert_c(x, String_size(x), 3, 'e');
255+ assert(String_size(x) == 14);
256+ assert(strcmp("bcdcbbaaaaaeee", String_c_str(x)) == 0);
257+ String_insert_c(x, String_size(x)-1, 2, 'f');
258+ assert(String_size(x) == 16);
259+ assert(strcmp("bcdcbbaaaaaeeffe", String_c_str(x)) == 0);
260+ String_insert_c(x, 5, 0, 'g');
261+ assert(String_size(x) == 16);
262+ assert(strcmp("bcdcbbaaaaaeeffe", String_c_str(x)) == 0);
263+ String_insert_c(x, 0, 17, 'g');
264+ assert(String_size(x) == 33);
265+ assert(strcmp("gggggggggggggggggbcdcbbaaaaaeeffe", String_c_str(x)) == 0);
266+ String_clear(x);
267+ assert(String_size(x) == 0);
268+
269+
270+#if 0
271+ string s;
272+ s.insert(0, "abc", 3);
273+ printf("%s\n", s.c_str());
274+ s.insert(0, "def", 3);
275+ printf("%s\n", s.c_str());
276+ s.erase(0, string::npos);
277+ printf("%s\n", s.c_str());
278+#endif
279+
280+ String_delete(x);
281+}
282+
283+void StringTest_test_1_4(void)
284+{
285+ String *x;
286+ printf("***** test_1_4 *****\n");
287+ x = String_new(SIZE);
288+ String_assign(x, "abcdefghijklmnopqrstuvwxyz", NPOS);
289+ assert(String_size(x) == 26);
290+ assert(strcmp("abcdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
291+ /* replace */
292+ String_replace(x, 0, 3, "ABC", NPOS);
293+ assert(String_size(x) == 26);
294+ assert(strcmp("ABCdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
295+ String_replace(x, 6, 3, "GHI", NPOS);
296+ assert(String_size(x) == 26);
297+ assert(strcmp("ABCdefGHIjklmnopqrstuvwxyz", String_c_str(x)) == 0);
298+ String_replace(x, String_size(x)-3, NPOS, "XYZ", NPOS);
299+ assert(String_size(x) == 26);
300+ assert(strcmp("ABCdefGHIjklmnopqrstuvwXYZ", String_c_str(x)) == 0);
301+ String_shrink(x, 0);
302+ String_replace(x, 3, 0, "DEF", NPOS);
303+ assert(String_size(x) == 29);
304+ assert(strcmp("ABCDEFdefGHIjklmnopqrstuvwXYZ", String_c_str(x)) == 0);
305+ String_replace(x, 6, 3, "", NPOS);
306+ assert(String_size(x) == 26);
307+ assert(strcmp("ABCDEFGHIjklmnopqrstuvwXYZ", String_c_str(x)) == 0);
308+ String_replace(x, 0, 3, "hogehoge", 4);
309+ assert(String_size(x) == 27);
310+ assert(strcmp("hogeDEFGHIjklmnopqrstuvwXYZ", String_c_str(x)) == 0);
311+ String_replace(x, 3, 4, String_c_str(x), 4);
312+ assert(String_size(x) == 27);
313+ printf("%s\n", String_c_str(x));
314+ assert(strcmp("hoghogeGHIjklmnopqrstuvwXYZ", String_c_str(x)) == 0);
315+ String_replace(x, 0, NPOS, "", NPOS);
316+ assert(String_size(x) == 0);
317+ assert(strcmp("", String_c_str(x)) == 0);
318+ String_replace(x, 0, NPOS, "abc", NPOS);
319+ assert(String_size(x) == 3);
320+ assert(strcmp("abc", String_c_str(x)) == 0);
321+ /* replace_c */
322+ String_assign(x, "abcdefghijklmnopqrstuvwxyz", NPOS);
323+ assert(String_size(x) == 26);
324+ assert(strcmp("abcdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
325+ String_replace_c(x, 0, 3, 3, 'A');
326+ assert(String_size(x) == 26);
327+ assert(strcmp("AAAdefghijklmnopqrstuvwxyz", String_c_str(x)) == 0);
328+ String_replace_c(x, 6, 3, 3, 'G');
329+ assert(String_size(x) == 26);
330+ assert(strcmp("AAAdefGGGjklmnopqrstuvwxyz", String_c_str(x)) == 0);
331+ String_replace_c(x, String_size(x)-3, NPOS, 3, 'X');
332+ assert(String_size(x) == 26);
333+ assert(strcmp("AAAdefGGGjklmnopqrstuvwXXX", String_c_str(x)) == 0);
334+ String_shrink(x, 0);
335+ String_replace_c(x, 3, 0, 3, 'D');
336+ assert(String_size(x) == 29);
337+ assert(strcmp("AAADDDdefGGGjklmnopqrstuvwXXX", String_c_str(x)) == 0);
338+ String_replace_c(x, 6, 3, 0, 'E');
339+ assert(String_size(x) == 26);
340+ assert(strcmp("AAADDDGGGjklmnopqrstuvwXXX", String_c_str(x)) == 0);
341+ String_replace_c(x, 0, 3, 4, '0');
342+ assert(String_size(x) == 27);
343+ assert(strcmp("0000DDDGGGjklmnopqrstuvwXXX", String_c_str(x)) == 0);
344+ String_replace_c(x, 0, NPOS, 0, '0');
345+ assert(String_size(x) == 0);
346+ assert(strcmp("", String_c_str(x)) == 0);
347+ String_replace_c(x, 0, NPOS, 10, '0');
348+ assert(String_size(x) == 10);
349+ assert(strcmp("0000000000", String_c_str(x)) == 0);
350+
351+#if 0
352+ string s = "abcdefghijklmnopqrstuvwxyz";
353+ s.replace(1, string::npos, "hoge");
354+ printf("%s\n", s.c_str());
355+#endif
356+
357+ String_delete(x);
358+}
359+
360+void print_find_test(const char *cstr, string &t, size_t i, size_t j, size_t k, size_t idx1, size_t idx2, const char *f)
361+{
362+ if (idx1 != idx2) {
363+ if (k == NPOS) {
364+ t.assign(cstr);
365+ } else {
366+ t.assign(cstr, k);
367+ }
368+ printf("cstr[%s]\ni[%3d], j[%3d], k[%3d], idx1[%3d], idx2[%3d] :%s\n", t.c_str(), i, j, k, idx1, idx2, f);
369+ }
370+}
371+
372+void find_test(String *x, const string &s, size_t i, size_t j, size_t k, char *cstr)
373+{
374+ string t;
375+ size_t idx1, idx2;
376+ /* find */
377+ idx1 = String_find(String_c_str(x), &cstr[j], i, k);
378+ idx2 = (k == NPOS) ? s.find(&cstr[j], i) : s.find(&cstr[j], i, k);
379+ /* STLの実装によって動作が違うことがある */
380+ if (i == NPOS) {
381+ assert(idx1 == NPOS);
382+ } else {
383+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find");
384+ assert(idx1 == idx2);
385+ }
386+ /* find_c */
387+ idx1 = String_find_c(String_c_str(x), cstr[j], i);
388+ idx2 = s.find(cstr[j], i);
389+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_c");
390+ assert(idx1 == idx2);
391+ assert(String_find_c(String_c_str(x), cstr[j], i) == String_find_first_of_c(String_c_str(x), cstr[j], i));
392+
393+ /* rfind */
394+ idx1 = String_rfind(String_c_str(x), &cstr[j], i, k);
395+ idx2 = (k == NPOS) ? s.rfind(&cstr[j], i) : s.rfind(&cstr[j], i, k);
396+ print_find_test(&cstr[j], t, i, j, k, idx2, idx2, "rfind");
397+ assert(idx1 == idx2);
398+ /* rfind_c */
399+ idx1 = String_rfind_c(String_c_str(x), cstr[j], i);
400+ idx2 = s.rfind(cstr[j], i);
401+ print_find_test(&cstr[j], t, i, j, k, idx2, idx2, "rfind_c");
402+ assert(idx1 == idx2);
403+ assert(String_rfind_c(String_c_str(x), cstr[j], i) == String_find_last_of_c(String_c_str(x), cstr[j], i));
404+
405+ /* find_first_of */
406+ idx1 = String_find_first_of(String_c_str(x), &cstr[j], i, k);
407+ idx2 = (k == NPOS) ? s.find_first_of(&cstr[j], i) : s.find_first_of(&cstr[j], i, k);
408+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_first_of");
409+ assert(idx1 == idx2);
410+ /* find_first_of_c */
411+ idx1 = String_find_first_of_c(String_c_str(x), cstr[j], i);
412+ idx2 = s.find_first_of(cstr[j], i);
413+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_first_of_c");
414+ assert(idx1 == idx2);
415+
416+ /* find_last_of */
417+ idx1 = String_find_last_of(String_c_str(x), &cstr[j], i, k);
418+ idx2 = (k == NPOS) ? s.find_last_of(&cstr[j], i) : s.find_last_of(&cstr[j], i, k);
419+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_last_of");
420+ assert(idx1 == idx2);
421+ /* find_last_of_c */
422+ idx1 = String_find_last_of_c(String_c_str(x), cstr[j], i);
423+ idx2 = s.find_last_of(cstr[j], i);
424+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_last_of_c");
425+ assert(idx1 == idx2);
426+
427+ /* find_first_not_of */
428+ idx1 = String_find_first_not_of(String_c_str(x), &cstr[j], i, k);
429+ idx2 = (k == NPOS) ? s.find_first_not_of(&cstr[j], i) : s.find_first_not_of(&cstr[j], i, k);
430+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_first_not_of");
431+ assert(idx1 == idx2);
432+ /* find_first_not_of_c */
433+ idx1 = String_find_first_not_of_c(String_c_str(x), cstr[j], i);
434+ idx2 = s.find_first_not_of(cstr[j], i);
435+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_first_not_of_c");
436+ assert(idx1 == idx2);
437+
438+ /* find_last_not_of */
439+ idx1 = String_find_last_not_of(String_c_str(x), &cstr[j], i, k);
440+ idx2 = (k == NPOS) ? s.find_last_not_of(&cstr[j], i) : s.find_last_not_of(&cstr[j], i, k);
441+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_last_not_of");
442+ assert(idx1 == idx2);
443+ /* find_last_not_of_c */
444+ idx1 = String_find_last_not_of_c(String_c_str(x), cstr[j], i);
445+ idx2 = s.find_last_not_of(cstr[j], i);
446+ print_find_test(&cstr[j], t, i, j, k, idx1, idx2, "find_last_not_of_c");
447+ assert(idx1 == idx2);
448+}
449+
450+void StringTest_test_1_5(void)
451+{
452+ String *x;
453+ String *y;
454+ string s;
455+ size_t i, j, k;
456+ printf("***** test_1_5 *****\n");
457+ x = String_new(SIZE);
458+ y = String_new(SIZE);
459+ String_assign(x, "abc abcd abcde abcdef abcdefg abcdefgh abcdefghi", NPOS);
460+ String_assign(y, "abcdefghijklmnopqrstuvwxyz", NPOS);
461+ s = String_c_str(x);
462+ assert(strcmp(s.c_str(), String_c_str(x)) == 0);
463+ for (i = 0; i < String_size(x); i++) {
464+ for (j = 0; j < String_size(x); j++) {
465+ for (k = 0; k < String_size(x) -j +1; k++) {
466+ find_test(x, s, i, j, k, String_c_str(x));
467+ }
468+ if (k == String_size(x) -j +1) {
469+ find_test(x, s, i, j, NPOS, String_c_str(x));
470+ }
471+ if (j <= String_size(y)) {
472+ for (k = 0; k < String_size(y) -j +1; k++) {
473+ find_test(x, s, i, j, k, String_c_str(y));
474+ }
475+ if (k == String_size(y) -j +1) {
476+ find_test(x, s, i, j, NPOS, String_c_str(y));
477+ }
478+ }
479+ }
480+ }
481+ for (j = 0; j < String_size(x); j++) {
482+ for (k = 0; k < String_size(x) -j +1; k++) {
483+ find_test(x, s, NPOS, j, k, String_c_str(x));
484+ }
485+ if (k == String_size(x) -j +1) {
486+ find_test(x, s, NPOS, j, NPOS, String_c_str(x));
487+ }
488+ if (j <= String_size(y)) {
489+ for (k = 0; k < String_size(y) -j +1; k++) {
490+ find_test(x, s, NPOS, j, k, String_c_str(y));
491+ }
492+ if (k == String_size(y) -j +1) {
493+ find_test(x, s, NPOS, j, NPOS, String_c_str(y));
494+ }
495+ }
496+ }
497+
498+ String_delete(x);
499+ String_delete(y);
500+}
501+
502+
503+void StringTest_run(void)
504+{
505+ printf("\n===== string test =====\n");
506+ StringTest_test_1_1();
507+ StringTest_test_1_2();
508+ StringTest_test_1_3();
509+ StringTest_test_1_4();
510+ StringTest_test_1_5();
511+}
512+
513+double buf[1024*1024/sizeof(double)];
514+Heap heap;
515+
516+int main(void)
517+{
518+#ifdef MY_MALLOC
519+ Heap_init(&heap, buf, sizeof buf);
520+#endif
521+ StringTest_run();
522+#ifdef MY_MALLOC
523+ DUMP_MEMORY_LEAK(&heap, 0);
524+#endif
525+ return 0;
526+}
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/set_test.c (nonexistent)
+++ tags/rel-0.2.1/test/set_test.c (revision 43)
@@ -0,0 +1,941 @@
1+#include "test.h"
2+
3+#include <string.h>
4+#include <time.h>
5+
6+/* int */
7+SET_IMPLEMENT(IntSetA, int, SIMPLE_CMP, ASC)
8+SET_DEBUG_IMPLEMENT(IntSetA, int, SIMPLE_CMP, ASC, %d, VISUAL)
9+static IntSetA *ia;
10+
11+SET_IMPLEMENT(IntSetD, int, SIMPLE_CMP, DESC)
12+SET_DEBUG_IMPLEMENT(IntSetD, int, SIMPLE_CMP, DESC, %d, VISUAL)
13+static IntSetD *id;
14+
15+MULTISET_IMPLEMENT(IntMSetA, int, SIMPLE_CMP, ASC)
16+SET_DEBUG_IMPLEMENT(IntMSetA, int, SIMPLE_CMP, ASC, %d, VISUAL)
17+static IntMSetA *ima;
18+
19+
20+/* double */
21+SET_IMPLEMENT(DoubleSetA, double, SIMPLE_CMP, ASC)
22+SET_DEBUG_IMPLEMENT(DoubleSetA, double, SIMPLE_CMP, ASC, %g, VISUAL)
23+static DoubleSetA *da;
24+
25+
26+/* ptr */
27+SET_IMPLEMENT(PtrSetA, int*, SIMPLE_CMP, ASC)
28+SET_DEBUG_IMPLEMENT(PtrSetA, int*, SIMPLE_CMP, ASC, %p, VISUAL)
29+static PtrSetA *pa;
30+
31+
32+/* str */
33+SET_IMPLEMENT(StrSetA, char*, strcmp, ASC)
34+SET_DEBUG_IMPLEMENT(StrSetA, char*, strcmp, ASC, %s, VISUAL)
35+static StrSetA *sa;
36+
37+/* unsigned int */
38+SET_IMPLEMENT(UIntSetA, unsigned int, SIMPLE_CMP, ASC)
39+SET_DEBUG_IMPLEMENT(UIntSetA, unsigned int, SIMPLE_CMP, ASC, %d, VISUAL)
40+static UIntSetA *uia;
41+
42+
43+
44+#define SIZE 32
45+static int hoge_int[SIZE];
46+static double hoge_double[SIZE];
47+static int *hoge_ptr[SIZE];
48+static char *hoge_str[SIZE];
49+static char str[SIZE][16];
50+
51+void set_init_hoge(void)
52+{
53+ int i;
54+ int tmp;
55+ IntDeque *q = IntDeque_new(SIZE/2);
56+ srand(time(0));
57+/* printf("hoge data\n");*/
58+ for (i = 0; i < SIZE/2; i++) {
59+ IntDeque_push_back(q, i);
60+ }
61+ for (i = 0; i < SIZE; i++) {
62+ if (!IntDeque_empty(q)) {
63+ int rnd = rand() % IntDeque_size(q);
64+ tmp = *IntDeque_at(q, rnd);
65+ IntDeque_erase(q, rnd, 1);
66+ } else {
67+ tmp = i/2;
68+ }
69+ hoge_int[i] = tmp;
70+ hoge_double[i] = hoge_int[i] * 0.5;
71+ hoge_ptr[i] = &hoge_int[tmp];
72+ sprintf(str[i], "%05d", i < SIZE/2 ? i : tmp);
73+ hoge_str[i] = str[i];
74+/* printf("%4d: int[%3d], double[%5g], ptr[%p], str[%s]\n",*/
75+/* i, hoge_int[i], hoge_double[i], hoge_ptr[i], hoge_str[i]);*/
76+ }
77+ IntDeque_delete(q);
78+}
79+
80+void SetTest_test_1_1(void)
81+{
82+ int i;
83+ size_t count = 0;
84+ int success[SIZE];
85+ IntSetAIterator pos[SIZE];
86+ IntSetAIterator p;
87+ IntSetA *x;
88+ int b[] = {100, 101, 102, 103, 104, 105, 106, 107, 108, 109};
89+ printf("***** test_1_1 *****\n");
90+ ia = IntSetA_new();
91+ /* 初期状態 */
92+ assert(IntSetA_empty(ia));
93+ assert(IntSetA_size(ia) == 0);
94+ assert(IntSetA_begin(ia) == IntSetA_end(ia));
95+ assert(IntSetA_rbegin(ia) == IntSetA_rend(ia));
96+ /* insert */
97+ for (i = 0; i < SIZE; i++) {
98+ pos[i] = IntSetA_insert(ia, hoge_int[i], &success[i]);
99+ assert(pos[i]);
100+ if (i < SIZE/2) {
101+ assert(success[i]);
102+ count++;
103+ } else {
104+ assert(!success[i]);
105+ }
106+ }
107+/* IntSetA_print(ia);*/
108+ assert(!IntSetA_empty(ia));
109+ assert(IntSetA_size(ia) == count);
110+ assert(count == SIZE/2);
111+ /* count, find, lower_bound, upper_bound */
112+ for (i = 0; i < SIZE/2; i++) {
113+ assert(IntSetA_count(ia, hoge_int[i]) == 1);
114+ assert(pos[i] == IntSetA_find(ia, hoge_int[i]));
115+ assert(pos[i] == IntSetA_lower_bound(ia, hoge_int[i]));
116+ assert(pos[i] == IntSetA_upper_bound(ia, hoge_int[i]-1));
117+ assert(IntSetA_lower_bound(ia, hoge_int[i]+1) == IntSetA_upper_bound(ia, hoge_int[i]));
118+ }
119+ assert(IntSetA_find(ia, IntSetA_key(IntSetA_begin(ia)) -1) == IntSetA_end(ia));
120+ assert(IntSetA_lower_bound(ia, IntSetA_key(IntSetA_rbegin(ia)) +1) == IntSetA_end(ia));
121+ assert(IntSetA_upper_bound(ia, IntSetA_key(IntSetA_rbegin(ia))) == IntSetA_end(ia));
122+ /* begin, end, next, key */
123+ for (p = IntSetA_begin(ia), i = 0; p != IntSetA_end(ia); p = IntSetA_next(p), i++) {
124+ assert(IntSetA_key(p) == i);
125+ }
126+ assert(i == SIZE/2);
127+ assert(IntSetA_next(IntSetA_rbegin(ia)) == IntSetA_end(ia));
128+ /* rbegin, rend, prev, key */
129+ for (p = IntSetA_rbegin(ia), i = SIZE/2 -1; p != IntSetA_rend(ia); p = IntSetA_prev(p), i--) {
130+ assert(IntSetA_key(p) == i);
131+ }
132+ assert(i == -1);
133+ assert(IntSetA_prev(IntSetA_begin(ia)) == IntSetA_rend(ia));
134+ /* erase */
135+ for (i = 0; i < SIZE; i++) {
136+ if (pos[i] && success[i]) {
137+ IntSetAIterator itr = IntSetA_next(pos[i]);
138+ assert(itr == IntSetA_erase(ia, pos[i]));
139+ count--;
140+ }
141+ }
142+ assert(IntSetA_empty(ia));
143+ assert(IntSetA_size(ia) == 0);
144+ assert(count == 0);
145+ /* erase_range */
146+ for (i = 0; i < SIZE/2; i++) {
147+ pos[i] = IntSetA_insert(ia, hoge_int[i], NULL);
148+ assert(pos[i]);
149+ }
150+ assert(IntSetA_size(ia) == SIZE/2);
151+ assert(IntSetA_find(ia, SIZE/2 -2) == IntSetA_erase_range(ia, IntSetA_find(ia, 2), IntSetA_find(ia, SIZE/2 -2)));
152+ assert(IntSetA_size(ia) == 4);
153+ assert(IntSetA_end(ia) == IntSetA_erase_range(ia, IntSetA_begin(ia), IntSetA_end(ia)));
154+ assert(IntSetA_size(ia) == 0);
155+ assert(IntSetA_insert(ia, hoge_int[0], NULL));
156+ assert(IntSetA_size(ia) == 1);
157+ assert(IntSetA_next(IntSetA_begin(ia)) == IntSetA_erase_range(ia, IntSetA_begin(ia), IntSetA_next(IntSetA_begin(ia))));
158+ assert(IntSetA_size(ia) == 0);
159+ assert(IntSetA_insert(ia, 100, NULL));
160+ assert(IntSetA_insert(ia, 110, NULL));
161+ assert(IntSetA_size(ia) == 2);
162+ assert(IntSetA_upper_bound(ia, 110) == IntSetA_erase_range(ia, IntSetA_lower_bound(ia, 100), IntSetA_upper_bound(ia, 110)));
163+ assert(IntSetA_size(ia) == 0);
164+ /* erase_key */
165+ for (i = 0; i < SIZE/2; i++) {
166+ pos[i] = IntSetA_insert(ia, hoge_int[i], NULL);
167+ assert(pos[i]);
168+ }
169+ assert(IntSetA_size(ia) == SIZE/2);
170+ for (i = 0; i < SIZE/2; i++) {
171+ assert(IntSetA_erase_key(ia, hoge_int[i]) == 1);
172+ }
173+ assert(IntSetA_size(ia) == 0);
174+ /* 大量にinsert */
175+ count = 0;
176+ while (count < 1000000 && IntSetA_insert(ia, count, NULL)) {
177+ count++;
178+ }
179+ assert(IntSetA_size(ia) == count);
180+ printf("count: %d\n", count);
181+ printf("size: %d\n", IntSetA_size(ia));
182+ /* clear */
183+ IntSetA_clear(ia);
184+ printf("size: %d\n", IntSetA_size(ia));
185+ assert(IntSetA_size(ia) == 0);
186+ assert(IntSetA_insert(ia, 100, NULL));
187+ assert(IntSetA_size(ia) == 1);
188+ IntSetA_clear(ia);
189+ assert(IntSetA_size(ia) == 0);
190+ IntSetA_clear(ia);
191+ assert(IntSetA_size(ia) == 0);
192+ /* swap */
193+ for (i = 0; i < SIZE/2; i++) {
194+ pos[i] = IntSetA_insert(ia, hoge_int[i], &success[i]);
195+ assert(pos[i]);
196+ assert(success[i]);
197+ }
198+ assert(IntSetA_size(ia) == SIZE/2);
199+ x = IntSetA_new();
200+ for (i = 0; i < sizeof b / sizeof b[0]; i++) {
201+ pos[i] = IntSetA_insert(x, b[i], &success[i]);
202+ assert(pos[i]);
203+ assert(success[i]);
204+ }
205+ assert(IntSetA_size(x) == sizeof b / sizeof b[0]);
206+/* IntSetA_print(ia);*/
207+/* IntSetA_print(x);*/
208+
209+ IntSetA_swap(ia, x);
210+
211+/* IntSetA_print(ia);*/
212+/* IntSetA_print(x);*/
213+ assert(IntSetA_size(x) == SIZE/2);
214+ assert(IntSetA_size(ia) == sizeof b / sizeof b[0]);
215+ /* assign */
216+ assert(IntSetA_assign(x, IntSetA_begin(ia), IntSetA_end(ia)));
217+ assert(IntSetA_size(ia) == sizeof b / sizeof b[0]);
218+ assert(IntSetA_size(x) == sizeof b / sizeof b[0]);
219+ IntSetA_print(ia);
220+ IntSetA_print(x);
221+
222+ IntSetA_delete(ia);
223+ IntSetA_delete(x);
224+}
225+
226+void SetTest_test_1_2(void)
227+{
228+ int i;
229+ int success[SIZE];
230+ size_t count = 0;
231+ IntSetDIterator pos[SIZE];
232+ IntSetDIterator p;
233+ printf("***** test_1_2 *****\n");
234+ id = IntSetD_new();
235+ /* 初期状態 */
236+ assert(IntSetD_empty(id));
237+ assert(IntSetD_size(id) == 0);
238+ assert(IntSetD_begin(id) == IntSetD_end(id));
239+ assert(IntSetD_rbegin(id) == IntSetD_rend(id));
240+ /* insert */
241+ for (i = 0; i < SIZE; i++) {
242+ pos[i] = IntSetD_insert(id, hoge_int[i], &success[i]);
243+ if (i < SIZE/2) {
244+ assert(success[i]);
245+ count++;
246+ } else {
247+ assert(!success[i]);
248+ }
249+ assert(pos[i]);
250+ }
251+/* IntSetD_print(id);*/
252+ assert(!IntSetD_empty(id));
253+ assert(IntSetD_size(id) == count);
254+ assert(count == SIZE/2);
255+ /* count, find, lower_bound, upper_bound */
256+ for (i = 0; i < SIZE/2; i++) {
257+ assert(IntSetD_count(id, hoge_int[i]) == 1);
258+ assert(pos[i] == IntSetD_find(id, hoge_int[i]));
259+ assert(pos[i] == IntSetD_lower_bound(id, hoge_int[i]));
260+ assert(pos[i] == IntSetD_upper_bound(id, hoge_int[i]+1));
261+ assert(IntSetD_lower_bound(id, hoge_int[i]-1) == IntSetD_upper_bound(id, hoge_int[i]));
262+ }
263+ assert(IntSetD_find(id, IntSetD_key(IntSetD_begin(id)) +1) == IntSetD_end(id));
264+ assert(IntSetD_lower_bound(id, IntSetD_key(IntSetD_rbegin(id)) -1) == IntSetD_end(id));
265+ assert(IntSetD_upper_bound(id, IntSetD_key(IntSetD_rbegin(id))) == IntSetD_end(id));
266+ /* begin, end, next, key */
267+ for (p = IntSetD_begin(id), i = SIZE/2 -1; p != IntSetD_end(id); p = IntSetD_next(p), i--) {
268+ assert(IntSetD_key(p) == i);
269+ }
270+ assert(i == -1);
271+ assert(IntSetD_next(IntSetD_rbegin(id)) == IntSetD_end(id));
272+ /* rbegin, rend, prev, key */
273+ for (p = IntSetD_rbegin(id), i = 0; p != IntSetD_rend(id); p = IntSetD_prev(p), i++) {
274+ assert(IntSetD_key(p) == i);
275+ }
276+ assert(i == SIZE/2);
277+ assert(IntSetD_prev(IntSetD_begin(id)) == IntSetD_rend(id));
278+ /* erase */
279+ for (i = 0; i < SIZE; i++) {
280+ if (pos[i] && success[i]) {
281+ IntSetDIterator itr = IntSetD_next(pos[i]);
282+ assert(itr == IntSetD_erase(id, pos[i]));
283+ count--;
284+ }
285+ }
286+ assert(IntSetD_empty(id));
287+ assert(IntSetD_size(id) == 0);
288+ assert(count == 0);
289+ /* erase_range */
290+ for (i = 0; i < SIZE/2; i++) {
291+ pos[i] = IntSetD_insert(id, hoge_int[i], NULL);
292+ assert(pos[i]);
293+ }
294+ assert(IntSetD_size(id) == SIZE/2);
295+ assert(IntSetD_find(id, 1) == IntSetD_erase_range(id, IntSetD_find(id, SIZE/2-1 -2), IntSetD_find(id, 1)));
296+ assert(IntSetD_size(id) == 4);
297+ assert(IntSetD_end(id) == IntSetD_erase_range(id, IntSetD_begin(id), IntSetD_end(id)));
298+ assert(IntSetD_size(id) == 0);
299+ assert(IntSetD_insert(id, hoge_int[0], NULL));
300+ assert(IntSetD_size(id) == 1);
301+ assert(IntSetD_next(IntSetD_begin(id)) == IntSetD_erase_range(id, IntSetD_begin(id), IntSetD_next(IntSetD_begin(id))));
302+ assert(IntSetD_size(id) == 0);
303+ assert(IntSetD_insert(id, 100, NULL));
304+ assert(IntSetD_insert(id, 110, NULL));
305+ assert(IntSetD_size(id) == 2);
306+ assert(IntSetD_upper_bound(id, 100) == IntSetD_erase_range(id, IntSetD_lower_bound(id, 110), IntSetD_upper_bound(id, 100)));
307+ assert(IntSetD_size(id) == 0);
308+ /* erase_key */
309+ for (i = 0; i < SIZE/2; i++) {
310+ pos[i] = IntSetD_insert(id, hoge_int[i], NULL);
311+ assert(pos[i]);
312+ }
313+ assert(IntSetD_size(id) == SIZE/2);
314+ for (i = 0; i < SIZE/2; i++) {
315+ assert(IntSetD_erase_key(id, hoge_int[i]) == 1);
316+ }
317+ assert(IntSetD_size(id) == 0);
318+ /* 大量にinsert */
319+ count = 0;
320+ while (count < 1000000 && IntSetD_insert(id, count, NULL)) {
321+ count++;
322+ }
323+ assert(IntSetD_size(id) == count);
324+ printf("count: %d\n", count);
325+
326+ IntSetD_delete(id);
327+}
328+
329+void SetTest_test_1_3(void)
330+{
331+ int i;
332+ size_t count = 0;
333+ IntMSetAIterator pos[SIZE];
334+ IntMSetAIterator p;
335+ int flag[SIZE/2] = {0};
336+ printf("***** test_1_3 *****\n");
337+ ima = IntMSetA_new();
338+ /* 初期状態 */
339+ assert(IntMSetA_empty(ima));
340+ assert(IntMSetA_size(ima) == 0);
341+ assert(IntMSetA_begin(ima) == IntMSetA_end(ima));
342+ assert(IntMSetA_rbegin(ima) == IntMSetA_rend(ima));
343+ /* insert */
344+ for (i = 0; i < SIZE; i++) {
345+ pos[i] = IntMSetA_insert(ima, hoge_int[i]);
346+ assert(pos[i] && pos[i] != IntMSetA_end(ima));
347+ count++;
348+ }
349+/* IntMSetA_print(ima);*/
350+ assert(!IntMSetA_empty(ima));
351+ assert(IntMSetA_size(ima) == count);
352+ assert(count == SIZE);
353+ /* count */
354+ for (i = 0; i < SIZE; i++) {
355+ if (i < SIZE/2/2) {
356+ assert(IntMSetA_count(ima, i) == 1);
357+ } else if (i < SIZE/2) {
358+ assert(IntMSetA_count(ima, i) == 3);
359+ } else {
360+ assert(IntMSetA_count(ima, i) == 0);
361+ }
362+ }
363+ /* find, lower_bound, upper_bound */
364+ for (i = 0; i < SIZE; i++) {
365+ if (IntMSetA_count(ima, hoge_int[i]) == 1) {
366+ assert(pos[i] == IntMSetA_find(ima, hoge_int[i]));
367+ assert(pos[i] == IntMSetA_lower_bound(ima, hoge_int[i]));
368+ assert(pos[i] == IntMSetA_upper_bound(ima, hoge_int[i]-1));
369+ } else if (IntMSetA_count(ima, hoge_int[i]) == 3) {
370+ if (!flag[hoge_int[i]]) {
371+ flag[hoge_int[i]] = 1;
372+ assert(pos[i] == IntMSetA_lower_bound(ima, hoge_int[i]));
373+ assert(pos[i] == IntMSetA_upper_bound(ima, hoge_int[i]-1));
374+ }
375+ } else {
376+ assert(0);
377+ }
378+ assert(IntMSetA_lower_bound(ima, hoge_int[i]+1) == IntMSetA_upper_bound(ima, hoge_int[i]));
379+ }
380+ assert(IntMSetA_find(ima, IntMSetA_key(IntMSetA_begin(ima)) -1) == IntMSetA_end(ima));
381+ assert(IntMSetA_lower_bound(ima, IntMSetA_key(IntMSetA_rbegin(ima)) +1) == IntMSetA_end(ima));
382+ assert(IntMSetA_upper_bound(ima, IntMSetA_key(IntMSetA_rbegin(ima))) == IntMSetA_end(ima));
383+ /* begin, end, next, key */
384+ for (p = IntMSetA_begin(ima), i = 0; p != IntMSetA_end(ima); p = IntMSetA_next(p), i++) {
385+/* printf("%d, %d\n", i, IntMSetA_key(p));*/
386+ }
387+ assert(i == SIZE);
388+ assert(IntMSetA_next(IntMSetA_rbegin(ima)) == IntMSetA_end(ima));
389+ /* rbegin, rend, prev, key */
390+ for (p = IntMSetA_rbegin(ima), i = SIZE -1; p != IntMSetA_rend(ima); p = IntMSetA_prev(p), i--) {
391+/* printf("%d, %d\n", i, IntMSetA_key(p));*/
392+ }
393+ assert(i == -1);
394+ assert(IntMSetA_prev(IntMSetA_begin(ima)) == IntMSetA_rend(ima));
395+ /* erase */
396+ for (i = 0; i < SIZE; i++) {
397+ IntMSetAIterator itr = IntMSetA_next(pos[i]);
398+ assert(itr == IntMSetA_erase(ima, pos[i]));
399+ count--;
400+ }
401+ assert(IntMSetA_empty(ima));
402+ assert(IntMSetA_size(ima) == 0);
403+ assert(count == 0);
404+ /* erase_range */
405+ for (i = 0; i < SIZE; i++) {
406+ pos[i] = IntMSetA_insert(ima, hoge_int[i]);
407+ assert(pos[i] && pos[i] != IntMSetA_end(ima));
408+ }
409+ assert(IntMSetA_size(ima) == SIZE);
410+ assert(IntMSetA_next(IntMSetA_find(ima, SIZE/2/2 -1)) == IntMSetA_erase_range(ima, IntMSetA_find(ima, 0), IntMSetA_next(IntMSetA_find(ima, SIZE/2/2 -1))));
411+ assert(IntMSetA_size(ima) == SIZE - SIZE/2/2);
412+ assert(IntMSetA_end(ima) == IntMSetA_erase_range(ima, IntMSetA_begin(ima), IntMSetA_end(ima)));
413+ assert(IntMSetA_size(ima) == 0);
414+ assert(IntMSetA_insert(ima, hoge_int[0]));
415+ assert(IntMSetA_size(ima) == 1);
416+ assert(IntMSetA_next(IntMSetA_begin(ima)) == IntMSetA_erase_range(ima, IntMSetA_begin(ima), IntMSetA_next(IntMSetA_begin(ima))));
417+ assert(IntMSetA_size(ima) == 0);
418+ assert(IntMSetA_insert(ima, 100));
419+ assert(IntMSetA_insert(ima, 100));
420+ assert(IntMSetA_insert(ima, 100));
421+ assert(IntMSetA_insert(ima, 110));
422+ assert(IntMSetA_insert(ima, 110));
423+ assert(IntMSetA_size(ima) == 5);
424+ assert(IntMSetA_upper_bound(ima, 110) == IntMSetA_erase_range(ima, IntMSetA_lower_bound(ima, 100), IntMSetA_upper_bound(ima, 110)));
425+ assert(IntMSetA_size(ima) == 0);
426+ /* erase_key */
427+ for (i = 0; i < SIZE; i++) {
428+ pos[i] = IntMSetA_insert(ima, hoge_int[i]);
429+ assert(pos[i] && pos[i] != IntMSetA_end(ima));
430+ }
431+ assert(IntMSetA_size(ima) == SIZE);
432+ for (i = 0; i < SIZE/2/2; i++) {
433+ assert(IntMSetA_erase_key(ima, i) == 1);
434+ }
435+ assert(IntMSetA_size(ima) == SIZE - SIZE/2/2);
436+ for (i = SIZE/2/2; i < SIZE/2; i++) {
437+ assert(IntMSetA_erase_key(ima, i) == 3);
438+ }
439+ assert(IntMSetA_size(ima) == 0);
440+ /* 大量にinsert */
441+ count = 0;
442+ while (count < 1000000 && IntMSetA_insert(ima, 0)) {
443+ count++;
444+ }
445+ assert(IntMSetA_size(ima) == count);
446+ printf("count: %d\n", count);
447+
448+ IntMSetA_delete(ima);
449+}
450+
451+void SetTest_test_2_1(void)
452+{
453+ int i;
454+ int success[SIZE];
455+ size_t count = 0;
456+ DoubleSetAIterator pos[SIZE];
457+ DoubleSetAIterator p;
458+ printf("***** test_2_1 *****\n");
459+ da = DoubleSetA_new();
460+ /* 初期状態 */
461+ assert(DoubleSetA_empty(da));
462+ assert(DoubleSetA_size(da) == 0);
463+ assert(DoubleSetA_begin(da) == DoubleSetA_end(da));
464+ assert(DoubleSetA_rbegin(da) == DoubleSetA_rend(da));
465+ /* insert */
466+ for (i = 0; i < SIZE; i++) {
467+ pos[i] = DoubleSetA_insert(da, hoge_double[i], &success[i]);
468+ if (i < SIZE/2) {
469+ assert(success[i]);
470+ count++;
471+ } else {
472+ assert(!success[i]);
473+ }
474+ assert(pos[i]);
475+ }
476+ assert(!DoubleSetA_empty(da));
477+ assert(DoubleSetA_size(da) == count);
478+ assert(count == SIZE/2);
479+ /* count, find, lower_bound, upper_bound */
480+ for (i = 0; i < SIZE/2; i++) {
481+ assert(DoubleSetA_count(da, hoge_double[i]) == 1);
482+ assert(pos[i] == DoubleSetA_find(da, hoge_double[i]));
483+ assert(pos[i] == DoubleSetA_lower_bound(da, hoge_double[i]));
484+ assert(pos[i] == DoubleSetA_upper_bound(da, hoge_double[i]-0.5));
485+ assert(DoubleSetA_lower_bound(da, hoge_double[i]+0.5) == DoubleSetA_upper_bound(da, hoge_double[i]));
486+ }
487+ assert(DoubleSetA_find(da, DoubleSetA_key(DoubleSetA_begin(da)) -1) == DoubleSetA_end(da));
488+ assert(DoubleSetA_lower_bound(da, DoubleSetA_key(DoubleSetA_rbegin(da)) +1) == DoubleSetA_end(da));
489+ assert(DoubleSetA_upper_bound(da, DoubleSetA_key(DoubleSetA_rbegin(da))) == DoubleSetA_end(da));
490+ /* begin, end, next, key */
491+ for (p = DoubleSetA_begin(da), i = 0; p != DoubleSetA_end(da); p = DoubleSetA_next(p), i++) {
492+ assert(DoubleSetA_key(p) == i*0.5);
493+ }
494+ assert(i == SIZE/2);
495+ assert(DoubleSetA_next(DoubleSetA_rbegin(da)) == DoubleSetA_end(da));
496+ /* rbegin, rend, prev, key */
497+ for (p = DoubleSetA_rbegin(da), i = SIZE/2 -1; p != DoubleSetA_rend(da); p = DoubleSetA_prev(p), i--) {
498+ assert(DoubleSetA_key(p) == i*0.5);
499+ }
500+ assert(i == -1);
501+ assert(DoubleSetA_prev(DoubleSetA_begin(da)) == DoubleSetA_rend(da));
502+ /* erase */
503+ for (i = 0; i < SIZE; i++) {
504+ if (pos[i] && success[i]) {
505+ DoubleSetAIterator itr = DoubleSetA_next(pos[i]);
506+ assert(itr == DoubleSetA_erase(da, pos[i]));
507+ count--;
508+ }
509+ }
510+ assert(DoubleSetA_empty(da));
511+ assert(DoubleSetA_size(da) == 0);
512+ assert(count == 0);
513+ /* erase_range */
514+ for (i = 0; i < SIZE/2; i++) {
515+ pos[i] = DoubleSetA_insert(da, hoge_double[i], NULL);
516+ assert(pos[i]);
517+ }
518+ assert(DoubleSetA_size(da) == SIZE/2);
519+ assert(DoubleSetA_find(da, SIZE/2 -2) == DoubleSetA_erase_range(da, DoubleSetA_find(da, 2), DoubleSetA_find(da, SIZE/2 -2)));
520+ assert(DoubleSetA_size(da) == 4);
521+ assert(DoubleSetA_end(da) == DoubleSetA_erase_range(da, DoubleSetA_begin(da), DoubleSetA_end(da)));
522+ assert(DoubleSetA_size(da) == 0);
523+ assert(DoubleSetA_insert(da, hoge_double[0], NULL));
524+ assert(DoubleSetA_size(da) == 1);
525+ assert(DoubleSetA_next(DoubleSetA_begin(da)) == DoubleSetA_erase_range(da, DoubleSetA_begin(da), DoubleSetA_next(DoubleSetA_begin(da))));
526+ assert(DoubleSetA_size(da) == 0);
527+ assert(DoubleSetA_insert(da, 100.1, NULL));
528+ assert(DoubleSetA_insert(da, 110.1, NULL));
529+ assert(DoubleSetA_size(da) == 2);
530+ assert(DoubleSetA_upper_bound(da, 110.1) == DoubleSetA_erase_range(da, DoubleSetA_lower_bound(da, 100.1), DoubleSetA_upper_bound(da, 110.1)));
531+ assert(DoubleSetA_size(da) == 0);
532+ /* erase_key */
533+ for (i = 0; i < SIZE/2; i++) {
534+ pos[i] = DoubleSetA_insert(da, hoge_double[i], NULL);
535+ assert(pos[i]);
536+ }
537+ assert(DoubleSetA_size(da) == SIZE/2);
538+ for (i = 0; i < SIZE/2; i++) {
539+ assert(DoubleSetA_erase_key(da, hoge_double[i]) == 1);
540+ }
541+ assert(DoubleSetA_size(da) == 0);
542+ /* 大量にinsert */
543+ count = 0;
544+ while (count < 1000000 && DoubleSetA_insert(da, count, NULL)) {
545+ count++;
546+ }
547+ assert(DoubleSetA_size(da) == count);
548+ printf("count: %d\n", count);
549+ printf("size: %d\n", DoubleSetA_size(da));
550+ /* clear */
551+ DoubleSetA_clear(da);
552+ printf("size: %d\n", DoubleSetA_size(da));
553+ assert(DoubleSetA_size(da) == 0);
554+ assert(DoubleSetA_insert(da, 100, NULL));
555+ assert(DoubleSetA_size(da) == 1);
556+ DoubleSetA_clear(da);
557+ assert(DoubleSetA_size(da) == 0);
558+ DoubleSetA_clear(da);
559+ assert(DoubleSetA_size(da) == 0);
560+
561+ DoubleSetA_delete(da);
562+}
563+
564+void SetTest_test_3_1(void)
565+{
566+ int i;
567+ int success[SIZE];
568+ size_t count = 0;
569+ int *intp = NULL;
570+ PtrSetAIterator pos[SIZE];
571+ PtrSetAIterator p;
572+ printf("***** test_3_1 *****\n");
573+ pa = PtrSetA_new();
574+ /* 初期状態 */
575+ assert(PtrSetA_empty(pa));
576+ assert(PtrSetA_size(pa) == 0);
577+ assert(PtrSetA_begin(pa) == PtrSetA_end(pa));
578+ assert(PtrSetA_rbegin(pa) == PtrSetA_rend(pa));
579+ /* insert */
580+ for (i = 0; i < SIZE; i++) {
581+ pos[i] = PtrSetA_insert(pa, hoge_ptr[i], &success[i]);
582+ if (i < SIZE/2) {
583+ assert(success[i]);
584+ count++;
585+ } else {
586+ assert(!success[i]);
587+ }
588+ assert(pos[i]);
589+ }
590+ assert(!PtrSetA_empty(pa));
591+ assert(PtrSetA_size(pa) == count);
592+ assert(count == SIZE/2);
593+ /* count, find, lower_bound, upper_bound */
594+ for (i = 0; i < SIZE/2; i++) {
595+ assert(PtrSetA_count(pa, hoge_ptr[i]) == 1);
596+ assert(pos[i] == PtrSetA_find(pa, hoge_ptr[i]));
597+ assert(pos[i] == PtrSetA_lower_bound(pa, hoge_ptr[i]));
598+ assert(pos[i] == PtrSetA_upper_bound(pa, hoge_ptr[i]-1));
599+ assert(PtrSetA_lower_bound(pa, hoge_ptr[i]+1) == PtrSetA_upper_bound(pa, hoge_ptr[i]));
600+ }
601+ assert(PtrSetA_find(pa, PtrSetA_key(PtrSetA_begin(pa)) -1) == PtrSetA_end(pa));
602+ assert(PtrSetA_lower_bound(pa, PtrSetA_key(PtrSetA_rbegin(pa)) +1) == PtrSetA_end(pa));
603+ assert(PtrSetA_upper_bound(pa, PtrSetA_key(PtrSetA_rbegin(pa))) == PtrSetA_end(pa));
604+ /* begin, end, next, key */
605+ for (p = PtrSetA_begin(pa), i = 0; p != PtrSetA_end(pa); p = PtrSetA_next(p), i++) {
606+ assert(PtrSetA_key(p) == &hoge_int[i]);
607+ }
608+ assert(i == SIZE/2);
609+ assert(PtrSetA_next(PtrSetA_rbegin(pa)) == PtrSetA_end(pa));
610+ /* rbegin, rend, prev, key */
611+ for (p = PtrSetA_rbegin(pa), i = SIZE/2 -1; p != PtrSetA_rend(pa); p = PtrSetA_prev(p), i--) {
612+ assert(PtrSetA_key(p) == &hoge_int[i]);
613+ }
614+ assert(i == -1);
615+ assert(PtrSetA_prev(PtrSetA_begin(pa)) == PtrSetA_rend(pa));
616+ /* erase */
617+ for (i = 0; i < SIZE; i++) {
618+ if (pos[i] && success[i]) {
619+ PtrSetAIterator itr = PtrSetA_next(pos[i]);
620+ assert(itr == PtrSetA_erase(pa, pos[i]));
621+ count--;
622+ }
623+ }
624+ assert(PtrSetA_empty(pa));
625+ assert(PtrSetA_size(pa) == 0);
626+ assert(count == 0);
627+ /* erase_range */
628+ for (i = 0; i < SIZE/2; i++) {
629+ pos[i] = PtrSetA_insert(pa, hoge_ptr[i], NULL);
630+ assert(pos[i]);
631+ }
632+ assert(PtrSetA_size(pa) == SIZE/2);
633+ assert(PtrSetA_find(pa, &hoge_int[SIZE/2 -2]) == PtrSetA_erase_range(pa, PtrSetA_find(pa, &hoge_int[2]), PtrSetA_find(pa, &hoge_int[SIZE/2 -2])));
634+ assert(PtrSetA_size(pa) == 4);
635+ assert(PtrSetA_end(pa) == PtrSetA_erase_range(pa, PtrSetA_begin(pa), PtrSetA_end(pa)));
636+ assert(PtrSetA_size(pa) == 0);
637+ assert(PtrSetA_insert(pa, hoge_ptr[0], NULL));
638+ assert(PtrSetA_size(pa) == 1);
639+ assert(PtrSetA_next(PtrSetA_begin(pa)) == PtrSetA_erase_range(pa, PtrSetA_begin(pa), PtrSetA_next(PtrSetA_begin(pa))));
640+ assert(PtrSetA_size(pa) == 0);
641+ assert(PtrSetA_insert(pa, (int*)100, NULL));
642+ assert(PtrSetA_insert(pa, (int*)100 + 1, NULL));
643+ assert(PtrSetA_size(pa) == 2);
644+ assert(PtrSetA_upper_bound(pa, (int*)100 + 1) == PtrSetA_erase_range(pa, PtrSetA_lower_bound(pa, (int*)100), PtrSetA_upper_bound(pa, (int*)100 + 1)));
645+ assert(PtrSetA_size(pa) == 0);
646+ /* erase_key */
647+ for (i = 0; i < SIZE/2; i++) {
648+ pos[i] = PtrSetA_insert(pa, hoge_ptr[i], NULL);
649+ assert(pos[i]);
650+ }
651+ assert(PtrSetA_size(pa) == SIZE/2);
652+ for (i = 0; i < SIZE/2; i++) {
653+ assert(PtrSetA_erase_key(pa, hoge_ptr[i]) == 1);
654+ }
655+ assert(PtrSetA_size(pa) == 0);
656+ /* 大量にinsert */
657+ count = 0;
658+ while (count < 1000000 && PtrSetA_insert(pa, intp, NULL)) {
659+ count++;
660+ intp++;
661+ }
662+ assert(PtrSetA_size(pa) == count);
663+ printf("count: %d\n", count);
664+ printf("size: %d\n", PtrSetA_size(pa));
665+ /* clear */
666+ PtrSetA_clear(pa);
667+ printf("size: %d\n", PtrSetA_size(pa));
668+ assert(PtrSetA_size(pa) == 0);
669+ assert(PtrSetA_insert(pa, intp, NULL));
670+ assert(PtrSetA_size(pa) == 1);
671+ PtrSetA_clear(pa);
672+ assert(PtrSetA_size(pa) == 0);
673+ PtrSetA_clear(pa);
674+ assert(PtrSetA_size(pa) == 0);
675+
676+ PtrSetA_delete(pa);
677+}
678+
679+void SetTest_test_4_1(void)
680+{
681+ int i;
682+ int success[SIZE];
683+ size_t count = 0;
684+ StrSetAIterator pos[SIZE];
685+ StrSetAIterator p;
686+ printf("***** test_4_1 *****\n");
687+ sa = StrSetA_new();
688+ /* 初期状態 */
689+ assert(StrSetA_empty(sa));
690+ assert(StrSetA_size(sa) == 0);
691+ assert(StrSetA_begin(sa) == StrSetA_end(sa));
692+ assert(StrSetA_rbegin(sa) == StrSetA_rend(sa));
693+ /* insert */
694+ for (i = 0; i < SIZE; i++) {
695+ pos[i] = StrSetA_insert(sa, hoge_str[i], &success[i]);
696+ if (i < SIZE/2) {
697+ assert(success[i]);
698+ count++;
699+ } else {
700+ assert(!success[i]);
701+ }
702+ assert(pos[i]);
703+ }
704+/* StrSetA_print(sa);*/
705+ assert(!StrSetA_empty(sa));
706+ assert(StrSetA_size(sa) == count);
707+ assert(count == SIZE/2);
708+ /* count, find, lower_bound, upper_bound */
709+ for (i = 0; i < SIZE/2; i++) {
710+ assert(StrSetA_count(sa, hoge_str[i]) == 1);
711+ assert(pos[i] == StrSetA_find(sa, hoge_str[i]));
712+ assert(pos[i] == StrSetA_lower_bound(sa, hoge_str[i]));
713+ if (i != SIZE/2-1) {
714+ assert(pos[i+1] == StrSetA_upper_bound(sa, hoge_str[i]));
715+ assert(StrSetA_lower_bound(sa, hoge_str[i+1]) == StrSetA_upper_bound(sa, hoge_str[i]));
716+ }
717+ }
718+ assert(StrSetA_find(sa, "hoge") == StrSetA_end(sa));
719+ assert(StrSetA_lower_bound(sa, "hoge") == StrSetA_end(sa));
720+ assert(StrSetA_upper_bound(sa, StrSetA_key(StrSetA_rbegin(sa))) == StrSetA_end(sa));
721+ /* begin, end, next, key */
722+ for (p = StrSetA_begin(sa), i = 0; p != StrSetA_end(sa); p = StrSetA_next(p), i++) {
723+ assert(StrSetA_key(p) == hoge_str[i]);
724+ }
725+ assert(i == SIZE/2);
726+ assert(StrSetA_next(StrSetA_rbegin(sa)) == StrSetA_end(sa));
727+ /* rbegin, rend, prev, key */
728+ for (p = StrSetA_rbegin(sa), i = SIZE/2 -1; p != StrSetA_rend(sa); p = StrSetA_prev(p), i--) {
729+ assert(StrSetA_key(p) == hoge_str[i]);
730+ }
731+ assert(i == -1);
732+ assert(StrSetA_prev(StrSetA_begin(sa)) == StrSetA_rend(sa));
733+ /* erase */
734+ for (i = 0; i < SIZE; i++) {
735+ if (pos[i] && success[i]) {
736+ StrSetAIterator itr = StrSetA_next(pos[i]);
737+ assert(itr == StrSetA_erase(sa, pos[i]));
738+ count--;
739+ }
740+ }
741+ assert(StrSetA_empty(sa));
742+ assert(StrSetA_size(sa) == 0);
743+ assert(count == 0);
744+ /* erase_range */
745+ for (i = 0; i < SIZE/2; i++) {
746+ pos[i] = StrSetA_insert(sa, hoge_str[i], NULL);
747+ assert(pos[i]);
748+ }
749+ assert(StrSetA_size(sa) == SIZE/2);
750+ assert(StrSetA_find(sa, hoge_str[SIZE/2 -2]) == StrSetA_erase_range(sa, StrSetA_find(sa, hoge_str[2]), StrSetA_find(sa, hoge_str[SIZE/2 -2])));
751+ assert(StrSetA_size(sa) == 4);
752+ assert(StrSetA_end(sa) == StrSetA_erase_range(sa, StrSetA_begin(sa), StrSetA_end(sa)));
753+ assert(StrSetA_size(sa) == 0);
754+ assert(StrSetA_insert(sa, hoge_str[0], NULL));
755+ assert(StrSetA_size(sa) == 1);
756+ assert(StrSetA_next(StrSetA_begin(sa)) == StrSetA_erase_range(sa, StrSetA_begin(sa), StrSetA_next(StrSetA_begin(sa))));
757+ assert(StrSetA_size(sa) == 0);
758+ assert(StrSetA_insert(sa, "100", NULL));
759+ assert(StrSetA_insert(sa, "110", NULL));
760+ assert(StrSetA_size(sa) == 2);
761+ assert(StrSetA_upper_bound(sa, "110") == StrSetA_erase_range(sa, StrSetA_lower_bound(sa, "100"), StrSetA_upper_bound(sa, "110")));
762+ assert(StrSetA_size(sa) == 0);
763+ /* erase_key */
764+ for (i = 0; i < SIZE/2; i++) {
765+ pos[i] = StrSetA_insert(sa, hoge_str[i], NULL);
766+ assert(pos[i]);
767+ }
768+ assert(StrSetA_size(sa) == SIZE/2);
769+ for (i = 0; i < SIZE/2; i++) {
770+ assert(StrSetA_erase_key(sa, hoge_str[i]) == 1);
771+ }
772+ assert(StrSetA_size(sa) == 0);
773+ /* clear */
774+ assert(StrSetA_insert(sa, "103", NULL));
775+ assert(StrSetA_insert(sa, "101", NULL));
776+ assert(StrSetA_insert(sa, "100", NULL));
777+ assert(StrSetA_insert(sa, "104", NULL));
778+ assert(StrSetA_insert(sa, "106", NULL));
779+ assert(StrSetA_insert(sa, "105", NULL));
780+ assert(StrSetA_insert(sa, "102", NULL));
781+ printf("size: %d\n", StrSetA_size(sa));
782+ StrSetA_clear(sa);
783+ printf("size: %d\n", StrSetA_size(sa));
784+ assert(StrSetA_size(sa) == 0);
785+ assert(StrSetA_insert(sa, "100", NULL));
786+ assert(StrSetA_size(sa) == 1);
787+ StrSetA_clear(sa);
788+ assert(StrSetA_size(sa) == 0);
789+ StrSetA_clear(sa);
790+ assert(StrSetA_size(sa) == 0);
791+
792+ StrSetA_delete(sa);
793+}
794+
795+void SetTest_test_5_1(void)
796+{
797+ int i;
798+ size_t count = 0;
799+ int success[SIZE];
800+ UIntSetAIterator pos[SIZE];
801+ UIntSetAIterator p;
802+ printf("***** test_5_1 *****\n");
803+ uia = UIntSetA_new();
804+ /* 初期状態 */
805+ assert(UIntSetA_empty(uia));
806+ assert(UIntSetA_size(uia) == 0);
807+ assert(UIntSetA_begin(uia) == UIntSetA_end(uia));
808+ assert(UIntSetA_rbegin(uia) == UIntSetA_rend(uia));
809+ /* insert */
810+ for (i = 0; i < SIZE; i++) {
811+ pos[i] = UIntSetA_insert(uia, hoge_int[i], &success[i]);
812+/* UIntSetA_print(uia);*/
813+ assert(pos[i]);
814+ if (i < SIZE/2) {
815+ assert(success[i]);
816+ count++;
817+ } else {
818+ assert(!success[i]);
819+ }
820+ }
821+/* UIntSetA_print(uia);*/
822+ assert(!UIntSetA_empty(uia));
823+ assert(UIntSetA_size(uia) == count);
824+ assert(count == SIZE/2);
825+ /* count, find, lower_bound, upper_bound */
826+ for (i = 0; i < SIZE/2; i++) {
827+ assert(UIntSetA_count(uia, hoge_int[i]) == 1);
828+ assert(pos[i] == UIntSetA_find(uia, hoge_int[i]));
829+ assert(pos[i] == UIntSetA_lower_bound(uia, hoge_int[i]));
830+ if (hoge_int[i] == 0) {
831+ assert(UIntSetA_end(uia) == UIntSetA_upper_bound(uia, hoge_int[i]-1));
832+ } else {
833+ assert(pos[i] == UIntSetA_upper_bound(uia, hoge_int[i]-1));
834+ }
835+ assert(UIntSetA_lower_bound(uia, hoge_int[i]+1) == UIntSetA_upper_bound(uia, hoge_int[i]));
836+ }
837+ assert(UIntSetA_find(uia, UIntSetA_key(UIntSetA_begin(uia)) -1) == UIntSetA_end(uia));
838+ assert(UIntSetA_lower_bound(uia, UIntSetA_key(UIntSetA_rbegin(uia)) +1) == UIntSetA_end(uia));
839+ assert(UIntSetA_upper_bound(uia, UIntSetA_key(UIntSetA_rbegin(uia))) == UIntSetA_end(uia));
840+ /* begin, end, next, key */
841+ for (p = UIntSetA_begin(uia), i = 0; p != UIntSetA_end(uia); p = UIntSetA_next(p), i++) {
842+ assert(UIntSetA_key(p) == i);
843+ }
844+ assert(i == SIZE/2);
845+ assert(UIntSetA_next(UIntSetA_rbegin(uia)) == UIntSetA_end(uia));
846+ /* rbegin, rend, prev, key */
847+ for (p = UIntSetA_rbegin(uia), i = SIZE/2 -1; p != UIntSetA_rend(uia); p = UIntSetA_prev(p), i--) {
848+ assert(UIntSetA_key(p) == i);
849+ }
850+ assert(i == -1);
851+ assert(UIntSetA_prev(UIntSetA_begin(uia)) == UIntSetA_rend(uia));
852+ /* erase */
853+ for (i = 0; i < SIZE; i++) {
854+ if (pos[i] && success[i]) {
855+ UIntSetAIterator itr = UIntSetA_next(pos[i]);
856+ assert(itr == UIntSetA_erase(uia, pos[i]));
857+ count--;
858+ }
859+ }
860+ assert(UIntSetA_empty(uia));
861+ assert(UIntSetA_size(uia) == 0);
862+ assert(count == 0);
863+ /* erase_range */
864+ for (i = 0; i < SIZE/2; i++) {
865+ pos[i] = UIntSetA_insert(uia, hoge_int[i], NULL);
866+ assert(pos[i]);
867+ }
868+ assert(UIntSetA_size(uia) == SIZE/2);
869+ assert(UIntSetA_find(uia, SIZE/2 -2) == UIntSetA_erase_range(uia, UIntSetA_find(uia, 2), UIntSetA_find(uia, SIZE/2 -2)));
870+ assert(UIntSetA_size(uia) == 4);
871+ assert(UIntSetA_end(uia) == UIntSetA_erase_range(uia, UIntSetA_begin(uia), UIntSetA_end(uia)));
872+ assert(UIntSetA_size(uia) == 0);
873+ assert(UIntSetA_insert(uia, hoge_int[0], NULL));
874+ assert(UIntSetA_size(uia) == 1);
875+ assert(UIntSetA_next(UIntSetA_begin(uia)) == UIntSetA_erase_range(uia, UIntSetA_begin(uia), UIntSetA_next(UIntSetA_begin(uia))));
876+ assert(UIntSetA_size(uia) == 0);
877+ assert(UIntSetA_insert(uia, 100, NULL));
878+ assert(UIntSetA_insert(uia, 110, NULL));
879+ assert(UIntSetA_size(uia) == 2);
880+ assert(UIntSetA_upper_bound(uia, 110) == UIntSetA_erase_range(uia, UIntSetA_lower_bound(uia, 100), UIntSetA_upper_bound(uia, 110)));
881+ assert(UIntSetA_size(uia) == 0);
882+ /* erase_key */
883+ for (i = 0; i < SIZE/2; i++) {
884+ pos[i] = UIntSetA_insert(uia, hoge_int[i], NULL);
885+ assert(pos[i]);
886+ }
887+ assert(UIntSetA_size(uia) == SIZE/2);
888+ for (i = 0; i < SIZE/2; i++) {
889+ assert(UIntSetA_erase_key(uia, hoge_int[i]) == 1);
890+ }
891+ assert(UIntSetA_size(uia) == 0);
892+ /* 大量にinsert */
893+ count = 0;
894+ while (count < 1000000 && UIntSetA_insert(uia, count, NULL)) {
895+ count++;
896+ }
897+ assert(UIntSetA_size(uia) == count);
898+ printf("count: %d\n", count);
899+ printf("size: %d\n", UIntSetA_size(uia));
900+ /* clear */
901+ UIntSetA_clear(uia);
902+ printf("size: %d\n", UIntSetA_size(uia));
903+ assert(UIntSetA_size(uia) == 0);
904+ assert(UIntSetA_insert(uia, 100, NULL));
905+ assert(UIntSetA_size(uia) == 1);
906+ UIntSetA_clear(uia);
907+ assert(UIntSetA_size(uia) == 0);
908+ UIntSetA_clear(uia);
909+ assert(UIntSetA_size(uia) == 0);
910+
911+ UIntSetA_delete(uia);
912+}
913+
914+
915+
916+
917+
918+
919+
920+
921+
922+
923+
924+
925+
926+void SetTest_run(void)
927+{
928+ printf("\n===== set test =====\n");
929+ set_init_hoge();
930+
931+ SetTest_test_1_1();
932+ SetTest_test_1_2();
933+ SetTest_test_1_3();
934+ SetTest_test_2_1();
935+ SetTest_test_3_1();
936+ SetTest_test_4_1();
937+ SetTest_test_5_1();
938+
939+
940+}
941+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/Makefile (nonexistent)
+++ tags/rel-0.2.1/test/Makefile (revision 43)
@@ -0,0 +1,35 @@
1+CC=gcc
2+# MEMORY=-DHEAP_DEBUG -DMY_MALLOC
3+CFLAGS=-Wall -g $(MEMORY)
4+
5+all: test
6+clean:
7+ rm -f *.exe *.o
8+
9+heap.o: heap.c heap.h
10+ $(CC) $(CFLAGS) -c heap.c
11+
12+vector_test.o: ../cstl/vector.h vector_test.c heap.o
13+ $(CC) $(CFLAGS) -c vector_test.c
14+
15+deque_test.o: ../cstl/deque.h deque_test.c heap.o
16+ $(CC) $(CFLAGS) -c deque_test.c
17+
18+list_test.o: ../cstl/list.h list_test.c heap.o
19+ $(CC) $(CFLAGS) -c list_test.c
20+
21+set_test.o: ../cstl/set.h ../cstl/rbtree.h set_test.c heap.o rbtree_debug.h
22+ $(CC) $(CFLAGS) -c set_test.c
23+
24+map_test.o: ../cstl/map.h ../cstl/rbtree.h map_test.c heap.o rbtree_debug.h
25+ $(CC) $(CFLAGS) -c map_test.c
26+
27+string_test.o: ../cstl/string.h ../cstl/vector.h string_test.cpp heap.o
28+ g++ $(CFLAGS) -c string_test.cpp
29+
30+test: test.h main.c vector_test.o deque_test.o list_test.o set_test.o map_test.o string_test.o
31+ $(CC) $(CFLAGS) -o $@.exe main.c heap.o vector_test.o deque_test.o list_test.o set_test.o map_test.o
32+ ./$@.exe
33+ g++ -o string_test.exe heap.o string_test.o
34+ ./string_test.exe
35+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/map_test.c (nonexistent)
+++ tags/rel-0.2.1/test/map_test.c (revision 43)
@@ -0,0 +1,350 @@
1+#include "test.h"
2+
3+#include <time.h>
4+
5+/* int */
6+MAP_IMPLEMENT(IntIntMapA, int, int, SIMPLE_CMP, ASC)
7+MAP_DEBUG_IMPLEMENT(IntIntMapA, int, int, SIMPLE_CMP, ASC, %d, %d, VISUAL)
8+static IntIntMapA *ia;
9+
10+MULTIMAP_IMPLEMENT(IntIntMMapA, int, int, SIMPLE_CMP, ASC)
11+MAP_DEBUG_IMPLEMENT(IntIntMMapA, int, int, SIMPLE_CMP, ASC, %d, %d, VISUAL)
12+static IntIntMMapA *ima;
13+
14+
15+
16+#define SIZE 32
17+static int hoge_int[SIZE];
18+static double hoge_double[SIZE];
19+static int *hoge_ptr[SIZE];
20+static char *hoge_str[SIZE];
21+static char str[SIZE][16];
22+
23+void map_init_hoge(void)
24+{
25+ int i;
26+ int tmp;
27+ IntDeque *q = IntDeque_new(SIZE/2);
28+ srand(time(0));
29+/* printf("hoge data\n");*/
30+ for (i = 0; i < SIZE/2; i++) {
31+ IntDeque_push_back(q, i);
32+ }
33+ for (i = 0; i < SIZE; i++) {
34+ if (!IntDeque_empty(q)) {
35+ int rnd = rand() % IntDeque_size(q);
36+ tmp = *IntDeque_at(q, rnd);
37+ IntDeque_erase(q, rnd, 1);
38+ } else {
39+ tmp = i/2;
40+ }
41+ hoge_int[i] = tmp;
42+ hoge_double[i] = hoge_int[i] * 0.5;
43+ hoge_ptr[i] = &hoge_int[tmp];
44+ sprintf(str[i], "%05d", i < SIZE/2 ? i : tmp);
45+ hoge_str[i] = str[i];
46+/* printf("%4d: int[%3d], double[%5g], ptr[%p], str[%s]\n",*/
47+/* i, hoge_int[i], hoge_double[i], hoge_ptr[i], hoge_str[i]);*/
48+ }
49+ IntDeque_delete(q);
50+}
51+
52+void MapTest_test_1_1(void)
53+{
54+ int i;
55+ int success[SIZE];
56+ size_t count = 0;
57+ IntIntMapAIterator pos[SIZE];
58+ IntIntMapAIterator p;
59+ IntIntMapA *x;
60+ printf("***** test_1_1 *****\n");
61+ ia = IntIntMapA_new();
62+ /* 初期状態 */
63+ assert(IntIntMapA_empty(ia));
64+ assert(IntIntMapA_size(ia) == 0);
65+ assert(IntIntMapA_begin(ia) == IntIntMapA_end(ia));
66+ assert(IntIntMapA_rbegin(ia) == IntIntMapA_rend(ia));
67+ /* insert */
68+ for (i = 0; i < SIZE; i++) {
69+ pos[i] = IntIntMapA_insert(ia, hoge_int[i], hoge_int[i], &success[i]);
70+ if (i < SIZE/2) {
71+ assert(success[i]);
72+ count++;
73+ } else {
74+ assert(!success[i]);
75+ }
76+ assert(pos[i]);
77+ }
78+/* IntIntMapA_print(ia);*/
79+ assert(!IntIntMapA_empty(ia));
80+ assert(IntIntMapA_size(ia) == count);
81+ assert(count == SIZE/2);
82+ /* count, find, lower_bound, upper_bound */
83+ for (i = 0; i < SIZE/2; i++) {
84+ assert(IntIntMapA_count(ia, hoge_int[i]) == 1);
85+ assert(pos[i] == IntIntMapA_find(ia, hoge_int[i]));
86+ assert(pos[i] == IntIntMapA_lower_bound(ia, hoge_int[i]));
87+ assert(pos[i] == IntIntMapA_upper_bound(ia, hoge_int[i]-1));
88+ assert(IntIntMapA_lower_bound(ia, hoge_int[i]+1) == IntIntMapA_upper_bound(ia, hoge_int[i]));
89+ }
90+ assert(IntIntMapA_find(ia, IntIntMapA_key(IntIntMapA_begin(ia)) -1) == IntIntMapA_end(ia));
91+ assert(IntIntMapA_lower_bound(ia, IntIntMapA_key(IntIntMapA_rbegin(ia)) +1) == IntIntMapA_end(ia));
92+ assert(IntIntMapA_upper_bound(ia, IntIntMapA_key(IntIntMapA_rbegin(ia))) == IntIntMapA_end(ia));
93+ /* begin, end, next, key, value, lookup */
94+ for (p = IntIntMapA_begin(ia), i = 0; p != IntIntMapA_end(ia); p = IntIntMapA_next(p), i++) {
95+ assert(IntIntMapA_key(p) == i);
96+ assert(*IntIntMapA_value(p) == i);
97+ assert(*IntIntMapA_lookup(ia, IntIntMapA_key(p)) == i);
98+ *IntIntMapA_value(p) = ~i;
99+ assert(*IntIntMapA_lookup(ia, IntIntMapA_key(p)) == ~i);
100+ *IntIntMapA_lookup(ia, IntIntMapA_key(p)) = i;
101+ }
102+ assert(i == SIZE/2);
103+ assert(IntIntMapA_next(IntIntMapA_rbegin(ia)) == IntIntMapA_end(ia));
104+ /* rbegin, rend, prev, key, value, lookup */
105+ for (p = IntIntMapA_rbegin(ia), i = SIZE/2 -1; p != IntIntMapA_rend(ia); p = IntIntMapA_prev(p), i--) {
106+ assert(IntIntMapA_key(p) == i);
107+ assert(*IntIntMapA_value(p) == i);
108+ assert(*IntIntMapA_lookup(ia, IntIntMapA_key(p)) == i);
109+ *IntIntMapA_value(p) = ~i;
110+ assert(*IntIntMapA_lookup(ia, IntIntMapA_key(p)) == ~i);
111+ *IntIntMapA_lookup(ia, IntIntMapA_key(p)) = i;
112+ }
113+ assert(i == -1);
114+ assert(IntIntMapA_prev(IntIntMapA_begin(ia)) == IntIntMapA_rend(ia));
115+ /* erase */
116+ for (i = 0; i < SIZE; i++) {
117+ if (pos[i] && success[i]) {
118+ IntIntMapAIterator itr = IntIntMapA_next(pos[i]);
119+ assert(itr == IntIntMapA_erase(ia, pos[i]));
120+ count--;
121+ }
122+ }
123+ assert(IntIntMapA_empty(ia));
124+ assert(IntIntMapA_size(ia) == 0);
125+ assert(count == 0);
126+ /* erase_range */
127+ for (i = 0; i < SIZE/2; i++) {
128+ pos[i] = IntIntMapA_insert(ia, hoge_int[i], hoge_int[i], NULL);
129+ assert(pos[i] && pos[i] != IntIntMapA_end(ia));
130+ }
131+ assert(IntIntMapA_size(ia) == SIZE/2);
132+ assert(IntIntMapA_find(ia, SIZE/2 -2) == IntIntMapA_erase_range(ia, IntIntMapA_find(ia, 2), IntIntMapA_find(ia, SIZE/2 -2)));
133+ assert(IntIntMapA_size(ia) == 4);
134+ assert(IntIntMapA_end(ia) == IntIntMapA_erase_range(ia, IntIntMapA_begin(ia), IntIntMapA_end(ia)));
135+ assert(IntIntMapA_size(ia) == 0);
136+ assert(IntIntMapA_insert(ia, hoge_int[0], hoge_int[0], NULL));
137+ assert(IntIntMapA_size(ia) == 1);
138+ assert(IntIntMapA_next(IntIntMapA_begin(ia)) == IntIntMapA_erase_range(ia, IntIntMapA_begin(ia), IntIntMapA_next(IntIntMapA_begin(ia))));
139+ assert(IntIntMapA_size(ia) == 0);
140+ assert(IntIntMapA_insert(ia, 100, 101, NULL));
141+ assert(IntIntMapA_insert(ia, 110, 111, NULL));
142+ assert(IntIntMapA_size(ia) == 2);
143+ assert(IntIntMapA_upper_bound(ia, 110) == IntIntMapA_erase_range(ia, IntIntMapA_lower_bound(ia, 100), IntIntMapA_upper_bound(ia, 110)));
144+ assert(IntIntMapA_size(ia) == 0);
145+ /* erase_key */
146+ for (i = 0; i < SIZE/2; i++) {
147+ pos[i] = IntIntMapA_insert(ia, hoge_int[i], hoge_int[i], NULL);
148+ assert(pos[i] && pos[i] != IntIntMapA_end(ia));
149+ }
150+ assert(IntIntMapA_size(ia) == SIZE/2);
151+ for (i = 0; i < SIZE/2; i++) {
152+ assert(IntIntMapA_erase_key(ia, hoge_int[i]) == 1);
153+ }
154+ assert(IntIntMapA_size(ia) == 0);
155+ /* 大量にinsert */
156+ count = 0;
157+ while (count < 1000000 && IntIntMapA_insert(ia, count, 9999, NULL)) {
158+ count++;
159+ }
160+ assert(IntIntMapA_size(ia) == count);
161+ printf("count: %d\n", count);
162+ printf("size: %d\n", IntIntMapA_size(ia));
163+ /* clear */
164+ IntIntMapA_clear(ia);
165+ printf("size: %d\n", IntIntMapA_size(ia));
166+ assert(IntIntMapA_size(ia) == 0);
167+ assert(IntIntMapA_insert(ia, 100, 123, NULL));
168+ assert(IntIntMapA_size(ia) == 1);
169+ IntIntMapA_clear(ia);
170+ assert(IntIntMapA_size(ia) == 0);
171+ IntIntMapA_clear(ia);
172+ assert(IntIntMapA_size(ia) == 0);
173+
174+ /* lookup */
175+ /* 挿入 */
176+ *IntIntMapA_lookup(ia, 1) = 482;
177+ *IntIntMapA_lookup(ia, 2) = 4;
178+ *IntIntMapA_lookup(ia, 3) = 554;
179+ *IntIntMapA_lookup(ia, 4) = 44;
180+ *IntIntMapA_lookup(ia, 5) = 89;
181+/* IntIntMapA_print(ia);*/
182+ /* 書換え */
183+ *IntIntMapA_lookup(ia, 1) = 82;
184+/* IntIntMapA_print(ia);*/
185+
186+ /* swap */
187+ x = IntIntMapA_new();
188+ *IntIntMapA_lookup(x, 10) = 2;
189+ *IntIntMapA_lookup(x, 20) = 442;
190+ *IntIntMapA_lookup(x, 30) = 694;
191+/* IntIntMapA_print(ia);*/
192+/* IntIntMapA_print(x);*/
193+ IntIntMapA_swap(ia, x);
194+/* IntIntMapA_print(ia);*/
195+/* IntIntMapA_print(x);*/
196+ assert(IntIntMapA_size(ia) == 3);
197+ assert(IntIntMapA_size(x) == 5);
198+ /* assign */
199+ assert(IntIntMapA_assign(x, IntIntMapA_begin(ia), IntIntMapA_end(ia)));
200+ assert(IntIntMapA_size(ia) == IntIntMapA_size(x));
201+ IntIntMapA_print(ia);
202+ IntIntMapA_print(x);
203+
204+
205+
206+ IntIntMapA_delete(ia);
207+ IntIntMapA_delete(x);
208+}
209+
210+void MapTest_test_1_2(void)
211+{
212+ int i;
213+ size_t count = 0;
214+ IntIntMMapAIterator pos[SIZE];
215+ IntIntMMapAIterator p;
216+ int flag[SIZE/2] = {0};
217+ printf("***** test_1_2 *****\n");
218+ ima = IntIntMMapA_new();
219+ /* 初期状態 */
220+ assert(IntIntMMapA_empty(ima));
221+ assert(IntIntMMapA_size(ima) == 0);
222+ assert(IntIntMMapA_begin(ima) == IntIntMMapA_end(ima));
223+ assert(IntIntMMapA_rbegin(ima) == IntIntMMapA_rend(ima));
224+ /* insert */
225+ for (i = 0; i < SIZE; i++) {
226+ pos[i] = IntIntMMapA_insert(ima, hoge_int[i], hoge_int[i]);
227+ assert(pos[i] && pos[i] != IntIntMMapA_end(ima));
228+ count++;
229+ }
230+/* IntIntMMapA_print(ima);*/
231+ assert(!IntIntMMapA_empty(ima));
232+ assert(IntIntMMapA_size(ima) == count);
233+ assert(count == SIZE);
234+ /* count */
235+ for (i = 0; i < SIZE; i++) {
236+ if (i < SIZE/2/2) {
237+ assert(IntIntMMapA_count(ima, i) == 1);
238+ } else if (i < SIZE/2) {
239+ assert(IntIntMMapA_count(ima, i) == 3);
240+ } else {
241+ assert(IntIntMMapA_count(ima, i) == 0);
242+ }
243+ }
244+ /* find, lower_bound, upper_bound */
245+ for (i = 0; i < SIZE; i++) {
246+ if (IntIntMMapA_count(ima, hoge_int[i]) == 1) {
247+ assert(pos[i] == IntIntMMapA_find(ima, hoge_int[i]));
248+ assert(pos[i] == IntIntMMapA_lower_bound(ima, hoge_int[i]));
249+ assert(pos[i] == IntIntMMapA_upper_bound(ima, hoge_int[i]-1));
250+ } else if (IntIntMMapA_count(ima, hoge_int[i]) == 3) {
251+ if (!flag[hoge_int[i]]) {
252+ flag[hoge_int[i]] = 1;
253+ assert(pos[i] == IntIntMMapA_lower_bound(ima, hoge_int[i]));
254+ assert(pos[i] == IntIntMMapA_upper_bound(ima, hoge_int[i]-1));
255+ }
256+ } else {
257+ assert(0);
258+ }
259+ assert(IntIntMMapA_lower_bound(ima, hoge_int[i]+1) == IntIntMMapA_upper_bound(ima, hoge_int[i]));
260+ }
261+ assert(IntIntMMapA_find(ima, IntIntMMapA_key(IntIntMMapA_begin(ima)) -1) == IntIntMMapA_end(ima));
262+ assert(IntIntMMapA_lower_bound(ima, IntIntMMapA_key(IntIntMMapA_rbegin(ima)) +1) == IntIntMMapA_end(ima));
263+ assert(IntIntMMapA_upper_bound(ima, IntIntMMapA_key(IntIntMMapA_rbegin(ima))) == IntIntMMapA_end(ima));
264+ /* begin, end, next, key, value */
265+ for (p = IntIntMMapA_begin(ima), i = 0; p != IntIntMMapA_end(ima); p = IntIntMMapA_next(p), i++) {
266+/* printf("%d, %d, %d\n", i, IntIntMMapA_key(p), *IntIntMMapA_value(p));*/
267+ }
268+ assert(i == SIZE);
269+ assert(IntIntMMapA_next(IntIntMMapA_rbegin(ima)) == IntIntMMapA_end(ima));
270+ /* rbegin, rend, prev, key, value */
271+ for (p = IntIntMMapA_rbegin(ima), i = SIZE -1; p != IntIntMMapA_rend(ima); p = IntIntMMapA_prev(p), i--) {
272+/* printf("%d, %d, %d\n", i, IntIntMMapA_key(p), *IntIntMMapA_value(ima, p));*/
273+ }
274+ assert(i == -1);
275+ assert(IntIntMMapA_prev(IntIntMMapA_begin(ima)) == IntIntMMapA_rend(ima));
276+ /* erase */
277+ for (i = 0; i < SIZE; i++) {
278+ IntIntMMapAIterator itr = IntIntMMapA_next(pos[i]);
279+ assert(itr == IntIntMMapA_erase(ima, pos[i]));
280+ count--;
281+ }
282+ assert(IntIntMMapA_empty(ima));
283+ assert(IntIntMMapA_size(ima) == 0);
284+ assert(count == 0);
285+ /* erase_range */
286+ for (i = 0; i < SIZE; i++) {
287+ pos[i] = IntIntMMapA_insert(ima, hoge_int[i], hoge_int[i]);
288+ assert(pos[i] && pos[i] != IntIntMMapA_end(ima));
289+ }
290+ assert(IntIntMMapA_size(ima) == SIZE);
291+ assert(IntIntMMapA_next(IntIntMMapA_find(ima, SIZE/2/2 -1)) == IntIntMMapA_erase_range(ima, IntIntMMapA_find(ima, 0), IntIntMMapA_next(IntIntMMapA_find(ima, SIZE/2/2 -1))));
292+ assert(IntIntMMapA_size(ima) == SIZE - SIZE/2/2);
293+ assert(IntIntMMapA_end(ima) == IntIntMMapA_erase_range(ima, IntIntMMapA_begin(ima), IntIntMMapA_end(ima)));
294+ assert(IntIntMMapA_size(ima) == 0);
295+ assert(IntIntMMapA_insert(ima, hoge_int[0], hoge_int[0]));
296+ assert(IntIntMMapA_size(ima) == 1);
297+ assert(IntIntMMapA_next(IntIntMMapA_begin(ima)) == IntIntMMapA_erase_range(ima, IntIntMMapA_begin(ima), IntIntMMapA_next(IntIntMMapA_begin(ima))));
298+ assert(IntIntMMapA_size(ima) == 0);
299+ assert(IntIntMMapA_insert(ima, 100, 100));
300+ assert(IntIntMMapA_insert(ima, 100, 100));
301+ assert(IntIntMMapA_insert(ima, 100, 100));
302+ assert(IntIntMMapA_insert(ima, 110, 110));
303+ assert(IntIntMMapA_insert(ima, 110, 110));
304+ assert(IntIntMMapA_size(ima) == 5);
305+ assert(IntIntMMapA_upper_bound(ima, 110) == IntIntMMapA_erase_range(ima, IntIntMMapA_lower_bound(ima, 100), IntIntMMapA_upper_bound(ima, 110)));
306+ assert(IntIntMMapA_size(ima) == 0);
307+ /* erase_key */
308+ for (i = 0; i < SIZE; i++) {
309+ pos[i] = IntIntMMapA_insert(ima, hoge_int[i], hoge_int[i]);
310+ assert(pos[i] && pos[i] != IntIntMMapA_end(ima));
311+ }
312+ assert(IntIntMMapA_size(ima) == SIZE);
313+ for (i = 0; i < SIZE/2/2; i++) {
314+ assert(IntIntMMapA_erase_key(ima, i) == 1);
315+ }
316+ assert(IntIntMMapA_size(ima) == SIZE - SIZE/2/2);
317+ for (i = SIZE/2/2; i < SIZE/2; i++) {
318+ assert(IntIntMMapA_erase_key(ima, i) == 3);
319+ }
320+ assert(IntIntMMapA_size(ima) == 0);
321+ /* 大量にinsert */
322+ count = 0;
323+ while (count < 1000000 && IntIntMMapA_insert(ima, 0, count)) {
324+ count++;
325+ }
326+ assert(IntIntMMapA_size(ima) == count);
327+ printf("count: %d\n", count);
328+
329+ IntIntMMapA_delete(ima);
330+}
331+
332+
333+
334+
335+
336+
337+
338+
339+
340+
341+
342+void MapTest_run(void)
343+{
344+ printf("\n===== map test =====\n");
345+ map_init_hoge();
346+
347+ MapTest_test_1_1();
348+ MapTest_test_1_2();
349+}
350+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/test.h (nonexistent)
+++ tags/rel-0.2.1/test/test.h (revision 43)
@@ -0,0 +1,79 @@
1+#ifndef TEST_H_INCLUDED
2+#define TEST_H_INCLUDED
3+
4+#include <stdio.h>
5+#include <string.h>
6+#include <assert.h>
7+#include "../cstl/vector.h"
8+#include "../cstl/deque.h"
9+#include "../cstl/list.h"
10+#include "../cstl/set.h"
11+#include "../cstl/map.h"
12+#include "rbtree_debug.h"
13+#include "heap.h"
14+#ifdef MY_MALLOC
15+extern Heap heap;
16+#define malloc(s) Heap_alloc(&heap, s)
17+#define realloc(p, s) Heap_realloc(&heap, p, s)
18+#define free(p) Heap_free(&heap, p)
19+#endif
20+
21+/* vector */
22+VECTOR_INTERFACE(UCharVector, unsigned char)
23+VECTOR_INTERFACE(IntVector, int)
24+
25+
26+/* deque */
27+DEQUE_INTERFACE(UCharDeque, unsigned char)
28+DEQUE_INTERFACE(IntDeque, int)
29+
30+
31+/* list */
32+typedef struct Hoge_t {
33+ char *key;
34+ int value;
35+} Hoge;
36+
37+LIST_INTERFACE(UCharList, unsigned char)
38+LIST_INTERFACE(IntList, int)
39+LIST_INTERFACE(StrList, char *)
40+LIST_INTERFACE(HogeList, Hoge)
41+
42+
43+/* set */
44+SET_INTERFACE(IntSetA, int)
45+SET_DEBUG_INTERFACE(IntSetA)
46+
47+SET_INTERFACE(IntSetD, int)
48+SET_DEBUG_INTERFACE(IntSetD)
49+
50+MULTISET_INTERFACE(IntMSetA, int)
51+SET_DEBUG_INTERFACE(IntMSetA)
52+
53+SET_INTERFACE(DoubleSetA, double)
54+SET_DEBUG_INTERFACE(DoubleSetA)
55+
56+SET_INTERFACE(PtrSetA, int*)
57+SET_DEBUG_INTERFACE(PtrSetA)
58+
59+SET_INTERFACE(StrSetA, char*)
60+SET_DEBUG_INTERFACE(StrSetA)
61+
62+SET_INTERFACE(UIntSetA, unsigned int)
63+SET_DEBUG_INTERFACE(UIntSetA)
64+
65+/* map */
66+MAP_INTERFACE(IntIntMapA, int, int)
67+MAP_DEBUG_INTERFACE(IntIntMapA)
68+
69+MULTIMAP_INTERFACE(IntIntMMapA, int, int)
70+MAP_DEBUG_INTERFACE(IntIntMMapA)
71+
72+
73+void VectorTest_run(void);
74+void DequeTest_run(void);
75+void ListTest_run(void);
76+void SetTest_run(void);
77+void MapTest_run(void);
78+
79+#endif /* TEST_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/heap.h (nonexistent)
+++ tags/rel-0.2.1/test/heap.h (revision 43)
@@ -0,0 +1,143 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file heap.h
27+ * \brief 動的メモリの確保と解放
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-03-22
30+ *
31+ * 必要ならば排他制御をすること。
32+ *
33+ * \par 使用例
34+ *
35+ * \code
36+ * #include "heap.h"
37+ * Heap heap;
38+ * double buf[256];
39+ * #define malloc(s) Heap_alloc(&heap, s)
40+ * #define realloc(p, s) Heap_realloc(&heap, p, s)
41+ * #define free(p) Heap_free(&heap, p)
42+ *
43+ * int main(void)
44+ * {
45+ * int i;
46+ * char *hoge;
47+ * Heap_init(&heap, buf, sizeof buf);
48+ * hoge = Heap_alloc(&heap, 16);
49+ * for (i = 0; i < 16; i++) {
50+ * hoge[i] = i;
51+ * }
52+ * Heap_free(&heap, hoge);
53+ *
54+ * hoge = malloc(64);
55+ * for (i = 0; i < 64; i++) {
56+ * hoge[i] = i;
57+ * }
58+ * hoge = realloc(hoge, 128);
59+ * for (i = 64; i < 128; i++) {
60+ * hoge[i] = i;
61+ * }
62+ * free(hoge);
63+ *
64+ * DUMP_MEMORY_LEAK(&heap, 1);
65+ * return 0;
66+ * }
67+ * \endcode
68+ */
69+#ifndef HEAP_H_INCLUDED
70+#define HEAP_H_INCLUDED
71+
72+#include <stddef.h>
73+
74+/*
75+ * SLIST_BLOCKをプリプロセッサシンボル定義した場合、メモリブロックのリストが
76+ * 単方向リストとなる。メモリブロックヘッダのサイズがポインタ一つ分小さくな
77+ * り、freeの計算量がO(n)になる。
78+ * SLIST_BLOCKを定義しない場合、メモリブロックのリストが双方向リストとなる。
79+ * メモリブロックヘッダのサイズがポインタ一つ分大きくなり、freeの計算量が
80+ * O(1)になる。
81+ */
82+/*#define SLIST_BLOCK*/
83+
84+/*!
85+ * \brief メモリブロックヘッダ構造体
86+ */
87+typedef struct BlockHeader_t BlockHeader;
88+struct BlockHeader_t {
89+#ifndef SLIST_BLOCK
90+ BlockHeader *prev;
91+#endif
92+ BlockHeader *next;
93+ size_t size; /* ブロックのサイズ(ヘッダのサイズも含む) */
94+ unsigned char occupied; /* 使用中フラグ */
95+ unsigned char magic; /* マジックナンバー */
96+#ifdef HEAP_DEBUG
97+ char *file;
98+ size_t line;
99+#endif
100+};
101+
102+/*!
103+ * \brief ヒープ構造体
104+ *
105+ * メモリブロックヘッダのリストを持つ
106+ */
107+typedef struct Heap_t Heap;
108+struct Heap_t {
109+ BlockHeader list_term; /* メモリブロックリストターミネータ */
110+ BlockHeader *loop_p; /* ループ用ポインタ */
111+ void *init_flag;
112+};
113+
114+#ifdef __cplusplus
115+extern "C" {
116+#endif
117+void Heap_init(Heap *self, double *buf, size_t size);
118+void Heap_free(Heap *self, void *ptr);
119+#ifdef HEAP_DEBUG
120+void *Heap_alloc_debug(Heap *self, size_t size, char *file, size_t line);
121+void *Heap_realloc_debug(Heap *self, void *ptr, size_t newsize, char *file, size_t line);
122+void hex_dump(void *buf, size_t size);
123+size_t dump_memory_leak(Heap *self, int dump);
124+void dump_memory_block(Heap *self, void *ptr);
125+void dump_memory_list(Heap *self);
126+#define Heap_alloc(h, s) Heap_alloc_debug(h, s, __FILE__, __LINE__)
127+#define Heap_realloc(h, p, s) Heap_realloc_debug(h, p, s, __FILE__, __LINE__)
128+#define DUMP_MEMORY_LEAK(h, d) dump_memory_leak(h, d)
129+#define DUMP_MEMORY_BLOCK(h, p) dump_memory_block(h, p)
130+#define DUMP_MEMORY_LIST(h) dump_memory_list(h)
131+#else
132+void *Heap_alloc(Heap *self, size_t size);
133+void *Heap_realloc(Heap *self, void *ptr, size_t newsize);
134+#define DUMP_MEMORY_LEAK(h, d)
135+#define DUMP_MEMORY_BLOCK(h, p)
136+#define DUMP_MEMORY_LIST(h)
137+#endif
138+#ifdef __cplusplus
139+}
140+#endif
141+
142+
143+#endif /* HEAP_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/heap.c (nonexistent)
+++ tags/rel-0.2.1/test/heap.c (revision 43)
@@ -0,0 +1,481 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file heap.c
27+ * \brief 動的メモリの確保と解放
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-03-22
30+ */
31+
32+#include <assert.h>
33+#include "heap.h"
34+
35+
36+/* アラインメントのサイズ。環境に合わせて変更してもよい。 */
37+#define ALIGN_SIZE (sizeof(double))
38+/* ALIGN_SIZE単位の切り上げ */
39+#define ALIGN_UP(s) (((s) + (ALIGN_SIZE - 1)) & (~(ALIGN_SIZE - 1)))
40+/* ALIGN_SIZE単位の切り捨て */
41+#define ALIGN_DOWN(s) ((s) & (~(ALIGN_SIZE - 1)))
42+
43+#define SPLIT_MIN (ALIGN_SIZE*4) /* 分割された空きブロックのヘッダサイズ分を除いた最小サイズ */
44+#define MAGIC_NO 0x17 /* マジックナンバー */
45+
46+/* ヘッダサイズ */
47+static const size_t HEADER_SIZE = ALIGN_UP(sizeof(BlockHeader));
48+
49+/*!
50+ * \brief ヒープの初期化
51+ * \param self ヒープへのポインタ
52+ * \param buf バッファへのポインタ
53+ * \param size バッファのサイズ(バイト数)
54+ */
55+void Heap_init(Heap *self, double *buf, size_t size)
56+{
57+ BlockHeader *p = (BlockHeader *) buf;
58+
59+ self->list_term.size = 0;
60+ self->list_term.occupied = 1;
61+ self->list_term.magic = ~MAGIC_NO;
62+ self->list_term.next = p;
63+#ifndef SLIST_BLOCK
64+ self->list_term.prev = p;
65+#endif
66+
67+ p->size = ALIGN_DOWN(size);
68+ p->occupied = 0;
69+ p->magic = MAGIC_NO;
70+ p->next = &self->list_term;
71+#ifndef SLIST_BLOCK
72+ p->prev = &self->list_term;
73+#endif
74+ self->loop_p = &self->list_term;
75+
76+ self->init_flag = self;
77+}
78+
79+/*!
80+ * \brief メモリ確保
81+ * \param self ヒープへのポインタ
82+ * \param size サイズ
83+ * \param file 呼び出し側のファイル名
84+ * \param line 呼び出し側のファイルの行
85+ * \return 割り当てたメモリへのポインタ。失敗すると0が返る。
86+ */
87+#ifdef HEAP_DEBUG
88+void *Heap_alloc_debug(Heap *self, size_t size, char *file, size_t line)
89+#else
90+void *Heap_alloc(Heap *self, size_t size)
91+#endif
92+{
93+ BlockHeader *p;
94+ BlockHeader *s;
95+ size_t alloc_block_size = ALIGN_UP(HEADER_SIZE + size);
96+
97+ if (self->init_flag != self) {
98+ assert(0);
99+ return 0;
100+ }
101+ for (p = self->loop_p->next; p != self->loop_p; p = p->next) {
102+ if (!p->occupied && p->size >= alloc_block_size) {
103+ if (p->size >= alloc_block_size + HEADER_SIZE + SPLIT_MIN) {
104+ s = (BlockHeader *) ((char *) p + alloc_block_size);
105+ s->size = p->size - alloc_block_size;
106+ s->occupied = 0;
107+ s->magic = MAGIC_NO;
108+ p->size = alloc_block_size;
109+ s->next = p->next;
110+#ifndef SLIST_BLOCK
111+ s->prev = p;
112+ p->next->prev = s;
113+#endif
114+ p->next = s;
115+ }
116+ p->occupied = 1;
117+#ifdef HEAP_DEBUG
118+ p->file = file;
119+ p->line = line;
120+#endif
121+ self->loop_p = p;
122+ return (char *) p + HEADER_SIZE;
123+ }
124+ }
125+ return 0;
126+}
127+
128+/*!
129+ * \brief 既に確保したメモリのサイズ変更
130+ * \param self ヒープへのポインタ
131+ * \param ptr 既に確保したメモリへのポインタ
132+ * \param newsize 新しいサイズ
133+ * \param file 呼び出し側のファイル名
134+ * \param line 呼び出し側のファイルの行
135+ * \return 新たに割り当てたメモリへのポインタ。失敗すると0が返る。
136+ */
137+#ifdef HEAP_DEBUG
138+void *Heap_realloc_debug(Heap *self, void *ptr, size_t newsize, char *file, size_t line)
139+#else
140+void *Heap_realloc(Heap *self, void *ptr, size_t newsize)
141+#endif
142+{
143+ BlockHeader *p;
144+ BlockHeader *s;
145+ size_t new_alloc_block_size;
146+ if (self->init_flag != self) {
147+ assert(0);
148+ return 0;
149+ }
150+ if (!ptr) {
151+#ifdef HEAP_DEBUG
152+ return Heap_alloc_debug(self, newsize, file, line);
153+#else
154+ return Heap_alloc(self, newsize);
155+#endif
156+ }
157+ if (!newsize) {
158+ Heap_free(self, ptr);
159+ return 0;
160+ }
161+ p = (BlockHeader *) ((char *) ptr - HEADER_SIZE);
162+ if (p->magic != MAGIC_NO || !p->occupied) return 0;
163+ new_alloc_block_size = ALIGN_UP(HEADER_SIZE + newsize);
164+ if (p->size >= new_alloc_block_size) {
165+ if (p->size >= 2 * new_alloc_block_size && new_alloc_block_size >= HEADER_SIZE + SPLIT_MIN) {
166+ /* 縮んだ分を空き領域として回収 */
167+ s = (BlockHeader *) ((char *) p + new_alloc_block_size);
168+ s->occupied = 0;
169+ s->magic = MAGIC_NO;
170+#ifndef SLIST_BLOCK
171+ s->prev = p;
172+#endif
173+ if (p->next->occupied) {
174+ s->size = p->size - new_alloc_block_size;
175+ s->next = p->next;
176+#ifndef SLIST_BLOCK
177+ p->next->prev = s;
178+#endif
179+ } else {
180+ if (self->loop_p == p->next) {
181+ self->loop_p = p->next->next;
182+ }
183+ s->size = p->size - new_alloc_block_size + p->next->size;
184+ s->next = p->next->next;
185+#ifndef SLIST_BLOCK
186+ p->next->next->prev = s;
187+#endif
188+ }
189+ p->size = new_alloc_block_size;
190+ p->next = s;
191+ }
192+ return ptr;
193+ }
194+ /* 以下、拡張 */
195+ if (p->next->occupied || p->size + p->next->size < new_alloc_block_size) {
196+ size_t i;
197+#ifdef HEAP_DEBUG
198+ void *newptr = Heap_alloc_debug(self, newsize, file, line);
199+#else
200+ void *newptr = Heap_alloc(self, newsize);
201+#endif
202+ if (!newptr) {
203+ return 0;
204+ }
205+ for (i = 0; i < p->size - HEADER_SIZE; i++) {
206+ ((char *) newptr)[i] = ((char *) ptr)[i];
207+ }
208+ Heap_free(self, ptr);
209+ return newptr;
210+ }
211+ if (self->loop_p == p->next) {
212+ self->loop_p = p->next->next;
213+ }
214+ if (p->size + p->next->size >= new_alloc_block_size + HEADER_SIZE + SPLIT_MIN) {
215+ BlockHeader tmp = *p->next;
216+ s = (BlockHeader *) ((char *) p + new_alloc_block_size);
217+ s->size = p->size + p->next->size - new_alloc_block_size;
218+ s->occupied = 0;
219+ s->magic = MAGIC_NO;
220+#ifndef SLIST_BLOCK
221+ s->prev = p;
222+#endif
223+ s->next = tmp.next;
224+ p->size = new_alloc_block_size;
225+#ifndef SLIST_BLOCK
226+ tmp.next->prev = s;
227+#endif
228+ p->next = s;
229+ } else {
230+ p->size = p->size + p->next->size;
231+#ifndef SLIST_BLOCK
232+ p->next->next->prev = p;
233+#endif
234+ p->next = p->next->next;
235+ }
236+#ifdef HEAP_DEBUG
237+ p->file = file;
238+ p->line = line;
239+#endif
240+ return ptr;
241+}
242+
243+/*!
244+ * \brief メモリ解放
245+ * \param self ヒープへのポインタ
246+ * \param ptr 解放されるメモリへのポインタ
247+ */
248+#ifdef SLIST_BLOCK
249+void Heap_free(Heap *self, void *ptr)
250+{
251+ BlockHeader *p;
252+ BlockHeader *prev;
253+ BlockHeader *pprev;
254+
255+ if (self->init_flag != self) {
256+ assert(0);
257+ return;
258+ }
259+ if (!ptr) {
260+ return;
261+ }
262+ prev = &self->list_term;
263+ pprev = &self->list_term;
264+ for (p = self->list_term.next; p != &self->list_term; p = p->next) {
265+ if (p == (BlockHeader *) ((char *) ptr - HEADER_SIZE)) {
266+ if (p->magic != MAGIC_NO || !p->occupied) {
267+ assert(0);
268+ return;
269+ }
270+ p->occupied = 0;
271+ if (p == self->loop_p) {
272+ self->loop_p = prev;
273+ }
274+ if (!prev->occupied) {
275+ prev->next = p->next;
276+ prev->size += p->size;
277+ p = prev;
278+ if (p == self->loop_p) {
279+ self->loop_p = pprev;
280+ }
281+ }
282+ if (!p->next->occupied) {
283+ p->size += p->next->size;
284+ p->next = p->next->next;
285+ }
286+ return;
287+ }
288+ pprev = prev;
289+ prev = p;
290+ }
291+ assert(0);
292+}
293+#else
294+void Heap_free(Heap *self, void *ptr)
295+{
296+ BlockHeader *p;
297+ BlockHeader *tmp;
298+
299+ if (self->init_flag != self) {
300+ assert(0);
301+ return;
302+ }
303+ if (!ptr) {
304+ return;
305+ }
306+ p = (BlockHeader *) ((char *) ptr - HEADER_SIZE);
307+ if (p->magic != MAGIC_NO || !p->occupied) {
308+ assert(0);
309+ return;
310+ }
311+ p->occupied = 0;
312+ if (p == self->loop_p) {
313+ self->loop_p = p->prev;
314+ }
315+ if (!p->prev->occupied) {
316+ tmp = p->prev;
317+ tmp->next = p->next;
318+ p->next->prev = tmp;
319+ tmp->size += p->size;
320+ p = tmp;
321+ if (p == self->loop_p) {
322+ self->loop_p = p->prev;
323+ }
324+ }
325+ if (!p->next->occupied) {
326+ tmp = p->next;
327+ p->next = tmp->next;
328+ tmp->next->prev = p;
329+ p->size += tmp->size;
330+ }
331+}
332+#endif
333+
334+
335+/* 以下、デバッグ用 */
336+#ifdef HEAP_DEBUG
337+#include <stdio.h>
338+#include <ctype.h>
339+/*!
340+ * \brief 16進ダンプ
341+ * \param buf バッファ
342+ * \param size バッファサイズ
343+ */
344+void hex_dump(void *buf, size_t size)
345+{
346+ size_t i;
347+ size_t j;
348+ unsigned char tmp[16];
349+ unsigned char *p;
350+ p = (unsigned char *) buf;
351+ for (i = 0; i < size; i++) {
352+ if (i%16 == 0) {
353+ printf("%p: ", &p[i]);
354+ }
355+ printf("%02x ", p[i]);
356+ tmp[i%16] = p[i];
357+ if (i == size -1) {
358+ for (j = i+1; j < i + (16 - i%16); j++) {
359+ tmp[j%16] = ' ';
360+ printf(" ");
361+ }
362+ i = j - 1;
363+ }
364+ if (i%16 == 15) {
365+ printf(" ");
366+ for (j = 0; j < 16; j++) {
367+ if (isprint(tmp[j])) {
368+ printf("%c", tmp[j]);
369+ } else {
370+ printf(".");
371+ }
372+ }
373+ printf("\n");
374+ }
375+ }
376+}
377+
378+/*!
379+ * \brief メモリリークを検出する
380+ * \param self ヒープへのポインタ
381+ * \param dump 真ならばリークをダンプする
382+ * \return メモリリークの検出個数
383+ */
384+size_t dump_memory_leak(Heap *self, int dump)
385+{
386+ BlockHeader *p;
387+ size_t n = 0;
388+ size_t total = 0;
389+ int flag = 0;
390+ if (self->init_flag != self) {
391+ assert(0);
392+ return 0;
393+ }
394+ for (p = self->list_term.next; p != &self->list_term; p = p->next) {
395+ if (p->occupied) {
396+ if (!flag) {
397+ printf("\ndetected memory leaks!\n");
398+ flag = 1;
399+ }
400+ if (dump || n < 10) {
401+ printf("%s(%d): ", p->file, p->line);
402+ printf("%d bytes\n", p->size - HEADER_SIZE);
403+ } else if (n == 10) {
404+ printf("...\n");
405+ }
406+ if (dump) {
407+ hex_dump((char *) p + HEADER_SIZE, p->size - HEADER_SIZE);
408+ }
409+ n++;
410+ total += p->size - HEADER_SIZE;
411+ }
412+ }
413+ printf("\ntotal memory leaks: %d bytes\n", total);
414+ return n;
415+}
416+
417+/*!
418+ * \brief メモリブロックをダンプする
419+ * \param self ヒープへのポインタ
420+ * \param ptr Heap_alloc()で取得したポインタ
421+ */
422+void dump_memory_block(Heap *self, void *ptr)
423+{
424+ BlockHeader *p;
425+
426+ if (self->init_flag != self) {
427+ assert(0);
428+ return;
429+ }
430+ if (!ptr) goto NG_block;
431+ p = (BlockHeader *) ((char *) ptr - HEADER_SIZE);
432+ if (p->magic != MAGIC_NO) goto NG_block;
433+
434+ if (p->occupied) {
435+ printf("%s(%d): ", p->file, p->line);
436+ } else {
437+ printf("*** free ***: ");
438+ }
439+ printf("block(%d bytes): alloc(%d bytes)\n"
440+ , p->size, p->size - HEADER_SIZE);
441+#ifdef SLIST_BLOCK
442+ printf("next(%p)\n", p->next);
443+#else
444+ printf("prev(%p): next(%p)\n", p->prev, p->next);
445+#endif
446+ hex_dump((char *) p, HEADER_SIZE);
447+ hex_dump((char *) p + HEADER_SIZE, p->size - HEADER_SIZE);
448+ printf("\n");
449+ return;
450+NG_block:
451+ printf("NG memory block: %p\n", p);
452+ return;
453+}
454+
455+/*!
456+ * \brief 全メモリブロックのリストをダンプする
457+ * \param self ヒープへのポインタ
458+ */
459+void dump_memory_list(Heap *self)
460+{
461+ BlockHeader *p;
462+ if (self->init_flag != self) {
463+ assert(0);
464+ return;
465+ }
466+ printf("\ndump_memory_list()\n");
467+ printf("block header size: %d bytes\n", HEADER_SIZE);
468+ printf("block list terminator\n");
469+#ifdef SLIST_BLOCK
470+ printf("next(%p)\n", self->list_term.next);
471+#else
472+ printf("prev(%p): next(%p)\n", self->list_term.prev, self->list_term.next);
473+#endif
474+ hex_dump((char *) (&self->list_term), HEADER_SIZE);
475+ printf("\n");
476+ for (p = self->list_term.next; p != &self->list_term; p = p->next) {
477+ dump_memory_block(self, (char *) p + HEADER_SIZE);
478+ }
479+}
480+#endif
481+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/main.c (nonexistent)
+++ tags/rel-0.2.1/test/main.c (revision 43)
@@ -0,0 +1,22 @@
1+#include "test.h"
2+
3+double buf[1024*1024/sizeof(double)];
4+Heap heap;
5+
6+int main(void)
7+{
8+#ifdef MY_MALLOC
9+ Heap_init(&heap, buf, sizeof buf);
10+#endif
11+
12+ VectorTest_run();
13+ DequeTest_run();
14+ ListTest_run();
15+ SetTest_run();
16+ MapTest_run();
17+
18+#ifdef MY_MALLOC
19+ DUMP_MEMORY_LEAK(&heap, 0);
20+#endif
21+ return 0;
22+}
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/test/rbtree_debug.h (nonexistent)
+++ tags/rel-0.2.1/test/rbtree_debug.h (revision 43)
@@ -0,0 +1,193 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file rbtree_debug.h
27+ * \brief set/mapデバッグ用
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-11-14
30+ *
31+ * 赤黒木の表示とベリファイ
32+ */
33+#ifndef CSTL_RBTREE_DEBUG_H_INCLUDED
34+#define CSTL_RBTREE_DEBUG_H_INCLUDED
35+
36+#include <stdio.h>
37+#include <limits.h>
38+
39+enum {
40+ VERBOSE = 0,
41+ VISUAL
42+};
43+
44+#define RBTREE_DEBUG_IMPLEMENT(Name, KeyType, ValueType, Compare, Order, format1, format2, map, value, visual) \
45+\
46+static size_t Name##_max_depth;\
47+static size_t Name##_min_depth;\
48+\
49+static char *Name##_str_color(RBTreeColor c)\
50+{\
51+ char *r = "(r)";\
52+ char *b = "(b)";\
53+ if (c == RBTREE_RED) {\
54+ return r;\
55+ } else if (c == RBTREE_BLACK) {\
56+ return b;\
57+ } else {\
58+ assert(0);\
59+ return 0;\
60+ }\
61+}\
62+\
63+static void Name##_p(Name##RBTreeNode *self, size_t depth)\
64+{\
65+ size_t i;\
66+ if (visual) {\
67+ for (i = 0; i < depth; i++) {\
68+ printf(" ");\
69+ }\
70+ if (map) {\
71+ printf("%s["#format1"]["#format2"]", Name##_str_color(self->color), self->key, self->value);\
72+ } else {\
73+ printf("%s["#format1"]", Name##_str_color(self->color), self->key);\
74+ }\
75+ } else {\
76+ if (map) {\
77+ printf("n[%p], p[%p], l[%p], r[%p], k["#format1"], v["#format2"], c[%s], d[%d]",\
78+ self, self->parent, self->left, self->right, self->key, self->value,\
79+ Name##_str_color(self->color), depth);\
80+ } else {\
81+ printf("n[%p], p[%p], l[%p], r[%p], v["#format1"], c[%s], d[%d]",\
82+ self, self->parent, self->left, self->right, self->key,\
83+ Name##_str_color(self->color), depth);\
84+ }\
85+ if (Name##RBTreeNode_is_root(self)) {\
86+ printf(", root");\
87+ } else if (Name##RBTreeNode_is_nil(self->left) && Name##RBTreeNode_is_nil(self->right)) {\
88+ printf(", leaf");\
89+ }\
90+ }\
91+ printf("\n");\
92+}\
93+\
94+static void Name##Node_print(Name##RBTreeNode *self)\
95+{\
96+ static size_t depth = 0;\
97+ if (Name##RBTreeNode_is_nil(self)) {\
98+ return;\
99+ }\
100+ depth++;\
101+ if (Name##_max_depth < depth) Name##_max_depth = depth;\
102+ if (Name##RBTreeNode_is_nil(self->right) && Name##RBTreeNode_is_nil(self->left)) {\
103+ if (Name##_min_depth > depth) Name##_min_depth = depth;\
104+ }\
105+ if (visual) {\
106+ Name##Node_print(self->right);\
107+ Name##_p(self, depth);\
108+ Name##Node_print(self->left);\
109+ } else {\
110+ Name##_p(self, depth);\
111+ Name##Node_print(self->left);\
112+ Name##Node_print(self->right);\
113+ }\
114+ depth--;\
115+}\
116+\
117+void Name##_print(Name *self)\
118+{\
119+ Name##RBTreeNode *root;\
120+ assert(Name##RBTreeNode_is_head(self->tree));\
121+ root = Name##RBTree_get_root(self->tree);\
122+ Name##_max_depth = 0;\
123+ Name##_min_depth = UINT_MAX;\
124+ Name##Node_print(root);\
125+ printf("min depth[%d], max depth[%d]\n", Name##_min_depth, Name##_max_depth);\
126+ printf("\n");\
127+}\
128+\
129+static size_t Name##RBTreeNode_black_count(Name##RBTreeNode *t, Name##RBTreeNode *root)\
130+{\
131+ size_t count;\
132+ if (Name##RBTreeNode_is_nil(t)) {\
133+ return 0;\
134+ }\
135+ count = t->color == RBTREE_BLACK ? 1 : 0;\
136+ if (t == root) {\
137+ return count;\
138+ } else {\
139+ return count + Name##RBTreeNode_black_count(t->parent, root);\
140+ }\
141+}\
142+\
143+int Name##_verify(Name *self)\
144+{\
145+ size_t len;\
146+ Name##RBTreeNode *l;\
147+ Name##RBTreeNode *r;\
148+ Name##Iterator pos;\
149+ Name##RBTree *tree = self->tree;\
150+ if (Name##RBTree_empty(tree) || Name##RBTree_begin(tree) == Name##RBTree_end(tree)) {\
151+ return Name##RBTree_empty(tree) && Name##RBTree_begin(tree) == Name##RBTree_end(tree) &&\
152+ tree->left == &Name##RBTree_nil && tree->right == &Name##RBTree_nil && tree->parent == &Name##RBTree_nil;\
153+ }\
154+ len = Name##RBTreeNode_black_count(Name##RBTree_begin(tree), Name##RBTree_get_root(tree));\
155+ for (pos = Name##RBTree_begin(tree); pos != Name##RBTree_end(tree); pos = Name##RBTree_next(pos)) {\
156+ l = pos->left;\
157+ r = pos->right;\
158+ if (pos->color == RBTREE_RED) {\
159+ if ((!Name##RBTreeNode_is_nil(l) && l->color == RBTREE_RED) ||\
160+ (!Name##RBTreeNode_is_nil(r) && r->color == RBTREE_RED)) {\
161+ return 0;\
162+ }\
163+ }\
164+ if (!Name##RBTreeNode_is_nil(l) && Name##RBTree_cmp(pos->key, l->key, Order) < 0) {\
165+ return 0;\
166+ }\
167+ if (!Name##RBTreeNode_is_nil(r) && Name##RBTree_cmp(r->key, pos->key, Order) < 0) {\
168+ return 0;\
169+ }\
170+ if (Name##RBTreeNode_is_nil(l) && Name##RBTreeNode_is_nil(r) &&\
171+ Name##RBTreeNode_black_count(pos, Name##RBTree_get_root(tree)) != len) {\
172+ return 0;\
173+ }\
174+ }\
175+ return 1;\
176+}\
177+\
178+
179+#define SET_DEBUG_INTERFACE(Name) \
180+void Name##_print(Name *self);\
181+int Name##_verify(Name *self);\
182+
183+#define MAP_DEBUG_INTERFACE(Name) \
184+void Name##_print(Name *self);\
185+int Name##_verify(Name *self);\
186+
187+#define SET_DEBUG_IMPLEMENT(Name, Type, Compare, Order, format, visual) \
188+RBTREE_DEBUG_IMPLEMENT(Name, Type, Type, Compare, Order, format, format, 0, key, visual) \
189+
190+#define MAP_DEBUG_IMPLEMENT(Name, KeyType, ValueType, Compare, Order, format1, format2, visual) \
191+RBTREE_DEBUG_IMPLEMENT(Name, KeyType, ValueType, Compare, Order, format1, format2, 1, value, visual) \
192+
193+#endif /* CSTL_RBTREE_DEBUG_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/sample/vector.c (nonexistent)
+++ tags/rel-0.2.1/sample/vector.c (revision 43)
@@ -0,0 +1,38 @@
1+#include <stdio.h>
2+#include <cstl/vector.h>
3+
4+VECTOR_INTERFACE(CharVector, char)
5+VECTOR_IMPLEMENT(CharVector, char)
6+
7+int main(void)
8+{
9+ CharVector *x = CharVector_new(32);
10+ CharVector_insert_array(x, 0, "aaaa", 4);
11+ CharVector_push_back(x, '\0');
12+ printf("%s\n", CharVector_at(x, 0));
13+ printf("size:%d\n", CharVector_size(x));
14+
15+ CharVector_pop_back(x);
16+ CharVector_insert_array(x, CharVector_size(x), "bbbb", 4);
17+ CharVector_push_back(x, '\0');
18+ printf("%s\n", CharVector_at(x, 0));
19+ printf("size:%d\n", CharVector_size(x));
20+
21+ *CharVector_at(x, 0) = 'c';
22+ *CharVector_at(x, 1) = 'd';
23+ *CharVector_at(x, 4) = 'e';
24+ *CharVector_at(x, 5) = 'f';
25+ printf("%s\n", CharVector_at(x, 0));
26+
27+ CharVector_erase(x, 2, 4);
28+ printf("%s\n", CharVector_at(x, 0));
29+
30+ CharVector_insert(x, 0, 'g');
31+ CharVector_insert(x, 0, 'h');
32+ CharVector_insert(x, 0, 'i');
33+ CharVector_insert_array(x, 0, "jklm", 4);
34+ printf("%s\n", CharVector_at(x, 0));
35+
36+ CharVector_delete(x);
37+ return 0;
38+}
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/sample/deque.c (nonexistent)
+++ tags/rel-0.2.1/sample/deque.c (revision 43)
@@ -0,0 +1,41 @@
1+#include <stdio.h>
2+#include <cstl/deque.h>
3+#define NELEMS(array) (sizeof(array) / sizeof(array[0]))
4+
5+DEQUE_INTERFACE(UCharDeque, unsigned char)
6+DEQUE_IMPLEMENT(UCharDeque, unsigned char)
7+
8+int main(void)
9+{
10+ int i;
11+ unsigned char buf[32];
12+ UCharDeque *x;
13+ UCharDeque y;
14+
15+ x = UCharDeque_new(31);
16+ UCharDeque_push_back(x, 0x01);
17+ UCharDeque_push_back(x, 0x02);
18+ UCharDeque_push_front(x, 0x03);
19+ UCharDeque_push_front(x, 0x04);
20+ UCharDeque_insert(x, 1, 0x05);
21+ UCharDeque_erase(x, 2, 1);
22+ *UCharDeque_at(x, 0) = 0x06;
23+ for (i = 0; i < UCharDeque_size(x); i++) {
24+ printf("%02x\n", *UCharDeque_at(x, i));
25+ }
26+ while (!UCharDeque_empty(x)) {
27+ printf("%02x\n", UCharDeque_pop_front(x));
28+ }
29+ UCharDeque_delete(x);
30+
31+ /* 呼び出し側で用意したオブジェクトとバッファを利用する場合 */
32+ UCharDeque_init(&y, buf, NELEMS(buf));
33+ UCharDeque_push_back(&y, 0x01);
34+ UCharDeque_push_back(&y, 0x02);
35+ while (!UCharDeque_empty(&y)) {
36+ printf("%02x\n", UCharDeque_pop_front(&y));
37+ }
38+ /* UCharDeque_delete(&y)を呼び出してはいけない */
39+ return 0;
40+}
41+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/sample/string1.c (nonexistent)
+++ tags/rel-0.2.1/sample/string1.c (revision 43)
@@ -0,0 +1,62 @@
1+/* C++標準ライブラリ チュートリアル&リファレンス
2+ * Nicolai M. Josuttis著, 吉川邦夫訳, アスキー, 2001
3+ * p.450の例を移植 */
4+#include <stdio.h>
5+#include <cstl/string.h>
6+
7+STRING_INTERFACE(String, char)
8+STRING_IMPLEMENT(String, char)
9+
10+/* コマンドライン引数に.tmpという拡張子をつける
11+ * 拡張子が付いていれば.tmpに変える
12+ * 最初から.tmpという拡張子ならば.xxxという拡張子をつける */
13+int main(int argc, char *argv[])
14+{
15+ int i;
16+ String *filename = String_new(16);
17+ String *basename = String_new(16);
18+ String *extname = String_new(16);
19+ String *tmpname = String_new(16);
20+ String *suffix = String_new(8);
21+ String_assign(suffix, "tmp", NPOS);
22+
23+ for (i = 1; i < argc; i++) {
24+ size_t idx;
25+ /* コマンドライン引数をファイル名とする */
26+ String_assign(filename, argv[i], NPOS);
27+ /* ファイル名から'.'を探す */
28+ idx = String_find_c(String_c_str(filename), '.', 0);
29+ if (idx == NPOS) {
30+ /* ファイル名に'.'が含まれない
31+ * ".tmp"を追加 */
32+ String_assign(tmpname, String_c_str(filename), NPOS);
33+ String_push_back(tmpname, '.');
34+ String_append(tmpname, String_c_str(suffix), NPOS);
35+ } else {
36+ /* ファイル名を基本名と拡張子に分ける */
37+ String_assign(basename, String_c_str(filename), idx);
38+ String_assign(extname, String_c_str(filename) + idx+1, NPOS);
39+ if (String_empty(extname)) {
40+ /* '.'のあとに拡張子がない場合、"tmp"を付ける */
41+ String_assign(tmpname, String_c_str(filename), NPOS);
42+ String_append(tmpname, String_c_str(suffix), NPOS);
43+ } else if (String_compare(extname, suffix) == 0) {
44+ /* "tmp"を"xxx"に置換 */
45+ String_assign(tmpname, String_c_str(filename), NPOS);
46+ String_replace(tmpname, idx+1, String_size(extname), "xxx", NPOS);
47+ } else {
48+ /* その他の拡張子を"tmp"に置換 */
49+ String_assign(tmpname, String_c_str(filename), NPOS);
50+ String_replace(tmpname, idx+1, NPOS, String_c_str(suffix), NPOS);
51+ }
52+ }
53+ printf("%s => %s\n", String_c_str(filename), String_c_str(tmpname));
54+ }
55+
56+ String_delete(filename);
57+ String_delete(basename);
58+ String_delete(extname);
59+ String_delete(tmpname);
60+ String_delete(suffix);
61+ return 0;
62+}
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/sample/multimap.c (nonexistent)
+++ tags/rel-0.2.1/sample/multimap.c (revision 43)
@@ -0,0 +1,55 @@
1+/* C++標準ライブラリ チュートリアル&リファレンス
2+ * Nicolai M. Josuttis著, 吉川邦夫訳, アスキー, 2001
3+ * p.212の例を移植 */
4+#include <stdio.h>
5+#include <string.h>
6+#include <cstl/map.h>
7+
8+MULTIMAP_INTERFACE(StrStrMMap, char *, char *)
9+MULTIMAP_IMPLEMENT(StrStrMMap, char *, char *, strcmp, ASC)
10+
11+/* 辞書として使う */
12+int main(void)
13+{
14+ StrStrMMapIterator pos;
15+ StrStrMMap *dict = StrStrMMap_new();
16+ char *word;
17+
18+ StrStrMMap_insert(dict , "day" , "Tag");
19+ StrStrMMap_insert(dict , "strange" , "fremd");
20+ StrStrMMap_insert(dict , "car" , "Auto");
21+ StrStrMMap_insert(dict , "smart" , "elegant");
22+ StrStrMMap_insert(dict , "trait" , "Merkmal");
23+ StrStrMMap_insert(dict , "strange" , "seltsam");
24+ StrStrMMap_insert(dict , "smart" , "raffiniert");
25+ StrStrMMap_insert(dict , "smart" , "klug");
26+ StrStrMMap_insert(dict , "clever" , "raffiniert");
27+
28+ /* すべての要素を出力する */
29+ printf("english\t\tgerman\n");
30+ printf("-------------------------------\n");
31+ for (pos = StrStrMMap_begin(dict); pos != StrStrMMap_end(dict); pos = StrStrMMap_next(pos)) {
32+ printf("%s\t\t%s\n", StrStrMMap_key(pos), *StrStrMMap_value(pos));
33+ }
34+ printf("\n");
35+
36+ /* "smart"というキーに対応するすべての値を出力する */
37+ word = "smart";
38+ printf("%s: \n", word);
39+ for (pos = StrStrMMap_lower_bound(dict, word); pos != StrStrMMap_upper_bound(dict, word); pos = StrStrMMap_next(pos)) {
40+ printf("\t%s\n", *StrStrMMap_value(pos));
41+ }
42+ printf("\n");
43+
44+ /* "raffiniert"という値に対応するすべてのキーを出力する */
45+ word = "raffiniert";
46+ printf("%s: \n", word);
47+ for (pos = StrStrMMap_begin(dict); pos != StrStrMMap_end(dict); pos = StrStrMMap_next(pos)) {
48+ if (strcmp(word, *StrStrMMap_value(pos)) == 0) {
49+ printf("\t%s\n", StrStrMMap_key(pos));
50+ }
51+ }
52+
53+ StrStrMMap_delete(dict);
54+ return 0;
55+}
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/sample/string2.c (nonexistent)
+++ tags/rel-0.2.1/sample/string2.c (revision 43)
@@ -0,0 +1,47 @@
1+/* C++標準ライブラリ チュートリアル&リファレンス
2+ * Nicolai M. Josuttis著, 吉川邦夫訳, アスキー, 2001
3+ * p.454の例を移植 */
4+#include <stdio.h>
5+#include <cstl/string.h>
6+
7+STRING_INTERFACE(String, char)
8+STRING_IMPLEMENT(String, char)
9+
10+/* 標準入力から個々の単語を抽出し、それぞれの文字を逆順に出力する */
11+int main(int argc, char *argv[])
12+{
13+ char buf[1024];
14+ String *line = String_new(1024);
15+ /* 区切り文字 */
16+ String *delims = String_new(8);
17+ String_assign(delims, " \t,.;", NPOS);
18+
19+ while (fgets(buf, 1024, stdin)) {
20+ size_t begIdx, endIdx;
21+ String_assign(line, buf, NPOS);
22+ String_erase(line, String_length(line)-1, NPOS); /* '\n'を取り除く */
23+ /* 最初の単語の先頭を検索する */
24+ begIdx = String_find_first_not_of(String_c_str(line), String_c_str(delims), 0, NPOS);
25+ /* 単語の先頭が見つかるまで繰り返す */
26+ while (begIdx != NPOS) {
27+ size_t i;
28+ /* 単語の末尾を検索する */
29+ endIdx = String_find_first_of(String_c_str(line), String_c_str(delims), begIdx, NPOS);
30+ if (endIdx == NPOS) {
31+ /* 単語の終わりは行末である */
32+ endIdx = String_length(line);
33+ }
34+ /* 単語の文字を逆順に出力する */
35+ for (i = endIdx; i > begIdx; --i) {
36+ putchar(*String_at(line, i-1));
37+ }
38+ putchar(' ');
39+ /* 次の単語の先頭を検索する */
40+ begIdx = String_find_first_not_of(String_c_str(line), String_c_str(delims), endIdx, NPOS);
41+ }
42+ putchar('\n');
43+ }
44+ String_delete(line);
45+ String_delete(delims);
46+ return 0;
47+}
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/sample/list.c (nonexistent)
+++ tags/rel-0.2.1/sample/list.c (revision 43)
@@ -0,0 +1,73 @@
1+#include <stdlib.h>
2+#include <stdio.h>
3+#include <string.h>
4+#include <cstl/list.h>
5+
6+typedef struct Hoge_t {
7+ char *key;
8+ int value;
9+} Hoge;
10+
11+Hoge *Hoge_new(char *key, int value)
12+{
13+ Hoge *self = malloc(sizeof(Hoge));
14+ self->key = key; self->value = value;
15+ return self;
16+}
17+
18+void Hoge_delete(Hoge *self)
19+{
20+ free(self);
21+}
22+
23+void Hoge_print(Hoge *self)
24+{
25+ printf("%s: %d\n", self->key, self->value);
26+}
27+
28+LIST_INTERFACE(HogeList, Hoge *)
29+LIST_IMPLEMENT(HogeList, Hoge *)
30+
31+int main(void)
32+{
33+ Hoge *hoge;
34+ HogeListIterator pos;
35+ HogeList *x = HogeList_new();
36+
37+ hoge = Hoge_new("aaa", 1);
38+ HogeList_push_back(x, hoge);
39+ hoge = Hoge_new("bbb", 2);
40+ HogeList_push_back(x, hoge);
41+ hoge = Hoge_new("ccc", 3);
42+ HogeList_push_front(x, hoge);
43+ hoge = Hoge_new("ddd", 4);
44+ HogeList_push_front(x, hoge);
45+
46+ for (pos = HogeList_begin(x); pos != HogeList_end(x); pos = HogeList_next(pos)) {
47+ Hoge_print(*HogeList_at(pos));
48+ }
49+ for (pos = HogeList_begin(x); pos != HogeList_end(x); pos = HogeList_next(pos)) {
50+ Hoge *h = *HogeList_at(pos);
51+ if (!strcmp(h->key, "bbb")) {
52+ hoge = Hoge_new("eee", 5);
53+ HogeList_insert(x, pos, hoge);
54+ }
55+ }
56+ for (pos = HogeList_begin(x); pos != HogeList_end(x);) {
57+ Hoge *h = *HogeList_at(pos);
58+ if (!strcmp(h->key, "bbb")) {
59+ pos = HogeList_erase(x, pos);
60+ Hoge_delete(h);
61+ } else {
62+ pos = HogeList_next(pos);
63+ }
64+ }
65+ while (!HogeList_empty(x)) {
66+ Hoge *h = HogeList_pop_front(x);
67+ Hoge_print(h);
68+ Hoge_delete(h);
69+ }
70+ HogeList_delete(x);
71+ return 0;
72+}
73+
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/sample/set.c (nonexistent)
+++ tags/rel-0.2.1/sample/set.c (revision 43)
@@ -0,0 +1,49 @@
1+#include <stdio.h>
2+#include <cstl/set.h>
3+
4+SET_INTERFACE(IntSet, int)
5+SET_IMPLEMENT(IntSet, int, SIMPLE_CMP, ASC)
6+
7+int main(void)
8+{
9+ int success;
10+ IntSetIterator pos;
11+ IntSet *x = IntSet_new();
12+
13+ IntSet_insert(x, 4, NULL);
14+ IntSet_insert(x, 3, NULL);
15+ IntSet_insert(x, 5, NULL);
16+ IntSet_insert(x, 1, NULL);
17+ IntSet_insert(x, 6, NULL);
18+ IntSet_insert(x, 2, NULL);
19+ IntSet_insert(x, 5, NULL);
20+ IntSet_insert(x, 8, NULL);
21+ IntSet_insert(x, 7, NULL);
22+
23+ for (pos = IntSet_begin(x); pos != IntSet_end(x); pos = IntSet_next(pos)) {
24+ printf("%d ", IntSet_key(pos));
25+ }
26+ printf("\n");
27+
28+ IntSet_insert(x, 4, &success);
29+ if (success) {
30+ printf("4 inserted as element\n");
31+ } else {
32+ printf("4 already exists\n");
33+ }
34+
35+ IntSet_erase_range(x, IntSet_begin(x), IntSet_find(x, 3));
36+ IntSet_erase_key(x, 5);
37+
38+ for (pos = IntSet_begin(x); pos != IntSet_end(x); pos = IntSet_next(pos)) {
39+ printf("%d ", IntSet_key(pos));
40+ }
41+ printf("\n");
42+
43+ printf("lower_bound(5): %d\n", IntSet_key(IntSet_lower_bound(x, 5)));
44+ printf("upper_bound(5): %d\n", IntSet_key(IntSet_upper_bound(x, 5)));
45+ printf("lower_bound(6): %d\n", IntSet_key(IntSet_lower_bound(x, 6)));
46+ printf("upper_bound(6): %d\n", IntSet_key(IntSet_upper_bound(x, 6)));
47+ IntSet_delete(x);
48+ return 0;
49+}
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/sample/map.c (nonexistent)
+++ tags/rel-0.2.1/sample/map.c (revision 43)
@@ -0,0 +1,53 @@
1+/* C++標準ライブラリ チュートリアル&リファレンス
2+ * Nicolai M. Josuttis著, 吉川邦夫訳, アスキー, 2001
3+ * p.211の例を移植 */
4+#include <stdio.h>
5+#include <string.h>
6+#include <cstl/map.h>
7+
8+MAP_INTERFACE(StrFltMap, char *, float)
9+MAP_IMPLEMENT(StrFltMap, char *, float, strcmp, ASC)
10+
11+/* 連想配列として使う */
12+int main(void)
13+{
14+ StrFltMapIterator pos;
15+ StrFltMap *stocks = StrFltMap_new();
16+
17+ /* 要素の挿入 */
18+ *StrFltMap_lookup(stocks, "BASF") = 369.50;
19+ *StrFltMap_lookup(stocks, "VW") = 413.50;
20+ *StrFltMap_lookup(stocks, "Daimler") = 819.00;
21+ *StrFltMap_lookup(stocks, "BMW") = 834.00;
22+ *StrFltMap_lookup(stocks, "Siemens") = 842.20;
23+
24+ /* すべての要素を出力する */
25+ for (pos = StrFltMap_begin(stocks); pos != StrFltMap_end(stocks); pos = StrFltMap_next(pos)) {
26+ printf("stock: %s\tprice: %g\n", StrFltMap_key(pos), *StrFltMap_value(pos));
27+ }
28+ printf("\n");
29+
30+ /* 値が倍になった */
31+ for (pos = StrFltMap_begin(stocks); pos != StrFltMap_end(stocks); pos = StrFltMap_next(pos)) {
32+ *StrFltMap_value(pos) *= 2;
33+ }
34+ /* すべての要素を出力する */
35+ for (pos = StrFltMap_begin(stocks); pos != StrFltMap_end(stocks); pos = StrFltMap_next(pos)) {
36+ printf("stock: %s\tprice: %g\n", StrFltMap_key(pos), *StrFltMap_value(pos));
37+ }
38+ printf("\n");
39+
40+ /* キーの"VW"を"Volkswagen"に変更する
41+ * キーは変更不可なので値をコピーして古いキーを削除するしかない */
42+ *StrFltMap_lookup(stocks, "Volkswagen") = *StrFltMap_lookup(stocks, "VW");
43+ StrFltMap_erase_key(stocks, "VW");
44+
45+ /* すべての要素を出力する */
46+ for (pos = StrFltMap_begin(stocks); pos != StrFltMap_end(stocks); pos = StrFltMap_next(pos)) {
47+ printf("stock: %s\tprice: %g\n", StrFltMap_key(pos), *StrFltMap_value(pos));
48+ }
49+ printf("\n");
50+
51+ StrFltMap_delete(stocks);
52+ return 0;
53+}
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/cstl/list.h (nonexistent)
+++ tags/rel-0.2.1/cstl/list.h (revision 43)
@@ -0,0 +1,508 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file list.h
27+ * \brief listコンテナ
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-02-26
30+ */
31+#ifndef CSTL_LIST_H_INCLUDED
32+#define CSTL_LIST_H_INCLUDED
33+
34+#include <stddef.h>
35+#include <stdlib.h>
36+#include <assert.h>
37+
38+#ifdef __cplusplus
39+#define LIST_BEGIN_EXTERN_C() extern "C" {
40+#define LIST_END_EXTERN_C() }
41+#else
42+#define LIST_BEGIN_EXTERN_C()
43+#define LIST_END_EXTERN_C()
44+#endif
45+
46+#ifndef NDEBUG
47+#define LIST_MAGIC(x) x
48+#else
49+#define LIST_MAGIC(x)
50+#endif
51+
52+/*!
53+ * \brief インターフェイスマクロ
54+ *
55+ * \param Name コンテナ名
56+ * \param Type 要素の型
57+ */
58+#define LIST_INTERFACE(Name, Type) \
59+typedef struct Name##_t Name;\
60+/*!
61+ * \brief イテレータ
62+ */\
63+typedef struct Name##Node_t *Name##Iterator;\
64+\
65+LIST_BEGIN_EXTERN_C()\
66+Name *Name##_new(void);\
67+void Name##_delete(Name *self);\
68+int Name##_assign(Name *self, Name##Iterator first, Name##Iterator last);\
69+int Name##_push_back(Name *self, Type elem);\
70+int Name##_push_front(Name *self, Type elem);\
71+Type Name##_pop_front(Name *self);\
72+Type Name##_pop_back(Name *self);\
73+int Name##_empty(Name *self);\
74+size_t Name##_size(Name *self);\
75+void Name##_clear(Name *self);\
76+Type *Name##_at(Name##Iterator pos);\
77+Type Name##_front(Name *self);\
78+Type Name##_back(Name *self);\
79+Name##Iterator Name##_begin(Name *self);\
80+Name##Iterator Name##_end(Name *self);\
81+Name##Iterator Name##_rbegin(Name *self);\
82+Name##Iterator Name##_rend(Name *self);\
83+Name##Iterator Name##_next(Name##Iterator pos);\
84+Name##Iterator Name##_prev(Name##Iterator pos);\
85+Name##Iterator Name##_insert(Name *self, Name##Iterator pos, Type elem);\
86+int Name##_insert_array(Name *self, Name##Iterator pos, Type *elems, size_t n);\
87+int Name##_insert_range(Name *self, Name##Iterator pos, Name##Iterator first, Name##Iterator last);\
88+Name##Iterator Name##_erase(Name *self, Name##Iterator pos);\
89+Name##Iterator Name##_erase_range(Name *self, Name##Iterator first, Name##Iterator last);\
90+int Name##_resize(Name *self, size_t n, Type elem);\
91+void Name##_swap(Name *self, Name *x);\
92+void Name##_splice(Name *self, Name##Iterator pos, Name *x, Name##Iterator first, Name##Iterator last);\
93+LIST_END_EXTERN_C()\
94+
95+
96+/*!
97+ * \brief 実装マクロ
98+ *
99+ * \param Name コンテナ名
100+ * \param Type 要素の型
101+ */
102+#define LIST_IMPLEMENT(Name, Type) \
103+typedef struct Name##Node_t Name##Node;\
104+/*!
105+ * \brief listノード構造体
106+ */\
107+struct Name##Node_t {\
108+ Name##Node *prev;\
109+ Name##Node *next;\
110+ Type elem;\
111+ LIST_MAGIC(void *magic;)\
112+};\
113+\
114+/*!
115+ * \brief list構造体
116+ */\
117+struct Name##_t {\
118+ Name##Node *terminator;\
119+ size_t nelems;\
120+ LIST_MAGIC(void *magic;)\
121+};\
122+\
123+Name *Name##_new(void)\
124+{\
125+ Name *self = (Name *) malloc(sizeof(Name));\
126+ if (!self) return 0;\
127+ self->terminator = (Name##Node *) malloc(sizeof(Name##Node));\
128+ if (!self->terminator) {\
129+ free(self);\
130+ return 0;\
131+ }\
132+ self->terminator->next = self->terminator;\
133+ self->terminator->prev = self->terminator;\
134+ self->nelems = 0;\
135+ LIST_MAGIC(self->terminator->magic = self->terminator;)\
136+ LIST_MAGIC(self->magic = self;)\
137+ return self;\
138+}\
139+\
140+void Name##_delete(Name *self)\
141+{\
142+ assert(self && "List_delete");\
143+ assert(self->magic == self && "List_delete");\
144+ Name##_clear(self);\
145+ LIST_MAGIC(self->terminator->magic = 0;)\
146+ LIST_MAGIC(self->magic = 0;)\
147+ free(self->terminator);\
148+ free(self);\
149+}\
150+\
151+int Name##_assign(Name *self, Name##Iterator first, Name##Iterator last)\
152+{\
153+ Name *x;\
154+ Name##Iterator pos;\
155+ assert(self && "List_assign");\
156+ assert(self->magic == self && "List_assign");\
157+ assert(first && "List_assign");\
158+ assert(last && "List_assign");\
159+ assert(first->magic && "List_assign");\
160+ assert(last->magic && "List_assign");\
161+ x = Name##_new();\
162+ if (!x) return 0;\
163+ for (pos = first; pos != last; pos = Name##_next(pos)) {\
164+ if (!Name##_push_back(x, *Name##_at(pos))) {\
165+ Name##_delete(x);\
166+ return 0;\
167+ }\
168+ }\
169+ Name##_swap(self, x);\
170+ Name##_delete(x);\
171+ return 1;\
172+}\
173+\
174+int Name##_push_back(Name *self, Type elem)\
175+{\
176+ Name##Node *node;\
177+ assert(self && "List_push_back");\
178+ assert(self->magic == self && "List_push_back");\
179+ node = (Name##Node *) malloc(sizeof(Name##Node));\
180+ if (!node) return 0;\
181+ node->elem = elem;\
182+ node->next = self->terminator;\
183+ node->prev = self->terminator->prev;\
184+ self->terminator->prev->next = node;\
185+ self->terminator->prev = node;\
186+ self->nelems++;\
187+ LIST_MAGIC(node->magic = self->terminator;)\
188+ return 1;\
189+}\
190+\
191+int Name##_push_front(Name *self, Type elem)\
192+{\
193+ Name##Node *node;\
194+ assert(self && "List_push_front");\
195+ assert(self->magic == self && "List_push_front");\
196+ node = (Name##Node *) malloc(sizeof(Name##Node));\
197+ if (!node) return 0;\
198+ node->elem = elem;\
199+ node->next = self->terminator->next;\
200+ node->prev = self->terminator;\
201+ self->terminator->next->prev = node;\
202+ self->terminator->next = node;\
203+ self->nelems++;\
204+ LIST_MAGIC(node->magic = self->terminator;)\
205+ return 1;\
206+}\
207+\
208+Type Name##_pop_front(Name *self)\
209+{\
210+ Name##Node *node;\
211+ Type elem;\
212+ assert(self && "List_pop_front");\
213+ assert(self->magic == self && "List_pop_front");\
214+ assert(!Name##_empty(self) && "List_pop_front");\
215+ node = self->terminator->next;\
216+ elem = node->elem;\
217+ self->terminator->next = node->next;\
218+ node->next->prev = self->terminator;\
219+ LIST_MAGIC(node->magic = 0;)\
220+ free(node);\
221+ self->nelems--;\
222+ return elem;\
223+}\
224+\
225+Type Name##_pop_back(Name *self)\
226+{\
227+ Name##Node *node;\
228+ Type elem;\
229+ assert(self && "List_pop_back");\
230+ assert(self->magic == self && "List_pop_back");\
231+ assert(!Name##_empty(self) && "List_pop_back");\
232+ node = self->terminator->prev;\
233+ elem = node->elem;\
234+ self->terminator->prev = node->prev;\
235+ node->prev->next = self->terminator;\
236+ LIST_MAGIC(node->magic = 0;)\
237+ free(node);\
238+ self->nelems--;\
239+ return elem;\
240+}\
241+\
242+int Name##_empty(Name *self)\
243+{\
244+ assert(self && "List_empty");\
245+ assert(self->magic == self && "List_empty");\
246+ return self->terminator->next == self->terminator;\
247+}\
248+\
249+size_t Name##_size(Name *self)\
250+{\
251+ assert(self && "List_size");\
252+ assert(self->magic == self && "List_size");\
253+ return self->nelems;\
254+}\
255+\
256+void Name##_clear(Name *self)\
257+{\
258+ assert(self && "List_clear");\
259+ assert(self->magic == self && "List_clear");\
260+ Name##_erase_range(self, Name##_begin(self), Name##_end(self));\
261+}\
262+\
263+Type *Name##_at(Name##Iterator pos)\
264+{\
265+ assert(pos && "List_at");\
266+ assert(pos->magic && "List_at");\
267+ return &pos->elem;\
268+}\
269+\
270+Type Name##_front(Name *self)\
271+{\
272+ assert(self && "List_front");\
273+ assert(self->magic == self && "List_front");\
274+ assert(!Name##_empty(self) && "List_front");\
275+ return self->terminator->next->elem;\
276+}\
277+\
278+Type Name##_back(Name *self)\
279+{\
280+ assert(self && "List_back");\
281+ assert(self->magic == self && "List_back");\
282+ assert(!Name##_empty(self) && "List_back");\
283+ return self->terminator->prev->elem;\
284+}\
285+\
286+Name##Iterator Name##_begin(Name *self)\
287+{\
288+ assert(self && "List_begin");\
289+ assert(self->magic == self && "List_begin");\
290+ return self->terminator->next;\
291+}\
292+\
293+Name##Iterator Name##_end(Name *self)\
294+{\
295+ assert(self && "List_end");\
296+ assert(self->magic == self && "List_end");\
297+ return self->terminator;\
298+}\
299+\
300+Name##Iterator Name##_rbegin(Name *self)\
301+{\
302+ assert(self && "List_rbegin");\
303+ assert(self->magic == self && "List_rbegin");\
304+ return self->terminator->prev;\
305+}\
306+\
307+Name##Iterator Name##_rend(Name *self)\
308+{\
309+ assert(self && "List_rend");\
310+ assert(self->magic == self && "List_rend");\
311+ return self->terminator;\
312+}\
313+\
314+Name##Iterator Name##_next(Name##Iterator pos)\
315+{\
316+ assert(pos && "List_next");\
317+ assert(pos->magic && "List_next");\
318+ return pos->next;\
319+}\
320+\
321+Name##Iterator Name##_prev(Name##Iterator pos)\
322+{\
323+ assert(pos && "List_prev");\
324+ assert(pos->magic && "List_prev");\
325+ return pos->prev;\
326+}\
327+\
328+Name##Iterator Name##_insert(Name *self, Name##Iterator pos, Type elem)\
329+{\
330+ Name##Node *node;\
331+ assert(self && "List_insert");\
332+ assert(self->magic == self && "List_insert");\
333+ assert(pos && "List_insert");\
334+ assert(pos->magic == self->terminator && "List_insert");\
335+ node = (Name##Node *) malloc(sizeof(Name##Node));\
336+ if (!node) return 0;\
337+ node->elem = elem;\
338+ node->next = pos;\
339+ node->prev = pos->prev;\
340+ pos->prev = node;\
341+ node->prev->next = node;\
342+ self->nelems++;\
343+ LIST_MAGIC(node->magic = self->terminator;)\
344+ return node;\
345+}\
346+\
347+int Name##_insert_array(Name *self, Name##Iterator pos, Type *elems, size_t n)\
348+{\
349+ Name *x;\
350+ size_t i;\
351+ assert(self && "List_insert_array");\
352+ assert(self->magic == self && "List_insert_array");\
353+ assert(pos && "List_insert_array");\
354+ assert(pos->magic == self->terminator && "List_insert_array");\
355+ assert(elems && "List_insert_array");\
356+ x = Name##_new();\
357+ if (!x) return 0;\
358+ for (i = 0; i < n; i++) {\
359+ if (!Name##_push_back(x, elems[i])) {\
360+ Name##_delete(x);\
361+ return 0;\
362+ }\
363+ }\
364+ Name##_splice(self, pos, x, Name##_begin(x), Name##_end(x));\
365+ Name##_delete(x);\
366+ return 1;\
367+}\
368+\
369+int Name##_insert_range(Name *self, Name##Iterator pos, Name##Iterator first, Name##Iterator last)\
370+{\
371+ Name *x;\
372+ Name##Iterator i;\
373+ assert(self && "List_insert_range");\
374+ assert(self->magic == self && "List_insert_range");\
375+ assert(pos && "List_insert_range");\
376+ assert(pos->magic == self->terminator && "List_insert_range");\
377+ assert(first && "List_insert_range");\
378+ assert(last && "List_insert_range");\
379+ assert(first->magic && "List_insert_range");\
380+ assert(last->magic && "List_insert_range");\
381+ x = Name##_new();\
382+ if (!x) return 0;\
383+ for (i = first; i != last; i = Name##_next(i)) {\
384+ assert(i->magic && "List_insert_range");\
385+ if (!Name##_push_back(x, *Name##_at(i))) {\
386+ Name##_delete(x);\
387+ return 0;\
388+ }\
389+ }\
390+ Name##_splice(self, pos, x, Name##_begin(x), Name##_end(x));\
391+ Name##_delete(x);\
392+ return 1;\
393+}\
394+\
395+Name##Iterator Name##_erase(Name *self, Name##Iterator pos)\
396+{\
397+ Name##Node *node;\
398+ assert(self && "List_erase");\
399+ assert(self->magic == self && "List_erase");\
400+ assert(pos && "List_erase");\
401+ assert(pos != self->terminator && "List_erase");\
402+ assert(pos->magic == self->terminator && "List_erase");\
403+ assert(!Name##_empty(self) && "List_erase");\
404+ node = pos->next;\
405+ pos->prev->next = pos->next;\
406+ pos->next->prev = pos->prev;\
407+ LIST_MAGIC(pos->magic = 0;)\
408+ free(pos);\
409+ self->nelems--;\
410+ return node;\
411+}\
412+\
413+Name##Iterator Name##_erase_range(Name *self, Name##Iterator first, Name##Iterator last)\
414+{\
415+ Name##Iterator pos;\
416+ assert(self && "List_erase_range");\
417+ assert(self->magic == self && "List_erase_range");\
418+ assert(first && "List_erase_range");\
419+ assert(last && "List_erase_range");\
420+ assert(first->magic == self->terminator && "List_erase_range");\
421+ assert(last->magic == self->terminator && "List_erase_range");\
422+ pos = first;\
423+ while (pos != last) {\
424+ assert(!Name##_empty(self) && "List_erase_range");\
425+ pos = Name##_erase(self, pos);\
426+ }\
427+ return pos;\
428+}\
429+\
430+int Name##_resize(Name *self, size_t n, Type elem)\
431+{\
432+ size_t i;\
433+ size_t size;\
434+ assert(self && "List_resize");\
435+ assert(self->magic == self && "List_resize");\
436+ size = Name##_size(self);\
437+ if (size >= n) {\
438+ for (i = 0; i < size - n; i++) {\
439+ Name##_erase(self, Name##_rbegin(self));\
440+ }\
441+ } else {\
442+ Name *x;\
443+ x = Name##_new();\
444+ if (!x) return 0;\
445+ for (i = 0; i < n - size; i++) {\
446+ if (!Name##_push_back(x, elem)) {\
447+ Name##_delete(x);\
448+ return 0;\
449+ }\
450+ }\
451+ Name##_splice(self, Name##_end(self), x, Name##_begin(x), Name##_end(x));\
452+ Name##_delete(x);\
453+ }\
454+ return 1;\
455+}\
456+\
457+void Name##_swap(Name *self, Name *x)\
458+{\
459+ Name##Node *tmp_terminator;\
460+ size_t tmp_nelems;\
461+ assert(self && "List_swap");\
462+ assert(x && "List_swap");\
463+ assert(self->magic == self && "List_swap");\
464+ assert(x->magic == x && "List_swap");\
465+ tmp_terminator = self->terminator;\
466+ tmp_nelems = self->nelems;\
467+ self->terminator = x->terminator;\
468+ self->nelems = x->nelems;\
469+ x->terminator = tmp_terminator;\
470+ x->nelems = tmp_nelems;\
471+}\
472+\
473+void Name##_splice(Name *self, Name##Iterator pos, Name *x, Name##Iterator first, Name##Iterator last)\
474+{\
475+ Name##Iterator i;\
476+ Name##Node *tmp;\
477+ size_t count = 0;\
478+ assert(self && "List_splice");\
479+ assert(self->magic == self && "List_splice");\
480+ assert(pos && "List_splice");\
481+ assert(pos->magic == self->terminator && "List_splice");\
482+ assert(x && "List_splice");\
483+ assert(x->magic == x && "List_splice");\
484+ assert(first && "List_splice");\
485+ assert(last && "List_splice");\
486+ assert(first->magic == x->terminator && "List_splice");\
487+ assert(last->magic == x->terminator && "List_splice");\
488+ if (first == last || pos == last) return;\
489+ for (i = first; i != last; i = Name##_next(i)) {\
490+ assert(i != pos && "List_splice");\
491+ assert(i->magic && "List_splice");\
492+ LIST_MAGIC(i->magic = self->terminator;)\
493+ count++;\
494+ }\
495+ pos->prev->next = first;\
496+ tmp = first->prev;\
497+ first->prev = pos->prev;\
498+ tmp->next = last;\
499+ pos->prev = last->prev;\
500+ last->prev->next = pos;\
501+ last->prev = tmp;\
502+ self->nelems += count;\
503+ x->nelems -= count;\
504+}\
505+\
506+
507+
508+#endif /* CSTL_LIST_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/cstl/vector.h (nonexistent)
+++ tags/rel-0.2.1/cstl/vector.h (revision 43)
@@ -0,0 +1,358 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file vector.h
27+ * \brief vectorコンテナ
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-11-29
30+ */
31+#ifndef CSTL_VECTOR_H_INCLUDED
32+#define CSTL_VECTOR_H_INCLUDED
33+
34+#include <stddef.h>
35+#include <stdlib.h>
36+#include <assert.h>
37+
38+#ifdef __cplusplus
39+#define VECTOR_BEGIN_EXTERN_C() extern "C" {
40+#define VECTOR_END_EXTERN_C() }
41+#else
42+#define VECTOR_BEGIN_EXTERN_C()
43+#define VECTOR_END_EXTERN_C()
44+#endif
45+
46+#ifndef NDEBUG
47+#define VECTOR_MAGIC(x) x
48+#else
49+#define VECTOR_MAGIC(x)
50+#endif
51+
52+
53+/*!
54+ * \brief インターフェイスマクロ
55+ *
56+ * \param Name コンテナ名
57+ * \param Type 要素の型
58+ */
59+#define VECTOR_INTERFACE(Name, Type) \
60+typedef struct Name##_t Name;\
61+\
62+VECTOR_BEGIN_EXTERN_C()\
63+Name *Name##_new(size_t n);\
64+void Name##_delete(Name *self);\
65+int Name##_assign(Name *self, Name *x, size_t idx, size_t n);\
66+int Name##_push_back(Name *self, Type elem);\
67+Type Name##_pop_back(Name *self);\
68+size_t Name##_size(Name *self);\
69+size_t Name##_capacity(Name *self);\
70+int Name##_empty(Name *self);\
71+void Name##_clear(Name *self);\
72+int Name##_reserve(Name *self, size_t n);\
73+void Name##_shrink(Name *self, size_t n);\
74+int Name##_resize(Name *self, size_t n, Type elem);\
75+Type *Name##_at(Name *self, size_t idx);\
76+Type Name##_front(Name *self);\
77+Type Name##_back(Name *self);\
78+int Name##_insert(Name *self, size_t idx, Type elem);\
79+int Name##_insert_array(Name *self, size_t idx, Type *elems, size_t n);\
80+void Name##_erase(Name *self, size_t idx, size_t n);\
81+void Name##_swap(Name *self, Name *x);\
82+VECTOR_END_EXTERN_C()\
83+
84+
85+/*!
86+ * \brief 実装マクロ
87+ *
88+ * \param Name コンテナ名
89+ * \param Type 要素の型
90+ */
91+#define VECTOR_IMPLEMENT(Name, Type) \
92+/*!
93+ * \brief vector構造体
94+ */\
95+struct Name##_t {\
96+ size_t end;\
97+ size_t nelems;\
98+ Type *buf;\
99+ VECTOR_MAGIC(void *magic;)\
100+};\
101+\
102+static int Name##_full(Name *self)\
103+{\
104+ return (Name##_size(self) == Name##_capacity(self));\
105+}\
106+\
107+static int Name##_expand(Name *self, size_t size)\
108+{\
109+ size_t n;\
110+ n = (size > Name##_capacity(self) * 2) ? size : Name##_capacity(self) * 2;\
111+ return Name##_reserve(self, n);\
112+}\
113+\
114+Name *Name##_new(size_t n)\
115+{\
116+ Name *self;\
117+ Type *buf;\
118+ self = (Name *) malloc(sizeof(Name));\
119+ if (!self) return 0;\
120+ buf = (Type *) malloc(sizeof(Type) * (n+1));\
121+ if (!buf) {\
122+ free(self);\
123+ return 0;\
124+ }\
125+ self->end = 0;\
126+ self->buf = buf;\
127+ self->nelems = n+1;\
128+ VECTOR_MAGIC(self->magic = self;)\
129+ return self;\
130+}\
131+\
132+void Name##_delete(Name *self)\
133+{\
134+ assert(self && "Vector_delete");\
135+ assert(self->magic == self && "Vector_delete");\
136+ VECTOR_MAGIC(self->magic = 0;)\
137+ free(self->buf);\
138+ free(self);\
139+}\
140+\
141+int Name##_assign(Name *self, Name *x, size_t idx, size_t n)\
142+{\
143+ size_t i;\
144+ assert(self && "Vector_assign");\
145+ assert(self->magic == self && "Vector_assign");\
146+ assert(x && "Vector_assign");\
147+ assert(x->magic == x && "Vector_assign");\
148+ assert(Name##_size(x) >= idx + n && "Vector_assign");\
149+ if (n > Name##_capacity(self)) {\
150+ if (!Name##_expand(self, Name##_capacity(self) + n)) return 0;\
151+ }\
152+ if (self == x) {\
153+ Name##_erase(self, idx + n, Name##_size(self) - (idx + n));\
154+ Name##_erase(self, 0, idx);\
155+ } else {\
156+ Name##_clear(self);\
157+ for (i = 0; i < n; i++) {\
158+ Name##_push_back(self, *Name##_at(x, i));\
159+ }\
160+ }\
161+ return 1;\
162+}\
163+\
164+int Name##_push_back(Name *self, Type elem)\
165+{\
166+ assert(self && "Vector_push_back");\
167+ assert(self->magic == self && "Vector_push_back");\
168+ if (Name##_full(self)) {\
169+ if (!Name##_expand(self, Name##_capacity(self) + 1)) return 0;\
170+ }\
171+ self->buf[self->end] = elem;\
172+ self->end++;\
173+ return 1;\
174+}\
175+\
176+Type Name##_pop_back(Name *self)\
177+{\
178+ assert(self && "Vector_pop_back");\
179+ assert(self->magic == self && "Vector_pop_back");\
180+ assert(!Name##_empty(self) && "Vector_pop_back");\
181+ self->end--;\
182+ return self->buf[self->end];\
183+}\
184+\
185+size_t Name##_size(Name *self)\
186+{\
187+ assert(self && "Vector_size");\
188+ assert(self->magic == self && "Vector_size");\
189+ return self->end;\
190+}\
191+\
192+size_t Name##_capacity(Name *self)\
193+{\
194+ assert(self && "Vector_capacity");\
195+ assert(self->magic == self && "Vector_capacity");\
196+ return (self->nelems - 1);\
197+}\
198+\
199+int Name##_empty(Name *self)\
200+{\
201+ assert(self && "Vector_empty");\
202+ assert(self->magic == self && "Vector_empty");\
203+ return (self->end == 0);\
204+}\
205+\
206+void Name##_clear(Name *self)\
207+{\
208+ assert(self && "Vector_clear");\
209+ assert(self->magic == self && "Vector_clear");\
210+ self->end = 0;\
211+}\
212+\
213+int Name##_reserve(Name *self, size_t n)\
214+{\
215+ Type *newbuf;\
216+ assert(self && "Vector_reserve");\
217+ assert(self->magic == self && "Vector_reserve");\
218+ if (n <= Name##_capacity(self)) return 1;\
219+ newbuf = (Type *) realloc(self->buf, sizeof(Type) * (n+1));\
220+ if (!newbuf) return 0;\
221+ self->buf = newbuf;\
222+ self->nelems = n+1;\
223+ return 1;\
224+}\
225+\
226+void Name##_shrink(Name *self, size_t n)\
227+{\
228+ Type *newbuf;\
229+ assert(self && "Vector_shrink");\
230+ assert(self->magic == self && "Vector_shrink");\
231+ if (n >= Name##_capacity(self)) return;\
232+ if (n < Name##_size(self)) {\
233+ n = Name##_size(self);\
234+ }\
235+ newbuf = (Type *) realloc(self->buf, sizeof(Type) * (n+1));\
236+ if (newbuf) {\
237+ self->buf = newbuf;\
238+ }\
239+ self->nelems = n+1;\
240+}\
241+\
242+int Name##_resize(Name *self, size_t n, Type elem)\
243+{\
244+ size_t size;\
245+ assert(self && "Vector_resize");\
246+ assert(self->magic == self && "Vector_resize");\
247+ size = Name##_size(self);\
248+ if (size >= n) {\
249+ self->end = n;\
250+ } else {\
251+ size_t i;\
252+ if (!Name##_reserve(self, n)) {\
253+ return 0;\
254+ }\
255+ for (i = 0; i < n - size; i++) {\
256+ Name##_push_back(self, elem);\
257+ }\
258+ }\
259+ return 1;\
260+}\
261+\
262+Type *Name##_at(Name *self, size_t idx)\
263+{\
264+ assert(self && "Vector_at");\
265+ assert(self->magic == self && "Vector_at");\
266+ assert(Name##_size(self) > idx && "Vector_at");\
267+ return &self->buf[idx];\
268+}\
269+\
270+Type Name##_front(Name *self)\
271+{\
272+ assert(self && "Vector_front");\
273+ assert(self->magic == self && "Vector_front");\
274+ assert(!Name##_empty(self) && "Vector_front");\
275+ return self->buf[0];\
276+}\
277+\
278+Type Name##_back(Name *self)\
279+{\
280+ assert(self && "Vector_back");\
281+ assert(self->magic == self && "Vector_back");\
282+ assert(!Name##_empty(self) && "Vector_back");\
283+ return self->buf[self->end - 1];\
284+}\
285+\
286+static void Name##_move_forward(Name *self, size_t first, size_t last, size_t n)\
287+{\
288+ size_t i;\
289+ for (i = last; i > first; i--) {\
290+ self->buf[i-1+n] = self->buf[i-1];\
291+ }\
292+}\
293+\
294+static void Name##_move_backward(Name *self, size_t first, size_t last, size_t n)\
295+{\
296+ size_t i;\
297+ for (i = first; i < last; i++) {\
298+ self->buf[i-n] = self->buf[i];\
299+ }\
300+}\
301+\
302+int Name##_insert(Name *self, size_t idx, Type elem)\
303+{\
304+ assert(self && "Vector_insert");\
305+ assert(self->magic == self && "Vector_insert");\
306+ assert(Name##_size(self) >= idx && "Vector_insert");\
307+ return Name##_insert_array(self, idx, &elem, 1);\
308+}\
309+\
310+int Name##_insert_array(Name *self, size_t idx, Type *elems, size_t n)\
311+{\
312+ size_t i, j;\
313+ assert(self && "Vector_insert_array");\
314+ assert(self->magic == self && "Vector_insert_array");\
315+ assert(Name##_size(self) >= idx && "Vector_insert_array");\
316+ assert(elems && "Vector_insert_array");\
317+ if (Name##_size(self) + n > Name##_capacity(self)) {\
318+ if (!Name##_expand(self, Name##_capacity(self) + n)) return 0;\
319+ }\
320+ Name##_move_forward(self, idx, self->end, n);\
321+ self->end += n;\
322+ for (i = idx, j = 0; j < n; i++, j++) {\
323+ self->buf[i] = elems[j];\
324+ }\
325+ return 1;\
326+}\
327+\
328+void Name##_erase(Name *self, size_t idx, size_t n)\
329+{\
330+ assert(self && "Vector_erase");\
331+ assert(self->magic == self && "Vector_erase");\
332+ assert(Name##_size(self) >= idx + n && "Vector_erase");\
333+ Name##_move_backward(self, idx + n, self->end, n);\
334+ self->end -= n;\
335+}\
336+\
337+void Name##_swap(Name *self, Name *x)\
338+{\
339+ size_t tmp_end;\
340+ size_t tmp_nelems;\
341+ Type *tmp_buf;\
342+ assert(self && "Vector_swap");\
343+ assert(x && "Vector_swap");\
344+ assert(self->magic == self && "Vector_swap");\
345+ assert(x->magic == x && "Vector_swap");\
346+ tmp_end = self->end;\
347+ tmp_nelems = self->nelems;\
348+ tmp_buf = self->buf;\
349+ self->end = x->end;\
350+ self->nelems = x->nelems;\
351+ self->buf = x->buf;\
352+ x->end = tmp_end;\
353+ x->nelems = tmp_nelems;\
354+ x->buf = tmp_buf;\
355+}\
356+\
357+
358+#endif /* CSTL_VECTOR_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/cstl/deque.h (nonexistent)
+++ tags/rel-0.2.1/cstl/deque.h (revision 43)
@@ -0,0 +1,401 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file deque.h
27+ * \brief dequeコンテナ
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-02-25
30+ */
31+#ifndef CSTL_DEQUE_H_INCLUDED
32+#define CSTL_DEQUE_H_INCLUDED
33+
34+#include <stddef.h>
35+#include <stdlib.h>
36+#include <assert.h>
37+
38+#ifdef __cplusplus
39+#define DEQUE_BEGIN_EXTERN_C() extern "C" {
40+#define DEQUE_END_EXTERN_C() }
41+#else
42+#define DEQUE_BEGIN_EXTERN_C()
43+#define DEQUE_END_EXTERN_C()
44+#endif
45+
46+#ifndef NDEBUG
47+#define DEQUE_MAGIC(x) x
48+#else
49+#define DEQUE_MAGIC(x)
50+#endif
51+
52+/*!
53+ * \brief インターフェイスマクロ
54+ *
55+ * \param Name コンテナ名
56+ * \param Type 要素の型
57+ */
58+#define DEQUE_INTERFACE(Name, Type) \
59+typedef struct Name##_t Name;\
60+/*!
61+ * \brief deque構造体
62+ */\
63+struct Name##_t {\
64+ size_t begin;\
65+ size_t end;\
66+ size_t nelems;\
67+ Type *buf;\
68+ DEQUE_MAGIC(void *magic;)\
69+};\
70+\
71+DEQUE_BEGIN_EXTERN_C()\
72+Name *Name##_new(size_t n);\
73+void Name##_init(Name *self, Type *buf, size_t n);\
74+void Name##_delete(Name *self);\
75+int Name##_assign(Name *self, Name *x, size_t idx, size_t n);\
76+int Name##_push_back(Name *self, Type elem);\
77+int Name##_push_front(Name *self, Type elem);\
78+Type Name##_pop_front(Name *self);\
79+Type Name##_pop_back(Name *self);\
80+size_t Name##_size(Name *self);\
81+size_t Name##_max_size(Name *self);\
82+int Name##_empty(Name *self);\
83+int Name##_full(Name *self);\
84+void Name##_clear(Name *self);\
85+Type *Name##_at(Name *self, size_t idx);\
86+Type Name##_front(Name *self);\
87+Type Name##_back(Name *self);\
88+int Name##_insert(Name *self, size_t idx, Type elem);\
89+int Name##_insert_array(Name *self, size_t idx, Type *elems, size_t n);\
90+void Name##_erase(Name *self, size_t idx, size_t n);\
91+int Name##_resize(Name *self, size_t n, Type elem);\
92+void Name##_swap(Name *self, Name *x);\
93+DEQUE_END_EXTERN_C()\
94+
95+
96+/*!
97+ * \brief 実装マクロ
98+ *
99+ * \param Name コンテナ名
100+ * \param Type 要素の型
101+ */
102+#define DEQUE_IMPLEMENT(Name, Type) \
103+\
104+static size_t Name##_forward(Name *self, size_t idx, size_t n)\
105+{\
106+ return (idx + n) % self->nelems;\
107+}\
108+\
109+static size_t Name##_backward(Name *self, size_t idx, size_t n)\
110+{\
111+ return (idx >= n) ? idx - n : self->nelems + idx - n;\
112+}\
113+\
114+static size_t Name##_next(Name *self, size_t idx)\
115+{\
116+ return Name##_forward(self, idx, 1);\
117+}\
118+\
119+static size_t Name##_prev(Name *self, size_t idx)\
120+{\
121+ return Name##_backward(self, idx, 1);\
122+}\
123+\
124+static size_t Name##_distance(Name *self, size_t first, size_t last)\
125+{\
126+ return (first <= last) ? last - first : self->nelems - first + last;\
127+}\
128+\
129+Name *Name##_new(size_t n)\
130+{\
131+ Name *self;\
132+ Type *buf;\
133+ if (!n) return 0;\
134+ self = (Name *) malloc(sizeof(Name));\
135+ if (!self) return 0;\
136+ buf = (Type *) malloc(sizeof(Type) * (n+1));\
137+ if (!buf) {\
138+ free(self);\
139+ return 0;\
140+ }\
141+ Name##_init(self, buf, (n+1));\
142+ return self;\
143+}\
144+\
145+void Name##_delete(Name *self)\
146+{\
147+ assert(self && "Deque_delete");\
148+ assert(self->magic == self && "Deque_delete");\
149+ DEQUE_MAGIC(self->magic = 0;)\
150+ free(self->buf);\
151+ free(self);\
152+}\
153+\
154+void Name##_init(Name *self, Type *buf, size_t n)\
155+{\
156+ assert(self && "Deque_init");\
157+ assert(buf && "Deque_init");\
158+ self->begin = 0;\
159+ self->end = 0;\
160+ self->buf = buf;\
161+ self->nelems = n;\
162+ DEQUE_MAGIC(self->magic = self;)\
163+}\
164+\
165+int Name##_assign(Name *self, Name *x, size_t idx, size_t n)\
166+{\
167+ size_t i;\
168+ assert(self && "Deque_assign");\
169+ assert(self->magic == self && "Deque_assign");\
170+ assert(x && "Deque_assign");\
171+ assert(x->magic == x && "Deque_assign");\
172+ assert(Name##_size(x) >= idx + n && "Deque_assign");\
173+ if (n > Name##_max_size(self)) return 0;\
174+ if (self == x) {\
175+ Name##_erase(self, idx + n, Name##_size(self) - (idx + n));\
176+ Name##_erase(self, 0, idx);\
177+ } else {\
178+ Name##_clear(self);\
179+ for (i = 0; i < n; i++) {\
180+ Name##_push_back(self, *Name##_at(x, i));\
181+ }\
182+ }\
183+ return 1;\
184+}\
185+\
186+int Name##_push_back(Name *self, Type elem)\
187+{\
188+ assert(self && "Deque_push_back");\
189+ assert(self->magic == self && "Deque_push_back");\
190+ if (Name##_full(self)) return 0;\
191+ self->buf[self->end] = elem;\
192+ self->end = Name##_next(self, self->end);\
193+ return 1;\
194+}\
195+\
196+int Name##_push_front(Name *self, Type elem)\
197+{\
198+ assert(self && "Deque_push_front");\
199+ assert(self->magic == self && "Deque_push_front");\
200+ if (Name##_full(self)) return 0;\
201+ self->begin = Name##_prev(self, self->begin);\
202+ self->buf[self->begin] = elem;\
203+ return 1;\
204+}\
205+\
206+Type Name##_pop_front(Name *self)\
207+{\
208+ size_t idx;\
209+ assert(self && "Deque_pop_front");\
210+ assert(self->magic == self && "Deque_pop_front");\
211+ assert(!Name##_empty(self) && "Deque_pop_front");\
212+ idx = self->begin;\
213+ self->begin = Name##_next(self, self->begin);\
214+ return self->buf[idx];\
215+}\
216+\
217+Type Name##_pop_back(Name *self)\
218+{\
219+ assert(self && "Deque_pop_back");\
220+ assert(self->magic == self && "Deque_pop_back");\
221+ assert(!Name##_empty(self) && "Deque_pop_back");\
222+ self->end = Name##_prev(self, self->end);\
223+ return self->buf[self->end];\
224+}\
225+\
226+size_t Name##_size(Name *self)\
227+{\
228+ assert(self && "Deque_size");\
229+ assert(self->magic == self && "Deque_size");\
230+ return Name##_distance(self, self->begin, self->end);\
231+}\
232+\
233+size_t Name##_max_size(Name *self)\
234+{\
235+ assert(self && "Deque_max_size");\
236+ assert(self->magic == self && "Deque_max_size");\
237+ return (self->nelems - 1);\
238+}\
239+\
240+int Name##_empty(Name *self)\
241+{\
242+ assert(self && "Deque_empty");\
243+ assert(self->magic == self && "Deque_empty");\
244+ return (self->begin == self->end);\
245+}\
246+\
247+int Name##_full(Name *self)\
248+{\
249+ assert(self && "Deque_full");\
250+ assert(self->magic == self && "Deque_full");\
251+ return (Name##_next(self, self->end) == self->begin);\
252+}\
253+\
254+void Name##_clear(Name *self)\
255+{\
256+ assert(self && "Deque_clear");\
257+ assert(self->magic == self && "Deque_clear");\
258+ self->end = self->begin;\
259+}\
260+\
261+Type *Name##_at(Name *self, size_t idx)\
262+{\
263+ assert(self && "Deque_at");\
264+ assert(self->magic == self && "Deque_at");\
265+ assert(Name##_size(self) > idx && "Deque_at");\
266+ return &self->buf[Name##_forward(self, self->begin, idx)];\
267+}\
268+\
269+Type Name##_front(Name *self)\
270+{\
271+ assert(self && "Deque_front");\
272+ assert(self->magic == self && "Deque_front");\
273+ assert(!Name##_empty(self) && "Deque_front");\
274+ return self->buf[self->begin];\
275+}\
276+\
277+Type Name##_back(Name *self)\
278+{\
279+ assert(self && "Deque_back");\
280+ assert(self->magic == self && "Deque_back");\
281+ assert(!Name##_empty(self) && "Deque_back");\
282+ return self->buf[Name##_prev(self, self->end)];\
283+}\
284+\
285+static void Name##_move_forward(Name *self, size_t first, size_t last, size_t n)\
286+{\
287+ size_t i;\
288+ for (i = Name##_prev(self, last); i != Name##_prev(self, first); i = Name##_prev(self, i)) {\
289+ self->buf[Name##_forward(self, i, n)] = self->buf[i];\
290+ }\
291+}\
292+\
293+static void Name##_move_backward(Name *self, size_t first, size_t last, size_t n)\
294+{\
295+ size_t i;\
296+ for (i = first; i != last; i = Name##_next(self, i)) {\
297+ self->buf[Name##_backward(self, i, n)] = self->buf[i];\
298+ }\
299+}\
300+\
301+int Name##_insert(Name *self, size_t idx, Type elem)\
302+{\
303+ assert(self && "Deque_insert");\
304+ assert(self->magic == self && "Deque_insert");\
305+ assert(Name##_size(self) >= idx && "Deque_insert");\
306+ return Name##_insert_array(self, idx, &elem, 1);\
307+}\
308+\
309+int Name##_insert_array(Name *self, size_t idx, Type *elems, size_t n)\
310+{\
311+ size_t i, j;\
312+ size_t pos;\
313+ assert(self && "Deque_insert_array");\
314+ assert(self->magic == self && "Deque_insert_array");\
315+ assert(Name##_size(self) >= idx && "Deque_insert_array");\
316+ assert(elems && "Deque_insert_array");\
317+ if (Name##_size(self) + n > Name##_max_size(self)) return 0;\
318+ pos = Name##_forward(self, self->begin, idx);\
319+ if (Name##_size(self) / 2 < idx) {\
320+ /* end側を移動 */\
321+ Name##_move_forward(self, pos, self->end, n);\
322+ self->end = Name##_forward(self, self->end, n);\
323+ } else {\
324+ /* begin側を移動 */\
325+ Name##_move_backward(self, self->begin, pos, n);\
326+ self->begin = Name##_backward(self, self->begin, n);\
327+ pos = Name##_backward(self, pos, n);\
328+ }\
329+ for (i = pos, j = 0; j < n; i = Name##_next(self, i), j++) {\
330+ self->buf[i] = elems[j];\
331+ }\
332+ return 1;\
333+}\
334+\
335+void Name##_erase(Name *self, size_t idx, size_t n)\
336+{\
337+ size_t pos1;\
338+ size_t pos2;\
339+ assert(self && "Deque_erase");\
340+ assert(self->magic == self && "Deque_erase");\
341+ assert(Name##_size(self) >= idx + n && "Deque_erase");\
342+ pos1 = Name##_forward(self, self->begin, idx);\
343+ pos2 = Name##_forward(self, pos1, n);\
344+ if (Name##_distance(self, self->begin, pos1) >= \
345+ Name##_distance(self, pos2, self->end)) {\
346+ /* end側を移動 */\
347+ Name##_move_backward(self, pos2, self->end, n);\
348+ self->end = Name##_backward(self, self->end, n);\
349+ } else {\
350+ /* begin側を移動 */\
351+ Name##_move_forward(self, self->begin, pos1, n);\
352+ self->begin = Name##_forward(self, self->begin, n);\
353+ }\
354+}\
355+\
356+int Name##_resize(Name *self, size_t n, Type elem)\
357+{\
358+ size_t size;\
359+ assert(self && "Deque_resize");\
360+ assert(self->magic == self && "Deque_resize");\
361+ size = Name##_size(self);\
362+ if (size >= n) {\
363+ self->end = Name##_backward(self, self->end, size - n);\
364+ } else {\
365+ size_t i;\
366+ if (Name##_max_size(self) < n) {\
367+ return 0;\
368+ }\
369+ for (i = 0; i < n - size; i++) {\
370+ Name##_push_back(self, elem);\
371+ }\
372+ }\
373+ return 1;\
374+}\
375+\
376+void Name##_swap(Name *self, Name *x)\
377+{\
378+ size_t tmp_begin;\
379+ size_t tmp_end;\
380+ size_t tmp_nelems;\
381+ Type *tmp_buf;\
382+ assert(self && "Deque_swap");\
383+ assert(x && "Deque_swap");\
384+ assert(self->magic == self && "Deque_swap");\
385+ assert(x->magic == x && "Deque_swap");\
386+ tmp_begin = self->begin;\
387+ tmp_end = self->end;\
388+ tmp_nelems = self->nelems;\
389+ tmp_buf = self->buf;\
390+ self->begin = x->begin;\
391+ self->end = x->end;\
392+ self->nelems = x->nelems;\
393+ self->buf = x->buf;\
394+ x->begin = tmp_begin;\
395+ x->end = tmp_end;\
396+ x->nelems = tmp_nelems;\
397+ x->buf = tmp_buf;\
398+}\
399+\
400+
401+#endif /* CSTL_DEQUE_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/cstl/string.h (nonexistent)
+++ tags/rel-0.2.1/cstl/string.h (revision 43)
@@ -0,0 +1,690 @@
1+/*
2+ * Copyright (c) 2007, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file string.h
27+ * \brief 文字列コンテナ
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2007-01-09
30+ */
31+#ifndef CSTL_STRING_H_INCLUDED
32+#define CSTL_STRING_H_INCLUDED
33+
34+#include <stddef.h>
35+#include <stdlib.h>
36+#include <assert.h>
37+#include "vector.h"
38+
39+#ifdef __cplusplus
40+#define STRING_BEGIN_EXTERN_C() extern "C" {
41+#define STRING_END_EXTERN_C() }
42+#else
43+#define STRING_BEGIN_EXTERN_C()
44+#define STRING_END_EXTERN_C()
45+#endif
46+
47+#ifndef NDEBUG
48+#define STRING_MAGIC(x) x
49+#else
50+#define STRING_MAGIC(x)
51+#endif
52+
53+/*!
54+ * \brief 検索失敗用インデックス
55+ */
56+#define NPOS ((size_t)-1)
57+
58+/*!
59+ * \brief インターフェイスマクロ
60+ *
61+ * \param Name コンテナ名
62+ * \param Type 要素の型
63+ */
64+#define STRING_INTERFACE(Name, Type) \
65+typedef struct Name##_t Name;\
66+\
67+STRING_BEGIN_EXTERN_C()\
68+Name *Name##_new(size_t n);\
69+void Name##_delete(Name *self);\
70+void Name##_clear(Name *self);\
71+size_t Name##_size(Name *self);\
72+size_t Name##_length(Name *self);\
73+size_t Name##_capacity(Name *self);\
74+int Name##_reserve(Name *self, size_t n);\
75+void Name##_shrink(Name *self, size_t n);\
76+int Name##_empty(Name *self);\
77+int Name##_compare(Name *self, Name *x);\
78+Type *Name##_at(Name *self, size_t idx);\
79+Type *Name##_c_str(Name *self);\
80+void Name##_erase(Name *self, size_t idx, size_t len);\
81+int Name##_resize(Name *self, size_t n, Type c);\
82+void Name##_swap(Name *self, Name *x);\
83+int Name##_assign(Name *self, Type *cstr, size_t cstr_len);\
84+int Name##_assign_c(Name *self, size_t n, Type c);\
85+int Name##_append(Name *self, Type *cstr, size_t cstr_len);\
86+int Name##_append_c(Name *self, size_t n, Type c);\
87+int Name##_push_back(Name *self, Type c);\
88+int Name##_insert(Name *self, size_t idx, Type *cstr, size_t cstr_len);\
89+int Name##_insert_c(Name *self, size_t idx, size_t n, Type c);\
90+int Name##_replace(Name *self, size_t idx, size_t len, Type *cstr, size_t cstr_len);\
91+int Name##_replace_c(Name *self, size_t idx, size_t len, size_t n, Type c);\
92+size_t Name##_find(Type *x, Type *cstr, size_t idx, size_t cstr_len);\
93+size_t Name##_find_c(Type *x, Type c, size_t idx);\
94+size_t Name##_rfind(Type *x, Type *cstr, size_t idx, size_t cstr_len);\
95+size_t Name##_rfind_c(Type *x, Type c, size_t idx);\
96+size_t Name##_find_first_of(Type *x, Type *cstr, size_t idx, size_t cstr_len);\
97+size_t Name##_find_first_of_c(Type *x, Type c, size_t idx);\
98+size_t Name##_find_last_of(Type *x, Type *cstr, size_t idx, size_t cstr_len);\
99+size_t Name##_find_last_of_c(Type *x, Type c, size_t idx);\
100+size_t Name##_find_first_not_of(Type *x, Type *cstr, size_t idx, size_t cstr_len);\
101+size_t Name##_find_first_not_of_c(Type *x, Type c, size_t idx);\
102+size_t Name##_find_last_not_of(Type *x, Type *cstr, size_t idx, size_t cstr_len);\
103+size_t Name##_find_last_not_of_c(Type *x, Type c, size_t idx);\
104+STRING_END_EXTERN_C()\
105+
106+
107+/*!
108+ * \brief 実装マクロ
109+ *
110+ * \param Name コンテナ名
111+ * \param Type 要素の型
112+ */
113+#define STRING_IMPLEMENT(Name, Type) \
114+\
115+static int Name##my_memcmp(const Type *x, const Type *y, size_t size)\
116+{\
117+ if (size) {\
118+ do {\
119+ if (*x != *y) {\
120+ return *x - *y;\
121+ }\
122+ x++;\
123+ y++;\
124+ size--;\
125+ } while (size);\
126+ }\
127+ return 0;\
128+}\
129+\
130+static size_t Name##my_strlen(const Type *cstr)\
131+{\
132+ size_t i = 0;\
133+ while (*cstr != '\0') {\
134+ cstr++;\
135+ i++;\
136+ }\
137+ return i;\
138+}\
139+\
140+VECTOR_INTERFACE(Name##CharVector, Type)\
141+VECTOR_IMPLEMENT(Name##CharVector, Type)\
142+\
143+/*!
144+ * \brief string構造体
145+ */\
146+struct Name##_t {\
147+ Name##CharVector *data;\
148+ STRING_MAGIC(void *magic;)\
149+};\
150+\
151+Name *Name##_new(size_t n)\
152+{\
153+ Name *self;\
154+ self = (Name *) malloc(sizeof(Name));\
155+ if (!self) return 0;\
156+ self->data = Name##CharVector_new(n+1);\
157+ if (!self->data) {\
158+ free(self);\
159+ return 0;\
160+ }\
161+ Name##CharVector_push_back(self->data, '\0');\
162+ STRING_MAGIC(self->magic = self;)\
163+ return self;\
164+}\
165+\
166+void Name##_delete(Name *self)\
167+{\
168+ assert(self && "String_delete");\
169+ assert(self->magic == self && "String_delete");\
170+ STRING_MAGIC(self->magic = 0;)\
171+ Name##CharVector_delete(self->data);\
172+ free(self);\
173+}\
174+\
175+void Name##_clear(Name *self)\
176+{\
177+ assert(self && "String_clear");\
178+ assert(self->magic == self && "String_clear");\
179+ Name##CharVector_clear(self->data);\
180+ Name##CharVector_push_back(self->data, '\0');\
181+}\
182+\
183+size_t Name##_size(Name *self)\
184+{\
185+ assert(self && "String_size");\
186+ assert(self->magic == self && "String_size");\
187+ return Name##CharVector_size(self->data) -1;\
188+}\
189+\
190+size_t Name##_length(Name *self)\
191+{\
192+ assert(self && "String_length");\
193+ assert(self->magic == self && "String_length");\
194+ return Name##CharVector_size(self->data) -1;\
195+}\
196+\
197+size_t Name##_capacity(Name *self)\
198+{\
199+ assert(self && "String_capacity");\
200+ assert(self->magic == self && "String_capacity");\
201+ return Name##CharVector_capacity(self->data) -1;\
202+}\
203+\
204+int Name##_reserve(Name *self, size_t n)\
205+{\
206+ assert(self && "String_reserve");\
207+ assert(self->magic == self && "String_reserve");\
208+ return Name##CharVector_reserve(self->data, n +1);\
209+}\
210+\
211+void Name##_shrink(Name *self, size_t n)\
212+{\
213+ assert(self && "String_shrink");\
214+ assert(self->magic == self && "String_shrink");\
215+ Name##CharVector_shrink(self->data, n +1);\
216+}\
217+\
218+int Name##_empty(Name *self)\
219+{\
220+ assert(self && "String_empty");\
221+ assert(self->magic == self && "String_empty");\
222+ return Name##CharVector_size(self->data) == 1;\
223+}\
224+\
225+int Name##_compare(Name *self, Name *x)\
226+{\
227+ size_t len, xlen;\
228+ int ret;\
229+ assert(self && "String_compare");\
230+ assert(x && "String_compare");\
231+ assert(self->magic == self && "String_compare");\
232+ assert(x->magic == x && "String_compare");\
233+ len = Name##_size(self);\
234+ xlen = Name##_size(x);\
235+ ret = Name##my_memcmp(Name##_c_str(self), Name##_c_str(x), (len > xlen) ? xlen : len);\
236+ return ret ? ret : len - xlen;\
237+}\
238+\
239+Type *Name##_at(Name *self, size_t idx)\
240+{\
241+ assert(self && "String_at");\
242+ assert(self->magic == self && "String_at");\
243+ assert(Name##_size(self) > idx && "String_at");\
244+ return Name##CharVector_at(self->data, idx);\
245+}\
246+\
247+Type *Name##_c_str(Name *self)\
248+{\
249+ assert(self && "String_c_str");\
250+ assert(self->magic == self && "String_c_str");\
251+ return Name##CharVector_at(self->data, 0);\
252+}\
253+\
254+void Name##_erase(Name *self, size_t idx, size_t len)\
255+{\
256+ size_t size;\
257+ assert(self && "String_erase");\
258+ assert(self->magic == self && "String_erase");\
259+ size = Name##_size(self);\
260+ assert(size >= idx && "String_erase");\
261+ if (len > size - idx) {\
262+ len = size - idx;\
263+ }\
264+ Name##CharVector_erase(self->data, idx, len);\
265+}\
266+\
267+int Name##_resize(Name *self, size_t n, Type c)\
268+{\
269+ size_t num;\
270+ assert(self && "String_resize");\
271+ assert(self->magic == self && "String_resize");\
272+ num = Name##CharVector_size(self->data) -1;\
273+ if (!Name##CharVector_resize(self->data, n +1, c)) {\
274+ return 0;\
275+ }\
276+ if (num < Name##CharVector_size(self->data)) {\
277+ /* '\0'を上書き */\
278+ *Name##CharVector_at(self->data, num) = c;\
279+ }\
280+ *Name##CharVector_at(self->data, Name##CharVector_size(self->data) -1) = '\0';\
281+ return 1;\
282+}\
283+\
284+void Name##_swap(Name *self, Name *x)\
285+{\
286+ Name##CharVector *tmp_data;\
287+ assert(self && "String_swap");\
288+ assert(x && "String_swap");\
289+ assert(self->magic == self && "String_swap");\
290+ assert(x->magic == x && "String_swap");\
291+ tmp_data = self->data;\
292+ self->data = x->data;\
293+ x->data = tmp_data;\
294+}\
295+\
296+int Name##_assign(Name *self, Type *cstr, size_t cstr_len)\
297+{\
298+ assert(self && "String_assign");\
299+ assert(self->magic == self && "String_assign");\
300+ assert(cstr && "String_assign");\
301+ if (cstr_len == NPOS) {\
302+ cstr_len = Name##my_strlen(cstr);\
303+ }\
304+ if (!Name##CharVector_reserve(self->data, cstr_len +1)) {\
305+ return 0;\
306+ }\
307+ Name##CharVector_clear(self->data);\
308+ Name##CharVector_insert_array(self->data, 0, cstr, cstr_len);\
309+ Name##CharVector_push_back(self->data, '\0');\
310+ return 1;\
311+}\
312+\
313+int Name##_assign_c(Name *self, size_t n, Type c)\
314+{\
315+ size_t i;\
316+ assert(self && "String_assign_c");\
317+ assert(self->magic == self && "String_assign_c");\
318+ if (!Name##_reserve(self, n)) {\
319+ return 0;\
320+ }\
321+ Name##_clear(self);\
322+ for (i = 0; i < n; i++) {\
323+ Name##_push_back(self, c);\
324+ }\
325+ return 1;\
326+}\
327+\
328+int Name##_append(Name *self, Type *cstr, size_t cstr_len)\
329+{\
330+ assert(self && "String_append");\
331+ assert(self->magic == self && "String_append");\
332+ assert(cstr && "String_append");\
333+ if (cstr_len == NPOS) {\
334+ cstr_len = Name##my_strlen(cstr);\
335+ }\
336+ return Name##CharVector_insert_array(self->data, Name##CharVector_size(self->data)-1, cstr, cstr_len);\
337+}\
338+\
339+int Name##_append_c(Name *self, size_t n, Type c)\
340+{\
341+ size_t i;\
342+ assert(self && "String_append_c");\
343+ assert(self->magic == self && "String_append_c");\
344+ if (!Name##_reserve(self, Name##_size(self) + n)) {\
345+ return 0;\
346+ }\
347+ for (i = 0; i < n; i++) {\
348+ Name##_push_back(self, c);\
349+ }\
350+ return 1;\
351+}\
352+\
353+int Name##_push_back(Name *self, Type c)\
354+{\
355+ assert(self && "String_push_back");\
356+ assert(self->magic == self && "String_push_back");\
357+ return Name##CharVector_insert(self->data, Name##CharVector_size(self->data)-1, c);\
358+}\
359+\
360+int Name##_insert(Name *self, size_t idx, Type *cstr, size_t cstr_len)\
361+{\
362+ int ret;\
363+ assert(self && "String_insert");\
364+ assert(self->magic == self && "String_insert");\
365+ assert(Name##_size(self) >= idx && "String_insert");\
366+ assert(cstr && "String_insert");\
367+ if (cstr_len == NPOS) {\
368+ cstr_len = Name##my_strlen(cstr);\
369+ }\
370+ if (cstr_len && Name##_c_str(self) + idx < cstr + cstr_len && cstr < Name##_c_str(self) + Name##_size(self)) {\
371+ size_t i;\
372+ Type *buf;\
373+ buf = (Type *) malloc(sizeof(Type) * cstr_len);\
374+ if (!buf) return 0;\
375+ for (i = 0; i < cstr_len; i++) {\
376+ buf[i] = cstr[i];\
377+ }\
378+ ret = Name##CharVector_insert_array(self->data, idx, buf, cstr_len);\
379+ free(buf);\
380+ } else {\
381+ ret = Name##CharVector_insert_array(self->data, idx, cstr, cstr_len);\
382+ }\
383+ return ret;\
384+}\
385+\
386+int Name##_insert_c(Name *self, size_t idx, size_t n, Type c)\
387+{\
388+ int ret = 1;\
389+ assert(self && "String_insert_c");\
390+ assert(self->magic == self && "String_insert_c");\
391+ assert(Name##_size(self) >= idx && "String_insert_c");\
392+ if (n > 1) {\
393+ size_t i;\
394+ Type *buf;\
395+ buf = (Type *) malloc(sizeof(Type) * n);\
396+ if (!buf) return 0;\
397+ for (i = 0; i < n; i++) {\
398+ buf[i] = c;\
399+ }\
400+ ret = Name##_insert(self, idx, buf, n);\
401+ free(buf);\
402+ } else if (n == 1) {\
403+ ret = Name##CharVector_insert(self->data, idx, c);\
404+ }\
405+ return ret;\
406+}\
407+\
408+int Name##_replace(Name *self, size_t idx, size_t len, Type *cstr, size_t cstr_len)\
409+{\
410+ size_t i;\
411+ size_t size;\
412+ Type *buf;\
413+ int flag = 0;\
414+ assert(self && "String_replace");\
415+ assert(self->magic == self && "String_replace");\
416+ assert(cstr && "String_replace");\
417+ size = Name##_size(self);\
418+ assert(size >= idx && "String_replace");\
419+ if (len > size - idx) {\
420+ len = size - idx;\
421+ }\
422+ if (cstr_len == NPOS) {\
423+ cstr_len = Name##my_strlen(cstr);\
424+ }\
425+ if (cstr_len && Name##_c_str(self) + idx < cstr + cstr_len && cstr < Name##_c_str(self) + Name##_size(self)) {\
426+ buf = (Type *) malloc(sizeof(Type) * cstr_len);\
427+ if (!buf) return 0;\
428+ for (i = 0; i < cstr_len; i++) {\
429+ buf[i] = cstr[i];\
430+ }\
431+ flag = 1;\
432+ } else {\
433+ buf = cstr;\
434+ }\
435+ if (cstr_len <= len) {\
436+ /* 拡張必要なし */\
437+ for (i = 0; i < len; i++) {\
438+ if (i < cstr_len) {\
439+ *Name##_at(self, i + idx) = buf[i];\
440+ } else {\
441+ size_t j = len - cstr_len;\
442+ if (j > Name##_size(self) - (cstr_len + idx)) {\
443+ j = Name##_size(self) - (cstr_len + idx);\
444+ }\
445+ Name##_erase(self, cstr_len + idx, j);\
446+ break;\
447+ }\
448+ }\
449+ } else {\
450+ /* 拡張必要あり */\
451+ if (!Name##_reserve(self, Name##_capacity(self) + (cstr_len - len))) {\
452+ if (flag) free(buf);\
453+ return 0;\
454+ }\
455+ for (i = 0; i < len; i++) {\
456+ *Name##_at(self, i + idx) = buf[i];\
457+ }\
458+ Name##_insert(self, len + idx, &buf[len], cstr_len - len);\
459+ }\
460+ if (flag) free(buf);\
461+ return 1;\
462+}\
463+\
464+int Name##_replace_c(Name *self, size_t idx, size_t len, size_t n, Type c)\
465+{\
466+ int ret;\
467+ assert(self && "String_replace_c");\
468+ assert(self->magic == self && "String_replace_c");\
469+ assert(Name##_size(self) >= idx && "String_replace_c");\
470+ if (n > 1) {\
471+ size_t i;\
472+ Type *buf;\
473+ buf = (Type *) malloc(sizeof(Type) * n);\
474+ if (!buf) return 0;\
475+ for (i = 0; i < n; i++) {\
476+ buf[i] = c;\
477+ }\
478+ ret = Name##_replace(self, idx, len, buf, n);\
479+ free(buf);\
480+ } else {\
481+ ret = Name##_replace(self, idx, len, &c, n);\
482+ }\
483+ return ret;\
484+}\
485+\
486+static size_t Name##_brute_force_search(Type *str, size_t str_len, Type *ptn, size_t ptn_len)\
487+{\
488+ size_t i = 0;\
489+ size_t j = 0;\
490+ if (str_len < ptn_len) return NPOS;\
491+ while (i < str_len && j < ptn_len) {\
492+ if (str[i] == ptn[j]) {\
493+ i++;\
494+ j++;\
495+ } else {\
496+ i += 1 - j;\
497+ j = 0;\
498+ }\
499+ }\
500+ return (j == ptn_len) ? i - j : NPOS;\
501+}\
502+\
503+static size_t Name##_brute_force_search_r(Type *str, size_t str_len, Type *ptn, size_t ptn_len)\
504+{\
505+ size_t i = str_len;\
506+ size_t j = ptn_len;\
507+ if (str_len < ptn_len) return NPOS;\
508+ while (i > 0 && j > 0) {\
509+ if (str[i-1] == ptn[j-1]) {\
510+ i--;\
511+ j--;\
512+ } else {\
513+ i += ptn_len - j - 1;\
514+ j = ptn_len;\
515+ }\
516+ }\
517+ return (j == 0) ? i : NPOS;\
518+}\
519+\
520+size_t Name##_find(Type *x, Type *cstr, size_t idx, size_t cstr_len)\
521+{\
522+ size_t i;\
523+ size_t size;\
524+ assert(x && "String_find");\
525+ assert(cstr && "String_find");\
526+ if (cstr_len == NPOS) {\
527+ cstr_len = Name##my_strlen(cstr);\
528+ }\
529+ if (cstr_len == 0) {\
530+ return idx;\
531+ }\
532+ size = Name##my_strlen(x);\
533+ if (size < idx) return NPOS;\
534+ i = Name##_brute_force_search(&x[idx], size - idx, cstr, cstr_len);\
535+ if (i == NPOS) return i;\
536+ return i + idx;\
537+}\
538+\
539+size_t Name##_find_c(Type *x, Type c, size_t idx)\
540+{\
541+ assert(x && "String_find_c");\
542+ return Name##_find(x, &c, idx, 1);\
543+}\
544+\
545+size_t Name##_rfind(Type *x, Type *cstr, size_t idx, size_t cstr_len)\
546+{\
547+ size_t size;\
548+ assert(x && "String_rfind");\
549+ assert(cstr && "String_rfind");\
550+ if (cstr_len == NPOS) {\
551+ cstr_len = Name##my_strlen(cstr);\
552+ }\
553+ size = Name##my_strlen(x);\
554+ if (size - cstr_len < idx) {\
555+ idx = size - cstr_len;\
556+ }\
557+ return Name##_brute_force_search_r(&x[0], idx + cstr_len, cstr, cstr_len);\
558+}\
559+\
560+size_t Name##_rfind_c(Type *x, Type c, size_t idx)\
561+{\
562+ assert(x && "String_rfind_c");\
563+ return Name##_rfind(x, &c, idx, 1);\
564+}\
565+\
566+size_t Name##_find_first_of(Type *x, Type *cstr, size_t idx, size_t cstr_len)\
567+{\
568+ size_t i, j;\
569+ size_t size;\
570+ assert(x && "String_find_first_of");\
571+ assert(cstr && "String_find_first_of");\
572+ size = Name##my_strlen(x);\
573+ if (size < idx) return NPOS;\
574+ if (cstr_len == NPOS) {\
575+ cstr_len = Name##my_strlen(cstr);\
576+ }\
577+ for (i = idx; i < size; i++) {\
578+ for (j = 0; j < cstr_len; j++) {\
579+ if (x[i] == cstr[j]) {\
580+ return i;\
581+ }\
582+ }\
583+ }\
584+ return NPOS;\
585+}\
586+\
587+size_t Name##_find_first_of_c(Type *x, Type c, size_t idx)\
588+{\
589+ assert(x && "String_find_first_of_c");\
590+ return Name##_find_first_of(x, &c, idx, 1);\
591+}\
592+\
593+size_t Name##_find_last_of(Type *x, Type *cstr, size_t idx, size_t cstr_len)\
594+{\
595+ size_t i, j;\
596+ size_t size;\
597+ assert(x && "String_find_last_of");\
598+ assert(cstr && "String_find_last_of");\
599+ size = Name##my_strlen(x);\
600+ if (size <= idx) {\
601+ idx = size -1;\
602+ }\
603+ if (cstr_len == NPOS) {\
604+ cstr_len = Name##my_strlen(cstr);\
605+ }\
606+ for (i = idx +1; i > 0; i--) {\
607+ for (j = 0; j < cstr_len; j++) {\
608+ if (x[i-1] == cstr[j]) {\
609+ return i-1;\
610+ }\
611+ }\
612+ }\
613+ return NPOS;\
614+}\
615+\
616+size_t Name##_find_last_of_c(Type *x, Type c, size_t idx)\
617+{\
618+ assert(x && "String_find_last_of_c");\
619+ return Name##_find_last_of(x, &c, idx, 1);\
620+}\
621+\
622+size_t Name##_find_first_not_of(Type *x, Type *cstr, size_t idx, size_t cstr_len)\
623+{\
624+ size_t i, j;\
625+ size_t size;\
626+ assert(x && "String_find_first_not_of");\
627+ assert(cstr && "String_find_first_not_of");\
628+ size = Name##my_strlen(x);\
629+ if (size < idx) return NPOS;\
630+ if (cstr_len == NPOS) {\
631+ cstr_len = Name##my_strlen(cstr);\
632+ }\
633+ if (cstr_len == 0) {\
634+ return idx;\
635+ }\
636+ for (i = idx; i < size; i++) {\
637+ for (j = 0; j < cstr_len; j++) {\
638+ if (x[i] != cstr[j]) {\
639+ if (j == cstr_len - 1) return i;\
640+ } else {\
641+ break;\
642+ }\
643+ }\
644+ }\
645+ return NPOS;\
646+}\
647+\
648+size_t Name##_find_first_not_of_c(Type *x, Type c, size_t idx)\
649+{\
650+ assert(x && "String_find_first_not_of_c");\
651+ return Name##_find_first_not_of(x, &c, idx, 1);\
652+}\
653+\
654+size_t Name##_find_last_not_of(Type *x, Type *cstr, size_t idx, size_t cstr_len)\
655+{\
656+ size_t i, j;\
657+ size_t size;\
658+ assert(x && "String_find_last_not_of");\
659+ assert(cstr && "String_find_last_not_of");\
660+ size = Name##my_strlen(x);\
661+ if (size <= idx) {\
662+ idx = size -1;\
663+ }\
664+ if (cstr_len == NPOS) {\
665+ cstr_len = Name##my_strlen(cstr);\
666+ }\
667+ if (cstr_len == 0) {\
668+ return idx;\
669+ }\
670+ for (i = idx +1; i > 0; i--) {\
671+ for (j = 0; j < cstr_len; j++) {\
672+ if (x[i-1] != cstr[j]) {\
673+ if (j == cstr_len - 1) return i-1;\
674+ } else {\
675+ break;\
676+ }\
677+ }\
678+ }\
679+ return NPOS;\
680+}\
681+\
682+size_t Name##_find_last_not_of_c(Type *x, Type c, size_t idx)\
683+{\
684+ assert(x && "String_find_last_not_of_c");\
685+ return Name##_find_last_not_of(x, &c, idx, 1);\
686+}\
687+\
688+
689+
690+#endif /* CSTL_STRING_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/cstl/set.h (nonexistent)
+++ tags/rel-0.2.1/cstl/set.h (revision 43)
@@ -0,0 +1,205 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file set.h
27+ * \brief set/multisetコンテナ
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-11-11
30+ */
31+#ifndef CSTL_SET_H_INCLUDED
32+#define CSTL_SET_H_INCLUDED
33+
34+#include "rbtree.h"
35+
36+#ifdef __cplusplus
37+#define SET_BEGIN_EXTERN_C() extern "C" {
38+#define SET_END_EXTERN_C() }
39+#else
40+#define SET_BEGIN_EXTERN_C()
41+#define SET_END_EXTERN_C()
42+#endif
43+
44+
45+#define COMMON_SET_IMPLEMENT(Name, Type, Compare, Order) \
46+/*!
47+ * \brief setノード構造体
48+ */\
49+struct Name##RBTreeNode_t {\
50+ struct Name##RBTreeNode_t *parent;\
51+ struct Name##RBTreeNode_t *left;\
52+ struct Name##RBTreeNode_t *right;\
53+ RBTreeColor color;\
54+ Type key;\
55+ RBTREE_MAGIC(void *magic;)\
56+};\
57+\
58+RBTREE_WRAPPER_IMPLEMENT(Name, Type, Type, Compare, Order)\
59+\
60+static Name##RBTreeNode *Name##RBTreeNode_new(Type elem, Type value, RBTreeColor color)\
61+{\
62+ Name##RBTreeNode *self;\
63+ self = (Name##RBTreeNode *) malloc(sizeof(Name##RBTreeNode));\
64+ if (!self) return 0;\
65+ self->key = elem;\
66+ self->left = &Name##RBTree_nil;\
67+ self->right = &Name##RBTree_nil;\
68+ self->parent = &Name##RBTree_nil;\
69+ self->color = color;\
70+ return self;\
71+}\
72+\
73+
74+
75+/*!
76+ * \brief インターフェイスマクロ
77+ *
78+ * \param Name コンテナ名
79+ * \param Type 要素の型
80+ */
81+#define SET_INTERFACE(Name, Type) \
82+\
83+SET_BEGIN_EXTERN_C()\
84+RBTREE_WRAPPER_INTERFACE(Name, Type, Type)\
85+Name##Iterator Name##_insert(Name *self, Type elem, int *success);\
86+SET_END_EXTERN_C()\
87+
88+/*!
89+ * \brief 実装マクロ
90+ *
91+ * \param Name コンテナ名
92+ * \param Type 要素の型
93+ * \param Compare 要素の比較ルーチン
94+ * \param Order ソートの順序
95+ */
96+#define SET_IMPLEMENT(Name, Type, Compare, Order) \
97+COMMON_SET_IMPLEMENT(Name, Type, Compare, Order)\
98+\
99+Name##Iterator Name##_insert(Name *self, Type elem, int *success)\
100+{\
101+ Name##Iterator pos;\
102+ assert(self && "Set_insert");\
103+ assert(self->magic == self && "Set_insert");\
104+ pos = Name##RBTree_find(self->tree, elem);\
105+ if (pos == Name##RBTree_end(self->tree)) {\
106+ pos = Name##RBTree_insert(self->tree, elem, elem);\
107+ if (pos) {\
108+ if (success) *success = 1;\
109+ self->nelems++;\
110+ } else {\
111+ if (success) *success = 0;\
112+ }\
113+ } else {\
114+ if (success) *success = 0;\
115+ }\
116+ return pos;\
117+}\
118+\
119+int Name##_assign(Name *self, Name##Iterator first, Name##Iterator last)\
120+{\
121+ Name *x;\
122+ Name##Iterator pos;\
123+ assert(self && "Set_assign");\
124+ assert(self->magic == self && "Set_assign");\
125+ assert(first && "Set_assign");\
126+ assert(last && "Set_assign");\
127+ assert(first->magic && "Set_assign");\
128+ assert(last->magic && "Set_assign");\
129+ x = Name##_new();\
130+ if (!x) return 0;\
131+ for (pos = first; pos != last; pos = Name##_next(pos)) {\
132+ if (!Name##_insert(x, Name##_key(pos), 0)) {\
133+ Name##_delete(x);\
134+ return 0;\
135+ }\
136+ }\
137+ Name##_swap(self, x);\
138+ Name##_delete(x);\
139+ return 1;\
140+}\
141+\
142+
143+
144+/*!
145+ * \brief インターフェイスマクロ
146+ *
147+ * \param Name コンテナ名
148+ * \param Type 要素の型
149+ */
150+#define MULTISET_INTERFACE(Name, Type) \
151+\
152+SET_BEGIN_EXTERN_C()\
153+RBTREE_WRAPPER_INTERFACE(Name, Type, Type)\
154+Name##Iterator Name##_insert(Name *self, Type elem);\
155+SET_END_EXTERN_C()\
156+
157+/*!
158+ * \brief 実装マクロ
159+ *
160+ * \param Name コンテナ名
161+ * \param Type 要素の型
162+ * \param Compare 要素の比較ルーチン
163+ * \param Order ソートの順序
164+ */
165+#define MULTISET_IMPLEMENT(Name, Type, Compare, Order) \
166+COMMON_SET_IMPLEMENT(Name, Type, Compare, Order)\
167+\
168+Name##Iterator Name##_insert(Name *self, Type elem)\
169+{\
170+ Name##Iterator pos;\
171+ assert(self && "MultiSet_insert");\
172+ assert(self->magic == self && "MultiSet_insert");\
173+ pos = Name##RBTree_insert(self->tree, elem, elem);\
174+ if (pos) {\
175+ self->nelems++;\
176+ }\
177+ return pos;\
178+}\
179+\
180+int Name##_assign(Name *self, Name##Iterator first, Name##Iterator last)\
181+{\
182+ Name *x;\
183+ Name##Iterator pos;\
184+ assert(self && "MultiSet_assign");\
185+ assert(self->magic == self && "MultiSet_assign");\
186+ assert(first && "MultiSet_assign");\
187+ assert(last && "MultiSet_assign");\
188+ assert(first->magic && "MultiSet_assign");\
189+ assert(last->magic && "MultiSet_assign");\
190+ x = Name##_new();\
191+ if (!x) return 0;\
192+ for (pos = first; pos != last; pos = Name##_next(pos)) {\
193+ if (!Name##_insert(x, Name##_key(pos))) {\
194+ Name##_delete(x);\
195+ return 0;\
196+ }\
197+ }\
198+ Name##_swap(self, x);\
199+ Name##_delete(x);\
200+ return 1;\
201+}\
202+\
203+
204+
205+#endif /* CSTL_SET_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/cstl/map.h (nonexistent)
+++ tags/rel-0.2.1/cstl/map.h (revision 43)
@@ -0,0 +1,242 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file map.h
27+ * \brief map/multimapコンテナ
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-11-11
30+ */
31+#ifndef CSTL_MAP_H_INCLUDED
32+#define CSTL_MAP_H_INCLUDED
33+
34+#include "rbtree.h"
35+
36+#ifdef __cplusplus
37+#define MAP_BEGIN_EXTERN_C() extern "C" {
38+#define MAP_END_EXTERN_C() }
39+#else
40+#define MAP_BEGIN_EXTERN_C()
41+#define MAP_END_EXTERN_C()
42+#endif
43+
44+
45+#define COMMON_MAP_IMPLEMENT(Name, KeyType, ValueType, Compare, Order) \
46+/*!
47+ * \brief mapノード構造体
48+ */\
49+struct Name##RBTreeNode_t {\
50+ struct Name##RBTreeNode_t *parent;\
51+ struct Name##RBTreeNode_t *left;\
52+ struct Name##RBTreeNode_t *right;\
53+ RBTreeColor color;\
54+ KeyType key;\
55+ ValueType value;\
56+ RBTREE_MAGIC(void *magic;)\
57+};\
58+\
59+RBTREE_WRAPPER_IMPLEMENT(Name, KeyType, ValueType, Compare, Order)\
60+\
61+static Name##RBTreeNode *Name##RBTreeNode_new(KeyType key, ValueType value, RBTreeColor color)\
62+{\
63+ Name##RBTreeNode *self;\
64+ self = (Name##RBTreeNode *) malloc(sizeof(Name##RBTreeNode));\
65+ if (!self) return 0;\
66+ self->key = key;\
67+ self->value = value;\
68+ self->left = &Name##RBTree_nil;\
69+ self->right = &Name##RBTree_nil;\
70+ self->parent = &Name##RBTree_nil;\
71+ self->color = color;\
72+ return self;\
73+}\
74+\
75+ValueType *Name##_value(Name##Iterator pos)\
76+{\
77+ assert(pos && "Map_value");\
78+ assert(pos->magic && "Map_value");\
79+ assert(!Name##RBTreeNode_is_head(pos) && "Map_value");\
80+ return &pos->value;\
81+}\
82+\
83+
84+
85+/*!
86+ * \brief インターフェイスマクロ
87+ *
88+ * \param Name コンテナ名
89+ * \param KeyType 要素のキーの型
90+ * \param ValueType 要素の値の型
91+ */
92+#define MAP_INTERFACE(Name, KeyType, ValueType) \
93+MAP_BEGIN_EXTERN_C()\
94+RBTREE_WRAPPER_INTERFACE(Name, KeyType, ValueType)\
95+Name##Iterator Name##_insert(Name *self, KeyType key, ValueType value, int *success);\
96+ValueType *Name##_value(Name##Iterator pos);\
97+ValueType *Name##_lookup(Name *self, KeyType key);\
98+MAP_END_EXTERN_C()\
99+
100+/*!
101+ * \brief 実装マクロ
102+ *
103+ * \param Name コンテナ名
104+ * \param KeyType 要素のキーの型
105+ * \param ValueType 要素の値の型
106+ * \param Compare 要素の比較ルーチン
107+ * \param Order ソートの順序
108+ */
109+#define MAP_IMPLEMENT(Name, KeyType, ValueType, Compare, Order) \
110+\
111+COMMON_MAP_IMPLEMENT(Name, KeyType, ValueType, Compare, Order) \
112+\
113+Name##Iterator Name##_insert(Name *self, KeyType key, ValueType value, int *success)\
114+{\
115+ Name##Iterator pos;\
116+ assert(self && "Map_insert");\
117+ assert(self->magic == self && "Map_insert");\
118+ pos = Name##RBTree_find(self->tree, key);\
119+ if (pos == Name##RBTree_end(self->tree)) {\
120+ pos = Name##RBTree_insert(self->tree, key, value);\
121+ if (pos) {\
122+ if (success) *success = 1;\
123+ self->nelems++;\
124+ } else {\
125+ if (success) *success = 0;\
126+ }\
127+ } else {\
128+ if (success) *success = 0;\
129+ }\
130+ return pos;\
131+}\
132+\
133+int Name##_assign(Name *self, Name##Iterator first, Name##Iterator last)\
134+{\
135+ Name *x;\
136+ Name##Iterator pos;\
137+ assert(self && "Map_assign");\
138+ assert(self->magic == self && "Map_assign");\
139+ assert(first && "Map_assign");\
140+ assert(last && "Map_assign");\
141+ assert(first->magic && "Map_assign");\
142+ assert(last->magic && "Map_assign");\
143+ x = Name##_new();\
144+ if (!x) return 0;\
145+ for (pos = first; pos != last; pos = Name##_next(pos)) {\
146+ if (!Name##_insert(x, Name##_key(pos), *Name##_value(pos), 0)) {\
147+ Name##_delete(x);\
148+ return 0;\
149+ }\
150+ }\
151+ Name##_swap(self, x);\
152+ Name##_delete(x);\
153+ return 1;\
154+}\
155+\
156+ValueType *Name##_lookup(Name *self, KeyType key)\
157+{\
158+ Name##Iterator pos;\
159+ assert(self && "Map_lookup");\
160+ assert(self->magic == self && "Map_lookup");\
161+ pos = Name##RBTree_find(self->tree, key);\
162+ if (pos == Name##RBTree_end(self->tree)) {\
163+ static ValueType value; /* 新しい要素の値 */\
164+ pos = Name##RBTree_insert(self->tree, key, value);\
165+ if (pos) {\
166+ self->nelems++;\
167+ } else {\
168+ /* メモリ不足 */\
169+ assert(0 && "Map_lookup");\
170+ return 0;\
171+ }\
172+ }\
173+ return &pos->value;\
174+}\
175+\
176+
177+
178+/*!
179+ * \brief インターフェイスマクロ
180+ *
181+ * \param Name コンテナ名
182+ * \param KeyType 要素のキーの型
183+ * \param ValueType 要素の値の型
184+ */
185+#define MULTIMAP_INTERFACE(Name, KeyType, ValueType) \
186+MAP_BEGIN_EXTERN_C()\
187+RBTREE_WRAPPER_INTERFACE(Name, KeyType, ValueType)\
188+Name##Iterator Name##_insert(Name *self, KeyType key, ValueType value);\
189+ValueType *Name##_value(Name##Iterator pos);\
190+MAP_END_EXTERN_C()\
191+
192+/*!
193+ * \brief 実装マクロ
194+ *
195+ * \param Name コンテナ名
196+ * \param KeyType 要素のキーの型
197+ * \param ValueType 要素の値の型
198+ * \param Compare 要素の比較ルーチン
199+ * \param Order ソートの順序
200+ */
201+#define MULTIMAP_IMPLEMENT(Name, KeyType, ValueType, Compare, Order) \
202+\
203+COMMON_MAP_IMPLEMENT(Name, KeyType, ValueType, Compare, Order) \
204+\
205+Name##Iterator Name##_insert(Name *self, KeyType key, ValueType value)\
206+{\
207+ Name##Iterator pos;\
208+ assert(self && "MultiMap_insert");\
209+ assert(self->magic == self && "MultiMap_insert");\
210+ pos = Name##RBTree_insert(self->tree, key, value);\
211+ if (pos) {\
212+ self->nelems++;\
213+ }\
214+ return pos;\
215+}\
216+\
217+int Name##_assign(Name *self, Name##Iterator first, Name##Iterator last)\
218+{\
219+ Name *x;\
220+ Name##Iterator pos;\
221+ assert(self && "MultiMap_assign");\
222+ assert(self->magic == self && "MultiMap_assign");\
223+ assert(first && "MultiMap_assign");\
224+ assert(last && "MultiMap_assign");\
225+ assert(first->magic && "MultiMap_assign");\
226+ assert(last->magic && "MultiMap_assign");\
227+ x = Name##_new();\
228+ if (!x) return 0;\
229+ for (pos = first; pos != last; pos = Name##_next(pos)) {\
230+ if (!Name##_insert(x, Name##_key(pos), *Name##_value(pos))) {\
231+ Name##_delete(x);\
232+ return 0;\
233+ }\
234+ }\
235+ Name##_swap(self, x);\
236+ Name##_delete(x);\
237+ return 1;\
238+}\
239+\
240+
241+
242+#endif /* CSTL_MAP_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/cstl/rbtree.h (nonexistent)
+++ tags/rel-0.2.1/cstl/rbtree.h (revision 43)
@@ -0,0 +1,985 @@
1+/*
2+ * Copyright (c) 2006, KATO Noriaki
3+ * All rights reserved.
4+ *
5+ * Redistribution and use in source and binary forms, with or without
6+ * modification, are permitted provided that the following conditions are met:
7+ *
8+ * 1. Redistributions of source code must retain the above copyright notice,
9+ * this list of conditions and the following disclaimer.
10+ * 2. Redistributions in binary form must reproduce the above copyright notice,
11+ * this list of conditions and the following disclaimer in the documentation
12+ * and/or other materials provided with the distribution.
13+ *
14+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15+ * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17+ * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
18+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
21+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
23+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24+ */
25+/*!
26+ * \file rbtree.h
27+ * \brief 赤黒木
28+ * \author KATO Noriaki <katono@users.sourceforge.jp>
29+ * \date 2006-10-29
30+ *
31+ * このファイルを直接インクルードしないこと
32+ */
33+#ifndef CSTL_RBTREE_H_INCLUDED
34+#define CSTL_RBTREE_H_INCLUDED
35+
36+#include <stddef.h>
37+#include <stdlib.h>
38+#include <assert.h>
39+
40+#ifndef NDEBUG
41+#define RBTREE_MAGIC(x) x
42+#else
43+#define RBTREE_MAGIC(x)
44+#endif
45+
46+/*!
47+ * \brief 赤黒木の色
48+ */
49+typedef enum RBTreeColor_t {
50+ RBTREE_RED,
51+ RBTREE_BLACK,
52+ RBTREE_HEAD
53+} RBTreeColor;
54+
55+/*!
56+ * \brief ソートの順序
57+ */
58+enum {
59+ ASC = 0,
60+ DESC
61+};
62+
63+/*!
64+ * \brief 単純比較ルーチン
65+ */
66+#define SIMPLE_CMP(x, y) ((x) == (y) ? 0 : (x) > (y) ? 1 : -1)
67+
68+
69+#define RBTREE_IMPLEMENT(Name, KeyType, ValueType, Compare, Order) \
70+\
71+typedef struct Name##Node_t Name##Node;\
72+typedef struct Name##Node_t *Name##Iterator;\
73+static Name##Node Name##_nil = {&Name##_nil, &Name##_nil, &Name##_nil, RBTREE_BLACK};\
74+\
75+static int Name##_cmp(KeyType x, KeyType y, int order)\
76+{\
77+ return order ? Compare(y, x) : Compare(x, y);\
78+}\
79+\
80+\
81+static Name *Name##_new(void);\
82+static void Name##_delete(Name *self);\
83+static void Name##_clear(Name *self);\
84+static int Name##_empty(Name *self);\
85+static Name##Iterator Name##_insert(Name *self, KeyType key, ValueType value);\
86+static void Name##_erase(Name *self, Name##Iterator pos);\
87+static size_t Name##_count(Name *self, KeyType key);\
88+static Name##Iterator Name##_find(Name *self, KeyType key);\
89+static Name##Iterator Name##_lower_bound(Name *self, KeyType key);\
90+static Name##Iterator Name##_upper_bound(Name *self, KeyType key);\
91+static Name##Iterator Name##_begin(Name *self);\
92+static Name##Iterator Name##_end(Name *self);\
93+static Name##Iterator Name##_rbegin(Name *self);\
94+static Name##Iterator Name##_rend(Name *self);\
95+static Name##Iterator Name##_next(Name##Iterator pos);\
96+static Name##Iterator Name##_prev(Name##Iterator pos);\
97+\
98+static Name##Node *Name##Node_new(KeyType key, ValueType value, RBTreeColor color);\
99+static int Name##Node_is_head(Name##Node *self);\
100+static int Name##Node_is_root(Name##Node *self);\
101+static int Name##Node_is_nil(Name##Node *self);\
102+static void Name##Node_set_left(Name##Node *self, Name##Node *t);\
103+static void Name##Node_set_right(Name##Node *self, Name##Node *t);\
104+static Name##Node *Name##_get_root(Name *self);\
105+static void Name##_set_root(Name *self, Name##Node *t);\
106+static Name##Node *Name##Node_find(Name *self, Name##Node *t, KeyType key);\
107+static Name##Node *Name##Node_replace_subtree(Name##Node *self, Name##Node *t);\
108+static void Name##Node_swap_parent_child(Name##Node *p, Name##Node *c);\
109+static void Name##Node_swap(Name##Node *s, Name##Node *t);\
110+static void Name##Node_rotate_right(Name##Node *self);\
111+static void Name##Node_rotate_left(Name##Node *self);\
112+static Name##Node *Name##Node_get_sibling(Name##Node *self);\
113+static Name##Node *Name##Node_get_uncle(Name##Node *self);\
114+static void Name##Node_balance_for_insert(Name##Node *n);\
115+static void Name##Node_balance_for_erase(Name##Node *n, Name##Node *p_of_n);\
116+\
117+\
118+static int Name##Node_is_head(Name##Node *self)\
119+{\
120+ assert(self && "RBTreeNode_is_head");\
121+ return (self->color == RBTREE_HEAD);\
122+}\
123+\
124+static int Name##Node_is_root(Name##Node *self)\
125+{\
126+ assert(self && "RBTreeNode_is_root");\
127+ return Name##Node_is_head(self->parent);\
128+}\
129+\
130+static int Name##Node_is_nil(Name##Node *self)\
131+{\
132+ assert(self && "RBTreeNode_is_nil");\
133+ return (self == &Name##_nil);\
134+}\
135+\
136+static void Name##Node_set_left(Name##Node *self, Name##Node *t)\
137+{\
138+ assert(self && "RBTreeNode_set_left");\
139+ self->left = t;\
140+ if (!Name##Node_is_nil(t)) {\
141+ t->parent = self;\
142+ }\
143+}\
144+\
145+static void Name##Node_set_right(Name##Node *self, Name##Node *t)\
146+{\
147+ assert(self && "RBTreeNode_set_right");\
148+ self->right = t;\
149+ if (!Name##Node_is_nil(t)) {\
150+ t->parent = self;\
151+ }\
152+}\
153+\
154+static Name##Node *Name##_get_root(Name *self)\
155+{\
156+ assert(Name##Node_is_head(self) && "RBTree_get_root");\
157+ return self->right;\
158+}\
159+\
160+static void Name##_set_root(Name *self, Name##Node *t)\
161+{\
162+ assert(Name##Node_is_head(self) && "RBTree_set_root");\
163+ self->right = t;\
164+ if (!Name##Node_is_nil(t)) {\
165+ t->parent = self;\
166+ }\
167+}\
168+\
169+static Name *Name##_new(void)\
170+{\
171+ Name *self;\
172+ self = (Name *) malloc(sizeof(Name));\
173+ if (!self) return 0;\
174+ self->left = &Name##_nil;\
175+ self->right = &Name##_nil;\
176+ self->parent = &Name##_nil;\
177+ self->color = RBTREE_HEAD;\
178+ RBTREE_MAGIC(self->magic = self;)\
179+ return self;\
180+}\
181+\
182+static void Name##_clear(Name *self)\
183+{\
184+ Name##Node *t;\
185+ Name##Node *tmp;\
186+ assert(Name##Node_is_head(self) && "RBTree_clear");\
187+ t = Name##_get_root(self);\
188+ if (Name##Node_is_nil(t)) return;\
189+ while (1) {\
190+ if (!Name##Node_is_nil(t->left)) {\
191+ t = t->left;\
192+ continue;\
193+ }\
194+ if (!Name##Node_is_nil(t->right)) {\
195+ t = t->right;\
196+ continue;\
197+ }\
198+ if (t == t->parent->left) {\
199+ t->parent->left = &Name##_nil;\
200+ } else {\
201+ t->parent->right = &Name##_nil;\
202+ }\
203+ tmp = t->parent;\
204+ RBTREE_MAGIC(t->magic = 0;)\
205+ free(t);\
206+ t = tmp;\
207+ if (Name##Node_is_head(t)) break;\
208+ }\
209+}\
210+\
211+static void Name##_delete(Name *self)\
212+{\
213+ assert(Name##Node_is_head(self) && "RBTree_delete");\
214+ Name##_clear(self);\
215+ RBTREE_MAGIC(self->magic = 0;)\
216+ free(self);\
217+}\
218+\
219+static int Name##_empty(Name *self)\
220+{\
221+ assert(Name##Node_is_head(self) && "RBTree_empty");\
222+ return Name##Node_is_nil(Name##_get_root(self));\
223+}\
224+\
225+static Name##Node *Name##Node_find(Name *self, Name##Node *t, KeyType key)\
226+{\
227+ int cmp;\
228+ while (!Name##Node_is_nil(t)) {\
229+ cmp = Name##_cmp(key, t->key, Order);\
230+ if (cmp < 0) {\
231+ t = t->left;\
232+ } else if (cmp > 0) {\
233+ t = t->right;\
234+ } else {\
235+ break;\
236+ }\
237+ }\
238+ return t;\
239+}\
240+\
241+static Name##Iterator Name##_find(Name *self, KeyType key)\
242+{\
243+ Name##Node *t;\
244+ assert(Name##Node_is_head(self) && "RBTree_find");\
245+ t = Name##Node_find(self, Name##_get_root(self), key);\
246+ return Name##Node_is_nil(t) ? Name##_end(self) : t;\
247+}\
248+\
249+static size_t Name##_count(Name *self, KeyType key)\
250+{\
251+ Name##Iterator first;\
252+ Name##Iterator last;\
253+ Name##Iterator pos;\
254+ size_t count = 0;\
255+ assert(Name##Node_is_head(self) && "RBTree_count");\
256+ first = Name##_lower_bound(self, key);\
257+ last = Name##_upper_bound(self, key);\
258+ for (pos = first; pos != last; pos = Name##_next(pos)) {\
259+ count++;\
260+ }\
261+ return count;\
262+}\
263+\
264+static Name##Iterator Name##_lower_bound(Name *self, KeyType key)\
265+{\
266+ Name##Node *t;\
267+ Name##Node *tmp;\
268+ assert(Name##Node_is_head(self) && "RBTree_lower_bound");\
269+ tmp = Name##_end(self);\
270+ t = Name##_get_root(self);\
271+ while (!Name##Node_is_nil(t)) {\
272+ if (Name##_cmp(key, t->key, Order) <= 0) {\
273+ tmp = t;\
274+ t = t->left;\
275+ } else {\
276+ t = t->right;\
277+ }\
278+ }\
279+ return tmp;\
280+}\
281+\
282+static Name##Iterator Name##_upper_bound(Name *self, KeyType key)\
283+{\
284+ Name##Node *t;\
285+ Name##Node *tmp;\
286+ assert(Name##Node_is_head(self) && "RBTree_upper_bound");\
287+ tmp = Name##_end(self);\
288+ t = Name##_get_root(self);\
289+ while (!Name##Node_is_nil(t)) {\
290+ if (Name##_cmp(key, t->key, Order) < 0) {\
291+ tmp = t;\
292+ t = t->left;\
293+ } else {\
294+ t = t->right;\
295+ }\
296+ }\
297+ return tmp;\
298+}\
299+\
300+static Name##Node *Name##Node_replace_subtree(Name##Node *self, Name##Node *t)\
301+{\
302+ assert(!Name##Node_is_head(self) && "RBTreeNode_replace_subtree");\
303+ assert(!Name##Node_is_head(t) && "RBTreeNode_replace_subtree");\
304+ if (self->parent->left == self) {\
305+ Name##Node_set_left(self->parent, t);\
306+ } else {\
307+ Name##Node_set_right(self->parent, t);\
308+ }\
309+ return self;\
310+}\
311+\
312+static void Name##Node_swap_parent_child(Name##Node *p, Name##Node *c)\
313+{\
314+ Name##Node *cl;\
315+ Name##Node *cr;\
316+ if (p->parent->left == p) {\
317+ Name##Node_set_left(p->parent, c);\
318+ } else {\
319+ Name##Node_set_right(p->parent, c);\
320+ }\
321+ cl = c->left;\
322+ cr = c->right;\
323+ if (p->left == c) {\
324+ Name##Node_set_left(c, p);\
325+ Name##Node_set_right(c, p->right);\
326+ } else {\
327+ Name##Node_set_right(c, p);\
328+ Name##Node_set_left(c, p->left);\
329+ }\
330+ Name##Node_set_left(p, cl);\
331+ Name##Node_set_right(p, cr);\
332+}\
333+\
334+static void Name##Node_swap(Name##Node *s, Name##Node *t)\
335+{\
336+ Name##Node *tp;\
337+ Name##Node *tl;\
338+ Name##Node *tr;\
339+ RBTreeColor c;\
340+ assert(!Name##Node_is_head(s) && "RBTreeNode_swap");\
341+ assert(!Name##Node_is_head(t) && "RBTreeNode_swap");\
342+ assert(!Name##Node_is_nil(s) && "RBTreeNode_swap");\
343+ assert(!Name##Node_is_nil(t) && "RBTreeNode_swap");\
344+ if (t->parent == s) {\
345+ Name##Node_swap_parent_child(s, t);\
346+ } else if (s->parent == t) {\
347+ Name##Node_swap_parent_child(t, s);\
348+ } else {\
349+ tp = t->parent;\
350+ tl = t->left;\
351+ tr = t->right;\
352+ if (s->parent->left == s) {\
353+ Name##Node_set_left(s->parent, t);\
354+ } else {\
355+ Name##Node_set_right(s->parent, t);\
356+ }\
357+ Name##Node_set_left(t, s->left);\
358+ Name##Node_set_right(t, s->right);\
359+ if (tp->left == t) {\
360+ Name##Node_set_left(tp, s);\
361+ } else {\
362+ Name##Node_set_right(tp, s);\
363+ }\
364+ Name##Node_set_left(s, tl);\
365+ Name##Node_set_right(s, tr);\
366+ }\
367+ c = s->color;\
368+ s->color = t->color;\
369+ t->color = c;\
370+}\
371+\
372+static void Name##Node_rotate_right(Name##Node *self)\
373+{\
374+ Name##Node *p;\
375+ Name##Node *n;\
376+ assert(!Name##Node_is_head(self) && "RBTreeNode_rotate_right");\
377+ assert(!Name##Node_is_nil(self) && "RBTreeNode_rotate_right");\
378+ p = self->parent;\
379+ n = self->left;\
380+ assert(!Name##Node_is_nil(n) && "RBTreeNode_rotate_right");\
381+ Name##Node_set_left(self, n->right);\
382+ Name##Node_set_right(n, self);\
383+ if (p->left == self) {\
384+ Name##Node_set_left(p, n);\
385+ } else {\
386+ Name##Node_set_right(p, n);\
387+ }\
388+}\
389+\
390+static void Name##Node_rotate_left(Name##Node *self)\
391+{\
392+ Name##Node *p;\
393+ Name##Node *n;\
394+ assert(!Name##Node_is_head(self) && "RBTreeNode_rotate_left");\
395+ assert(!Name##Node_is_nil(self) && "RBTreeNode_rotate_left");\
396+ p = self->parent;\
397+ n = self->right;\
398+ assert(!Name##Node_is_nil(n) && "RBTreeNode_rotate_left");\
399+ Name##Node_set_right(self, n->left);\
400+ Name##Node_set_left(n, self);\
401+ if (p->left == self) {\
402+ Name##Node_set_left(p, n);\
403+ } else {\
404+ Name##Node_set_right(p, n);\
405+ }\
406+}\
407+\
408+static Name##Node *Name##Node_get_sibling(Name##Node *self)\
409+{\
410+ assert(!Name##Node_is_head(self) && "RBTreeNode_get_sibling");\
411+ assert(!Name##Node_is_nil(self) && "RBTreeNode_get_sibling");\
412+ assert(!Name##Node_is_root(self) && "RBTreeNode_get_sibling");\
413+ return (self->parent->left == self) ? \
414+ self->parent->right : self->parent->left;\
415+}\
416+\
417+static Name##Node *Name##Node_get_uncle(Name##Node *self)\
418+{\
419+ Name##Node *g;\
420+ assert(!Name##Node_is_head(self) && "RBTreeNode_get_uncle");\
421+ assert(!Name##Node_is_nil(self) && "RBTreeNode_get_uncle");\
422+ assert(!Name##Node_is_root(self) && "RBTreeNode_get_uncle");\
423+ assert(!Name##Node_is_root(self->parent) && "RBTreeNode_get_uncle");\
424+ g = self->parent->parent;\
425+ assert(!Name##Node_is_nil(g) && "RBTreeNode_get_uncle");\
426+ assert(!Name##Node_is_head(g) && "RBTreeNode_get_uncle");\
427+ return (g->left == self->parent) ?\
428+ g->right : g->left;\
429+}\
430+\
431+static void Name##Node_balance_for_insert(Name##Node *n)\
432+{\
433+ Name##Node *p;\
434+ Name##Node *g;\
435+ Name##Node *u;\
436+ Name##Node *tmp;\
437+ while (1) {\
438+ p = n->parent;\
439+ if (Name##Node_is_root(n)) {\
440+ /* case 1 nがroot */\
441+ n->color = RBTREE_BLACK;\
442+ break;\
443+ }\
444+ if (p->color == RBTREE_BLACK) {\
445+ /* case 2 pが黒 */\
446+ break;\
447+ }\
448+ /* 以下、pは赤、gは黒 */\
449+ g = p->parent;\
450+ u = Name##Node_get_uncle(n);\
451+ if (u->color == RBTREE_RED) {\
452+ /* case 3 uが赤 */\
453+ g->color = RBTREE_RED;\
454+ p->color = RBTREE_BLACK;\
455+ u->color = RBTREE_BLACK;\
456+ n = g;\
457+ /* gをnにして再調整。最悪の場合、根まで伝播 */\
458+ continue;\
459+ }\
460+ /* 以下、uは黒 */\
461+ if (g->left == p && p->right == n) {\
462+ /* case 4 pがgのleft */\
463+ Name##Node_rotate_left(p);\
464+ tmp = p;\
465+ p = n;\
466+ n = tmp;\
467+ /* case 5 leftへ */\
468+ } else if (g->right == p && p->left == n) {\
469+ /* case 4 pがgのright */\
470+ Name##Node_rotate_right(p);\
471+ tmp = p;\
472+ p = n;\
473+ n = tmp;\
474+ /* case 5 rightへ */\
475+ }\
476+ if (g->left == p) {\
477+ /* case 5 left */\
478+ Name##Node_rotate_right(g);\
479+ } else if (g->right == p) {\
480+ /* case 5 right */\
481+ Name##Node_rotate_left(g);\
482+ } else {\
483+ assert(0 && "RBTreeNode_balance_for_insert");\
484+ }\
485+ p->color = RBTREE_BLACK;\
486+ g->color = RBTREE_RED;\
487+ break;\
488+ }\
489+}\
490+\
491+static Name##Iterator Name##_insert(Name *self, KeyType key, ValueType value)\
492+{\
493+ Name##Node *n;\
494+ Name##Node *tmp;\
495+ assert(Name##Node_is_head(self) && "RBTree_insert");\
496+ n = Name##_get_root(self);\
497+ if (Name##Node_is_nil(n)) {\
498+ /* rootになる */\
499+ Name##Node *root = Name##Node_new(key, value, RBTREE_BLACK);\
500+ if (!root) return 0; /* メモリ不足 */\
501+ Name##_set_root(self, root);\
502+ RBTREE_MAGIC(root->magic = self;)\
503+ return root;\
504+ }\
505+ /* 2分探索木の挿入 */\
506+ while (!Name##Node_is_nil(n)) {\
507+ tmp = n;\
508+ if (Name##_cmp(key, n->key, Order) < 0) {\
509+ n = n->left;\
510+ } else {\
511+ /* 同じ値なら右へ */\
512+ n = n->right;\
513+ }\
514+ }\
515+ n = Name##Node_new(key, value, RBTREE_RED);\
516+ if (!n) return 0; /* メモリ不足 */\
517+ if (Name##_cmp(key, tmp->key, Order) < 0) {\
518+ Name##Node_set_left(tmp, n);\
519+ } else {\
520+ Name##Node_set_right(tmp, n);\
521+ }\
522+ Name##Node_balance_for_insert(n);\
523+ RBTREE_MAGIC(n->magic = self;)\
524+ return n;\
525+}\
526+\
527+static void Name##Node_balance_for_erase(Name##Node *n, Name##Node *p_of_n)\
528+{\
529+ Name##Node *p;\
530+ Name##Node *s;\
531+ Name##Node *sl;\
532+ Name##Node *sr;\
533+ RBTreeColor c;\
534+ if (n->color == RBTREE_RED) {\
535+ /* case 0 nが赤 */\
536+ n->color = RBTREE_BLACK;\
537+ return;\
538+ }\
539+ while (1) {\
540+ if (Name##Node_is_root(n)) {\
541+ /* case 1 nがroot */\
542+ n->color = RBTREE_BLACK;\
543+ break;\
544+ }\
545+ if (Name##Node_is_nil(n)) {\
546+ assert(!(Name##Node_is_nil(p_of_n->left) && Name##Node_is_nil(p_of_n->right)) && "RBTreeNode_balance_for_erase");\
547+ p = p_of_n;\
548+ s = (n == p_of_n->left) ? p_of_n->right : p_of_n->left;\
549+ } else {\
550+ p = n->parent;\
551+ s = Name##Node_get_sibling(n);\
552+ }\
553+ assert(!Name##Node_is_nil(s) && "RBTreeNode_balance_for_erase");\
554+ if (s->color == RBTREE_RED) {\
555+ /* case 2 sが赤 */\
556+ p->color = RBTREE_RED;\
557+ s->color = RBTREE_BLACK;\
558+ if (p->left == n) {\
559+ Name##Node_rotate_left(p);\
560+ s = p->right;\
561+ } else {\
562+ Name##Node_rotate_right(p);\
563+ s = p->left;\
564+ }\
565+ }\
566+ /* 以下、sは黒 */\
567+ assert(!Name##Node_is_nil(s) && "RBTreeNode_balance_for_erase");\
568+ sl = s->left;\
569+ sr = s->right;\
570+ if (p->color == RBTREE_BLACK && sl->color == RBTREE_BLACK && sr->color == RBTREE_BLACK) {\
571+ /* case 3 */\
572+ s->color = RBTREE_RED;\
573+ n = p;\
574+ /* pをnにして再調整。最悪の場合、根まで伝播 */\
575+ continue;\
576+ }\
577+ if (p->color == RBTREE_RED && sl->color == RBTREE_BLACK && sr->color == RBTREE_BLACK) {\
578+ /* case 4 */\
579+ p->color = RBTREE_BLACK;\
580+ s->color = RBTREE_RED;\
581+ break;\
582+ }\
583+ if (p->left == n && sl->color == RBTREE_RED && sr->color == RBTREE_BLACK) {\
584+ /* case 5 nがpのleft */\
585+ sl->color = RBTREE_BLACK;\
586+ s->color = RBTREE_RED;\
587+ Name##Node_rotate_right(s);\
588+ sr = s;\
589+ s = sl;\
590+ /* case 6 leftへ */\
591+ } else if (p->right == n && sl->color == RBTREE_BLACK && sr->color == RBTREE_RED) {\
592+ /* case 5 nがpのright */\
593+ sr->color = RBTREE_BLACK;\
594+ s->color = RBTREE_RED;\
595+ Name##Node_rotate_left(s);\
596+ sl = s;\
597+ s = sr;\
598+ /* case 6 rightへ */\
599+ }\
600+ if (p->left == n && sr->color == RBTREE_RED) {\
601+ /* case 6 left */\
602+ sr->color = RBTREE_BLACK;\
603+ Name##Node_rotate_left(p);\
604+ } else if (p->right == n && sl->color == RBTREE_RED) {\
605+ /* case 6 right */\
606+ sl->color = RBTREE_BLACK;\
607+ Name##Node_rotate_right(p);\
608+ } else {\
609+ assert(0 && "RBTreeNode_balance_for_erase");\
610+ }\
611+ c = p->color;\
612+ p->color = s->color;\
613+ s->color = c;\
614+ break;\
615+ }\
616+}\
617+\
618+static void Name##_erase(Name *self, Name##Iterator pos)\
619+{\
620+ Name##Node *n;\
621+ Name##Node *x;\
622+ assert(Name##Node_is_head(self) && "RBTree_erase");\
623+ assert(!Name##Node_is_head(pos) && "RBTree_erase");\
624+ n = pos;\
625+ assert(!Name##Node_is_nil(n) && "RBTree_erase");\
626+ if (Name##Node_is_nil(n->left) && Name##Node_is_nil(n->right)) {\
627+ if (Name##Node_is_root(n)) {\
628+ /* 最後の一つを削除 */\
629+ Name##_set_root(self, &Name##_nil);\
630+ } else {\
631+ n = Name##Node_replace_subtree(n, &Name##_nil);\
632+ if (n->color == RBTREE_BLACK) {\
633+ Name##Node_balance_for_erase(&Name##_nil, n->parent);\
634+ }\
635+ }\
636+ goto end;\
637+ }\
638+ if (Name##Node_is_nil(n->left)) {\
639+ n = Name##Node_replace_subtree(n, n->right);\
640+ if (n->color == RBTREE_BLACK) {\
641+ assert(!Name##Node_is_nil(n->right) && "RBTree_erase");\
642+ Name##Node_balance_for_erase(n->right, 0);\
643+ }\
644+ goto end;\
645+ }\
646+ if (Name##Node_is_nil(n->right)) {\
647+ n = Name##Node_replace_subtree(n, n->left);\
648+ if (n->color == RBTREE_BLACK) {\
649+ assert(!Name##Node_is_nil(n->left) && "RBTree_erase");\
650+ Name##Node_balance_for_erase(n->left, 0);\
651+ }\
652+ goto end;\
653+ }\
654+ assert(!(Name##Node_is_nil(n->left) || Name##Node_is_nil(n->right)) && "RBTree_erase");\
655+ x = n->left;\
656+ while (!Name##Node_is_nil(x->right)) {\
657+ x = x->right;\
658+ }\
659+ Name##Node_swap(n, x);\
660+ n = Name##Node_replace_subtree(n, n->left);\
661+ if (n->color == RBTREE_BLACK) {\
662+ assert(!Name##Node_is_nil(n) && "RBTree_erase");\
663+ Name##Node_balance_for_erase(n->left, n->parent);\
664+ }\
665+end:\
666+ RBTREE_MAGIC(n->magic = 0;)\
667+ free(n);\
668+}\
669+\
670+static Name##Iterator Name##_begin(Name *self)\
671+{\
672+ Name##Node *t;\
673+ Name##Node *tmp;\
674+ assert(Name##Node_is_head(self) && "RBTree_begin");\
675+ tmp = Name##_end(self);\
676+ t = Name##_get_root(self);\
677+ while (!Name##Node_is_nil(t)) {\
678+ tmp = t;\
679+ t = t->left;\
680+ }\
681+ return tmp;\
682+}\
683+\
684+static Name##Iterator Name##_end(Name *self)\
685+{\
686+ assert(Name##Node_is_head(self) && "RBTree_end");\
687+ return self;\
688+}\
689+\
690+static Name##Iterator Name##_rbegin(Name *self)\
691+{\
692+ Name##Node *t;\
693+ Name##Node *tmp;\
694+ assert(Name##Node_is_head(self) && "RBTree_rbegin");\
695+ tmp = Name##_rend(self);\
696+ t = Name##_get_root(self);\
697+ while (!Name##Node_is_nil(t)) {\
698+ tmp = t;\
699+ t = t->right;\
700+ }\
701+ return tmp;\
702+}\
703+\
704+static Name##Iterator Name##_rend(Name *self)\
705+{\
706+ assert(Name##Node_is_head(self) && "RBTree_rend");\
707+ return self;\
708+}\
709+\
710+static Name##Iterator Name##_next(Name##Iterator pos)\
711+{\
712+ assert(!Name##Node_is_head(pos) && "RBTree_next");\
713+ assert(!Name##Node_is_nil(pos) && "RBTree_next");\
714+ /* 下位検索 */\
715+ if (!Name##Node_is_nil(pos->right)) {\
716+ pos = pos->right;\
717+ while (!Name##Node_is_nil(pos->left)) {\
718+ pos = pos->left;\
719+ }\
720+ return pos;\
721+ }\
722+ /* 上位検索 */\
723+ while (!Name##Node_is_root(pos) && pos == pos->parent->right) {\
724+ pos = pos->parent;\
725+ }\
726+ /* 引数のposがrbegin()の時、pos->parentはend()となる。 */\
727+ return pos->parent;\
728+}\
729+\
730+static Name##Iterator Name##_prev(Name##Iterator pos)\
731+{\
732+ assert(!Name##Node_is_head(pos) && "RBTree_prev");\
733+ assert(!Name##Node_is_nil(pos) && "RBTree_prev");\
734+ /* 下位検索 */\
735+ if (!Name##Node_is_nil(pos->left)) {\
736+ pos = pos->left;\
737+ while (!Name##Node_is_nil(pos->right)) {\
738+ pos = pos->right;\
739+ }\
740+ return pos;\
741+ }\
742+ /* 上位検索 */\
743+ while (!Name##Node_is_root(pos) && pos == pos->parent->left) {\
744+ pos = pos->parent;\
745+ }\
746+ /* 引数のposがbegin()の時、pos->parentはrend()となる。 */\
747+ return pos->parent;\
748+}\
749+\
750+
751+
752+
753+#define RBTREE_WRAPPER_INTERFACE(Name, KeyType, ValueType) \
754+\
755+typedef struct Name##_t Name;\
756+typedef struct Name##RBTreeNode_t *Name##Iterator;\
757+\
758+Name *Name##_new(void);\
759+void Name##_delete(Name *self);\
760+int Name##_assign(Name *self, Name##Iterator first, Name##Iterator last);\
761+void Name##_clear(Name *self);\
762+int Name##_empty(Name *self);\
763+size_t Name##_size(Name *self);\
764+Name##Iterator Name##_erase(Name *self, Name##Iterator pos);\
765+Name##Iterator Name##_erase_range(Name *self, Name##Iterator first, Name##Iterator last);\
766+size_t Name##_erase_key(Name *self, KeyType key);\
767+size_t Name##_count(Name *self, KeyType key);\
768+Name##Iterator Name##_find(Name *self, KeyType key);\
769+Name##Iterator Name##_lower_bound(Name *self, KeyType key);\
770+Name##Iterator Name##_upper_bound(Name *self, KeyType key);\
771+Name##Iterator Name##_begin(Name *self);\
772+Name##Iterator Name##_end(Name *self);\
773+Name##Iterator Name##_rbegin(Name *self);\
774+Name##Iterator Name##_rend(Name *self);\
775+Name##Iterator Name##_next(Name##Iterator pos);\
776+Name##Iterator Name##_prev(Name##Iterator pos);\
777+KeyType Name##_key(Name##Iterator pos);\
778+void Name##_swap(Name *self, Name *x);\
779+
780+
781+#define RBTREE_WRAPPER_IMPLEMENT(Name, KeyType, ValueType, Compare, Order) \
782+\
783+typedef struct Name##RBTreeNode_t Name##RBTree;\
784+/*!
785+ * \brief set/map構造体
786+ */\
787+struct Name##_t {\
788+ Name##RBTree *tree;\
789+ size_t nelems;\
790+ RBTREE_MAGIC(void *magic;)\
791+};\
792+\
793+RBTREE_IMPLEMENT(Name##RBTree, KeyType, ValueType, Compare, Order)\
794+\
795+Name *Name##_new(void)\
796+{\
797+ Name *self;\
798+ self = (Name *) malloc(sizeof(Name));\
799+ if (!self) return 0;\
800+ self->tree = Name##RBTree_new();\
801+ if (!self->tree) {\
802+ free(self);\
803+ return 0;\
804+ }\
805+ self->nelems = 0;\
806+ RBTREE_MAGIC(self->magic = self;)\
807+ return self;\
808+}\
809+\
810+void Name##_delete(Name *self)\
811+{\
812+ assert(self && "RBTree_delete");\
813+ assert(self->magic == self && "RBTree_delete");\
814+ Name##RBTree_delete(self->tree);\
815+ RBTREE_MAGIC(self->magic = 0;)\
816+ free(self);\
817+}\
818+\
819+void Name##_clear(Name *self)\
820+{\
821+ assert(self && "RBTree_clear");\
822+ assert(self->magic == self && "RBTree_clear");\
823+ Name##RBTree_clear(self->tree);\
824+ self->nelems = 0;\
825+}\
826+\
827+int Name##_empty(Name *self)\
828+{\
829+ assert(self && "RBTree_empty");\
830+ assert(self->magic == self && "RBTree_empty");\
831+ return Name##RBTree_empty(self->tree);\
832+}\
833+\
834+size_t Name##_size(Name *self)\
835+{\
836+ assert(self && "RBTree_size");\
837+ assert(self->magic == self && "RBTree_size");\
838+ return self->nelems;\
839+}\
840+\
841+Name##Iterator Name##_erase(Name *self, Name##Iterator pos)\
842+{\
843+ Name##Iterator tmp;\
844+ assert(self && "RBTree_erase");\
845+ assert(self->magic == self && "RBTree_erase");\
846+ assert(pos && "RBTree_erase");\
847+ assert(pos != self->tree && "RBTree_erase");\
848+ assert(pos->magic == self->tree && "RBTree_erase");\
849+ tmp = Name##_next(pos);\
850+ Name##RBTree_erase(self->tree, pos);\
851+ self->nelems--;\
852+ return tmp;\
853+}\
854+\
855+Name##Iterator Name##_erase_range(Name *self, Name##Iterator first, Name##Iterator last)\
856+{\
857+ Name##Iterator pos;\
858+ assert(self && "RBTree_erase_range");\
859+ assert(self->magic == self && "RBTree_erase_range");\
860+ assert(first && "RBTree_erase_range");\
861+ assert(last && "RBTree_erase_range");\
862+ assert(first->magic == self->tree && "RBTree_erase_range");\
863+ assert(last->magic == self->tree && "RBTree_erase_range");\
864+ pos = first;\
865+ while (pos != last) {\
866+ assert(!Name##_empty(self) && "RBTree_erase_range");\
867+ pos = Name##_erase(self, pos);\
868+ }\
869+ return pos;\
870+}\
871+\
872+size_t Name##_erase_key(Name *self, KeyType key)\
873+{\
874+ size_t count = 0;\
875+ Name##Iterator pos;\
876+ Name##Iterator last;\
877+ assert(self && "RBTree_erase_key");\
878+ assert(self->magic == self && "RBTree_erase_key");\
879+ pos = Name##_lower_bound(self, key);\
880+ last = Name##_upper_bound(self, key);\
881+ while (pos != last) {\
882+ assert(!Name##_empty(self) && "RBTree_erase_key");\
883+ pos = Name##_erase(self, pos);\
884+ count++;\
885+ }\
886+ return count;\
887+}\
888+\
889+size_t Name##_count(Name *self, KeyType key)\
890+{\
891+ assert(self && "RBTree_count");\
892+ assert(self->magic == self && "RBTree_count");\
893+ return Name##RBTree_count(self->tree, key);\
894+}\
895+\
896+Name##Iterator Name##_find(Name *self, KeyType key)\
897+{\
898+ assert(self && "RBTree_find");\
899+ assert(self->magic == self && "RBTree_find");\
900+ return Name##RBTree_find(self->tree, key);\
901+}\
902+\
903+Name##Iterator Name##_lower_bound(Name *self, KeyType key)\
904+{\
905+ assert(self && "RBTree_lower_bound");\
906+ assert(self->magic == self && "RBTree_lower_bound");\
907+ return Name##RBTree_lower_bound(self->tree, key);\
908+}\
909+\
910+Name##Iterator Name##_upper_bound(Name *self, KeyType key)\
911+{\
912+ assert(self && "RBTree_upper_bound");\
913+ assert(self->magic == self && "RBTree_upper_bound");\
914+ return Name##RBTree_upper_bound(self->tree, key);\
915+}\
916+\
917+Name##Iterator Name##_begin(Name *self)\
918+{\
919+ assert(self && "RBTree_begin");\
920+ assert(self->magic == self && "RBTree_begin");\
921+ return Name##RBTree_begin(self->tree);\
922+}\
923+\
924+Name##Iterator Name##_end(Name *self)\
925+{\
926+ assert(self && "RBTree_end");\
927+ assert(self->magic == self && "RBTree_end");\
928+ return Name##RBTree_end(self->tree);\
929+}\
930+\
931+Name##Iterator Name##_rbegin(Name *self)\
932+{\
933+ assert(self && "RBTree_rbegin");\
934+ assert(self->magic == self && "RBTree_rbegin");\
935+ return Name##RBTree_rbegin(self->tree);\
936+}\
937+\
938+Name##Iterator Name##_rend(Name *self)\
939+{\
940+ assert(self && "RBTree_rend");\
941+ assert(self->magic == self && "RBTree_rend");\
942+ return Name##RBTree_rend(self->tree);\
943+}\
944+\
945+Name##Iterator Name##_next(Name##Iterator pos)\
946+{\
947+ assert(pos && "RBTree_next");\
948+ assert(pos->magic && "RBTree_next");\
949+ return Name##RBTree_next(pos);\
950+}\
951+\
952+Name##Iterator Name##_prev(Name##Iterator pos)\
953+{\
954+ assert(pos && "RBTree_prev");\
955+ assert(pos->magic && "RBTree_prev");\
956+ return Name##RBTree_prev(pos);\
957+}\
958+\
959+KeyType Name##_key(Name##Iterator pos)\
960+{\
961+ assert(pos && "RBTree_key");\
962+ assert(pos->magic && "RBTree_key");\
963+ assert(!Name##RBTreeNode_is_head(pos) && "RBTree_key");\
964+ return pos->key;\
965+}\
966+\
967+void Name##_swap(Name *self, Name *x)\
968+{\
969+ Name##RBTree *tmp_tree;\
970+ size_t tmp_nelems;\
971+ assert(self && "RBTree_swap");\
972+ assert(x && "RBTree_swap");\
973+ assert(self->magic == self && "RBTree_swap");\
974+ assert(x->magic == x && "RBTree_swap");\
975+ tmp_tree = self->tree;\
976+ tmp_nelems = self->nelems;\
977+ self->tree = x->tree;\
978+ self->nelems = x->nelems;\
979+ x->tree = tmp_tree;\
980+ x->nelems = tmp_nelems;\
981+}\
982+\
983+
984+
985+#endif /* CSTL_RBTREE_H_INCLUDED */
Added: svn:eol-style
## -0,0 +1 ##
+native
\ No newline at end of property
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
--- tags/rel-0.2.1/doc/sfjp.html (nonexistent)
+++ tags/rel-0.2.1/doc/sfjp.html (revision 43)
@@ -0,0 +1,4 @@
1+<hr>
2+<a href="http://sourceforge.jp/">
3+ <img src="http://sourceforge.jp/sflogo.php?group_id=2739" width="96" height="31" border="0" alt="SourceForge.jp">
4+</a>
--- tags/rel-0.2.1/doc/br.html (nonexistent)
+++ tags/rel-0.2.1/doc/br.html (revision 43)
@@ -0,0 +1 @@
1+<br>
--- tags/rel-0.2.1/doc/default.css (nonexistent)
+++ tags/rel-0.2.1/doc/default.css (revision 43)