2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.util;
21 import java.io.IOException;
22 import java.nio.ByteBuffer;
23 import java.nio.charset.StandardCharsets;
24 import java.util.ArrayList;
25 import java.util.HashSet;
26 import java.util.List;
29 /* ------------------------------------------------------------ */
30 /** A Trie String lookup data structure using a tree
31 * <p>This implementation is always case insensitive and is optimal for
32 * a variable number of fixed strings with few special characters.
36 public class TreeTrie<V> extends AbstractTrie<V>
38 private static final int[] __lookup =
39 { // 0 1 2 3 4 5 6 7 8 9 A B C D E F
40 /*0*/-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
41 /*1*/-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 30,
42 /*2*/31, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 26, -1, 27, -1, -1,
43 /*3*/-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 28, 29, -1, -1, -1, -1,
44 /*4*/-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
45 /*5*/15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
46 /*6*/-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
47 /*7*/15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
49 private static final int INDEX = 32;
50 private final TreeTrie<V>[] _nextIndex;
51 private final List<TreeTrie<V>> _nextOther=new ArrayList<>();
52 private final char _c;
59 _nextIndex = new TreeTrie[INDEX];
63 private TreeTrie(char c)
66 _nextIndex = new TreeTrie[INDEX];
71 public boolean put(String s, V v)
74 int limit = s.length();
75 for(int k=0; k < limit; k++)
79 int index=c>=0&&c<0x7f?__lookup[c]:-1;
82 if (t._nextIndex[index] == null)
83 t._nextIndex[index] = new TreeTrie<V>(c);
84 t = t._nextIndex[index];
89 for (int i=t._nextOther.size();i-->0;)
91 n=t._nextOther.get(i);
104 t._key=v==null?null:s;
110 public V get(String s,int offset, int len)
112 TreeTrie<V> t = this;
113 for(int i=0; i < len; i++)
115 char c=s.charAt(offset+i);
116 int index=c>=0&&c<0x7f?__lookup[c]:-1;
119 if (t._nextIndex[index] == null)
121 t = t._nextIndex[index];
126 for (int j=t._nextOther.size();j-->0;)
128 n=t._nextOther.get(j);
142 public V get(ByteBuffer b,int offset,int len)
144 TreeTrie<V> t = this;
145 for(int i=0; i < len; i++)
147 byte c=b.get(offset+i);
148 int index=c>=0&&c<0x7f?__lookup[c]:-1;
151 if (t._nextIndex[index] == null)
153 t = t._nextIndex[index];
158 for (int j=t._nextOther.size();j-->0;)
160 n=t._nextOther.get(j);
174 public V getBest(byte[] b,int offset,int len)
176 TreeTrie<V> t = this;
177 for(int i=0; i < len; i++)
180 int index=c>=0&&c<0x7f?__lookup[c]:-1;
183 if (t._nextIndex[index] == null)
185 t = t._nextIndex[index];
190 for (int j=t._nextOther.size();j-->0;)
192 n=t._nextOther.get(j);
202 // Is the next Trie is a match
205 // Recurse so we can remember this possibility
206 V best=t.getBest(b,offset+i+1,len-i-1);
216 public V getBest(String s, int offset, int len)
219 byte[] b=s.substring(offset,offset+len).getBytes(StandardCharsets.ISO_8859_1);
220 return getBest(b,0,b.length);
224 public V getBest(ByteBuffer b,int offset,int len)
227 return getBest(b.array(),b.arrayOffset()+b.position()+offset,len);
228 return getBestByteBuffer(b,offset,len);
231 private V getBestByteBuffer(ByteBuffer b,int offset,int len)
233 TreeTrie<V> t = this;
234 int pos=b.position()+offset;
235 for(int i=0; i < len; i++)
238 int index=c>=0&&c<0x7f?__lookup[c]:-1;
241 if (t._nextIndex[index] == null)
243 t = t._nextIndex[index];
248 for (int j=t._nextOther.size();j-->0;)
250 n=t._nextOther.get(j);
260 // Is the next Trie is a match
263 // Recurse so we can remember this possibility
264 V best=t.getBest(b,offset+i+1,len-i-1);
275 public String toString()
277 StringBuilder buf = new StringBuilder();
283 buf.setCharAt(0,'{');
285 return buf.toString();
288 private static <V> void toString(Appendable out, TreeTrie<V> t)
299 out.append(t._value.toString());
301 catch (IOException e)
303 throw new RuntimeException(e);
307 for(int i=0; i < INDEX; i++)
309 if (t._nextIndex[i] != null)
310 toString(out,t._nextIndex[i]);
312 for (int i=t._nextOther.size();i-->0;)
313 toString(out,t._nextOther.get(i));
318 public Set<String> keySet()
320 Set<String> keys = new HashSet<>();
325 private static <V> void keySet(Set<String> set, TreeTrie<V> t)
332 for(int i=0; i < INDEX; i++)
334 if (t._nextIndex[i] != null)
335 keySet(set,t._nextIndex[i]);
337 for (int i=t._nextOther.size();i-->0;)
338 keySet(set,t._nextOther.get(i));
343 public boolean isFull()