PatriciaTrie.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.collections4.trie;

  18. import java.util.Map;

  19. import org.apache.commons.collections4.trie.analyzer.StringKeyAnalyzer;

  20. /**
  21.  * Implements a PATRICIA Trie (Practical Algorithm to Retrieve Information
  22.  * Coded in Alphanumeric).
  23.  * <p>
  24.  * A PATRICIA {@link org.apache.commons.collections4.Trie} is a compressed
  25.  * {@link org.apache.commons.collections4.Trie}. Instead of storing
  26.  * all data at the edges of the {@link org.apache.commons.collections4.Trie}
  27.  * (and having empty internal nodes), PATRICIA stores data in every node.
  28.  * This allows for very efficient traversal, insert, delete, predecessor,
  29.  * successor, prefix, range, and {@link #select(Object)}
  30.  * operations. All operations are performed at worst in O(K) time, where K
  31.  * is the number of bits in the largest item in the tree. In practice,
  32.  * operations actually take O(A(K)) time, where A(K) is the average number of
  33.  * bits of all items in the tree.
  34.  * </p>
  35.  * <p>
  36.  * Most importantly, PATRICIA requires very few comparisons to keys while
  37.  * doing any operation. While performing a lookup, each comparison (at most
  38.  * K of them, described above) will perform a single bit comparison against
  39.  * the given key, instead of comparing the entire key to another key.
  40.  * </p>
  41.  * <p>
  42.  * The {@link org.apache.commons.collections4.Trie} can return operations in
  43.  * lexicographical order using the 'prefixMap', 'submap', or 'iterator' methods.
  44.  * The {@link org.apache.commons.collections4.Trie} can also
  45.  * scan for items that are 'bitwise' (using an XOR metric) by the 'select' method.
  46.  * Bitwise closeness is determined by the {@link KeyAnalyzer} returning true or
  47.  * false for a bit being set or not in a given key.
  48.  * </p>
  49.  * <p>
  50.  * This PATRICIA {@link org.apache.commons.collections4.Trie} supports both variable
  51.  * length &amp; fixed length keys. Some methods, such as {@link org.apache.commons.collections4.Trie#prefixMap(Object)}
  52.  * are suited only to variable length keys.
  53.  * </p>
  54.  *
  55.  * @param <V> the type of the values in this map
  56.  * @see <a href="https://en.wikipedia.org/wiki/Radix_tree">Radix Tree</a>
  57.  * @see <a href="https://users.monash.edu/~lloyd/tildeAlgDS/Tree/PATRICIA/">PATRICIA</a>
  58.  * @see <a href="https://www.imperialviolet.org/binary/critbit.pdf">Crit-Bit Tree</a>
  59.  * @since 4.0
  60.  */
  61. public class PatriciaTrie<V> extends AbstractPatriciaTrie<String, V> {

  62.     private static final long serialVersionUID = 4446367780901817838L;

  63.     /**
  64.      * Constructs a new instance.
  65.      */
  66.     public PatriciaTrie() {
  67.         super(StringKeyAnalyzer.INSTANCE);
  68.     }

  69.     /**
  70.      * Constructs a new instance.
  71.      *
  72.      * @param map mappings to be stored in this map.
  73.      */
  74.     public PatriciaTrie(final Map<? extends String, ? extends V> map) {
  75.         super(StringKeyAnalyzer.INSTANCE, map);
  76.     }

  77. }