2 //=======================================================================
3 // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
4 // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
6 // Distributed under the Boost Software License, Version 1.0. (See
7 // accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //=======================================================================
11 #ifndef BOOST_ARRAY_BINARY_TREE_HPP
12 #define BOOST_ARRAY_BINARY_TREE_HPP
16 #include <boost/config.hpp>
22 * Note: array_binary_tree is a completey balanced binary tree.
24 #if !defined BOOST_NO_STD_ITERATOR_TRAITS
25 template < class RandomAccessIterator, class ID >
27 template < class RandomAccessIterator, class ValueType, class ID >
29 class array_binary_tree_node
32 typedef array_binary_tree_node ArrayBinaryTreeNode;
33 typedef RandomAccessIterator rep_iterator;
34 #if !defined BOOST_NO_STD_ITERATOR_TRAITS
36 typename std::iterator_traits< RandomAccessIterator >::difference_type
38 typedef typename std::iterator_traits< RandomAccessIterator >::value_type
41 typedef int difference_type;
42 typedef ValueType value_type;
44 typedef difference_type size_type;
49 { // replace with iterator_adaptor implementation -JGS
50 typedef std::bidirectional_iterator_tag iterator_category;
51 typedef ArrayBinaryTreeNode value_type;
52 typedef size_type difference_type;
53 typedef array_binary_tree_node* pointer;
54 typedef ArrayBinaryTreeNode& reference;
56 inline iterator() : i(0), n(0) {}
57 inline iterator(const iterator& x)
58 : r(x.r), i(x.i), n(x.n), id(x.id)
61 inline iterator& operator=(const iterator& x)
66 /*egcs generate a warning*/
71 rep_iterator rr, size_type ii, size_type nn, const ID& _id)
72 : r(rr), i(ii), n(nn), id(_id)
75 inline array_binary_tree_node operator*()
77 return ArrayBinaryTreeNode(r, i, n, id);
79 inline iterator& operator++()
84 inline iterator operator++(int)
90 inline iterator& operator--()
95 inline iterator operator--(int)
101 inline bool operator==(const iterator& x) const { return i == x.i; }
102 inline bool operator!=(const iterator& x) const
104 return !(*this == x);
111 inline children_type() : i(0), n(0) {}
112 inline children_type(const children_type& x)
113 : r(x.r), i(x.i), n(x.n), id(x.id)
116 inline children_type& operator=(const children_type& x)
121 /*egcs generate a warning*/
125 inline children_type(
126 rep_iterator rr, size_type ii, size_type nn, const ID& _id)
127 : r(rr), i(ii), n(nn), id(_id)
130 inline iterator begin() { return iterator(r, 2 * i + 1, n, id); }
131 inline iterator end() { return iterator(r, 2 * i + 1 + size(), n, id); }
132 inline size_type size() const
134 size_type c = 2 * i + 1;
149 inline array_binary_tree_node() : i(0), n(0) {}
150 inline array_binary_tree_node(const array_binary_tree_node& x)
151 : r(x.r), i(x.i), n(x.n), id(x.id)
154 inline ArrayBinaryTreeNode& operator=(const ArrayBinaryTreeNode& x)
159 /*egcs generate a warning*/
163 inline array_binary_tree_node(
164 rep_iterator start, rep_iterator end, rep_iterator pos, const ID& _id)
165 : r(start), i(pos - start), n(end - start), id(_id)
168 inline array_binary_tree_node(
169 rep_iterator rr, size_type ii, size_type nn, const ID& _id)
170 : r(rr), i(ii), n(nn), id(_id)
173 inline value_type& value() { return *(r + i); }
174 inline const value_type& value() const { return *(r + i); }
175 inline ArrayBinaryTreeNode parent() const
177 return ArrayBinaryTreeNode(r, (i - 1) / 2, n, id);
179 inline bool has_parent() const { return i != 0; }
180 inline children_type children() { return children_type(r, i, n, id); }
182 inline void swap(array_binary_tree_node x) {
183 value_type tmp = x.value();
189 template < class ExternalData >
190 inline void swap(ArrayBinaryTreeNode x, ExternalData& edata)
194 value_type tmp = x.value();
196 /*swap external data*/
197 edata[get(id, tmp)] = i;
198 edata[get(id, value())] = x.i;
204 inline const children_type children() const
206 return children_type(r, i, n);
208 inline size_type index() const { return i; }
215 template < class RandomAccessContainer,
216 class Compare = std::less< typename RandomAccessContainer::value_type > >
217 struct compare_array_node
219 typedef typename RandomAccessContainer::value_type value_type;
220 compare_array_node(const Compare& x) : comp(x) {}
221 compare_array_node(const compare_array_node& x) : comp(x.comp) {}
223 template < class node_type >
224 inline bool operator()(const node_type& x, const node_type& y)
226 return comp(x.value(), y.value());
229 template < class node_type >
230 inline bool operator()(const node_type& x, const node_type& y) const
232 return comp(x.value(), y.value());
239 #endif /* BOOST_ARRAY_BINARY_TREE_HPP */