trie_policy.hpp

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2007 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 __gnu_pbds
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_CLASS_T_DEC                       \
00065   template<typename String, typename String::value_type Min_E_Val, typename String::value_type Max_E_Val, bool Reverse, typename Allocator>
00066 
00067 #define PB_DS_CLASS_C_DEC                       \
00068   string_trie_e_access_traits<String, Min_E_Val,Max_E_Val,Reverse,Allocator>
00069 
00070   // Element access traits for string types.
00071   template<typename String = std::string,
00072        typename String::value_type Min_E_Val = detail::__numeric_traits<typename String::value_type>::__min, 
00073        typename String::value_type Max_E_Val = detail::__numeric_traits<typename String::value_type>::__max, 
00074        bool Reverse = false,
00075        typename Allocator = std::allocator<char> >
00076   struct string_trie_e_access_traits
00077   {
00078   public:
00079     typedef typename Allocator::size_type size_type;
00080     typedef String key_type;
00081     typedef typename Allocator::template rebind<key_type>::other key_rebind;
00082     typedef typename key_rebind::const_reference const_key_reference;
00083 
00084     enum
00085       {
00086     reverse = Reverse
00087       };
00088 
00089     // Element const iterator type.
00090     typedef typename detail::__conditional_type<Reverse, typename String::const_reverse_iterator, typename String::const_iterator>::__type const_iterator;
00091 
00092     // Element type.
00093     typedef typename std::iterator_traits<const_iterator>::value_type e_type;
00094 
00095     enum
00096       {
00097     min_e_val = Min_E_Val,
00098     max_e_val = Max_E_Val,
00099     max_size = max_e_val - min_e_val + 1
00100       };
00101     PB_DS_STATIC_ASSERT(min_max_size, max_size >= 2);
00102 
00103     // Returns a const_iterator to the first element of
00104     // const_key_reference agumnet.
00105     inline static const_iterator
00106     begin(const_key_reference);
00107 
00108     // Returns a const_iterator to the after-last element of
00109     // const_key_reference argument.
00110     inline static const_iterator
00111     end(const_key_reference);
00112 
00113     // Maps an element to a position.
00114     inline static size_type
00115     e_pos(e_type e);
00116 
00117   private:
00118 
00119     inline static const_iterator
00120     begin_imp(const_key_reference, detail::false_type);
00121 
00122     inline static const_iterator
00123     begin_imp(const_key_reference, detail::true_type);
00124 
00125     inline static const_iterator
00126     end_imp(const_key_reference, detail::false_type);
00127 
00128     inline static const_iterator
00129     end_imp(const_key_reference, detail::true_type);
00130 
00131     static detail::integral_constant<int, Reverse> s_rev_ind;
00132   };
00133 
00134 #include <ext/pb_ds/detail/trie_policy/string_trie_e_access_traits_imp.hpp>
00135 
00136 #undef PB_DS_CLASS_T_DEC
00137 #undef PB_DS_CLASS_C_DEC
00138 
00139 #define PB_DS_CLASS_T_DEC \
00140   template<typename Const_Node_Iterator,typename Node_Iterator,class E_Access_Traits, typename Allocator>
00141 
00142 #define PB_DS_CLASS_C_DEC \
00143   trie_prefix_search_node_update<Const_Node_Iterator, Node_Iterator, E_Access_Traits,Allocator>
00144 
00145 #define PB_DS_BASE_C_DEC \
00146   detail::trie_policy_base<Const_Node_Iterator,Node_Iterator,E_Access_Traits, Allocator>
00147 
00148   // A node updator that allows tries to be searched for the range of
00149   // values that match a certain prefix.
00150   template<typename Const_Node_Iterator,
00151        typename Node_Iterator,
00152        typename E_Access_Traits,
00153        typename Allocator>
00154   class trie_prefix_search_node_update : private PB_DS_BASE_C_DEC
00155   {
00156   private:
00157     typedef PB_DS_BASE_C_DEC base_type;
00158 
00159   public:
00160     typedef typename base_type::key_type key_type;
00161     typedef typename base_type::const_key_reference const_key_reference;
00162 
00163     // Element access traits.
00164     typedef E_Access_Traits e_access_traits;
00165 
00166     // Const element iterator.
00167     typedef typename e_access_traits::const_iterator const_e_iterator;
00168 
00169     // Allocator type.
00170     typedef Allocator allocator;
00171 
00172     // Size type.
00173     typedef typename allocator::size_type size_type;
00174     typedef detail::null_node_metadata metadata_type;
00175     typedef Const_Node_Iterator const_node_iterator;
00176     typedef Node_Iterator node_iterator;
00177     typedef typename const_node_iterator::value_type const_iterator;
00178     typedef typename node_iterator::value_type iterator;
00179 
00180     // Finds the const iterator range corresponding to all values
00181     // whose prefixes match r_key.
00182     std::pair<const_iterator, const_iterator>
00183     prefix_range(const_key_reference) const;
00184 
00185     // Finds the iterator range corresponding to all values whose
00186     // prefixes match r_key.
00187     std::pair<iterator, iterator>
00188     prefix_range(const_key_reference);
00189 
00190     // Finds the const iterator range corresponding to all values
00191     // whose prefixes match [b, e).
00192     std::pair<const_iterator, const_iterator>
00193     prefix_range(const_e_iterator, const_e_iterator) const;
00194 
00195     // Finds the iterator range corresponding to all values whose
00196     // prefixes match [b, e).
00197     std::pair<iterator, iterator>
00198     prefix_range(const_e_iterator, const_e_iterator);
00199 
00200   protected:
00201     // Called to update a node's metadata.
00202     inline void
00203     operator()(node_iterator node_it, const_node_iterator end_nd_it) const;
00204 
00205   private:
00206     // Returns the const iterator associated with the just-after last element.
00207     virtual const_iterator
00208     end() const = 0;
00209 
00210     // Returns the iterator associated with the just-after last element.
00211     virtual iterator
00212     end() = 0;
00213 
00214     // Returns the const_node_iterator associated with the trie's root node.
00215     virtual const_node_iterator
00216     node_begin() const = 0;
00217 
00218     // Returns the node_iterator associated with the trie's root node.
00219     virtual node_iterator
00220     node_begin() = 0;
00221 
00222     // Returns the const_node_iterator associated with a just-after leaf node.
00223     virtual const_node_iterator
00224     node_end() const = 0;
00225 
00226     // Returns the node_iterator associated with a just-after leaf node.
00227     virtual node_iterator
00228     node_end() = 0;
00229 
00230     // Access to the cmp_fn object.
00231     virtual const e_access_traits& 
00232     get_e_access_traits() const = 0;
00233 
00234     node_iterator
00235     next_child(node_iterator, const_e_iterator, const_e_iterator, 
00236            node_iterator, const e_access_traits&);
00237   };
00238 
00239 #include <ext/pb_ds/detail/trie_policy/prefix_search_node_update_imp.hpp>
00240 
00241 #undef PB_DS_CLASS_C_DEC
00242 
00243 #define PB_DS_CLASS_C_DEC \
00244   trie_order_statistics_node_update<Const_Node_Iterator, Node_Iterator,E_Access_Traits, Allocator>
00245 
00246   // Functor updating ranks of entrees.
00247   template<typename Const_Node_Iterator,
00248        typename Node_Iterator,
00249        typename E_Access_Traits,
00250        typename Allocator>
00251   class trie_order_statistics_node_update : private PB_DS_BASE_C_DEC
00252   {
00253   private:
00254     typedef PB_DS_BASE_C_DEC base_type;
00255 
00256   public:
00257     typedef E_Access_Traits e_access_traits;
00258     typedef typename e_access_traits::const_iterator const_e_iterator;
00259     typedef Allocator allocator;
00260     typedef typename allocator::size_type size_type;
00261     typedef typename base_type::key_type key_type;
00262     typedef typename base_type::const_key_reference const_key_reference;
00263 
00264     typedef size_type metadata_type;
00265     typedef Const_Node_Iterator const_node_iterator;
00266     typedef Node_Iterator node_iterator;
00267     typedef typename const_node_iterator::value_type const_iterator;
00268     typedef typename node_iterator::value_type iterator;
00269 
00270     // Finds an entry by __order. Returns a const_iterator to the
00271     // entry with the __order order, or a const_iterator to the
00272     // container object's end if order is at least the size of the
00273     // container object.
00274     inline const_iterator
00275     find_by_order(size_type) const;
00276 
00277     // Finds an entry by __order. Returns an iterator to the entry
00278     // with the __order order, or an iterator to the container
00279     // object's end if order is at least the size of the container
00280     // object.
00281     inline iterator
00282     find_by_order(size_type);
00283 
00284     // Returns the order of a key within a sequence. For exapmle, if
00285     // r_key is the smallest key, this method will return 0; if r_key
00286     // is a key between the smallest and next key, this method will
00287     // return 1; if r_key is a key larger than the largest key, this
00288     // method will return the size of r_c.
00289     inline size_type
00290     order_of_key(const_key_reference) const;
00291 
00292     // Returns the order of a prefix within a sequence. For exapmle,
00293     // if [b, e] is the smallest prefix, this method will return 0; if
00294     // r_key is a key between the smallest and next key, this method
00295     // will return 1; if r_key is a key larger than the largest key,
00296     // this method will return the size of r_c.
00297     inline size_type
00298     order_of_prefix(const_e_iterator, const_e_iterator) const;
00299 
00300   private:
00301     typedef typename base_type::const_reference const_reference;
00302     typedef typename base_type::const_pointer const_pointer;
00303 
00304     typedef typename Allocator::template rebind<metadata_type>::other metadata_rebind;
00305     typedef typename metadata_rebind::const_reference const_metadata_reference;
00306     typedef typename metadata_rebind::reference metadata_reference;
00307 
00308     // Returns true if the container is empty.
00309     virtual bool
00310     empty() const = 0;
00311 
00312     // Returns the iterator associated with the trie's first element.
00313     virtual iterator
00314     begin() = 0;
00315 
00316     // Returns the iterator associated with the trie's
00317     // just-after-last element.
00318     virtual iterator
00319     end() = 0;
00320 
00321     // Returns the const_node_iterator associated with the trie's root node.
00322     virtual const_node_iterator
00323     node_begin() const = 0;
00324 
00325     // Returns the node_iterator associated with the trie's root node.
00326     virtual node_iterator
00327     node_begin() = 0;
00328 
00329     // Returns the const_node_iterator associated with a just-after
00330     // leaf node.
00331     virtual const_node_iterator
00332     node_end() const = 0;
00333 
00334     // Returns the node_iterator associated with a just-after leaf node.
00335     virtual node_iterator
00336     node_end() = 0;
00337 
00338     // Access to the cmp_fn object.
00339     virtual e_access_traits& 
00340     get_e_access_traits() = 0;
00341 
00342   protected:
00343     // Updates the rank of a node through a node_iterator node_it;
00344     // end_nd_it is the end node iterator.
00345     inline void
00346     operator()(node_iterator, const_node_iterator) const;
00347 
00348     // Destructor.
00349     virtual
00350     ~trie_order_statistics_node_update();
00351   };
00352 
00353 #include <ext/pb_ds/detail/trie_policy/order_statistics_imp.hpp>
00354 
00355 #undef PB_DS_CLASS_T_DEC
00356 #undef PB_DS_CLASS_C_DEC
00357 #undef PB_DS_BASE_C_DEC
00358 
00359 } // namespace __gnu_pbds
00360 
00361 #endif

Generated on Wed Dec 31 12:49:05 2008 for libstdc++ by  doxygen 1.5.6