]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/graph/detail/array_binary_tree.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / graph / detail / array_binary_tree.hpp
CommitLineData
7c673cae
FG
1//
2//=======================================================================
3// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
4// Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
5//
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//=======================================================================
10//
11#ifndef BOOST_ARRAY_BINARY_TREE_HPP
12#define BOOST_ARRAY_BINARY_TREE_HPP
13
14#include <iterator>
15#include <functional>
16#include <boost/config.hpp>
17
18namespace boost {
19
20/*
21 * Note: array_binary_tree is a completey balanced binary tree.
22 */
23#if !defined BOOST_NO_STD_ITERATOR_TRAITS
24 template <class RandomAccessIterator, class ID>
25#else
26 template <class RandomAccessIterator, class ValueType, class ID>
27#endif
28class array_binary_tree_node {
29public:
30 typedef array_binary_tree_node ArrayBinaryTreeNode;
31 typedef RandomAccessIterator rep_iterator;
32#if !defined BOOST_NO_STD_ITERATOR_TRAITS
33 typedef typename std::iterator_traits<RandomAccessIterator>::difference_type
34 difference_type;
35 typedef typename std::iterator_traits<RandomAccessIterator>::value_type
36 value_type;
37#else
38 typedef int difference_type;
39 typedef ValueType value_type;
40#endif
41 typedef difference_type size_type;
42
43 struct children_type {
44 struct iterator
7c673cae 45 { // replace with iterator_adaptor implementation -JGS
92f5a8d4
TL
46 typedef std::bidirectional_iterator_tag iterator_category;
47 typedef ArrayBinaryTreeNode value_type;
48 typedef size_type difference_type;
49 typedef array_binary_tree_node* pointer;
50 typedef ArrayBinaryTreeNode& reference;
7c673cae
FG
51
52 inline iterator() : i(0), n(0) { }
53 inline iterator(const iterator& x) : r(x.r), i(x.i), n(x.n), id(x.id) { }
54 inline iterator& operator=(const iterator& x) {
55 r = x.r; i = x.i; n = x.n;
56 /*egcs generate a warning*/
57 id = x.id;
58 return *this;
59 }
60 inline iterator(rep_iterator rr,
61 size_type ii,
62 size_type nn,
63 const ID& _id) : r(rr), i(ii), n(nn), id(_id) { }
64 inline array_binary_tree_node operator*() {
65 return ArrayBinaryTreeNode(r, i, n, id); }
66 inline iterator& operator++() { ++i; return *this; }
67 inline iterator operator++(int)
68 { iterator t = *this; ++(*this); return t; }
69 inline iterator& operator--() { --i; return *this; }
70 inline iterator operator--(int)
71 { iterator t = *this; --(*this); return t; }
72 inline bool operator==(const iterator& x) const { return i == x.i; }
73 inline bool operator!=(const iterator& x) const
74 { return !(*this == x); }
75 rep_iterator r;
76 size_type i;
77 size_type n;
78 ID id;
79 };
80 inline children_type() : i(0), n(0) { }
81 inline children_type(const children_type& x)
82 : r(x.r), i(x.i), n(x.n), id(x.id) { }
83 inline children_type& operator=(const children_type& x) {
84 r = x.r; i = x.i; n = x.n;
85 /*egcs generate a warning*/
86 id = x.id;
87 return *this;
88 }
89 inline children_type(rep_iterator rr,
90 size_type ii,
91 size_type nn,
92 const ID& _id) : r(rr), i(ii), n(nn), id(_id) { }
93 inline iterator begin() { return iterator(r, 2 * i + 1, n, id); }
94 inline iterator end() { return iterator(r, 2 * i + 1 + size(), n, id); }
95 inline size_type size() const {
96 size_type c = 2 * i + 1;
97 size_type s;
98 if (c + 1 < n) s = 2;
99 else if (c < n) s = 1;
100 else s = 0;
101 return s;
102 }
103 rep_iterator r;
104 size_type i;
105 size_type n;
106 ID id;
107 };
108 inline array_binary_tree_node() : i(0), n(0) { }
109 inline array_binary_tree_node(const array_binary_tree_node& x)
110 : r(x.r), i(x.i), n(x.n), id(x.id) { }
111 inline ArrayBinaryTreeNode& operator=(const ArrayBinaryTreeNode& x) {
112 r = x.r;
113 i = x.i;
114 n = x.n;
115 /*egcs generate a warning*/
116 id = x.id;
117 return *this;
118 }
119 inline array_binary_tree_node(rep_iterator start,
120 rep_iterator end,
121 rep_iterator pos, const ID& _id)
122 : r(start), i(pos - start), n(end - start), id(_id) { }
123 inline array_binary_tree_node(rep_iterator rr,
124 size_type ii,
125 size_type nn, const ID& _id)
126 : r(rr), i(ii), n(nn), id(_id) { }
127 inline value_type& value() { return *(r + i); }
128 inline const value_type& value() const { return *(r + i); }
129 inline ArrayBinaryTreeNode parent() const {
130 return ArrayBinaryTreeNode(r, (i - 1) / 2, n, id);
131 }
132 inline bool has_parent() const { return i != 0; }
133 inline children_type children() { return children_type(r, i, n, id); }
134 /*
135 inline void swap(array_binary_tree_node x) {
136 value_type tmp = x.value();
137 x.value() = value();
138 value() = tmp;
139 i = x.i;
140 }
141 */
142 template <class ExternalData>
143 inline void swap(ArrayBinaryTreeNode x, ExternalData& edata ) {
144 using boost::get;
145
146 value_type tmp = x.value();
147
148 /*swap external data*/
149 edata[ get(id, tmp) ] = i;
150 edata[ get(id, value()) ] = x.i;
151
152 x.value() = value();
153 value() = tmp;
154 i = x.i;
155 }
156 inline const children_type children() const {
157 return children_type(r, i, n);
158 }
159 inline size_type index() const { return i; }
160 rep_iterator r;
161 size_type i;
162 size_type n;
163 ID id;
164};
165
166template <class RandomAccessContainer,
167 class Compare = std::less<typename RandomAccessContainer::value_type> >
168struct compare_array_node {
169 typedef typename RandomAccessContainer::value_type value_type;
170 compare_array_node(const Compare& x) : comp(x) {}
171 compare_array_node(const compare_array_node& x) : comp(x.comp) {}
172
173 template< class node_type >
174 inline bool operator()(const node_type& x, const node_type& y) {
175 return comp(x.value(), y.value());
176 }
177
178 template< class node_type >
179 inline bool operator()(const node_type& x, const node_type& y) const {
180 return comp(x.value(), y.value());
181 }
182 Compare comp;
183};
184
185} // namespace boost
186
187#endif /* BOOST_ARRAY_BINARY_TREE_HPP */