RubyTrie 2.1 → 2.2

Sign up to get free protection for your applications and to get access to all the features.
data/ChangeLog CHANGED
@@ -1,4 +1,5 @@
1
1
  1.0 added children and each methods
2
2
  1.1 added levenshtein algorithm implementation - inspired from: http://stevehanov.ca/blog/index.php?id=114
3
3
  2.0 completely rewritten the trie implementation to use more optimized data structure. For /usr/share/dict/web2 it takes the memory to about 6Mb from 40Mb. The levenshtein now takes a block and passes in the value but it can be easy extened to return an array. It also has a glitch where some values are for some reason just false... :|
4
- 2.1 bug fixes, fixing levenshtein search
4
+ 2.1 bug fixes, fixing levenshtein search
5
+ 2.2 fixes child search
@@ -13,6 +13,8 @@
13
13
 
14
14
  #include "otrie2.h"
15
15
 
16
+ Node* node_find_impl(Node* this, const char* string, bool include_partial);
17
+
16
18
  Node* new_node() {
17
19
  Node *node = malloc(sizeof(Node));
18
20
  memset(node, 0, sizeof(Node));
@@ -61,6 +63,14 @@ void node_insert(Node* node, const char* string, const VALUE value) {
61
63
  }
62
64
 
63
65
  Node* node_find(Node* this, const char* string) {
66
+ return node_find_impl(this, string, false);
67
+ }
68
+
69
+ Node* partial_node_find(Node* this, const char* string) {
70
+ return node_find_impl(this, string, true);
71
+ }
72
+
73
+ Node* node_find_impl(Node* this, const char* string, bool include_partial) {
64
74
  int len = strlen(string);
65
75
  Pos *cur = new_pos(this, 0);
66
76
  int i=0;
@@ -68,7 +78,7 @@ Node* node_find(Node* this, const char* string) {
68
78
  pos_next(cur, string + i, false);
69
79
  if (cur->node == NULL) { return NULL; }
70
80
  }
71
- if (strlen(cur->node->data) == cur->offset + 1)
81
+ if (strlen(cur->node->data) == cur->offset + 1 || include_partial)
72
82
  return cur->node;
73
83
  return NULL;
74
84
  }
@@ -129,11 +139,10 @@ void pos_next(Pos *this, const char* string, bool insert) {
129
139
  Node *splitChild = new_node_string(this->node->data + this->offset + 1);
130
140
  splitChild -> value = this -> node -> value;
131
141
  Node *newChild = new_node_string(string);
132
- newChild -> value = Qnil;
142
+ newChild -> value = Qnil;
133
143
  node_update_data(this->node, this->node->data, this->offset + 1);
134
144
  splitChild -> next_sibling = newChild;
135
145
  this->node -> first_child = splitChild;
136
- this->node -> value = Qnil;
137
146
 
138
147
  this->node = newChild;
139
148
  this->offset = 0;
@@ -33,6 +33,7 @@ Node* new_node();
33
33
  void free_node(Node*);
34
34
  void node_insert(Node* node, const char* string, const VALUE value);
35
35
  Node* node_find(Node* this, const char* string);
36
+ Node* partial_node_find(Node* this, const char* string);
36
37
 
37
38
  Pos* new_pos(Node *node, int offset);
38
39
  Node* pos_find_or_create_child(Pos* this, const char* string, bool down, bool insert);
@@ -117,7 +117,7 @@ static VALUE rb_trie_find_children(VALUE self, VALUE key) {
117
117
  key_cstring = StringValuePtr(key);
118
118
  Data_Get_Struct(self, Node, root);
119
119
 
120
- node = node_find(root, key_cstring);
120
+ node = partial_node_find(root, key_cstring);
121
121
 
122
122
  if (node != NULL && node->value != Qnil) {
123
123
  rb_ary_push(rary, node->value);
@@ -144,7 +144,7 @@ static VALUE rb_trie_find_children_with_block(VALUE self, VALUE key) {
144
144
  key_cstring = StringValuePtr(key);
145
145
  Data_Get_Struct(self, Node, root);
146
146
 
147
- node = node_find(root, key_cstring);
147
+ node = partial_node_find(root, key_cstring);
148
148
 
149
149
  if (node != NULL && node->value != Qnil) {
150
150
  rb_yield(node->value);
@@ -168,12 +168,10 @@ static VALUE rb_trie_set_key_to_value(VALUE self, VALUE key, VALUE value) {
168
168
 
169
169
  node = node_find(root, key_cstring);
170
170
  if (node == NULL || node -> value == Qnil) {
171
- // printf("New node for %s -> %d\n", key_cstring, value);
172
171
  VALUE arr = rb_ary_new();
173
172
  rb_ary_push(arr, value);
174
173
  node_insert(root, key_cstring, arr);
175
174
  } else {
176
- // printf("Append value %s to %s -> %d\n", node->data, key_cstring, node->value);
177
175
  rb_ary_push(node->value, value);
178
176
  }
179
177
 
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: RubyTrie
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 2
8
- - 1
9
- version: "2.1"
8
+ - 2
9
+ version: "2.2"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Petrica Ghiurca
@@ -15,7 +15,7 @@ autorequire: trie
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-21 00:00:00 +02:00
18
+ date: 2011-03-26 23:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies: []
21
21