1 /*
2  * Hunt - A refined core library for D programming language.
3  *
4  * Copyright (C) 2018-2019 HuntLabs
5  *
6  * Website: https://www.huntlabs.net/
7  *
8  * Licensed under the Apache-2.0 License.
9  *
10  */
11 
12 module hunt.collection.Trie;
13 
14 import hunt.io.ByteBuffer;
15 import hunt.collection.Set;
16 
17 /* ------------------------------------------------------------ */
18 
19 /**
20  * A Trie string lookup data structure.
21  *
22  * @param (V) the Trie entry type
23  */
24 interface Trie(V) {
25     /* ------------------------------------------------------------ */
26 
27     /**
28      * Put an entry into the Trie
29      *
30      * @param s The key for the entry
31      * @param v The value of the entry
32      * @return True if the Trie had capacity to add the field.
33      */
34     bool put(string s, V v);
35 
36     /* ------------------------------------------------------------ */
37 
38     /**
39      * Put a value as both a key and a value.
40      *
41      * @param v The value and key
42      * @return True if the Trie had capacity to add the field.
43      */
44     bool put(V v);
45 
46     /* ------------------------------------------------------------ */
47     V remove(string s);
48 
49     /* ------------------------------------------------------------ */
50 
51     /**
52      * Get an exact match from a string key
53      *
54      * @param s The key
55      * @return the value for the string key
56      */
57     
58     V get(string s);
59     /* ------------------------------------------------------------ */
60 
61     /**
62      * Get an exact match from a string key
63      *
64      * @param s      The key
65      * @param offset The offset within the string of the key
66      * @param len    the length of the key
67      * @return the value for the string / offset / length
68      */
69     V get(string s, int offset, int len);
70 
71     /* ------------------------------------------------------------ */
72 
73     /**
74      * Get an exact match from a segment of a ByteBuufer as key
75      *
76      * @param b The buffer
77      * @return The value or null if not found
78      */
79     V get(ByteBuffer b);
80 
81     /* ------------------------------------------------------------ */
82 
83     /**
84      * Get an exact match from a segment of a ByteBuufer as key
85      *
86      * @param b      The buffer
87      * @param offset The offset within the buffer of the key
88      * @param len    the length of the key
89      * @return The value or null if not found
90      */
91     V get(ByteBuffer b, int offset, int len);
92 
93     /* ------------------------------------------------------------ */
94 
95     /**
96      * Get the best match from key in a string.
97      *
98      * @param s The string
99      * @return The value or null if not found
100      */
101     V getBest(string s);
102 
103     /* ------------------------------------------------------------ */
104 
105     /**
106      * Get the best match from key in a string.
107      *
108      * @param s      The string
109      * @param offset The offset within the string of the key
110      * @param len    the length of the key
111      * @return The value or null if not found
112      */
113     V getBest(string s, int offset, int len);
114 
115     /* ------------------------------------------------------------ */
116 
117     /**
118      * Get the best match from key in a byte array.
119      * The key is assumed to by ISO_8859_1 characters.
120      *
121      * @param b      The buffer
122      * @param offset The offset within the array of the key
123      * @param len    the length of the key
124      * @return The value or null if not found
125      */
126     V getBest(byte[] b, int offset, int len);
127 
128     /* ------------------------------------------------------------ */
129 
130     /**
131      * Get the best match from key in a byte buffer.
132      * The key is assumed to by ISO_8859_1 characters.
133      *
134      * @param b      The buffer
135      * @param offset The offset within the buffer of the key
136      * @param len    the length of the key
137      * @return The value or null if not found
138      */
139     V getBest(ByteBuffer b, int offset, int len);
140 
141     /* ------------------------------------------------------------ */
142     Set!string keySet();
143 
144     /* ------------------------------------------------------------ */
145     bool isFull();
146 
147     /* ------------------------------------------------------------ */
148     bool isCaseInsensitive();
149 
150     /* ------------------------------------------------------------ */
151     void clear();
152 
153 }