chaine 3.13.1__cp312-cp312-musllinux_1_2_i686.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of chaine might be problematic. Click here for more details.

Files changed (70) hide show
  1. chaine/__init__.py +2 -0
  2. chaine/_core/crf.cpp +19854 -0
  3. chaine/_core/crf.cpython-312-i386-linux-musl.so +0 -0
  4. chaine/_core/crf.pyx +271 -0
  5. chaine/_core/crfsuite/COPYING +27 -0
  6. chaine/_core/crfsuite/README +183 -0
  7. chaine/_core/crfsuite/include/crfsuite.h +1077 -0
  8. chaine/_core/crfsuite/include/crfsuite.hpp +649 -0
  9. chaine/_core/crfsuite/include/crfsuite_api.hpp +406 -0
  10. chaine/_core/crfsuite/include/os.h +65 -0
  11. chaine/_core/crfsuite/lib/cqdb/COPYING +28 -0
  12. chaine/_core/crfsuite/lib/cqdb/include/cqdb.h +518 -0
  13. chaine/_core/crfsuite/lib/cqdb/src/cqdb.c +639 -0
  14. chaine/_core/crfsuite/lib/cqdb/src/lookup3.c +1271 -0
  15. chaine/_core/crfsuite/lib/cqdb/src/main.c +184 -0
  16. chaine/_core/crfsuite/lib/crf/src/crf1d.h +354 -0
  17. chaine/_core/crfsuite/lib/crf/src/crf1d_context.c +788 -0
  18. chaine/_core/crfsuite/lib/crf/src/crf1d_encode.c +1020 -0
  19. chaine/_core/crfsuite/lib/crf/src/crf1d_feature.c +382 -0
  20. chaine/_core/crfsuite/lib/crf/src/crf1d_model.c +1085 -0
  21. chaine/_core/crfsuite/lib/crf/src/crf1d_tag.c +582 -0
  22. chaine/_core/crfsuite/lib/crf/src/crfsuite.c +500 -0
  23. chaine/_core/crfsuite/lib/crf/src/crfsuite_internal.h +233 -0
  24. chaine/_core/crfsuite/lib/crf/src/crfsuite_train.c +302 -0
  25. chaine/_core/crfsuite/lib/crf/src/dataset.c +115 -0
  26. chaine/_core/crfsuite/lib/crf/src/dictionary.c +127 -0
  27. chaine/_core/crfsuite/lib/crf/src/holdout.c +83 -0
  28. chaine/_core/crfsuite/lib/crf/src/json.c +1497 -0
  29. chaine/_core/crfsuite/lib/crf/src/json.h +120 -0
  30. chaine/_core/crfsuite/lib/crf/src/logging.c +85 -0
  31. chaine/_core/crfsuite/lib/crf/src/logging.h +49 -0
  32. chaine/_core/crfsuite/lib/crf/src/params.c +370 -0
  33. chaine/_core/crfsuite/lib/crf/src/params.h +84 -0
  34. chaine/_core/crfsuite/lib/crf/src/quark.c +180 -0
  35. chaine/_core/crfsuite/lib/crf/src/quark.h +46 -0
  36. chaine/_core/crfsuite/lib/crf/src/rumavl.c +1178 -0
  37. chaine/_core/crfsuite/lib/crf/src/rumavl.h +144 -0
  38. chaine/_core/crfsuite/lib/crf/src/train_arow.c +409 -0
  39. chaine/_core/crfsuite/lib/crf/src/train_averaged_perceptron.c +237 -0
  40. chaine/_core/crfsuite/lib/crf/src/train_l2sgd.c +491 -0
  41. chaine/_core/crfsuite/lib/crf/src/train_lbfgs.c +323 -0
  42. chaine/_core/crfsuite/lib/crf/src/train_passive_aggressive.c +442 -0
  43. chaine/_core/crfsuite/lib/crf/src/vecmath.h +360 -0
  44. chaine/_core/crfsuite/swig/crfsuite.cpp +1 -0
  45. chaine/_core/crfsuite_api.pxd +67 -0
  46. chaine/_core/liblbfgs/COPYING +22 -0
  47. chaine/_core/liblbfgs/README +71 -0
  48. chaine/_core/liblbfgs/include/lbfgs.h +745 -0
  49. chaine/_core/liblbfgs/lib/arithmetic_ansi.h +142 -0
  50. chaine/_core/liblbfgs/lib/arithmetic_sse_double.h +303 -0
  51. chaine/_core/liblbfgs/lib/arithmetic_sse_float.h +312 -0
  52. chaine/_core/liblbfgs/lib/lbfgs.c +1531 -0
  53. chaine/_core/tagger_wrapper.hpp +58 -0
  54. chaine/_core/trainer_wrapper.cpp +32 -0
  55. chaine/_core/trainer_wrapper.hpp +26 -0
  56. chaine/crf.py +505 -0
  57. chaine/logging.py +214 -0
  58. chaine/optimization/__init__.py +10 -0
  59. chaine/optimization/metrics.py +129 -0
  60. chaine/optimization/spaces.py +394 -0
  61. chaine/optimization/trial.py +103 -0
  62. chaine/optimization/utils.py +119 -0
  63. chaine/training.py +184 -0
  64. chaine/typing.py +18 -0
  65. chaine/validation.py +43 -0
  66. chaine-3.13.1.dist-info/METADATA +348 -0
  67. chaine-3.13.1.dist-info/RECORD +70 -0
  68. chaine-3.13.1.dist-info/WHEEL +4 -0
  69. chaine.libs/libgcc_s-1257a076.so.1 +0 -0
  70. chaine.libs/libstdc++-0530927c.so.6.0.32 +0 -0
@@ -0,0 +1,639 @@
1
+ /*
2
+ * Constant Quark Database (CQDB).
3
+ *
4
+ * Copyright (c) 2007, Naoaki Okazaki
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ * * Redistributions of source code must retain the above copyright
10
+ * notice, this list of conditions and the following disclaimer.
11
+ * * Redistributions in binary form must reproduce the above copyright
12
+ * notice, this list of conditions and the following disclaimer in the
13
+ * documentation and/or other materials provided with the distribution.
14
+ * * Neither the name of the Northwestern University, University of Tokyo,
15
+ * nor the names of its contributors may be used to endorse or promote
16
+ * products derived from this software without specific prior written
17
+ * permission.
18
+ *
19
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
23
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
+ */
31
+
32
+ /* $Id$ */
33
+
34
+ #include <stdio.h>
35
+ #include <stdlib.h>
36
+ #include <stdint.h>
37
+ #include <string.h>
38
+
39
+ #include <cqdb.h>
40
+
41
+ #define CHUNKID "CQDB"
42
+ #define BYTEORDER_CHECK (0x62445371)
43
+ #define NUM_TABLES (256)
44
+ #define OFFSET_REFS (0 + sizeof(header_t))
45
+ #define OFFSET_DATA (OFFSET_REFS + sizeof(tableref_t) * NUM_TABLES)
46
+
47
+ /**
48
+ * An element of a hash table.
49
+ */
50
+ typedef struct
51
+ {
52
+ uint32_t hash; /**< Hash value of the record. */
53
+ uint32_t offset; /**< Offset address to the actual record. */
54
+ } bucket_t;
55
+
56
+ /**
57
+ * A hash table.
58
+ */
59
+ typedef struct
60
+ {
61
+ uint32_t num; /**< Number of elements in the table. */
62
+ uint32_t size; /**< Maximum number of elements. */
63
+ bucket_t *bucket; /**< Bucket (array of bucket_t). */
64
+ } table_t;
65
+
66
+ /**
67
+ * CQDB chunk header.
68
+ */
69
+ typedef struct
70
+ {
71
+ int8_t chunkid[4]; /**< Chunk identifier, "CQDB". */
72
+ uint32_t size; /**< Chunk size including this header. */
73
+ uint32_t flag; /**< Global flags. */
74
+ uint32_t byteorder; /**< Byte-order indicator. */
75
+ uint32_t bwd_size; /**< Number of elements in the backward array. */
76
+ uint32_t bwd_offset; /**< Offset to the backward array. */
77
+ } header_t;
78
+
79
+ /**
80
+ * Reference to a hash table.
81
+ */
82
+ typedef struct
83
+ {
84
+ uint32_t offset; /**< Offset to a hash table. */
85
+ uint32_t num; /**< Number of elements in the hash table. */
86
+ } tableref_t;
87
+
88
+ /**
89
+ * Writer for a constant quark database.
90
+ */
91
+ struct tag_cqdb_writer
92
+ {
93
+ uint32_t flag; /**< Operation flag. */
94
+ FILE *fp; /**< File pointer. */
95
+ uint32_t begin; /**< Offset address to the head of this database. */
96
+ uint32_t cur; /**< Offset address to a new key/data pair. */
97
+ table_t ht[NUM_TABLES]; /**< Hash tables (string -> id). */
98
+
99
+ uint32_t *bwd; /**< Backlink array. */
100
+ uint32_t bwd_num; /**< */
101
+ uint32_t bwd_size; /**< Number of elements in the backlink array. */
102
+ };
103
+
104
+ /**
105
+ * Constant quark database (CQDB).
106
+ */
107
+ struct tag_cqdb
108
+ {
109
+ const uint8_t *buffer; /**< Pointer to the memory block. */
110
+ size_t size; /**< Size of the memory block. */
111
+
112
+ header_t header; /**< Chunk header. */
113
+ table_t ht[NUM_TABLES]; /**< Hash tables (string -> id). */
114
+
115
+ uint32_t *bwd; /**< Array for backward look-up (id -> string). */
116
+
117
+ int num; /**< Number of key/data pairs. */
118
+ };
119
+
120
+ uint32_t hashlittle(const void *key, size_t length, uint32_t initval);
121
+
122
+ static size_t write_uint32(cqdb_writer_t *wt, uint32_t value)
123
+ {
124
+ uint8_t buffer[4];
125
+ buffer[0] = (uint8_t)(value & 0xFF);
126
+ buffer[1] = (uint8_t)(value >> 8);
127
+ buffer[2] = (uint8_t)(value >> 16);
128
+ buffer[3] = (uint8_t)(value >> 24);
129
+ return fwrite(buffer, sizeof(uint8_t), 4, wt->fp) / sizeof(value);
130
+ }
131
+
132
+ static size_t write_data(cqdb_writer_t *wt, const void *data, size_t size)
133
+ {
134
+ return fwrite(data, size, 1, wt->fp);
135
+ }
136
+
137
+ cqdb_writer_t *cqdb_writer(FILE *fp, int flag)
138
+ {
139
+ int i;
140
+ cqdb_writer_t *dbw = (cqdb_writer_t *)calloc(1, sizeof(cqdb_writer_t));
141
+
142
+ if (dbw != NULL)
143
+ {
144
+ /* Initialize cqdb_writer_t members. */
145
+ memset(dbw, 0, sizeof(*dbw));
146
+ dbw->flag = flag;
147
+ dbw->fp = fp;
148
+ dbw->begin = ftell(dbw->fp);
149
+ dbw->cur = OFFSET_DATA;
150
+
151
+ /* Initialize the hash tables.*/
152
+ for (i = 0; i < NUM_TABLES; ++i)
153
+ {
154
+ dbw->ht[i].bucket = NULL;
155
+ }
156
+
157
+ dbw->bwd = NULL;
158
+ dbw->bwd_num = 0;
159
+ dbw->bwd_size = 0;
160
+
161
+ /* Move the file pointer to the offset to the first key/data pair. */
162
+ if (fseek(dbw->fp, dbw->begin + dbw->cur, SEEK_SET) != 0)
163
+ {
164
+ goto error_exit; /* Seek error. */
165
+ }
166
+ }
167
+
168
+ return dbw;
169
+
170
+ error_exit:
171
+ free(dbw);
172
+ return NULL;
173
+ }
174
+
175
+ static int cqdb_writer_delete(cqdb_writer_t *dbw)
176
+ {
177
+ int i;
178
+
179
+ /* Free allocated memory blocks. */
180
+ for (i = 0; i < NUM_TABLES; ++i)
181
+ {
182
+ free(dbw->ht[i].bucket);
183
+ }
184
+ free(dbw->bwd);
185
+ free(dbw);
186
+ return 0;
187
+ }
188
+
189
+ int cqdb_writer_put(cqdb_writer_t *dbw, const char *str, int id)
190
+ {
191
+ int ret = 0;
192
+ const void *key = str;
193
+ uint32_t ksize = (uint32_t)(strlen(str) + 1);
194
+
195
+ /* Compute the hash value and choose a hash table. */
196
+ uint32_t hv = hashlittle(key, ksize, 0);
197
+ table_t *ht = &dbw->ht[hv % 256];
198
+
199
+ /* Check for non-negative identifier. */
200
+ if (id < 0)
201
+ {
202
+ ret = CQDB_ERROR_INVALIDID;
203
+ goto error_exit;
204
+ }
205
+
206
+ /* Write out the current data. */
207
+ write_uint32(dbw, (uint32_t)id);
208
+ write_uint32(dbw, (uint32_t)ksize);
209
+ write_data(dbw, key, ksize);
210
+ if (ferror(dbw->fp))
211
+ {
212
+ ret = CQDB_ERROR_FILEWRITE;
213
+ goto error_exit;
214
+ }
215
+
216
+ /* Expand the bucket if necessary. */
217
+ if (ht->size <= ht->num)
218
+ {
219
+ ht->size = (ht->size + 1) * 2;
220
+ ht->bucket = (bucket_t *)realloc(ht->bucket, sizeof(bucket_t) * ht->size);
221
+ if (ht->bucket == NULL)
222
+ {
223
+ ret = CQDB_ERROR_OUTOFMEMORY;
224
+ goto error_exit;
225
+ }
226
+ }
227
+
228
+ /* Set the hash value and current offset position. */
229
+ ht->bucket[ht->num].hash = hv;
230
+ ht->bucket[ht->num].offset = dbw->cur;
231
+ ++ht->num;
232
+
233
+ /* Store the backlink if specified. */
234
+ if (!(dbw->flag & CQDB_ONEWAY))
235
+ {
236
+ /* Expand the backlink array if necessary. */
237
+ if (dbw->bwd_size <= (uint32_t)id)
238
+ {
239
+ uint32_t size = dbw->bwd_size;
240
+
241
+ while (size <= (uint32_t)id)
242
+ size = (size + 1) * 2;
243
+ dbw->bwd = (uint32_t *)realloc(dbw->bwd, sizeof(uint32_t) * size);
244
+ if (dbw->bwd == NULL)
245
+ {
246
+ ret = CQDB_ERROR_OUTOFMEMORY;
247
+ goto error_exit;
248
+ }
249
+ while (dbw->bwd_size < size)
250
+ {
251
+ dbw->bwd[dbw->bwd_size++] = 0;
252
+ }
253
+ }
254
+
255
+ if (dbw->bwd_num <= (uint32_t)id)
256
+ {
257
+ dbw->bwd_num = (uint32_t)id + 1;
258
+ }
259
+
260
+ dbw->bwd[id] = dbw->cur;
261
+ }
262
+
263
+ /* Increment the current position. */
264
+ dbw->cur += sizeof(uint32_t) + sizeof(uint32_t) + ksize;
265
+ return 0;
266
+
267
+ error_exit:
268
+ dbw->flag |= CQDB_ERROR_OCCURRED;
269
+ return ret;
270
+ }
271
+
272
+ int cqdb_writer_close(cqdb_writer_t *dbw)
273
+ {
274
+ uint32_t i, j;
275
+ int k, ret = 0;
276
+ long offset = 0;
277
+ header_t header;
278
+
279
+ /* If an error have occurred, just free the memory blocks. */
280
+ if (dbw->flag & CQDB_ERROR_OCCURRED)
281
+ {
282
+ cqdb_writer_delete(dbw);
283
+ return 0;
284
+ }
285
+
286
+ /* Initialize the file header. */
287
+ strncpy((char *)header.chunkid, CHUNKID, 4);
288
+ header.flag = 0;
289
+ header.byteorder = BYTEORDER_CHECK;
290
+ header.bwd_offset = 0;
291
+ header.bwd_size = dbw->bwd_num;
292
+
293
+ /*
294
+ Store the hash tables. At this moment, the file pointer refers to
295
+ the offset succeeding the last key/data pair.
296
+ */
297
+ for (i = 0; i < NUM_TABLES; ++i)
298
+ {
299
+ table_t *ht = &dbw->ht[i];
300
+
301
+ /* Do not write empty hash tables. */
302
+ if (ht->bucket != NULL)
303
+ {
304
+ /*
305
+ Actual bucket will have the double size; half elements
306
+ in the bucket are kept empty.
307
+ */
308
+ int n = ht->num * 2;
309
+
310
+ /* Allocate the bucket. */
311
+ bucket_t *dst = (bucket_t *)calloc(n, sizeof(bucket_t));
312
+ if (dst == NULL)
313
+ {
314
+ ret = CQDB_ERROR_OUTOFMEMORY;
315
+ goto error_exit;
316
+ }
317
+
318
+ /*
319
+ Put hash elements to the bucket with the open-address method.
320
+ */
321
+ for (j = 0; j < ht->num; ++j)
322
+ {
323
+ const bucket_t *src = &ht->bucket[j];
324
+ int k = (src->hash >> 8) % n;
325
+
326
+ /* Find a vacant element. */
327
+ while (dst[k].offset != 0)
328
+ {
329
+ k = (k + 1) % n;
330
+ }
331
+
332
+ /* Store the hash element. */
333
+ dst[k].hash = src->hash;
334
+ dst[k].offset = src->offset;
335
+ }
336
+
337
+ /* Write the bucket. */
338
+ for (k = 0; k < n; ++k)
339
+ {
340
+ write_uint32(dbw, dst[k].hash);
341
+ write_uint32(dbw, dst[k].offset);
342
+ }
343
+
344
+ /* Free the bucket. */
345
+ free(dst);
346
+ }
347
+ }
348
+
349
+ /* Write the backlink array if specified. */
350
+ if (!(dbw->flag & CQDB_ONEWAY) && 0 < dbw->bwd_size)
351
+ {
352
+ /* Store the offset to the head of this array. */
353
+ header.bwd_offset = ftell(dbw->fp) - dbw->begin;
354
+ /* Store the contents of the backlink array. */
355
+ for (i = 0; i < dbw->bwd_num; ++i)
356
+ {
357
+ write_uint32(dbw, dbw->bwd[i]);
358
+ }
359
+ }
360
+
361
+ /* Check for an occurrence of a file-related error. */
362
+ if (ferror(dbw->fp))
363
+ {
364
+ ret = CQDB_ERROR_FILEWRITE;
365
+ goto error_exit;
366
+ }
367
+
368
+ /* Store the current position. */
369
+ offset = ftell(dbw->fp);
370
+ if (offset == -1)
371
+ {
372
+ ret = CQDB_ERROR_FILETELL;
373
+ goto error_exit;
374
+ }
375
+ header.size = (uint32_t)offset - dbw->begin;
376
+
377
+ /* Rewind the current position to the beginning. */
378
+ if (fseek(dbw->fp, dbw->begin, SEEK_SET) != 0)
379
+ {
380
+ ret = CQDB_ERROR_FILESEEK;
381
+ goto error_exit;
382
+ }
383
+
384
+ /* Write the file header. */
385
+ write_data(dbw, header.chunkid, 4);
386
+ write_uint32(dbw, header.size);
387
+ write_uint32(dbw, header.flag);
388
+ write_uint32(dbw, header.byteorder);
389
+ write_uint32(dbw, header.bwd_size);
390
+ write_uint32(dbw, header.bwd_offset);
391
+
392
+ /*
393
+ Write references to hash tables. At this moment, dbw->cur points
394
+ to the offset succeeding the last key/data pair.
395
+ */
396
+ for (i = 0; i < NUM_TABLES; ++i)
397
+ {
398
+ /* Offset to the hash table (or zero for non-existent tables). */
399
+ write_uint32(dbw, dbw->ht[i].num ? dbw->cur : 0);
400
+ /* Bucket size is double to the number of elements. */
401
+ write_uint32(dbw, dbw->ht[i].num * 2);
402
+ /* Advance the offset counter. */
403
+ dbw->cur += (dbw->ht[i].num * 2) * sizeof(bucket_t);
404
+ }
405
+
406
+ /* Check an occurrence of a file-related error. */
407
+ if (ferror(dbw->fp))
408
+ {
409
+ ret = CQDB_ERROR_FILEWRITE;
410
+ goto error_exit;
411
+ }
412
+
413
+ /* Seek to the last position. */
414
+ if (fseek(dbw->fp, offset, SEEK_SET) != 0)
415
+ {
416
+ ret = CQDB_ERROR_FILESEEK;
417
+ goto error_exit;
418
+ }
419
+
420
+ cqdb_writer_delete(dbw);
421
+ return ret;
422
+
423
+ error_exit:
424
+ /* Seek to the first position. */
425
+ fseek(dbw->fp, dbw->begin, SEEK_SET);
426
+ cqdb_writer_delete(dbw);
427
+ return ret;
428
+ }
429
+
430
+ static uint32_t read_uint32(const uint8_t *p)
431
+ {
432
+ uint32_t value;
433
+ value = ((uint32_t)p[0]);
434
+ value |= ((uint32_t)p[1] << 8);
435
+ value |= ((uint32_t)p[2] << 16);
436
+ value |= ((uint32_t)p[3] << 24);
437
+ return value;
438
+ }
439
+
440
+ static const uint8_t *read_tableref(tableref_t *ref, const uint8_t *p)
441
+ {
442
+ ref->offset = read_uint32(p);
443
+ p += sizeof(uint32_t);
444
+ ref->num = read_uint32(p);
445
+ p += sizeof(uint32_t);
446
+ return p;
447
+ }
448
+
449
+ static bucket_t *read_bucket(const uint8_t *p, uint32_t num)
450
+ {
451
+ uint32_t i;
452
+ bucket_t *bucket = (bucket_t *)calloc(num, sizeof(bucket_t));
453
+ for (i = 0; i < num; ++i)
454
+ {
455
+ bucket[i].hash = read_uint32(p);
456
+ p += sizeof(uint32_t);
457
+ bucket[i].offset = read_uint32(p);
458
+ p += sizeof(uint32_t);
459
+ }
460
+ return bucket;
461
+ }
462
+
463
+ static uint32_t *read_backward_links(const uint8_t *p, uint32_t num)
464
+ {
465
+ uint32_t i;
466
+ uint32_t *bwd = (uint32_t *)calloc(num, sizeof(uint32_t));
467
+ for (i = 0; i < num; ++i)
468
+ {
469
+ bwd[i] = read_uint32(p);
470
+ p += sizeof(uint32_t);
471
+ }
472
+ return bwd;
473
+ }
474
+
475
+ cqdb_t *cqdb_reader(const void *buffer, size_t size)
476
+ {
477
+ int i;
478
+ cqdb_t *db = NULL;
479
+
480
+ /* The minimum size of a valid CQDB is OFFSET_DATA. */
481
+ if (size < OFFSET_DATA)
482
+ {
483
+ return NULL;
484
+ }
485
+
486
+ /* Check the file chunkid. */
487
+ if (memcmp(buffer, CHUNKID, 4) != 0)
488
+ {
489
+ return NULL;
490
+ }
491
+
492
+ db = (cqdb_t *)calloc(1, sizeof(cqdb_t));
493
+ if (db != NULL)
494
+ {
495
+ const uint8_t *p = NULL;
496
+
497
+ /* Set memory block and size. */
498
+ db->buffer = buffer;
499
+ db->size = size;
500
+
501
+ /* Read the database header. */
502
+ p = db->buffer;
503
+ strncpy((char *)db->header.chunkid, (const char *)p, 4);
504
+ p += sizeof(uint32_t);
505
+ db->header.size = read_uint32(p);
506
+ p += sizeof(uint32_t);
507
+ db->header.flag = read_uint32(p);
508
+ p += sizeof(uint32_t);
509
+ db->header.byteorder = read_uint32(p);
510
+ p += sizeof(uint32_t);
511
+ db->header.bwd_size = read_uint32(p);
512
+ p += sizeof(uint32_t);
513
+ db->header.bwd_offset = read_uint32(p);
514
+ p += sizeof(uint32_t);
515
+
516
+ /* Check the consistency of byte order. */
517
+ if (db->header.byteorder != BYTEORDER_CHECK)
518
+ {
519
+ free(db);
520
+ return NULL;
521
+ }
522
+
523
+ /* Check the chunk size. */
524
+ if (size < db->header.size)
525
+ {
526
+ free(db);
527
+ return NULL;
528
+ }
529
+
530
+ /* Set pointers to the hash tables. */
531
+ db->num = 0; /* Number of records. */
532
+ p = (db->buffer + OFFSET_REFS);
533
+ for (i = 0; i < NUM_TABLES; ++i)
534
+ {
535
+ tableref_t ref;
536
+ p = read_tableref(&ref, p);
537
+ if (ref.offset)
538
+ {
539
+ /* Set buckets. */
540
+ db->ht[i].bucket = read_bucket(db->buffer + ref.offset, ref.num);
541
+ db->ht[i].num = ref.num;
542
+ }
543
+ else
544
+ {
545
+ /* An empty hash table. */
546
+ db->ht[i].bucket = NULL;
547
+ db->ht[i].num = 0;
548
+ }
549
+
550
+ /* The number of records is the half of the table size.*/
551
+ db->num += ref.num / 2;
552
+ }
553
+
554
+ /* Set the pointer to the backlink array if any. */
555
+ if (db->header.bwd_offset)
556
+ {
557
+ db->bwd = read_backward_links(db->buffer + db->header.bwd_offset, db->num);
558
+ }
559
+ else
560
+ {
561
+ db->bwd = NULL;
562
+ }
563
+ }
564
+
565
+ return db;
566
+ }
567
+
568
+ void cqdb_delete(cqdb_t *db)
569
+ {
570
+ int i;
571
+
572
+ if (db != NULL)
573
+ {
574
+ for (i = 0; i < NUM_TABLES; ++i)
575
+ {
576
+ free(db->ht[i].bucket);
577
+ }
578
+ free(db->bwd);
579
+ free(db);
580
+ }
581
+ }
582
+
583
+ int cqdb_to_id(cqdb_t *db, const char *str)
584
+ {
585
+ uint32_t hv = hashlittle(str, strlen(str) + 1, 0);
586
+ int t = hv % 256;
587
+ table_t *ht = &db->ht[t];
588
+
589
+ if (ht->num && ht->bucket != NULL)
590
+ {
591
+ int n = ht->num;
592
+ int k = (hv >> 8) % n;
593
+ bucket_t *p = NULL;
594
+
595
+ while (p = &ht->bucket[k], p->offset)
596
+ {
597
+ if (p->hash == hv)
598
+ {
599
+ int value;
600
+ uint32_t ksize;
601
+ const uint8_t *q = db->buffer + p->offset;
602
+ value = (int)read_uint32(q);
603
+ q += sizeof(uint32_t);
604
+ ksize = read_uint32(q);
605
+ q += sizeof(uint32_t);
606
+ if (strcmp(str, (const char *)q) == 0)
607
+ {
608
+ return value;
609
+ }
610
+ }
611
+ k = (k + 1) % n;
612
+ }
613
+ }
614
+
615
+ return CQDB_ERROR_NOTFOUND;
616
+ }
617
+
618
+ const char *cqdb_to_string(cqdb_t *db, int id)
619
+ {
620
+ /* Check if the current database supports the backward look-up. */
621
+ if (db->bwd != NULL && (uint32_t)id < db->header.bwd_size)
622
+ {
623
+ uint32_t offset = db->bwd[id];
624
+ if (offset)
625
+ {
626
+ const uint8_t *p = db->buffer + offset;
627
+ p += sizeof(uint32_t); /* Skip key data. */
628
+ p += sizeof(uint32_t); /* Skip value size. */
629
+ return (const char *)p;
630
+ }
631
+ }
632
+
633
+ return NULL;
634
+ }
635
+
636
+ int cqdb_num(cqdb_t *db)
637
+ {
638
+ return db->num;
639
+ }