libstdc++
splay_tree_.hpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // Copyright (C) 2005, 2006, 2009, 2010, 2011 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
00026 
00027 // Permission to use, copy, modify, sell, and distribute this software
00028 // is hereby granted without fee, provided that the above copyright
00029 // notice appears in all copies, and that both that copyright notice
00030 // and this permission notice appear in supporting documentation. None
00031 // of the above authors, nor IBM Haifa Research Laboratories, make any
00032 // representation about the suitability of this software for any
00033 // purpose. It is provided "as is" without express or implied
00034 // warranty.
00035 
00036 /**
00037  * @file splay_tree_/splay_tree_.hpp
00038  * Contains an implementation class for splay trees.
00039  */
00040 /*
00041  * This implementation uses an idea from the SGI STL (using a @a header node
00042  *    which is needed for efficient iteration). Following is the SGI STL
00043  *    copyright.
00044  *
00045  * Copyright (c) 1996,1997
00046  * Silicon Graphics Computer Systems, Inc.
00047  *
00048  * Permission to use, copy, modify, distribute and sell this software
00049  * and its documentation for any purpose is hereby granted without fee,
00050  * provided that the above copyright notice appear in all copies and
00051  * that both that copyright notice and this permission notice appear
00052  * in supporting documentation.    Silicon Graphics makes no
00053  * representations about the suitability of this software for any
00054  * purpose.    It is provided "as is" without express or implied warranty.
00055  *
00056  *
00057  * Copyright (c) 1994
00058  * Hewlett-Packard Company
00059  *
00060  * Permission to use, copy, modify, distribute and sell this software
00061  * and its documentation for any purpose is hereby granted without fee,
00062  * provided that the above copyright notice appear in all copies and
00063  * that both that copyright notice and this permission notice appear
00064  * in supporting documentation.    Hewlett-Packard Company makes no
00065  * representations about the suitability of this software for any
00066  * purpose.    It is provided "as is" without express or implied warranty.
00067  *
00068  *
00069  */
00070 
00071 #include <utility>
00072 #include <vector>
00073 #include <assert.h>
00074 #include <debug/debug.h>
00075 
00076 namespace __gnu_pbds
00077 {
00078   namespace detail
00079   {
00080 #ifdef PB_DS_DATA_TRUE_INDICATOR
00081 # define PB_DS_S_TREE_NAME splay_tree_map
00082 # define PB_DS_S_TREE_BASE_NAME bin_search_tree_map
00083 #endif
00084 
00085 #ifdef PB_DS_DATA_FALSE_INDICATOR
00086 # define PB_DS_S_TREE_NAME splay_tree_set
00087 # define PB_DS_S_TREE_BASE_NAME bin_search_tree_set
00088 #endif
00089 
00090 #define PB_DS_CLASS_T_DEC \
00091     template<typename Key, typename Mapped, typename Cmp_Fn, \
00092          typename Node_And_It_Traits, typename _Alloc>
00093 
00094 #define PB_DS_CLASS_C_DEC \
00095     PB_DS_S_TREE_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, _Alloc>
00096 
00097 #define PB_DS_S_TREE_BASE \
00098     PB_DS_S_TREE_BASE_NAME<Key, Mapped, Cmp_Fn, Node_And_It_Traits, _Alloc>
00099 
00100 
00101     /**
00102      *  @brief Splay tree.
00103      *  @ingroup branch-detail
00104      */
00105     template<typename Key, typename Mapped, typename Cmp_Fn,
00106          typename Node_And_It_Traits, typename _Alloc>
00107     class PB_DS_S_TREE_NAME : public PB_DS_S_TREE_BASE
00108     {
00109     private:
00110       typedef PB_DS_S_TREE_BASE                  base_type;
00111 #ifdef _GLIBCXX_DEBUG
00112       typedef base_type debug_base;
00113 #endif
00114       typedef typename base_type::node_pointer       node_pointer;
00115 
00116     public:
00117       typedef splay_tree_tag                 container_category;
00118       typedef _Alloc                     allocator_type;
00119       typedef typename _Alloc::size_type         size_type;
00120       typedef typename _Alloc::difference_type       difference_type;
00121       typedef Cmp_Fn                     cmp_fn;
00122       typedef typename base_type::key_type       key_type;
00123       typedef typename base_type::key_pointer        key_pointer;
00124       typedef typename base_type::key_const_pointer      key_const_pointer;
00125       typedef typename base_type::key_reference      key_reference;
00126       typedef typename base_type::key_const_reference    key_const_reference;
00127       typedef typename base_type::mapped_type        mapped_type;
00128       typedef typename base_type::mapped_pointer     mapped_pointer;
00129       typedef typename base_type::mapped_const_pointer   mapped_const_pointer;
00130       typedef typename base_type::mapped_reference   mapped_reference;
00131       typedef typename base_type::mapped_const_reference mapped_const_reference;
00132       typedef typename base_type::value_type         value_type;
00133       typedef typename base_type::pointer        pointer;
00134       typedef typename base_type::const_pointer      const_pointer;
00135       typedef typename base_type::reference          reference;
00136       typedef typename base_type::const_reference    const_reference;
00137       typedef typename base_type::point_iterator     point_iterator;
00138       typedef typename base_type::const_iterator     point_const_iterator;
00139       typedef typename base_type::iterator       iterator;
00140       typedef typename base_type::const_iterator     const_iterator;
00141       typedef typename base_type::reverse_iterator   reverse_iterator;
00142       typedef typename base_type::const_reverse_iterator const_reverse_iterator;
00143       typedef typename base_type::node_update        node_update;
00144 
00145       PB_DS_S_TREE_NAME();
00146 
00147       PB_DS_S_TREE_NAME(const Cmp_Fn&);
00148 
00149       PB_DS_S_TREE_NAME(const Cmp_Fn&, const node_update&);
00150 
00151       PB_DS_S_TREE_NAME(const PB_DS_CLASS_C_DEC&);
00152 
00153       void
00154       swap(PB_DS_CLASS_C_DEC&);
00155 
00156       template<typename It>
00157       void
00158       copy_from_range(It, It);
00159 
00160       void
00161       initialize();
00162 
00163       inline std::pair<point_iterator, bool>
00164       insert(const_reference r_value);
00165 
00166       inline mapped_reference
00167       operator[](key_const_reference r_key)
00168       {
00169 #ifdef PB_DS_DATA_TRUE_INDICATOR
00170     _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);)
00171     std::pair<point_iterator, bool> ins_pair =
00172       insert_leaf_imp(value_type(r_key, mapped_type()));
00173 
00174     ins_pair.first.m_p_nd->m_special = false;
00175     _GLIBCXX_DEBUG_ONLY(base_type::assert_valid(__FILE__, __LINE__));
00176     splay(ins_pair.first.m_p_nd);
00177     _GLIBCXX_DEBUG_ONLY(assert_valid(__FILE__, __LINE__);)
00178     return ins_pair.first.m_p_nd->m_value.second;
00179 #else
00180     insert(r_key);
00181     return base_type::s_null_type;
00182 #endif
00183       }
00184 
00185       inline point_iterator
00186       find(key_const_reference);
00187 
00188       inline point_const_iterator
00189       find(key_const_reference) const;
00190 
00191       inline bool
00192       erase(key_const_reference);
00193 
00194       inline iterator
00195       erase(iterator it);
00196 
00197       inline reverse_iterator
00198       erase(reverse_iterator);
00199 
00200       template<typename Pred>
00201       inline size_type
00202       erase_if(Pred);
00203 
00204       void
00205       join(PB_DS_CLASS_C_DEC&);
00206 
00207       void
00208       split(key_const_reference, PB_DS_CLASS_C_DEC&);
00209 
00210     private:
00211       inline std::pair<point_iterator, bool>
00212       insert_leaf_imp(const_reference);
00213 
00214       inline node_pointer
00215       find_imp(key_const_reference);
00216 
00217       inline const node_pointer
00218       find_imp(key_const_reference) const;
00219 
00220 #ifdef _GLIBCXX_DEBUG
00221       void
00222       assert_valid(const char* file, int line) const;
00223 
00224       void
00225       assert_special_imp(const node_pointer, const char* file, int line) const;
00226 #endif
00227 
00228       void
00229       splay(node_pointer);
00230 
00231       inline void
00232       splay_zig_zag_left(node_pointer, node_pointer, node_pointer);
00233 
00234       inline void
00235       splay_zig_zag_right(node_pointer, node_pointer, node_pointer);
00236 
00237       inline void
00238       splay_zig_zig_left(node_pointer, node_pointer, node_pointer);
00239 
00240       inline void
00241       splay_zig_zig_right(node_pointer, node_pointer, node_pointer);
00242 
00243       inline void
00244       splay_zz_start(node_pointer, node_pointer, node_pointer);
00245 
00246       inline void
00247       splay_zz_end(node_pointer, node_pointer, node_pointer);
00248 
00249       inline node_pointer
00250       leftmost(node_pointer);
00251 
00252       void
00253       erase_node(node_pointer);
00254     };
00255 
00256 #define PB_DS_ASSERT_BASE_NODE_CONSISTENT(_Node)            \
00257   _GLIBCXX_DEBUG_ONLY(base_type::assert_node_consistent(_Node,      \
00258                             __FILE__, __LINE__);)
00259 
00260 #include <ext/pb_ds/detail/splay_tree_/constructors_destructor_fn_imps.hpp>
00261 #include <ext/pb_ds/detail/splay_tree_/insert_fn_imps.hpp>
00262 #include <ext/pb_ds/detail/splay_tree_/splay_fn_imps.hpp>
00263 #include <ext/pb_ds/detail/splay_tree_/erase_fn_imps.hpp>
00264 #include <ext/pb_ds/detail/splay_tree_/find_fn_imps.hpp>
00265 #include <ext/pb_ds/detail/splay_tree_/debug_fn_imps.hpp>
00266 #include <ext/pb_ds/detail/splay_tree_/split_join_fn_imps.hpp>
00267 
00268 #undef PB_DS_ASSERT_BASE_NODE_CONSISTENT
00269 #undef PB_DS_CLASS_T_DEC
00270 #undef PB_DS_CLASS_C_DEC
00271 #undef PB_DS_S_TREE_NAME
00272 #undef PB_DS_S_TREE_BASE_NAME
00273 #undef PB_DS_S_TREE_BASE
00274   } // namespace detail
00275 } // namespace __gnu_pbds