# trie_policy.hpp

Go to the documentation of this file.

    00001 // -*- C++ -*-
    00002
    00003 // Copyright (C) 2005, 2006 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the terms
    00007 // of the GNU General Public License as published by the Free Software
    00008 // Foundation; either version 2, or (at your option) any later
    00009 // version.
    00010
    00011 // This library is distributed in the hope that it will be useful, but
    00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00014 // General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License
    00017 // along with this library; see the file COPYING.  If not, write to
    00018 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
    00019 // MA 02111-1307, USA.
    00020
    00021 // As a special exception, you may use this file as part of a free
    00022 // software library without restriction.  Specifically, if other files
    00023 // instantiate templates or use macros or inline functions from this
    00024 // file, or you compile this file and link it with other files to
    00025 // produce an executable, this file does not by itself cause the
    00026 // resulting executable to be covered by the GNU General Public
    00027 // License.  This exception does not however invalidate any other
    00028 // reasons why the executable file might be covered by the GNU General
    00029 // Public License.
    00030
    00031 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
    00032
    00033 // Permission to use, copy, modify, sell, and distribute this software
    00034 // is hereby granted without fee, provided that the above copyright
    00035 // notice appears in all copies, and that both that copyright notice
    00036 // and this permission notice appear in supporting documentation. None
    00037 // of the above authors, nor IBM Haifa Research Laboratories, make any
    00038 // representation about the suitability of this software for any
    00039 // purpose. It is provided "as is" without express or implied
    00040 // warranty.
    00041
    00042 /**
    00043  * @file trie_policy.hpp
    00044  * Contains trie-related policies.
    00045  */
    00046
    00047 #ifndef PB_DS_TRIE_POLICY_HPP
    00048 #define PB_DS_TRIE_POLICY_HPP
    00049
    00050 #include <string>
    00051 #include <ext/pb_ds/detail/type_utils.hpp>
    00052 #include <ext/pb_ds/detail/trie_policy/trie_policy_base.hpp>
    00053
    00054 namespace pb_ds
    00055 {
    00056   // A null node updator, indicating that no node updates are required.
    00057   template<typename Const_Node_Iterator,
    00058        typename Node_Iterator,
    00059        typename E_Access_Traits,
    00060        typename Allocator>
    00061   struct null_trie_node_update
    00062   { };
    00063
    00064 #define PB_DS_STATIC_ASSERT(UNIQUE, E)                  \
    00065   typedef detail::static_assert_dumclass<sizeof(detail::static_assert<bool(E)>)> UNIQUE##_static_assert_type
    00066
    00067 #define PB_DS_CLASS_T_DEC                       \
    00068   template<typename String, typename String::value_type Min_E_Val, typename String::value_type Max_E_Val, bool Reverse, typename Allocator>
    00069
    00070 #define PB_DS_CLASS_C_DEC                       \
    00071   string_trie_e_access_traits<String, Min_E_Val,Max_E_Val,Reverse,Allocator>
    00072
    00073   // Element access traits for string types.
    00074   template<typename String = std::string,
    00075        typename String::value_type Min_E_Val = detail::__numeric_traits<typename String::value_type>::__min,
    00076        typename String::value_type Max_E_Val = detail::__numeric_traits<typename String::value_type>::__max,
    00077        bool Reverse = false,
    00078        typename Allocator = std::allocator<char> >
    00079   struct string_trie_e_access_traits
    00080   {
    00081   public:
    00082     typedef typename Allocator::size_type size_type;
    00083     typedef String key_type;
    00084     typedef typename Allocator::template rebind<key_type>::other key_rebind;
    00085     typedef typename key_rebind::const_reference const_key_reference;
    00086
    00087     enum
    00088       {
    00089     reverse = Reverse
    00090       };
    00091
    00092     // Element const iterator type.
    00093     typedef typename detail::__conditional_type<Reverse, typename String::const_reverse_iterator, typename String::const_iterator>::__type const_iterator;
    00094
    00095     // Element type.
    00096     typedef typename std::iterator_traits<const_iterator>::value_type e_type;
    00097
    00098     enum
    00099       {
    00100     min_e_val = Min_E_Val,
    00101     max_e_val = Max_E_Val,
    00102     max_size = max_e_val - min_e_val + 1
    00103       };
    00104     PB_DS_STATIC_ASSERT(min_max_size, max_size >= 2);
    00105
    00106     // Returns a const_iterator to the first element of
    00107     // const_key_reference agumnet.
    00108     inline static const_iterator
    00109     begin(const_key_reference);
    00110
    00111     // Returns a const_iterator to the after-last element of
    00112     // const_key_reference argument.
    00113     inline static const_iterator
    00114     end(const_key_reference);
    00115
    00116     // Maps an element to a position.
    00117     inline static size_type
    00118     e_pos(e_type e);
    00119
    00120   private:
    00121
    00122     inline static const_iterator
    00123     begin_imp(const_key_reference, detail::false_type);
    00124
    00125     inline static const_iterator
    00126     begin_imp(const_key_reference, detail::true_type);
    00127
    00128     inline static const_iterator
    00129     end_imp(const_key_reference, detail::false_type);
    00130
    00131     inline static const_iterator
    00132     end_imp(const_key_reference, detail::true_type);
    00133
    00134     static detail::integral_constant<int, Reverse> s_rev_ind;
    00135   };
    00136
    00137 #include <ext/pb_ds/detail/trie_policy/string_trie_e_access_traits_imp.hpp>
    00138
    00139 #undef PB_DS_CLASS_T_DEC
    00140 #undef PB_DS_CLASS_C_DEC
    00141
    00142 #define PB_DS_CLASS_T_DEC \
    00143   template<typename Const_Node_Iterator,typename Node_Iterator,class E_Access_Traits, typename Allocator>
    00144
    00145 #define PB_DS_CLASS_C_DEC \
    00146   trie_prefix_search_node_update<Const_Node_Iterator, Node_Iterator, E_Access_Traits,Allocator>
    00147
    00148 #define PB_DS_BASE_C_DEC \
    00149   detail::trie_policy_base<Const_Node_Iterator,Node_Iterator,E_Access_Traits, Allocator>
    00150
    00151   // A node updator that allows tries to be searched for the range of
    00152   // values that match a certain prefix.
    00153   template<typename Const_Node_Iterator,
    00154        typename Node_Iterator,
    00155        typename E_Access_Traits,
    00156        typename Allocator>
    00157   class trie_prefix_search_node_update : private PB_DS_BASE_C_DEC
    00158   {
    00159   private:
    00160     typedef PB_DS_BASE_C_DEC base_type;
    00161
    00162   public:
    00163     typedef typename base_type::key_type key_type;
    00164     typedef typename base_type::const_key_reference const_key_reference;
    00165
    00166     // Element access traits.
    00167     typedef E_Access_Traits e_access_traits;
    00168
    00169     // Const element iterator.
    00170     typedef typename e_access_traits::const_iterator const_e_iterator;
    00171
    00172     // Allocator type.
    00173     typedef Allocator allocator;
    00174
    00175     // Size type.
    00176     typedef typename allocator::size_type size_type;
    00177     typedef detail::null_node_metadata metadata_type;
    00178     typedef Const_Node_Iterator const_node_iterator;
    00179     typedef Node_Iterator node_iterator;
    00180     typedef typename const_node_iterator::value_type const_iterator;
    00181     typedef typename node_iterator::value_type iterator;
    00182
    00183     // Finds the const iterator range corresponding to all values
    00184     // whose prefixes match r_key.
    00185     std::pair<const_iterator, const_iterator>
    00186     prefix_range(const_key_reference) const;
    00187
    00188     // Finds the iterator range corresponding to all values whose
    00189     // prefixes match r_key.
    00190     std::pair<iterator, iterator>
    00191     prefix_range(const_key_reference);
    00192
    00193     // Finds the const iterator range corresponding to all values
    00194     // whose prefixes match [b, e).
    00195     std::pair<const_iterator, const_iterator>
    00196     prefix_range(const_e_iterator, const_e_iterator) const;
    00197
    00198     // Finds the iterator range corresponding to all values whose
    00199     // prefixes match [b, e).
    00200     std::pair<iterator, iterator>
    00201     prefix_range(const_e_iterator, const_e_iterator);
    00202
    00203   protected:
    00204     // Called to update a node's metadata.
    00205     inline void
    00206     operator()(node_iterator node_it, const_node_iterator end_nd_it) const;
    00207
    00208   private:
    00209     // Returns the const iterator associated with the just-after last element.
    00210     virtual const_iterator
    00211     end() const = 0;
    00212
    00213     // Returns the iterator associated with the just-after last element.
    00214     virtual iterator
    00215     end() = 0;
    00216
    00217     // Returns the const_node_iterator associated with the trie's root node.
    00218     virtual const_node_iterator
    00219     node_begin() const = 0;
    00220
    00221     // Returns the node_iterator associated with the trie's root node.
    00222     virtual node_iterator
    00223     node_begin() = 0;
    00224
    00225     // Returns the const_node_iterator associated with a just-after leaf node.
    00226     virtual const_node_iterator
    00227     node_end() const = 0;
    00228
    00229     // Returns the node_iterator associated with a just-after leaf node.
    00230     virtual node_iterator
    00231     node_end() = 0;
    00232
    00233     // Access to the cmp_fn object.
    00234     virtual const e_access_traits&
    00235     get_e_access_traits() const = 0;
    00236
    00237     node_iterator
    00238     next_child(node_iterator, const_e_iterator, const_e_iterator,
    00239            node_iterator, const e_access_traits&);
    00240   };
    00241
    00242 #include <ext/pb_ds/detail/trie_policy/prefix_search_node_update_imp.hpp>
    00243
    00244 #undef PB_DS_CLASS_C_DEC
    00245
    00246 #define PB_DS_CLASS_C_DEC \
    00247   trie_order_statistics_node_update<Const_Node_Iterator, Node_Iterator,E_Access_Traits, Allocator>
    00248
    00249   // Functor updating ranks of entrees.
    00250   template<typename Const_Node_Iterator,
    00251        typename Node_Iterator,
    00252        typename E_Access_Traits,
    00253        typename Allocator>
    00254   class trie_order_statistics_node_update : private PB_DS_BASE_C_DEC
    00255   {
    00256   private:
    00257     typedef PB_DS_BASE_C_DEC base_type;
    00258
    00259   public:
    00260     typedef E_Access_Traits e_access_traits;
    00261     typedef typename e_access_traits::const_iterator const_e_iterator;
    00262     typedef Allocator allocator;
    00263     typedef typename allocator::size_type size_type;
    00264     typedef typename base_type::key_type key_type;
    00265     typedef typename base_type::const_key_reference const_key_reference;
    00266
    00267     typedef size_type metadata_type;
    00268     typedef Const_Node_Iterator const_node_iterator;
    00269     typedef Node_Iterator node_iterator;
    00270     typedef typename const_node_iterator::value_type const_iterator;
    00271     typedef typename node_iterator::value_type iterator;
    00272
    00273     // Finds an entry by __order. Returns a const_iterator to the
    00274     // entry with the __order order, or a const_iterator to the
    00275     // container object's end if order is at least the size of the
    00276     // container object.
    00277     inline const_iterator
    00278     find_by_order(size_type) const;
    00279
    00280     // Finds an entry by __order. Returns an iterator to the entry
    00281     // with the __order order, or an iterator to the container
    00282     // object's end if order is at least the size of the container
    00283     // object.
    00284     inline iterator
    00285     find_by_order(size_type);
    00286
    00287     // Returns the order of a key within a sequence. For exapmle, if
    00288     // r_key is the smallest key, this method will return 0; if r_key
    00289     // is a key between the smallest and next key, this method will
    00290     // return 1; if r_key is a key larger than the largest key, this
    00291     // method will return the size of r_c.
    00292     inline size_type
    00293     order_of_key(const_key_reference) const;
    00294
    00295     // Returns the order of a prefix within a sequence. For exapmle,
    00296     // if [b, e] is the smallest prefix, this method will return 0; if
    00297     // r_key is a key between the smallest and next key, this method
    00298     // will return 1; if r_key is a key larger than the largest key,
    00299     // this method will return the size of r_c.
    00300     inline size_type
    00301     order_of_prefix(const_e_iterator, const_e_iterator) const;
    00302
    00303   private:
    00304     typedef typename base_type::const_reference const_reference;
    00305     typedef typename base_type::const_pointer const_pointer;
    00306
    00307     typedef typename Allocator::template rebind<metadata_type>::other metadata_rebind;
    00308     typedef typename metadata_rebind::const_reference const_metadata_reference;
    00309     typedef typename metadata_rebind::reference metadata_reference;
    00310
    00311     // Returns true if the container is empty.
    00312     virtual bool
    00313     empty() const = 0;
    00314
    00315     // Returns the iterator associated with the trie's first element.
    00316     virtual iterator
    00317     begin() = 0;
    00318
    00319     // Returns the iterator associated with the trie's
    00320     // just-after-last element.
    00321     virtual iterator
    00322     end() = 0;
    00323
    00324     // Returns the const_node_iterator associated with the trie's root node.
    00325     virtual const_node_iterator
    00326     node_begin() const = 0;
    00327
    00328     // Returns the node_iterator associated with the trie's root node.
    00329     virtual node_iterator
    00330     node_begin() = 0;
    00331
    00332     // Returns the const_node_iterator associated with a just-after
    00333     // leaf node.
    00334     virtual const_node_iterator
    00335     node_end() const = 0;
    00336
    00337     // Returns the node_iterator associated with a just-after leaf node.
    00338     virtual node_iterator
    00339     node_end() = 0;
    00340
    00341     // Access to the cmp_fn object.
    00342     virtual e_access_traits&
    00343     get_e_access_traits() = 0;
    00344
    00345   protected:
    00346     // Updates the rank of a node through a node_iterator node_it;
    00347     // end_nd_it is the end node iterator.
    00348     inline void
    00349     operator()(node_iterator, const_node_iterator) const;
    00350
    00351     // Destructor.
    00352     virtual
    00353     ~trie_order_statistics_node_update();
    00354   };
    00355
    00356 #include <ext/pb_ds/detail/trie_policy/order_statistics_imp.hpp>
    00357
    00358 #undef PB_DS_CLASS_T_DEC
    00359 #undef PB_DS_CLASS_C_DEC
    00360 #undef PB_DS_BASE_C_DEC
    00361 #undef PB_DS_STATIC_ASSERT
    00362
    00363 } // namespace pb_ds
    00364
    00365 #endif