ferret 0.9.6 → 0.10.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +1 -1
- data/README +12 -24
- data/Rakefile +38 -54
- data/TODO +14 -17
- data/ext/analysis.c +982 -823
- data/ext/analysis.h +133 -76
- data/ext/array.c +96 -58
- data/ext/array.h +40 -13
- data/ext/bitvector.c +476 -118
- data/ext/bitvector.h +264 -22
- data/ext/compound_io.c +217 -229
- data/ext/defines.h +49 -0
- data/ext/document.c +107 -317
- data/ext/document.h +31 -65
- data/ext/except.c +81 -36
- data/ext/except.h +117 -55
- data/ext/extconf.rb +2 -9
- data/ext/ferret.c +211 -104
- data/ext/ferret.h +22 -11
- data/ext/filter.c +97 -82
- data/ext/fs_store.c +348 -367
- data/ext/global.c +226 -188
- data/ext/global.h +44 -26
- data/ext/hash.c +474 -391
- data/ext/hash.h +441 -68
- data/ext/hashset.c +124 -96
- data/ext/hashset.h +169 -20
- data/ext/helper.c +56 -5
- data/ext/helper.h +7 -0
- data/ext/inc/lang.h +29 -49
- data/ext/inc/threading.h +31 -0
- data/ext/ind.c +288 -278
- data/ext/ind.h +68 -0
- data/ext/index.c +5688 -0
- data/ext/index.h +663 -616
- data/ext/lang.h +29 -49
- data/ext/libstemmer.c +3 -3
- data/ext/mem_pool.c +84 -0
- data/ext/mem_pool.h +35 -0
- data/ext/posh.c +1006 -0
- data/ext/posh.h +1007 -0
- data/ext/priorityqueue.c +117 -194
- data/ext/priorityqueue.h +135 -39
- data/ext/q_boolean.c +1305 -1108
- data/ext/q_const_score.c +106 -93
- data/ext/q_filtered_query.c +138 -135
- data/ext/q_fuzzy.c +206 -242
- data/ext/q_match_all.c +94 -80
- data/ext/q_multi_term.c +663 -0
- data/ext/q_parser.c +667 -593
- data/ext/q_phrase.c +992 -555
- data/ext/q_prefix.c +72 -61
- data/ext/q_range.c +235 -210
- data/ext/q_span.c +1480 -1166
- data/ext/q_term.c +273 -246
- data/ext/q_wildcard.c +127 -114
- data/ext/r_analysis.c +1720 -711
- data/ext/r_index.c +3049 -0
- data/ext/r_qparser.c +433 -146
- data/ext/r_search.c +2934 -1993
- data/ext/r_store.c +372 -143
- data/ext/r_utils.c +941 -0
- data/ext/ram_store.c +330 -326
- data/ext/search.c +1291 -668
- data/ext/search.h +403 -702
- data/ext/similarity.c +91 -113
- data/ext/similarity.h +45 -30
- data/ext/sort.c +721 -484
- data/ext/stopwords.c +361 -273
- data/ext/store.c +556 -58
- data/ext/store.h +706 -126
- data/ext/tags +3578 -2780
- data/ext/term_vectors.c +352 -0
- data/ext/threading.h +31 -0
- data/ext/win32.h +54 -0
- data/lib/ferret.rb +5 -17
- data/lib/ferret/document.rb +130 -2
- data/lib/ferret/index.rb +577 -26
- data/lib/ferret/number_tools.rb +157 -0
- data/lib/ferret_version.rb +3 -0
- data/test/test_helper.rb +5 -13
- data/test/unit/analysis/tc_analyzer.rb +513 -1
- data/test/unit/analysis/{ctc_tokenstream.rb → tc_token_stream.rb} +23 -0
- data/test/unit/index/tc_index.rb +183 -240
- data/test/unit/index/tc_index_reader.rb +312 -479
- data/test/unit/index/tc_index_writer.rb +397 -13
- data/test/unit/index/th_doc.rb +269 -206
- data/test/unit/query_parser/tc_query_parser.rb +40 -33
- data/test/unit/search/tc_filter.rb +59 -71
- data/test/unit/search/tc_fuzzy_query.rb +24 -16
- data/test/unit/search/tc_index_searcher.rb +23 -201
- data/test/unit/search/tc_multi_searcher.rb +78 -226
- data/test/unit/search/tc_search_and_sort.rb +93 -81
- data/test/unit/search/tc_sort.rb +23 -23
- data/test/unit/search/tc_sort_field.rb +7 -7
- data/test/unit/search/tc_spans.rb +51 -47
- data/test/unit/search/tm_searcher.rb +339 -0
- data/test/unit/store/tc_fs_store.rb +1 -1
- data/test/unit/store/tm_store_lock.rb +3 -3
- data/test/unit/tc_document.rb +81 -0
- data/test/unit/ts_analysis.rb +1 -1
- data/test/unit/ts_utils.rb +1 -1
- data/test/unit/utils/tc_bit_vector.rb +288 -0
- data/test/unit/utils/tc_number_tools.rb +117 -0
- data/test/unit/utils/tc_priority_queue.rb +106 -0
- metadata +140 -301
- data/CHANGELOG +0 -9
- data/ext/dummy.exe +0 -0
- data/ext/field.c +0 -408
- data/ext/frtio.h +0 -13
- data/ext/inc/except.h +0 -90
- data/ext/index_io.c +0 -382
- data/ext/index_rw.c +0 -2658
- data/ext/lang.c +0 -41
- data/ext/nix_io.c +0 -134
- data/ext/q_multi_phrase.c +0 -380
- data/ext/r_doc.c +0 -582
- data/ext/r_index_io.c +0 -1021
- data/ext/r_term.c +0 -219
- data/ext/term.c +0 -820
- data/ext/termdocs.c +0 -611
- data/ext/vector.c +0 -637
- data/ext/w32_io.c +0 -150
- data/lib/ferret/analysis.rb +0 -11
- data/lib/ferret/analysis/analyzers.rb +0 -112
- data/lib/ferret/analysis/standard_tokenizer.rb +0 -71
- data/lib/ferret/analysis/token.rb +0 -100
- data/lib/ferret/analysis/token_filters.rb +0 -86
- data/lib/ferret/analysis/token_stream.rb +0 -26
- data/lib/ferret/analysis/tokenizers.rb +0 -112
- data/lib/ferret/analysis/word_list_loader.rb +0 -27
- data/lib/ferret/document/document.rb +0 -152
- data/lib/ferret/document/field.rb +0 -312
- data/lib/ferret/index/compound_file_io.rb +0 -338
- data/lib/ferret/index/document_writer.rb +0 -289
- data/lib/ferret/index/field_infos.rb +0 -279
- data/lib/ferret/index/fields_io.rb +0 -181
- data/lib/ferret/index/index.rb +0 -675
- data/lib/ferret/index/index_file_names.rb +0 -33
- data/lib/ferret/index/index_reader.rb +0 -503
- data/lib/ferret/index/index_writer.rb +0 -534
- data/lib/ferret/index/multi_reader.rb +0 -377
- data/lib/ferret/index/multiple_term_doc_pos_enum.rb +0 -98
- data/lib/ferret/index/segment_infos.rb +0 -130
- data/lib/ferret/index/segment_merge_info.rb +0 -49
- data/lib/ferret/index/segment_merge_queue.rb +0 -16
- data/lib/ferret/index/segment_merger.rb +0 -358
- data/lib/ferret/index/segment_reader.rb +0 -412
- data/lib/ferret/index/segment_term_enum.rb +0 -169
- data/lib/ferret/index/segment_term_vector.rb +0 -58
- data/lib/ferret/index/term.rb +0 -53
- data/lib/ferret/index/term_buffer.rb +0 -83
- data/lib/ferret/index/term_doc_enum.rb +0 -291
- data/lib/ferret/index/term_enum.rb +0 -52
- data/lib/ferret/index/term_info.rb +0 -37
- data/lib/ferret/index/term_infos_io.rb +0 -321
- data/lib/ferret/index/term_vector_offset_info.rb +0 -20
- data/lib/ferret/index/term_vectors_io.rb +0 -553
- data/lib/ferret/query_parser.rb +0 -312
- data/lib/ferret/query_parser/query_parser.tab.rb +0 -928
- data/lib/ferret/search.rb +0 -50
- data/lib/ferret/search/boolean_clause.rb +0 -100
- data/lib/ferret/search/boolean_query.rb +0 -299
- data/lib/ferret/search/boolean_scorer.rb +0 -294
- data/lib/ferret/search/caching_wrapper_filter.rb +0 -40
- data/lib/ferret/search/conjunction_scorer.rb +0 -99
- data/lib/ferret/search/disjunction_sum_scorer.rb +0 -205
- data/lib/ferret/search/exact_phrase_scorer.rb +0 -32
- data/lib/ferret/search/explanation.rb +0 -41
- data/lib/ferret/search/field_cache.rb +0 -215
- data/lib/ferret/search/field_doc.rb +0 -31
- data/lib/ferret/search/field_sorted_hit_queue.rb +0 -184
- data/lib/ferret/search/filter.rb +0 -11
- data/lib/ferret/search/filtered_query.rb +0 -130
- data/lib/ferret/search/filtered_term_enum.rb +0 -79
- data/lib/ferret/search/fuzzy_query.rb +0 -154
- data/lib/ferret/search/fuzzy_term_enum.rb +0 -247
- data/lib/ferret/search/hit_collector.rb +0 -34
- data/lib/ferret/search/hit_queue.rb +0 -11
- data/lib/ferret/search/index_searcher.rb +0 -200
- data/lib/ferret/search/match_all_query.rb +0 -104
- data/lib/ferret/search/multi_phrase_query.rb +0 -216
- data/lib/ferret/search/multi_searcher.rb +0 -261
- data/lib/ferret/search/multi_term_query.rb +0 -65
- data/lib/ferret/search/non_matching_scorer.rb +0 -22
- data/lib/ferret/search/phrase_positions.rb +0 -55
- data/lib/ferret/search/phrase_query.rb +0 -214
- data/lib/ferret/search/phrase_scorer.rb +0 -152
- data/lib/ferret/search/prefix_query.rb +0 -54
- data/lib/ferret/search/query.rb +0 -140
- data/lib/ferret/search/query_filter.rb +0 -51
- data/lib/ferret/search/range_filter.rb +0 -103
- data/lib/ferret/search/range_query.rb +0 -139
- data/lib/ferret/search/req_excl_scorer.rb +0 -125
- data/lib/ferret/search/req_opt_sum_scorer.rb +0 -70
- data/lib/ferret/search/score_doc.rb +0 -38
- data/lib/ferret/search/score_doc_comparator.rb +0 -114
- data/lib/ferret/search/scorer.rb +0 -91
- data/lib/ferret/search/similarity.rb +0 -278
- data/lib/ferret/search/sloppy_phrase_scorer.rb +0 -47
- data/lib/ferret/search/sort.rb +0 -112
- data/lib/ferret/search/sort_comparator.rb +0 -60
- data/lib/ferret/search/sort_field.rb +0 -91
- data/lib/ferret/search/spans.rb +0 -12
- data/lib/ferret/search/spans/near_spans_enum.rb +0 -304
- data/lib/ferret/search/spans/span_first_query.rb +0 -79
- data/lib/ferret/search/spans/span_near_query.rb +0 -108
- data/lib/ferret/search/spans/span_not_query.rb +0 -130
- data/lib/ferret/search/spans/span_or_query.rb +0 -176
- data/lib/ferret/search/spans/span_query.rb +0 -25
- data/lib/ferret/search/spans/span_scorer.rb +0 -74
- data/lib/ferret/search/spans/span_term_query.rb +0 -105
- data/lib/ferret/search/spans/span_weight.rb +0 -84
- data/lib/ferret/search/spans/spans_enum.rb +0 -44
- data/lib/ferret/search/term_query.rb +0 -128
- data/lib/ferret/search/term_scorer.rb +0 -183
- data/lib/ferret/search/top_docs.rb +0 -36
- data/lib/ferret/search/top_field_docs.rb +0 -17
- data/lib/ferret/search/weight.rb +0 -54
- data/lib/ferret/search/wildcard_query.rb +0 -26
- data/lib/ferret/search/wildcard_term_enum.rb +0 -61
- data/lib/ferret/stemmers.rb +0 -1
- data/lib/ferret/stemmers/porter_stemmer.rb +0 -218
- data/lib/ferret/store.rb +0 -5
- data/lib/ferret/store/buffered_index_io.rb +0 -190
- data/lib/ferret/store/directory.rb +0 -141
- data/lib/ferret/store/fs_store.rb +0 -381
- data/lib/ferret/store/index_io.rb +0 -245
- data/lib/ferret/store/ram_store.rb +0 -286
- data/lib/ferret/utils.rb +0 -8
- data/lib/ferret/utils/bit_vector.rb +0 -123
- data/lib/ferret/utils/date_tools.rb +0 -138
- data/lib/ferret/utils/number_tools.rb +0 -91
- data/lib/ferret/utils/parameter.rb +0 -41
- data/lib/ferret/utils/priority_queue.rb +0 -120
- data/lib/ferret/utils/string_helper.rb +0 -47
- data/lib/ferret/utils/thread_local.rb +0 -28
- data/lib/ferret/utils/weak_key_hash.rb +0 -60
- data/lib/rferret.rb +0 -37
- data/rake_utils/code_statistics.rb +0 -106
- data/test/benchmark/tb_ram_store.rb +0 -76
- data/test/benchmark/tb_rw_vint.rb +0 -26
- data/test/functional/thread_safety_index_test.rb +0 -81
- data/test/functional/thread_safety_test.rb +0 -137
- data/test/longrunning/tc_numbertools.rb +0 -60
- data/test/longrunning/tm_store.rb +0 -19
- data/test/unit/analysis/ctc_analyzer.rb +0 -532
- data/test/unit/analysis/data/wordfile +0 -6
- data/test/unit/analysis/rtc_letter_tokenizer.rb +0 -20
- data/test/unit/analysis/rtc_lower_case_filter.rb +0 -20
- data/test/unit/analysis/rtc_lower_case_tokenizer.rb +0 -27
- data/test/unit/analysis/rtc_per_field_analyzer_wrapper.rb +0 -39
- data/test/unit/analysis/rtc_porter_stem_filter.rb +0 -16
- data/test/unit/analysis/rtc_standard_analyzer.rb +0 -20
- data/test/unit/analysis/rtc_standard_tokenizer.rb +0 -20
- data/test/unit/analysis/rtc_stop_analyzer.rb +0 -20
- data/test/unit/analysis/rtc_stop_filter.rb +0 -14
- data/test/unit/analysis/rtc_white_space_analyzer.rb +0 -21
- data/test/unit/analysis/rtc_white_space_tokenizer.rb +0 -20
- data/test/unit/analysis/rtc_word_list_loader.rb +0 -32
- data/test/unit/analysis/tc_token.rb +0 -25
- data/test/unit/document/rtc_field.rb +0 -28
- data/test/unit/document/tc_document.rb +0 -47
- data/test/unit/document/tc_field.rb +0 -98
- data/test/unit/index/rtc_compound_file_io.rb +0 -107
- data/test/unit/index/rtc_field_infos.rb +0 -127
- data/test/unit/index/rtc_fields_io.rb +0 -167
- data/test/unit/index/rtc_multiple_term_doc_pos_enum.rb +0 -83
- data/test/unit/index/rtc_segment_infos.rb +0 -74
- data/test/unit/index/rtc_segment_term_docs.rb +0 -17
- data/test/unit/index/rtc_segment_term_enum.rb +0 -60
- data/test/unit/index/rtc_segment_term_vector.rb +0 -71
- data/test/unit/index/rtc_term_buffer.rb +0 -57
- data/test/unit/index/rtc_term_info.rb +0 -19
- data/test/unit/index/rtc_term_infos_io.rb +0 -192
- data/test/unit/index/rtc_term_vectors_io.rb +0 -108
- data/test/unit/index/tc_term.rb +0 -27
- data/test/unit/index/tc_term_voi.rb +0 -18
- data/test/unit/search/rtc_similarity.rb +0 -37
- data/test/unit/search/rtc_sort_field.rb +0 -14
- data/test/unit/search/tc_multi_searcher2.rb +0 -126
- data/test/unit/store/rtc_fs_store.rb +0 -62
- data/test/unit/store/rtc_ram_store.rb +0 -15
- data/test/unit/store/rtm_store.rb +0 -150
- data/test/unit/store/rtm_store_lock.rb +0 -2
- data/test/unit/ts_document.rb +0 -2
- data/test/unit/utils/rtc_bit_vector.rb +0 -73
- data/test/unit/utils/rtc_date_tools.rb +0 -50
- data/test/unit/utils/rtc_number_tools.rb +0 -59
- data/test/unit/utils/rtc_parameter.rb +0 -40
- data/test/unit/utils/rtc_priority_queue.rb +0 -62
- data/test/unit/utils/rtc_string_helper.rb +0 -21
- data/test/unit/utils/rtc_thread.rb +0 -61
- data/test/unit/utils/rtc_weak_key_hash.rb +0 -25
- data/test/utils/number_to_spoken.rb +0 -132
data/ext/hash.h
CHANGED
@@ -3,87 +3,460 @@
|
|
3
3
|
|
4
4
|
#include "global.h"
|
5
5
|
|
6
|
-
#define NUM_ENTRIES 256
|
7
|
-
#define MULTIPLIER 31
|
8
|
-
|
9
|
-
typedef struct HashEntry {
|
10
|
-
char *name;
|
11
|
-
void *value;
|
12
|
-
struct HashEntry *next;
|
13
|
-
} HashEntry;
|
14
|
-
|
15
|
-
HashEntry **ht_create();
|
16
|
-
int ht_count(HashEntry **ht);
|
17
|
-
void ht_destroy(HashEntry **ht);
|
18
|
-
void ht_destroy_all(HashEntry **ht, void (*fn)(void *));
|
19
|
-
void ht_set(HashEntry **ht, char *name, void *value);
|
20
|
-
void *ht_get(HashEntry **ht, char *name);
|
21
|
-
void *ht_delete(HashEntry **ht, char *name);
|
22
|
-
|
23
6
|
/****************************************************************************
|
24
7
|
*
|
25
|
-
*
|
8
|
+
* HashTable
|
26
9
|
*
|
27
10
|
****************************************************************************/
|
28
11
|
|
29
|
-
#define
|
30
|
-
#define SLOW_DOWN 50000
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
12
|
+
#define HASH_MINSIZE 8
|
13
|
+
#define SLOW_DOWN 50000 /* stop increasing the hash table so quickly to
|
14
|
+
* conserve memory */
|
15
|
+
|
16
|
+
/**
|
17
|
+
* Return values for h_set
|
18
|
+
*/
|
19
|
+
enum HashSetValues
|
20
|
+
{
|
21
|
+
HASH_KEY_DOES_NOT_EXIST = 0,
|
22
|
+
HASH_KEY_EQUAL = 1,
|
23
|
+
HASH_KEY_SAME = 2
|
37
24
|
};
|
38
25
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
26
|
+
/**
|
27
|
+
* struct used internally to store values in the HashTable
|
28
|
+
*/
|
29
|
+
typedef struct
|
30
|
+
{
|
31
|
+
ulong hash;
|
32
|
+
void *key;
|
33
|
+
void *value;
|
34
|
+
} HashEntry;
|
35
|
+
|
36
|
+
/**
|
37
|
+
* As the hash table is filled and entries are deleted, Dummy HashEntries are
|
38
|
+
* put in place. We therefor keep two counts. +size+ is the number active
|
39
|
+
* elements and +fill+ is the number of active elements together with the
|
40
|
+
* number of dummy elements. +fill+ is basically just kept around so that we
|
41
|
+
* know when to resize. The HashTable is resized when more than two thirds of
|
42
|
+
* the HashTable is Filled.
|
43
|
+
*/
|
44
|
+
typedef struct HashTable
|
45
|
+
{
|
46
|
+
int fill; /* num Active + num Dummy */
|
47
|
+
int size; /* num Active ie, num keys set */
|
48
|
+
int mask; /* capacity_of_table - 1 */
|
49
|
+
|
50
|
+
/* table points to smalltable initially. If the table grows beyond 2/3 of
|
51
|
+
* HASH_MINSIZE it will point to newly malloced memory as it grows. */
|
52
|
+
HashEntry *table;
|
53
|
+
|
54
|
+
/* When a HashTable is created it needs an initial table to start if off.
|
55
|
+
* All HashTables will start with smalltable and then malloc a larger
|
56
|
+
* table as the HashTable grows */
|
57
|
+
HashEntry smalltable[HASH_MINSIZE];
|
58
|
+
|
59
|
+
/* the following function pointers are used internally and should not be
|
60
|
+
* used outside of the HashTable methods */
|
61
|
+
HashEntry *(*lookup_i)(struct HashTable *self, register const void *key);
|
62
|
+
ulong (*hash_i)(const void *key);
|
63
|
+
int (*eq_i)(const void *key1, const void *key2);
|
64
|
+
void (*free_key_i)(void *p);
|
65
|
+
void (*free_value_i)(void *p);
|
66
|
+
} HashTable;
|
67
|
+
|
68
|
+
/**
|
69
|
+
* Hashing function type used by HashTable. A function of this type must be
|
70
|
+
* passed to create a new HashTable.
|
71
|
+
*
|
72
|
+
* @param key object to hash
|
73
|
+
* @return an unsigned 32-bit integer hash value
|
74
|
+
*/
|
75
|
+
typedef ulong (*hash_ft)(const void *key);
|
76
|
+
|
77
|
+
/**
|
78
|
+
* Equals function type used by HashTable. A function of this type must be
|
79
|
+
* passed to create a new HashTable.
|
80
|
+
*/
|
62
81
|
typedef int (*eq_ft)(const void *key1, const void *key2);
|
63
82
|
|
64
|
-
HshTable *h_new_str(free_ft free_key, free_ft free_value);
|
65
|
-
HshTable *h_new(hash_ft hash, eq_ft eq, free_ft free_key, free_ft free_value);
|
66
|
-
void h_destroy(HshTable *ht);
|
67
|
-
void h_clear(HshTable *ht);
|
68
83
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
84
|
+
/**
|
85
|
+
* Create a pointer to an allocated U32 integer. This function is a utility
|
86
|
+
* function used to add integers to a HashTable, either as the key or the
|
87
|
+
* value.
|
88
|
+
*/
|
89
|
+
extern ulong *imalloc(ulong value);
|
90
|
+
|
91
|
+
/**
|
92
|
+
* Determine a hash value for a string. The string must be null terminated
|
93
|
+
*
|
94
|
+
* @param str string to hash
|
95
|
+
* @return an unsigned 32-bit integer hash value
|
96
|
+
*/
|
97
|
+
extern ulong str_hash(const char *const str);
|
98
|
+
|
99
|
+
/**
|
100
|
+
* Create a new HashTable that uses any type of object as it's key. The
|
101
|
+
* HashTable will store all keys and values so if you want to destroy those
|
102
|
+
* values when the HashTable is destroyed then you should pass free functions.
|
103
|
+
* NULL will suffice otherwise.
|
104
|
+
*
|
105
|
+
* @param hash function to determine the hash value of a key in the HashTable
|
106
|
+
* @param eq function to determine the equality of to keys in the HashTable
|
107
|
+
* @param free_key function to free the key stored in the HashTable when an
|
108
|
+
* entry is deleted, replaced or when the HashTable is destroyed. If you
|
109
|
+
* pass NULL in place of this parameter the key will not be destroyed.
|
110
|
+
* @param free_value function to free the value stored in the HashTable when
|
111
|
+
* an entry is deleted, replaced or when the HashTable is destroyed. If you
|
112
|
+
* pass NULL in place of this parameter the value will not be destroyed.
|
113
|
+
* @return A newly allocated HashTable
|
114
|
+
*/
|
115
|
+
extern HashTable *h_new(hash_ft hash, eq_ft eq, free_ft free_key,
|
116
|
+
free_ft free_value);
|
117
|
+
|
118
|
+
/**
|
119
|
+
* Create a new HashTable that uses null-terminated strings as it's keys. The
|
120
|
+
* HashTable will store all keys and values so if you want to destroy those
|
121
|
+
* values when the HashTable is destroyed then you should pass free functions.
|
122
|
+
* NULL will suffice otherwise.
|
123
|
+
*
|
124
|
+
* @param free_key function to free the key stored in the HashTable when an
|
125
|
+
* entry is deleted, replaced or when the HashTable is destroyed. If you
|
126
|
+
* pass NULL in place of this parameter the key will not be destroyed.
|
127
|
+
* @param free_value function to free the value stored in the HashTable when
|
128
|
+
* an entry is deleted, replaced or when the HashTable is destroyed. If you
|
129
|
+
* pass NULL in place of this parameter the value will not be destroyed.
|
130
|
+
* @return A newly allocated HashTable
|
131
|
+
*/
|
132
|
+
extern HashTable *h_new_str(free_ft free_key, free_ft free_value);
|
133
|
+
|
134
|
+
/**
|
135
|
+
* Create a new HashTable that uses integers as it's keys. The
|
136
|
+
* HashTable will store all values so if you want to destroy those
|
137
|
+
* values when the HashTable is destroyed then you should pass a free function.
|
138
|
+
* NULL will suffice otherwise.
|
139
|
+
*
|
140
|
+
* @param free_value function to free the value stored in the HashTable when
|
141
|
+
* an entry is deleted, replaced or when the HashTable is destroyed. If you
|
142
|
+
* pass NULL in place of this parameter the value will not be destroyed.
|
143
|
+
* @return A newly allocated HashTable
|
144
|
+
*/
|
145
|
+
extern HashTable *h_new_int(free_ft free_value);
|
146
|
+
|
147
|
+
/**
|
148
|
+
* Destroy the HashTable. This function will also destroy all keys and values
|
149
|
+
* in the HashTable depending on how the free_key and free_value were set.
|
150
|
+
*
|
151
|
+
* @param self the HashTable to destroy
|
152
|
+
*/
|
153
|
+
extern void h_destroy(HashTable *self);
|
154
|
+
|
155
|
+
/**
|
156
|
+
* Clear the HashTable. This function will delete all keys and values from the
|
157
|
+
* hash table, also destroying all keys and values in the HashTable depending
|
158
|
+
* on how the free_key and free_value were set.
|
159
|
+
*
|
160
|
+
* @param self the HashTable to clear
|
161
|
+
*/
|
162
|
+
extern void h_clear(HashTable *self);
|
163
|
+
|
164
|
+
/**
|
165
|
+
* Get the value in the HashTable referenced by the key +key+.
|
166
|
+
*
|
167
|
+
* @param self the HashTable to reference
|
168
|
+
* @param key the key to lookup
|
169
|
+
* @return the value referenced by the key +key+. If there is no value
|
170
|
+
* referenced by that key, NULL is returned.
|
171
|
+
*/
|
172
|
+
extern void *h_get(HashTable *self, const void *key);
|
173
|
+
|
174
|
+
/**
|
175
|
+
* Delete the value in HashTable referenced by the key +key+. When the value
|
176
|
+
* is deleted it is also destroyed along with the key depending on how
|
177
|
+
* free_key and free_value where set when the HashTable was created. If you
|
178
|
+
* don't want to destroy the value use h_rem.
|
179
|
+
*
|
180
|
+
* This functions returns +true+ if the value was deleted successfully or
|
181
|
+
* false if the key was not found.
|
182
|
+
*
|
183
|
+
* @see h_rem
|
184
|
+
*
|
185
|
+
* @param self the HashTable to reference
|
186
|
+
* @param key the key to lookup
|
187
|
+
* @return true if the object was successfully deleted or false if the key was
|
188
|
+
* not found
|
189
|
+
*/
|
190
|
+
extern int h_del(HashTable *self, const void *key);
|
191
|
+
|
192
|
+
/**
|
193
|
+
* Remove the value in HashTable referenced by the key +key+. When the value
|
194
|
+
* is removed it is returned rather than destroyed. The key however is
|
195
|
+
* destroyed using the free_key functions passed when the HashTable is created
|
196
|
+
* if del_key is true.
|
197
|
+
*
|
198
|
+
* If you want the value to be destroyed, use the h_del function.
|
199
|
+
*
|
200
|
+
* @see h_del
|
201
|
+
*
|
202
|
+
* @param self the HashTable to reference
|
203
|
+
* @param key the key to lookup
|
204
|
+
* @param del_key set to true if you want the key to be deleted when the value
|
205
|
+
* is removed from the HashTable
|
206
|
+
* @return the value referenced by +key+ if it can be found or NULL otherwise
|
207
|
+
*/
|
208
|
+
extern void *h_rem(HashTable *self, const void *key, bool del_key);
|
209
|
+
|
210
|
+
/**
|
211
|
+
* WARNING: this function may destroy an old value or key if the key already
|
212
|
+
* exists in the HashTable, depending on how free_value and free_key were set
|
213
|
+
* for this HashTable.
|
214
|
+
*
|
215
|
+
* Add the value +value+ to the HashTable referencing it with key +key+.
|
216
|
+
*
|
217
|
+
* When a value is added to the HashTable it replaces any value that
|
218
|
+
* might already be stored under that key. If free_value is already set then
|
219
|
+
* the old value will be freed using that function.
|
220
|
+
*
|
221
|
+
* Similarly the old key might replace be replaced by the new key if they are
|
222
|
+
* are equal (according to the HashTable's eq function) but seperately
|
223
|
+
* allocated objects.
|
224
|
+
*
|
225
|
+
* @param self the HashTable to add the value to
|
226
|
+
* @param key the key to use to reference the value
|
227
|
+
* @param value the value to add to the HashTable
|
228
|
+
* @return one of three values;
|
229
|
+
* <pre>
|
230
|
+
* HASH_KEY_DOES_NOT_EXIST there was no value stored with that key
|
231
|
+
* HASH_KEY_EQUAL the key existed and was seperately allocated.
|
232
|
+
* In this situation the old key will have been
|
233
|
+
* destroyed if free_key was set
|
234
|
+
* HASH_KEY_SAME the key was identical (same memory pointer) to
|
235
|
+
* the existing key so no key was freed
|
236
|
+
* </pre>
|
237
|
+
*/
|
238
|
+
extern int h_set(HashTable *self, const void *key, void *value);
|
239
|
+
|
240
|
+
/**
|
241
|
+
* Add the value +value+ to the HashTable referencing it with key +key+. If
|
242
|
+
* the key already exists in the HashTable, the value won't be added and the
|
243
|
+
* function will return false. Otherwise it will return true.
|
244
|
+
*
|
245
|
+
* @param self the HashTable to add the value to
|
246
|
+
* @param key the key to use to reference the value
|
247
|
+
* @param value the value to add to the HashTable
|
248
|
+
* @return true if the value was successfully added or false otherwise
|
249
|
+
*/
|
250
|
+
extern int h_set_safe(HashTable *self, const void *key, void *value);
|
251
|
+
|
252
|
+
/**
|
253
|
+
* Return a hash entry object so you can handle the insert yourself. This can
|
254
|
+
* be used for performance reasons or for more control over how a value is
|
255
|
+
* added. Say, for example, you wanted to append a value to an array, or add a
|
256
|
+
* new array if non-existed, you could use this method by checking the value
|
257
|
+
* of the HashEntry returned.
|
258
|
+
*
|
259
|
+
* @param self the HashTable to add the value to
|
260
|
+
* @param key the key to use to reference the value
|
261
|
+
* @return HashEntry a pointer to the hash entry object now reserved for this
|
262
|
+
* value. Be sure to set both the *key* and the *value*
|
263
|
+
*/
|
264
|
+
extern HashEntry *h_set_ext(HashTable *ht, const void *key);
|
265
|
+
|
266
|
+
/**
|
267
|
+
* Check whether key +key+ exists in the HashTable.
|
268
|
+
*
|
269
|
+
* @param self the HashTable to check in
|
270
|
+
* @param key the key to check for in the HashTable
|
271
|
+
* @return true if the key exists in the HashTable, false otherwise.
|
272
|
+
*/
|
273
|
+
extern int h_has_key(HashTable *self, const void *key);
|
274
|
+
|
275
|
+
/**
|
276
|
+
* Get the value in the HashTable referenced by an integer key +key+.
|
277
|
+
*
|
278
|
+
* @param self the HashTable to reference
|
279
|
+
* @param key the integer key to lookup
|
280
|
+
* @return the value referenced by the key +key+. If there is no value
|
281
|
+
* referenced by that key, NULL is returned.
|
282
|
+
*/
|
283
|
+
extern void *h_get_int(HashTable *self, const ulong key);
|
284
|
+
|
285
|
+
/**
|
286
|
+
* Delete the value in HashTable referenced by the integer key +key+. When the
|
287
|
+
* value is deleted it is also destroyed using the free_value function. If you
|
288
|
+
* don't want to destroy the value use h_rem.
|
289
|
+
*
|
290
|
+
* This functions returns +true+ if the value was deleted successfully or
|
291
|
+
* false if the key was not found.
|
292
|
+
*
|
293
|
+
* @see h_rem
|
294
|
+
*
|
295
|
+
* @param self the HashTable to reference
|
296
|
+
* @param key the integer key to lookup
|
297
|
+
* @return true if the object was successfully deleted or false if the key was
|
298
|
+
* not found
|
299
|
+
*/
|
300
|
+
extern int h_del_int(HashTable *self, const ulong key);
|
301
|
+
|
302
|
+
/**
|
303
|
+
* Remove the value in HashTable referenced by the integer key +key+. When the
|
304
|
+
* value is removed it is returned rather than destroyed.
|
305
|
+
*
|
306
|
+
* If you want the value to be destroyed, use the h_del function.
|
307
|
+
*
|
308
|
+
* @see h_del
|
309
|
+
*
|
310
|
+
* @param self the HashTable to reference
|
311
|
+
* @param key the integer key to lookup
|
312
|
+
* @return the value referenced by +key+ if it can be found or NULL otherwise
|
313
|
+
*/
|
314
|
+
extern void *h_rem_int(HashTable *self, const ulong key);
|
315
|
+
|
316
|
+
/**
|
317
|
+
* WARNING: this function may destroy an old value if the key already exists
|
318
|
+
* in the HashTable, depending on how free_value was set for this HashTable.
|
319
|
+
*
|
320
|
+
* Add the value +value+ to the HashTable referencing it with an integer key
|
321
|
+
* +key+.
|
322
|
+
*
|
323
|
+
* When a value is added to the HashTable it replaces any value that
|
324
|
+
* might already be stored under that key. If free_value is already set then
|
325
|
+
* the old value will be freed using that function.
|
326
|
+
*
|
327
|
+
* Similarly the old key might replace be replaced by the new key if they are
|
328
|
+
* are equal (according to the HashTable's eq function) but seperately
|
329
|
+
* allocated objects.
|
330
|
+
*
|
331
|
+
* @param self the HashTable to add the value to
|
332
|
+
* @param key the integer key to use to reference the value
|
333
|
+
* @param value the value to add to the HashTable
|
334
|
+
* @return one of three values;
|
335
|
+
* <pre>
|
336
|
+
* HASH_KEY_DOES_NOT_EXIST there was no value stored with that key
|
337
|
+
* HASH_KEY_EQUAL the key existed and was seperately allocated.
|
338
|
+
* In this situation the old key will have been
|
339
|
+
* destroyed if free_key was set
|
340
|
+
* HASH_KEY_SAME the key was identical (same memory pointer) to
|
341
|
+
* the existing key so no key was freed
|
342
|
+
* </pre>
|
343
|
+
*/
|
344
|
+
extern int h_set_int(HashTable *self, const ulong key, void *value);
|
345
|
+
|
346
|
+
/**
|
347
|
+
* Add the value +value+ to the HashTable referencing it with integer key
|
348
|
+
* +key+. If the key already exists in the HashTable, the value won't be added
|
349
|
+
* and the function will return false. Otherwise it will return true.
|
350
|
+
*
|
351
|
+
* @param self the HashTable to add the value to
|
352
|
+
* @param key the integer key to use to reference the value
|
353
|
+
* @param value the value to add to the HashTable
|
354
|
+
* @return true if the value was successfully added or false otherwise
|
355
|
+
*/
|
356
|
+
extern int h_set_safe_int(HashTable *self, const ulong key, void *value);
|
357
|
+
/**
|
358
|
+
* Check whether integer key +key+ exists in the HashTable.
|
359
|
+
*
|
360
|
+
* @param self the HashTable to check in
|
361
|
+
* @param key the integer key to check for in the HashTable
|
362
|
+
* @return true if the key exists in the HashTable, false otherwise.
|
363
|
+
*/
|
364
|
+
extern int h_has_key_int(HashTable *self, const ulong key);
|
365
|
+
|
366
|
+
typedef void (*h_each_key_val_ft)(void *key, void *value, void *arg);
|
76
367
|
|
77
|
-
|
78
|
-
|
79
|
-
|
368
|
+
/**
|
369
|
+
* Run function +each_key_val+ on each key and value in the HashTable. The third
|
370
|
+
* argument +arg+ will also be passed to +each_key_val+. If you need to pass
|
371
|
+
* more than one argument to the function you should pass a struct.
|
372
|
+
*
|
373
|
+
* example;
|
374
|
+
*
|
375
|
+
* // Lets say we have stored strings in a HashTable and we want to put them
|
376
|
+
* // all into an array. First we need to create a struct to store the
|
377
|
+
* // strings;
|
378
|
+
*
|
379
|
+
* struct StringArray {
|
380
|
+
* char **strings;
|
381
|
+
* int cnt;
|
382
|
+
* int size;
|
383
|
+
* };
|
384
|
+
*
|
385
|
+
* static void add_string_ekv(void *key, void *value,
|
386
|
+
* struct StringArray *str_arr)
|
387
|
+
* {
|
388
|
+
* str_arr->strings[str_arr->cnt] = (char *)value;
|
389
|
+
* str_arr->cnt++;
|
390
|
+
* }
|
391
|
+
*
|
392
|
+
* struct StringArray *h_extract_strings(HashTable *ht)
|
393
|
+
* {
|
394
|
+
* struct StringArray *str_arr = ALLOC(struct StringArray);
|
395
|
+
*
|
396
|
+
* str_arr->strings = ALLOC_N(char *, ht->size);
|
397
|
+
* str_arr->cnt = 0;
|
398
|
+
* str_arr->size = ht->size;
|
399
|
+
*
|
400
|
+
* h_each(ht, (h_each_key_val_ft)add_string_ekv, str_arr);
|
401
|
+
*
|
402
|
+
* return str_arr;
|
403
|
+
* }
|
404
|
+
*
|
405
|
+
* @param self the HashTable to run the function on
|
406
|
+
* @param each_key_val function to run on on each key and value in the
|
407
|
+
* HashTable
|
408
|
+
* @param arg an extra argument to pass to each_key_val each time it is called
|
409
|
+
*/
|
410
|
+
extern void h_each(HashTable *self,
|
411
|
+
void (*each_key_val)(void *key, void *value, void *arg),
|
412
|
+
void *arg);
|
80
413
|
|
81
414
|
typedef void *(*h_clone_func_t)(void *val);
|
82
|
-
|
83
|
-
|
84
|
-
|
415
|
+
/**
|
416
|
+
* Clone the HashTable as well as cloning each of the keys and values if you
|
417
|
+
* want to do a deep clone. To do a deep clone you will need to pass a
|
418
|
+
* clone_key function and/or a clone_value function.
|
419
|
+
*
|
420
|
+
* @param self the HashTable to clone
|
421
|
+
* @param clone_key the function to clone the key with
|
422
|
+
* @param clone_value the function to clone the value with
|
423
|
+
* @return a clone of the original HashTable
|
424
|
+
*/
|
425
|
+
extern HashTable *h_clone(HashTable *self,
|
426
|
+
h_clone_func_t clone_key,
|
427
|
+
h_clone_func_t clone_value);
|
428
|
+
|
429
|
+
/*
|
430
|
+
* The following functions should only be used in static HashTable
|
431
|
+
* declarations
|
432
|
+
*/
|
433
|
+
/**
|
434
|
+
* This is the lookup function for a hash table keyed with strings. Since it
|
435
|
+
* is so common for hash tables to be keyed with strings it gets it's own
|
436
|
+
* lookup function. This method will always return a HashEntry. If there is no
|
437
|
+
* entry with the given key then an empty entry will be returned with the key
|
438
|
+
* set to the key that was passed.
|
439
|
+
*
|
440
|
+
* @param ht the hash table to look in
|
441
|
+
* @param key the key to lookup
|
442
|
+
* @return the HashEntry that was found
|
443
|
+
*/
|
444
|
+
extern HashEntry *h_lookup_str(HashTable *ht, register const char *key);
|
445
|
+
|
446
|
+
/**
|
447
|
+
* This is the lookup function for a hash table with non-string keys. The
|
448
|
+
* hash() and eq() methods used are stored in the hash table. This method will
|
449
|
+
* always return a HashEntry. If there is no entry with the given key then an
|
450
|
+
* empty entry will be returned with the key set to the key that was passed.
|
451
|
+
*
|
452
|
+
* @param ht the hash table to look in
|
453
|
+
* @param key the key to lookup
|
454
|
+
* @return the HashEntry that was found
|
455
|
+
*/
|
456
|
+
extern HashEntry *h_lookup(HashTable *ht, register const void *key);
|
457
|
+
|
458
|
+
typedef HashEntry *(*h_lookup_ft)(HashTable *ht, register const void *key);
|
85
459
|
|
86
|
-
void
|
87
|
-
HshEntry *h_lookup_str(HshTable *ht, register const void *key_p);
|
460
|
+
extern void h_str_print_keys(HashTable *ht);
|
88
461
|
|
89
462
|
#endif
|