tyler-trie 0.2.3 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.textile +3 -14
- data/VERSION.yml +2 -2
- data/ext/trie/Makefile +149 -0
- data/ext/trie/darray.c +673 -0
- data/ext/trie/darray.h +233 -0
- data/ext/trie/extconf.rb +0 -7
- data/ext/trie/fileutils.c +151 -0
- data/ext/trie/fileutils.h +36 -0
- data/ext/trie/tail.c +340 -0
- data/ext/trie/tail.h +207 -0
- data/ext/trie/trie-private.c +271 -0
- data/ext/trie/trie-private.h +31 -0
- data/ext/trie/trie.c +204 -301
- data/ext/trie/trie.h +40 -0
- data/ext/trie/triedefs.h +73 -0
- data/lib/trie.rb +1 -1
- data/spec/trie_spec.rb +31 -47
- metadata +14 -7
- data/spec/test-trie/README +0 -1
data/ext/trie/trie.h
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#include "darray.h"
|
2
|
+
#include "tail.h"
|
3
|
+
|
4
|
+
typedef struct _Trie {
|
5
|
+
DArray *da;
|
6
|
+
Tail *tail;
|
7
|
+
} Trie;
|
8
|
+
|
9
|
+
typedef struct _TrieState {
|
10
|
+
const Trie *trie; /**< the corresponding trie */
|
11
|
+
TrieIndex index; /**< index in double-array/tail structures */
|
12
|
+
short suffix_idx; /**< suffix character offset, if in suffix */
|
13
|
+
short is_suffix; /**< whether it is currently in suffix part */
|
14
|
+
} TrieState;
|
15
|
+
|
16
|
+
|
17
|
+
#define trie_da_is_separate(da,s) (da_get_base ((da), (s)) < 0)
|
18
|
+
#define trie_da_get_tail_index(da,s) (-da_get_base ((da), (s)))
|
19
|
+
#define trie_da_set_tail_index(da,s,v) (da_set_base ((da), (s), -(v)))
|
20
|
+
#define trie_state_is_terminal(s) trie_state_is_walkable((s),TRIE_CHAR_TERM)
|
21
|
+
|
22
|
+
|
23
|
+
Trie* trie_new();
|
24
|
+
void trie_free(Trie *trie);
|
25
|
+
static Bool trie_branch_in_branch (Trie *trie, TrieIndex sep_node, const TrieChar *suffix, TrieData data);
|
26
|
+
static Bool trie_branch_in_tail(Trie *trie, TrieIndex sep_node, const TrieChar *suffix, TrieData data);
|
27
|
+
Bool trie_store (Trie *trie, const TrieChar *key, TrieData data);
|
28
|
+
Bool trie_retrieve (const Trie *trie, const TrieChar *key, TrieData *o_data);
|
29
|
+
Bool trie_delete (Trie *trie, const TrieChar *key);
|
30
|
+
TrieState * trie_root (const Trie *trie);
|
31
|
+
static TrieState * trie_state_new (const Trie *trie, TrieIndex index, short suffix_idx, short is_suffix);
|
32
|
+
TrieState * trie_state_clone (const TrieState *s);
|
33
|
+
void trie_state_free (TrieState *s);
|
34
|
+
void trie_state_rewind (TrieState *s);
|
35
|
+
Bool trie_state_walk (TrieState *s, TrieChar c);
|
36
|
+
Bool trie_state_is_walkable (const TrieState *s, TrieChar c);
|
37
|
+
Bool trie_state_is_leaf (const TrieState *s);
|
38
|
+
TrieData trie_state_get_data (const TrieState *s);
|
39
|
+
|
40
|
+
|
data/ext/trie/triedefs.h
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2
|
+
/*
|
3
|
+
* triedefs.h - General typedefs for trie
|
4
|
+
* Created: 2006-08-11
|
5
|
+
* Author: Theppitak Karoonboonyanan <thep@linux.thai.net>
|
6
|
+
*/
|
7
|
+
|
8
|
+
#ifndef __TRIEDEFS_H
|
9
|
+
#define __TRIEDEFS_H
|
10
|
+
|
11
|
+
#include <datrie/typedefs.h>
|
12
|
+
|
13
|
+
/**
|
14
|
+
* @file triedefs.h
|
15
|
+
* @brief General typedefs for trie
|
16
|
+
*/
|
17
|
+
|
18
|
+
/**
|
19
|
+
* @brief Trie IO modes
|
20
|
+
*/
|
21
|
+
typedef enum {
|
22
|
+
TRIE_IO_READ = 0x01,
|
23
|
+
TRIE_IO_WRITE = 0x02,
|
24
|
+
TRIE_IO_CREATE = 0x04
|
25
|
+
} TrieIOMode;
|
26
|
+
|
27
|
+
/**
|
28
|
+
* @brief Trie character type for alphabet
|
29
|
+
*/
|
30
|
+
typedef uint32 AlphaChar;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* @brief Error value for alphabet character
|
34
|
+
*/
|
35
|
+
#define ALPHA_CHAR_ERROR (~(AlphaChar)0)
|
36
|
+
|
37
|
+
/**
|
38
|
+
* @brief Trie character type for key
|
39
|
+
*/
|
40
|
+
typedef unsigned char TrieChar;
|
41
|
+
/**
|
42
|
+
* @brief Trie terminator character
|
43
|
+
*/
|
44
|
+
#define TRIE_CHAR_TERM '\0'
|
45
|
+
#define TRIE_CHAR_MAX 255
|
46
|
+
|
47
|
+
/**
|
48
|
+
* @brief Type of Trie index
|
49
|
+
*/
|
50
|
+
typedef int32 TrieIndex;
|
51
|
+
/**
|
52
|
+
* @brief Trie error index
|
53
|
+
*/
|
54
|
+
#define TRIE_INDEX_ERROR 0
|
55
|
+
/**
|
56
|
+
* @brief Maximum trie index value
|
57
|
+
*/
|
58
|
+
#define TRIE_INDEX_MAX 0x7fffffff
|
59
|
+
|
60
|
+
/**
|
61
|
+
* @brief Type of value associated to trie entries
|
62
|
+
*/
|
63
|
+
typedef int32 TrieData;
|
64
|
+
/**
|
65
|
+
* @brief Trie error data
|
66
|
+
*/
|
67
|
+
#define TRIE_DATA_ERROR -1
|
68
|
+
|
69
|
+
#endif /* __TRIEDEFS_H */
|
70
|
+
|
71
|
+
/*
|
72
|
+
vi:ts=4:ai:expandtab
|
73
|
+
*/
|
data/lib/trie.rb
CHANGED
@@ -1 +1 @@
|
|
1
|
-
require File.dirname(__FILE__) + '/../ext/trie
|
1
|
+
require File.dirname(__FILE__) + '/../ext/trie'
|
data/spec/trie_spec.rb
CHANGED
@@ -1,27 +1,18 @@
|
|
1
|
-
require 'trie'
|
2
|
-
|
3
|
-
TRIE_PATH = 'spec/test-trie'
|
1
|
+
require File.dirname(__FILE__) + '/../ext/trie'
|
4
2
|
|
5
3
|
describe Trie do
|
6
4
|
before :each do
|
7
|
-
@trie = Trie.new
|
5
|
+
@trie = Trie.new;
|
8
6
|
@trie.add('rocket')
|
9
7
|
@trie.add('rock')
|
10
8
|
@trie.add('frederico')
|
11
9
|
end
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
end
|
19
|
-
|
20
|
-
describe :path do
|
21
|
-
it 'returns the correct path' do
|
22
|
-
@trie.path.should == TRIE_PATH
|
23
|
-
end
|
24
|
-
end
|
11
|
+
#describe :path do
|
12
|
+
# it 'returns the correct path' do
|
13
|
+
# @trie.path.should == TRIE_PATH
|
14
|
+
# end
|
15
|
+
#end
|
25
16
|
|
26
17
|
describe :has_key? do
|
27
18
|
it 'returns true for words in the trie' do
|
@@ -120,23 +111,23 @@ describe Trie do
|
|
120
111
|
end
|
121
112
|
end
|
122
113
|
|
123
|
-
describe :walk_to_terminal do
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
114
|
+
#describe :walk_to_terminal do
|
115
|
+
# it 'returns the first word found along a path' do
|
116
|
+
# @trie.add 'anderson'
|
117
|
+
# @trie.add 'andreas'
|
118
|
+
# @trie.add 'and'
|
128
119
|
|
129
|
-
|
130
|
-
|
120
|
+
# @trie.walk_to_terminal('anderson').should == 'and'
|
121
|
+
# end
|
131
122
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
123
|
+
# it 'returns the first word and value along a path' do
|
124
|
+
# @trie.add 'anderson'
|
125
|
+
# @trie.add 'andreas'
|
126
|
+
# @trie.add 'and', 15
|
136
127
|
|
137
|
-
|
138
|
-
|
139
|
-
end
|
128
|
+
# @trie.walk_to_terminal('anderson',true).should == ['and', 15]
|
129
|
+
# end
|
130
|
+
#end
|
140
131
|
|
141
132
|
describe :root do
|
142
133
|
it 'returns a TrieNode' do
|
@@ -148,33 +139,26 @@ describe Trie do
|
|
148
139
|
end
|
149
140
|
end
|
150
141
|
|
151
|
-
describe :save do
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
end
|
142
|
+
#describe :save do
|
143
|
+
# it 'saves the trie to disk such that another trie can be spawned which will read succesfully' do
|
144
|
+
# @trie.add('omgwtf',123)
|
145
|
+
# @trie.save
|
146
|
+
#
|
147
|
+
# trie2 = Trie.new(TRIE_PATH)
|
148
|
+
# trie2.get('omgwtf').should == 123
|
149
|
+
# end
|
150
|
+
#end
|
160
151
|
end
|
161
152
|
|
162
153
|
describe TrieNode do
|
163
154
|
before :each do
|
164
|
-
@trie = Trie.new
|
155
|
+
@trie = Trie.new;
|
165
156
|
@trie.add('rocket',1)
|
166
157
|
@trie.add('rock',2)
|
167
158
|
@trie.add('frederico',3)
|
168
159
|
@node = @trie.root
|
169
160
|
end
|
170
161
|
|
171
|
-
after :each do
|
172
|
-
@trie.close
|
173
|
-
File.delete('spec/test-trie/trie.br')
|
174
|
-
File.delete('spec/test-trie/trie.tl')
|
175
|
-
File.delete('spec/test-trie/trie.sbm')
|
176
|
-
end
|
177
|
-
|
178
162
|
describe :state do
|
179
163
|
it 'returns the most recent state character' do
|
180
164
|
@node.walk!('r')
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tyler-trie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tyler McMullen
|
@@ -9,11 +9,11 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-04-16 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|
16
|
-
description:
|
16
|
+
description: Ruby Trie based on libdatrie.
|
17
17
|
email: tyler@scribd.com
|
18
18
|
executables: []
|
19
19
|
|
@@ -25,22 +25,29 @@ files:
|
|
25
25
|
- README.textile
|
26
26
|
- VERSION.yml
|
27
27
|
- lib/trie.rb
|
28
|
-
- spec/test-trie
|
29
|
-
- spec/test-trie/README
|
30
28
|
- spec/trie_spec.rb
|
31
|
-
- ext/Makefile
|
32
29
|
- ext/trie
|
30
|
+
- ext/trie/darray.c
|
31
|
+
- ext/trie/darray.h
|
33
32
|
- ext/trie/extconf.rb
|
33
|
+
- ext/trie/fileutils.c
|
34
|
+
- ext/trie/fileutils.h
|
34
35
|
- ext/trie/Makefile
|
36
|
+
- ext/trie/tail.c
|
37
|
+
- ext/trie/tail.h
|
38
|
+
- ext/trie/trie-private.c
|
39
|
+
- ext/trie/trie-private.h
|
35
40
|
- ext/trie/trie.c
|
41
|
+
- ext/trie/trie.h
|
42
|
+
- ext/trie/triedefs.h
|
36
43
|
has_rdoc: false
|
37
44
|
homepage: http://github.com/tyler/trie
|
38
45
|
post_install_message:
|
39
46
|
rdoc_options: []
|
40
47
|
|
41
48
|
require_paths:
|
42
|
-
- lib
|
43
49
|
- ext
|
50
|
+
- lib
|
44
51
|
required_ruby_version: !ruby/object:Gem::Requirement
|
45
52
|
requirements:
|
46
53
|
- - ">="
|
data/spec/test-trie/README
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
This directory is used to store Trie files while running the specs. Don't delete it.
|