-
Notifications
You must be signed in to change notification settings - Fork 3
/
tcutil.h
4184 lines (3228 loc) · 186 KB
/
tcutil.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*************************************************************************************************
* The utility API of Tokyo Cabinet
* Copyright (C) 2006-2011 FAL Labs
* This file is part of Tokyo Cabinet.
* Tokyo Cabinet is free software; you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software Foundation; either
* version 2.1 of the License or any later version. Tokyo Cabinet is distributed in the hope
* that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
* You should have received a copy of the GNU Lesser General Public License along with Tokyo
* Cabinet; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA.
*************************************************************************************************/
#ifndef _TCUTIL_H /* duplication check */
#define _TCUTIL_H
#if defined(__cplusplus)
#define __TCUTIL_CLINKAGEBEGIN extern "C" {
#define __TCUTIL_CLINKAGEEND }
#else
#define __TCUTIL_CLINKAGEBEGIN
#define __TCUTIL_CLINKAGEEND
#endif
__TCUTIL_CLINKAGEBEGIN
#include <stdlib.h>
#if ! defined(__cplusplus)
#include <stdbool.h>
#endif
#include <stdint.h>
#include <time.h>
#include <limits.h>
#include <math.h>
/*************************************************************************************************
* basic utilities
*************************************************************************************************/
/* String containing the version information. */
extern const char *tcversion;
/* Pointer to the call back function for handling a fatal error.
The argument specifies the error message.
The initial value of this variable is `NULL'. If the value is `NULL', the default function is
called when a fatal error occurs. A fatal error occurs when memory allocation is failed. */
extern void (*tcfatalfunc)(const char *);
/* Allocate a region on memory.
`size' specifies the size of the region.
The return value is the pointer to the allocated region.
This function handles failure of memory allocation implicitly. Because the region of the
return value is allocated with the `malloc' call, it should be released with the `free' call
when it is no longer in use. */
void *tcmalloc(size_t size);
/* Allocate a nullified region on memory.
`nmemb' specifies the number of elements.
`size' specifies the size of each element.
The return value is the pointer to the allocated nullified region.
This function handles failure of memory allocation implicitly. Because the region of the
return value is allocated with the `calloc' call, it should be released with the `free' call
when it is no longer in use. */
void *tccalloc(size_t nmemb, size_t size);
/* Re-allocate a region on memory.
`ptr' specifies the pointer to the region.
`size' specifies the size of the region.
The return value is the pointer to the re-allocated region.
This function handles failure of memory allocation implicitly. Because the region of the
return value is allocated with the `realloc' call, it should be released with the `free' call
when it is no longer in use. */
void *tcrealloc(void *ptr, size_t size);
/* Duplicate a region on memory.
`ptr' specifies the pointer to the region.
`size' specifies the size of the region.
The return value is the pointer to the allocated region of the duplicate.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. Because the region of the return
value is allocated with the `malloc' call, it should be released with the `free' call when
it is no longer in use. */
void *tcmemdup(const void *ptr, size_t size);
/* Duplicate a string on memory.
`str' specifies the string.
The return value is the allocated string equivalent to the specified string.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call when it is no longer in use. */
char *tcstrdup(const void *str);
/* Free a region on memory.
`ptr' specifies the pointer to the region. If it is `NULL', this function has no effect.
Although this function is just a wrapper of `free' call, this is useful in applications using
another package of the `malloc' series. */
void tcfree(void *ptr);
/*************************************************************************************************
* basic utilities (for experts)
*************************************************************************************************/
/* type of the pointer to a comparison function.
`aptr' specifies the pointer to the region of one key.
`asiz' specifies the size of the region of one key.
`bptr' specifies the pointer to the region of the other key.
`bsiz' specifies the size of the region of the other key.
`op' specifies the pointer to the optional opaque object.
The return value is positive if the former is big, negative if the latter is big, 0 if both
are equivalent. */
typedef int (*TCCMP)(const char *aptr, int asiz, const char *bptr, int bsiz, void *op);
/* type of the pointer to a encoding or decoding function.
`ptr' specifies the pointer to the region.
`size' specifies the size of the region.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
`op' specifies the pointer to the optional opaque object.
If successful, the return value is the pointer to the result object allocated with `malloc'
call, else, it is `NULL'. */
typedef void *(*TCCODEC)(const void *ptr, int size, int *sp, void *op);
/* type of the pointer to a callback function to process record duplication.
`vbuf' specifies the pointer to the region of the value.
`vsiz' specifies the size of the region of the value.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
`op' specifies the pointer to the optional opaque object.
The return value is the pointer to the result object allocated with `malloc'. It is
released by the caller. If it is `NULL', the record is not modified. */
typedef void *(*TCPDPROC)(const void *vbuf, int vsiz, int *sp, void *op);
/* type of the pointer to a iterator function.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`vbuf' specifies the pointer to the region of the value.
`vsiz' specifies the size of the region of the value.
`op' specifies the pointer to the optional opaque object.
The return value is true to continue iteration or false to stop iteration. */
typedef bool (*TCITER)(const void *kbuf, int ksiz, const void *vbuf, int vsiz, void *op);
/*************************************************************************************************
* extensible string
*************************************************************************************************/
typedef struct { /* type of structure for an extensible string object */
char *ptr; /* pointer to the region */
int size; /* size of the region */
int asize; /* size of the allocated region */
} TCXSTR;
/* Create an extensible string object.
The return value is the new extensible string object. */
TCXSTR *tcxstrnew(void);
/* Create an extensible string object from a character string.
`str' specifies the string of the initial content.
The return value is the new extensible string object containing the specified string. */
TCXSTR *tcxstrnew2(const char *str);
/* Create an extensible string object with the initial allocation size.
`asiz' specifies the initial allocation size.
The return value is the new extensible string object. */
TCXSTR *tcxstrnew3(int asiz);
/* Copy an extensible string object.
`xstr' specifies the extensible string object.
The return value is the new extensible string object equivalent to the specified object. */
TCXSTR *tcxstrdup(const TCXSTR *xstr);
/* Delete an extensible string object.
`xstr' specifies the extensible string object.
Note that the deleted object and its derivatives can not be used anymore. */
void tcxstrdel(TCXSTR *xstr);
/* Concatenate a region to the end of an extensible string object.
`xstr' specifies the extensible string object.
`ptr' specifies the pointer to the region to be appended.
`size' specifies the size of the region. */
void tcxstrcat(TCXSTR *xstr, const void *ptr, int size);
/* Concatenate a character string to the end of an extensible string object.
`xstr' specifies the extensible string object.
`str' specifies the string to be appended. */
void tcxstrcat2(TCXSTR *xstr, const char *str);
/* Get the pointer of the region of an extensible string object.
`xstr' specifies the extensible string object.
The return value is the pointer of the region of the object.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. */
const void *tcxstrptr(const TCXSTR *xstr);
/* Get the size of the region of an extensible string object.
`xstr' specifies the extensible string object.
The return value is the size of the region of the object. */
int tcxstrsize(const TCXSTR *xstr);
/* Clear an extensible string object.
`xstr' specifies the extensible string object.
The internal buffer of the object is cleared and the size is set zero. */
void tcxstrclear(TCXSTR *xstr);
/* Perform formatted output into an extensible string object.
`xstr' specifies the extensible string object.
`format' specifies the printf-like format string. The conversion character `%' can be used
with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@',
`?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as
with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary
numbers. The other conversion character work as with each original.
The other arguments are used according to the format string. */
void tcxstrprintf(TCXSTR *xstr, const char *format, ...);
/* Allocate a formatted string on memory.
`format' specifies the printf-like format string. The conversion character `%' can be used
with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@',
`?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as
with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary
numbers. The other conversion character work as with each original.
The other arguments are used according to the format string.
The return value is the pointer to the region of the result string.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call when it is no longer in use. */
char *tcsprintf(const char *format, ...);
/*************************************************************************************************
* extensible string (for experts)
*************************************************************************************************/
/* Convert an extensible string object into a usual allocated region.
`xstr' specifies the extensible string object.
The return value is the pointer to the allocated region of the object.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. Because the region of the return
value is allocated with the `malloc' call, it should be released with the `free' call when it
is no longer in use. Because the region of the original object is deleted, it should not be
deleted again. */
void *tcxstrtomalloc(TCXSTR *xstr);
/* Create an extensible string object from an allocated region.
`ptr' specifies the pointer to the region allocated with `malloc' call.
`size' specifies the size of the region.
The return value is the new extensible string object wrapping the specified region.
Note that the specified region is released when the object is deleted. */
TCXSTR *tcxstrfrommalloc(void *ptr, int size);
/*************************************************************************************************
* array list
*************************************************************************************************/
typedef struct { /* type of structure for an element of a list */
char *ptr; /* pointer to the region */
int size; /* size of the effective region */
} TCLISTDATUM;
typedef struct { /* type of structure for an array list */
TCLISTDATUM *array; /* array of data */
int anum; /* number of the elements of the array */
int start; /* start index of used elements */
int num; /* number of used elements */
} TCLIST;
/* Create a list object.
The return value is the new list object. */
TCLIST *tclistnew(void);
/* Create a list object with expecting the number of elements.
`anum' specifies the number of elements expected to be stored in the list.
The return value is the new list object. */
TCLIST *tclistnew2(int anum);
/* Create a list object with initial string elements.
`str' specifies the string of the first element.
The other arguments are other elements. They should be trailed by a `NULL' argument.
The return value is the new list object. */
TCLIST *tclistnew3(const char *str, ...);
/* Copy a list object.
`list' specifies the list object.
The return value is the new list object equivalent to the specified object. */
TCLIST *tclistdup(const TCLIST *list);
/* Delete a list object.
`list' specifies the list object.
Note that the deleted object and its derivatives can not be used anymore. */
void tclistdel(TCLIST *list);
/* Get the number of elements of a list object.
`list' specifies the list object.
The return value is the number of elements of the list. */
int tclistnum(const TCLIST *list);
/* Get the pointer to the region of an element of a list object.
`list' specifies the list object.
`index' specifies the index of the element.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
The return value is the pointer to the region of the value.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. If `index' is equal to or more than
the number of elements, the return value is `NULL'. */
const void *tclistval(const TCLIST *list, int index, int *sp);
/* Get the string of an element of a list object.
`list' specifies the list object.
`index' specifies the index of the element.
The return value is the string of the value.
If `index' is equal to or more than the number of elements, the return value is `NULL'. */
const char *tclistval2(const TCLIST *list, int index);
/* Add an element at the end of a list object.
`list' specifies the list object.
`ptr' specifies the pointer to the region of the new element.
`size' specifies the size of the region. */
void tclistpush(TCLIST *list, const void *ptr, int size);
/* Add a string element at the end of a list object.
`list' specifies the list object.
`str' specifies the string of the new element. */
void tclistpush2(TCLIST *list, const char *str);
/* Remove an element of the end of a list object.
`list' specifies the list object.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
The return value is the pointer to the region of the removed element.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. Because the region of the return
value is allocated with the `malloc' call, it should be released with the `free' call when it
is no longer in use. If the list is empty, the return value is `NULL'. */
void *tclistpop(TCLIST *list, int *sp);
/* Remove a string element of the end of a list object.
`list' specifies the list object.
The return value is the string of the removed element.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call when it is no longer in use. If the list is empty, the return
value is `NULL'. */
char *tclistpop2(TCLIST *list);
/* Add an element at the top of a list object.
`list' specifies the list object.
`ptr' specifies the pointer to the region of the new element.
`size' specifies the size of the region. */
void tclistunshift(TCLIST *list, const void *ptr, int size);
/* Add a string element at the top of a list object.
`list' specifies the list object.
`str' specifies the string of the new element. */
void tclistunshift2(TCLIST *list, const char *str);
/* Remove an element of the top of a list object.
`list' specifies the list object.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
The return value is the pointer to the region of the removed element.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. Because the region of the return
value is allocated with the `malloc' call, it should be released with the `free' call when it
is no longer in use. If the list is empty, the return value is `NULL'. */
void *tclistshift(TCLIST *list, int *sp);
/* Remove a string element of the top of a list object.
`list' specifies the list object.
The return value is the string of the removed element.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call when it is no longer in use. If the list is empty, the return
value is `NULL'. */
char *tclistshift2(TCLIST *list);
/* Add an element at the specified location of a list object.
`list' specifies the list object.
`index' specifies the index of the new element.
`ptr' specifies the pointer to the region of the new element.
`size' specifies the size of the region.
If `index' is equal to or more than the number of elements, this function has no effect. */
void tclistinsert(TCLIST *list, int index, const void *ptr, int size);
/* Add a string element at the specified location of a list object.
`list' specifies the list object.
`index' specifies the index of the new element.
`str' specifies the string of the new element.
If `index' is equal to or more than the number of elements, this function has no effect. */
void tclistinsert2(TCLIST *list, int index, const char *str);
/* Remove an element at the specified location of a list object.
`list' specifies the list object.
`index' specifies the index of the element to be removed.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
The return value is the pointer to the region of the removed element.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. Because the region of the return
value is allocated with the `malloc' call, it should be released with the `free' call when it
is no longer in use. If `index' is equal to or more than the number of elements, no element
is removed and the return value is `NULL'. */
void *tclistremove(TCLIST *list, int index, int *sp);
/* Remove a string element at the specified location of a list object.
`list' specifies the list object.
`index' specifies the index of the element to be removed.
The return value is the string of the removed element.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call when it is no longer in use. If `index' is equal to or more
than the number of elements, no element is removed and the return value is `NULL'. */
char *tclistremove2(TCLIST *list, int index);
/* Overwrite an element at the specified location of a list object.
`list' specifies the list object.
`index' specifies the index of the element to be overwritten.
`ptr' specifies the pointer to the region of the new content.
`size' specifies the size of the new content.
If `index' is equal to or more than the number of elements, this function has no effect. */
void tclistover(TCLIST *list, int index, const void *ptr, int size);
/* Overwrite a string element at the specified location of a list object.
`list' specifies the list object.
`index' specifies the index of the element to be overwritten.
`str' specifies the string of the new content.
If `index' is equal to or more than the number of elements, this function has no effect. */
void tclistover2(TCLIST *list, int index, const char *str);
/* Sort elements of a list object in lexical order.
`list' specifies the list object. */
void tclistsort(TCLIST *list);
/* Search a list object for an element using liner search.
`list' specifies the list object.
`ptr' specifies the pointer to the region of the key.
`size' specifies the size of the region.
The return value is the index of a corresponding element or -1 if there is no corresponding
element.
If two or more elements correspond, the former returns. */
int tclistlsearch(const TCLIST *list, const void *ptr, int size);
/* Search a list object for an element using binary search.
`list' specifies the list object. It should be sorted in lexical order.
`ptr' specifies the pointer to the region of the key.
`size' specifies the size of the region.
The return value is the index of a corresponding element or -1 if there is no corresponding
element.
If two or more elements correspond, which returns is not defined. */
int tclistbsearch(const TCLIST *list, const void *ptr, int size);
/* Clear a list object.
`list' specifies the list object.
All elements are removed. */
void tclistclear(TCLIST *list);
/* Serialize a list object into a byte array.
`list' specifies the list object.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
The return value is the pointer to the region of the result serial region.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call when it is no longer in use. */
void *tclistdump(const TCLIST *list, int *sp);
/* Create a list object from a serialized byte array.
`ptr' specifies the pointer to the region of serialized byte array.
`size' specifies the size of the region.
The return value is a new list object.
Because the object of the return value is created with the function `tclistnew', it should
be deleted with the function `tclistdel' when it is no longer in use. */
TCLIST *tclistload(const void *ptr, int size);
/*************************************************************************************************
* array list (for experts)
*************************************************************************************************/
/* Add an allocated element at the end of a list object.
`list' specifies the list object.
`ptr' specifies the pointer to the region allocated with `malloc' call.
`size' specifies the size of the region.
Note that the specified region is released when the object is deleted. */
void tclistpushmalloc(TCLIST *list, void *ptr, int size);
/* Sort elements of a list object in case-insensitive lexical order.
`list' specifies the list object. */
void tclistsortci(TCLIST *list);
/* Sort elements of a list object by an arbitrary comparison function.
`list' specifies the list object.
`cmp' specifies the pointer to the comparison function. The structure TCLISTDATUM has the
member "ptr" which is the pointer to the region of the element, and the member "size" which is
the size of the region. */
void tclistsortex(TCLIST *list, int (*cmp)(const TCLISTDATUM *, const TCLISTDATUM *));
/* Invert elements of a list object.
`list' specifies the list object. */
void tclistinvert(TCLIST *list);
/* Perform formatted output into a list object.
`list' specifies the list object.
`format' specifies the printf-like format string. The conversion character `%' can be used
with such flag characters as `s', `d', `o', `u', `x', `X', `c', `e', `E', `f', `g', `G', `@',
`?', `b', and `%'. `@' works as with `s' but escapes meta characters of XML. `?' works as
with `s' but escapes meta characters of URL. `b' converts an integer to the string as binary
numbers. The other conversion character work as with each original.
The other arguments are used according to the format string. */
void tclistprintf(TCLIST *list, const char *format, ...);
/*************************************************************************************************
* hash map
*************************************************************************************************/
typedef struct _TCMAPREC { /* type of structure for an element of a map */
int32_t ksiz; /* size of the region of the key */
int32_t vsiz; /* size of the region of the value */
struct _TCMAPREC *left; /* pointer to the left child */
struct _TCMAPREC *right; /* pointer to the right child */
struct _TCMAPREC *prev; /* pointer to the previous element */
struct _TCMAPREC *next; /* pointer to the next element */
} TCMAPREC;
typedef struct { /* type of structure for a map */
TCMAPREC **buckets; /* bucket array */
TCMAPREC *first; /* pointer to the first element */
TCMAPREC *last; /* pointer to the last element */
TCMAPREC *cur; /* pointer to the current element */
uint32_t bnum; /* number of buckets */
uint64_t rnum; /* number of records */
uint64_t msiz; /* total size of records */
} TCMAP;
/* Create a map object.
The return value is the new map object. */
TCMAP *tcmapnew(void);
/* Create a map object with specifying the number of the buckets.
`bnum' specifies the number of the buckets.
The return value is the new map object. */
TCMAP *tcmapnew2(uint32_t bnum);
/* Create a map object with initial string elements.
`str' specifies the string of the first element.
The other arguments are other elements. They should be trailed by a `NULL' argument.
The return value is the new map object.
The key and the value of each record are situated one after the other. */
TCMAP *tcmapnew3(const char *str, ...);
/* Copy a map object.
`map' specifies the map object.
The return value is the new map object equivalent to the specified object. */
TCMAP *tcmapdup(const TCMAP *map);
/* Delete a map object.
`map' specifies the map object.
Note that the deleted object and its derivatives can not be used anymore. */
void tcmapdel(TCMAP *map);
/* Store a record into a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`vbuf' specifies the pointer to the region of the value.
`vsiz' specifies the size of the region of the value.
If a record with the same key exists in the map, it is overwritten. */
void tcmapput(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
/* Store a string record into a map object.
`map' specifies the map object.
`kstr' specifies the string of the key.
`vstr' specifies the string of the value.
If a record with the same key exists in the map, it is overwritten. */
void tcmapput2(TCMAP *map, const char *kstr, const char *vstr);
/* Store a new record into a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`vbuf' specifies the pointer to the region of the value.
`vsiz' specifies the size of the region of the value.
If successful, the return value is true, else, it is false.
If a record with the same key exists in the map, this function has no effect. */
bool tcmapputkeep(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
/* Store a new string record into a map object.
`map' specifies the map object.
`kstr' specifies the string of the key.
`vstr' specifies the string of the value.
If successful, the return value is true, else, it is false.
If a record with the same key exists in the map, this function has no effect. */
bool tcmapputkeep2(TCMAP *map, const char *kstr, const char *vstr);
/* Concatenate a value at the end of the value of the existing record in a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`vbuf' specifies the pointer to the region of the value.
`vsiz' specifies the size of the region of the value.
If there is no corresponding record, a new record is created. */
void tcmapputcat(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
/* Concatenate a string value at the end of the value of the existing record in a map object.
`map' specifies the map object.
`kstr' specifies the string of the key.
`vstr' specifies the string of the value.
If there is no corresponding record, a new record is created. */
void tcmapputcat2(TCMAP *map, const char *kstr, const char *vstr);
/* Remove a record of a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
If successful, the return value is true. False is returned when no record corresponds to
the specified key. */
bool tcmapout(TCMAP *map, const void *kbuf, int ksiz);
/* Remove a string record of a map object.
`map' specifies the map object.
`kstr' specifies the string of the key.
If successful, the return value is true. False is returned when no record corresponds to
the specified key. */
bool tcmapout2(TCMAP *map, const char *kstr);
/* Retrieve a record in a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
If successful, the return value is the pointer to the region of the value of the
corresponding record. `NULL' is returned when no record corresponds.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. */
const void *tcmapget(const TCMAP *map, const void *kbuf, int ksiz, int *sp);
/* Retrieve a string record in a map object.
`map' specifies the map object.
`kstr' specifies the string of the key.
If successful, the return value is the string of the value of the corresponding record.
`NULL' is returned when no record corresponds. */
const char *tcmapget2(const TCMAP *map, const char *kstr);
/* Move a record to the edge of a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of a key.
`ksiz' specifies the size of the region of the key.
`head' specifies the destination which is the head if it is true or the tail if else.
If successful, the return value is true. False is returned when no record corresponds to
the specified key. */
bool tcmapmove(TCMAP *map, const void *kbuf, int ksiz, bool head);
/* Move a string record to the edge of a map object.
`map' specifies the map object.
`kstr' specifies the string of a key.
`head' specifies the destination which is the head if it is true or the tail if else.
If successful, the return value is true. False is returned when no record corresponds to
the specified key. */
bool tcmapmove2(TCMAP *map, const char *kstr, bool head);
/* Initialize the iterator of a map object.
`map' specifies the map object.
The iterator is used in order to access the key of every record stored in the map object. */
void tcmapiterinit(TCMAP *map);
/* Get the next key of the iterator of a map object.
`map' specifies the map object.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
If successful, the return value is the pointer to the region of the next key, else, it is
`NULL'. `NULL' is returned when no record can be fetched from the iterator.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string.
The order of iteration is assured to be the same as the stored order. */
const void *tcmapiternext(TCMAP *map, int *sp);
/* Get the next key string of the iterator of a map object.
`map' specifies the map object.
If successful, the return value is the pointer to the region of the next key, else, it is
`NULL'. `NULL' is returned when no record can be fetched from the iterator.
The order of iteration is assured to be the same as the stored order. */
const char *tcmapiternext2(TCMAP *map);
/* Get the number of records stored in a map object.
`map' specifies the map object.
The return value is the number of the records stored in the map object. */
uint64_t tcmaprnum(const TCMAP *map);
/* Get the total size of memory used in a map object.
`map' specifies the map object.
The return value is the total size of memory used in a map object. */
uint64_t tcmapmsiz(const TCMAP *map);
/* Create a list object containing all keys in a map object.
`map' specifies the map object.
The return value is the new list object containing all keys in the map object.
Because the object of the return value is created with the function `tclistnew', it should
be deleted with the function `tclistdel' when it is no longer in use. */
TCLIST *tcmapkeys(const TCMAP *map);
/* Create a list object containing all values in a map object.
`map' specifies the map object.
The return value is the new list object containing all values in the map object.
Because the object of the return value is created with the function `tclistnew', it should
be deleted with the function `tclistdel' when it is no longer in use. */
TCLIST *tcmapvals(const TCMAP *map);
/* Add an integer to a record in a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`num' specifies the additional value.
The return value is the summation value.
If the corresponding record exists, the value is treated as an integer and is added to. If no
record corresponds, a new record of the additional value is stored. */
int tcmapaddint(TCMAP *map, const void *kbuf, int ksiz, int num);
/* Add a real number to a record in a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`num' specifies the additional value.
The return value is the summation value.
If the corresponding record exists, the value is treated as a real number and is added to. If
no record corresponds, a new record of the additional value is stored. */
double tcmapadddouble(TCMAP *map, const void *kbuf, int ksiz, double num);
/* Clear a map object.
`map' specifies the map object.
All records are removed. */
void tcmapclear(TCMAP *map);
/* Remove front records of a map object.
`map' specifies the map object.
`num' specifies the number of records to be removed. */
void tcmapcutfront(TCMAP *map, int num);
/* Serialize a map object into a byte array.
`map' specifies the map object.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
The return value is the pointer to the region of the result serial region.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call when it is no longer in use. */
void *tcmapdump(const TCMAP *map, int *sp);
/* Create a map object from a serialized byte array.
`ptr' specifies the pointer to the region of serialized byte array.
`size' specifies the size of the region.
The return value is a new map object.
Because the object of the return value is created with the function `tcmapnew', it should be
deleted with the function `tcmapdel' when it is no longer in use. */
TCMAP *tcmapload(const void *ptr, int size);
/*************************************************************************************************
* hash map (for experts)
*************************************************************************************************/
/* Store a record and make it semivolatile in a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`vbuf' specifies the pointer to the region of the value.
`vsiz' specifies the size of the region of the value.
If a record with the same key exists in the map, it is overwritten. The record is moved to
the tail. */
void tcmapput3(TCMAP *map, const void *kbuf, int ksiz, const char *vbuf, int vsiz);
/* Store a record of the value of two regions into a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`fvbuf' specifies the pointer to the former region of the value.
`fvsiz' specifies the size of the former region of the value.
`lvbuf' specifies the pointer to the latter region of the value.
`lvsiz' specifies the size of the latter region of the value.
If a record with the same key exists in the map, it is overwritten. */
void tcmapput4(TCMAP *map, const void *kbuf, int ksiz,
const void *fvbuf, int fvsiz, const void *lvbuf, int lvsiz);
/* Concatenate a value at the existing record and make it semivolatile in a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`vbuf' specifies the pointer to the region of the value.
`vsiz' specifies the size of the region of the value.
If there is no corresponding record, a new record is created. */
void tcmapputcat3(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz);
/* Store a record into a map object with a duplication handler.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`vbuf' specifies the pointer to the region of the value. `NULL' means that record addition is
ommited if there is no corresponding record.
`vsiz' specifies the size of the region of the value.
`proc' specifies the pointer to the callback function to process duplication. It receives
four parameters. The first parameter is the pointer to the region of the value. The second
parameter is the size of the region of the value. The third parameter is the pointer to the
variable into which the size of the region of the return value is assigned. The fourth
parameter is the pointer to the optional opaque object. It returns the pointer to the result
object allocated with `malloc'. It is released by the caller. If it is `NULL', the record is
not modified. If it is `(void *)-1', the record is removed.
`op' specifies an arbitrary pointer to be given as a parameter of the callback function. If
it is not needed, `NULL' can be specified.
If successful, the return value is true, else, it is false. */
bool tcmapputproc(TCMAP *map, const void *kbuf, int ksiz, const void *vbuf, int vsiz,
TCPDPROC proc, void *op);
/* Retrieve a semivolatile record in a map object.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
If successful, the return value is the pointer to the region of the value of the
corresponding record. `NULL' is returned when no record corresponds.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. The internal region of the returned
record is moved to the tail so that the record will survive for a time under LRU cache
algorithm removing records from the head. */
const void *tcmapget3(TCMAP *map, const void *kbuf, int ksiz, int *sp);
/* Retrieve a string record in a map object with specifying the default value string.
`map' specifies the map object.
`kstr' specifies the string of the key.
`dstr' specifies the string of the default value.
The return value is the string of the value of the corresponding record or the default value
string. */
const char *tcmapget4(TCMAP *map, const char *kstr, const char *dstr);
/* Initialize the iterator of a map object at the record corresponding a key.
`map' specifies the map object.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
If there is no record corresponding the condition, the iterator is not modified. */
void tcmapiterinit2(TCMAP *map, const void *kbuf, int ksiz);
/* Initialize the iterator of a map object at the record corresponding a key string.
`map' specifies the map object.
`kstr' specifies the string of the key.
If there is no record corresponding the condition, the iterator is not modified. */
void tcmapiterinit3(TCMAP *map, const char *kstr);
/* Get the value bound to the key fetched from the iterator of a map object.
`kbuf' specifies the pointer to the region of the iteration key.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
The return value is the pointer to the region of the value of the corresponding record.
Because an additional zero code is appended at the end of the region of the return value,
the return value can be treated as a character string. */
const void *tcmapiterval(const void *kbuf, int *sp);
/* Get the value string bound to the key fetched from the iterator of a map object.
`kstr' specifies the string of the iteration key.
The return value is the pointer to the region of the value of the corresponding record. */
const char *tcmapiterval2(const char *kstr);
/* Create an array of strings of all keys in a map object.
`map' specifies the map object.
`np' specifies the pointer to a variable into which the number of elements of the return value
is assigned.
The return value is the pointer to the array of all string keys in the map object.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call if when is no longer in use. Note that elements of the array
point to the inner objects, whose life duration is synchronous with the map object. */
const char **tcmapkeys2(const TCMAP *map, int *np);
/* Create an array of strings of all values in a map object.
`map' specifies the map object.
`np' specifies the pointer to a variable into which the number of elements of the return value
is assigned.
The return value is the pointer to the array of all string values in the map object.
Because the region of the return value is allocated with the `malloc' call, it should be
released with the `free' call if when is no longer in use. Note that elements of the array
point to the inner objects, whose life duration is synchronous with the map object. */
const char **tcmapvals2(const TCMAP *map, int *np);
/* Extract a map record from a serialized byte array.
`ptr' specifies the pointer to the region of serialized byte array.
`size' specifies the size of the region.
`kbuf' specifies the pointer to the region of the key.
`ksiz' specifies the size of the region of the key.
`sp' specifies the pointer to the variable into which the size of the region of the return
value is assigned.
If successful, the return value is the pointer to the region of the value of the
corresponding record. `NULL' is returned when no record corresponds.
Because an additional zero code is appended at the end of the region of the return value,