Welcome to mirror list, hosted at ThFree Co, Russian Federation.

range_tree.hh « rangetree « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 919e0b049330d2ac53d7a0ada3cdda7dd338b32f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/

#include <cassert>
#include <climits>
#include <iostream>
#include <set>

#ifndef RANGE_TREE_DEBUG_PRINT_FUNCTION
#    define RANGE_TREE_DEBUG_PRINT_FUNCTION 0
#endif

template <typename T>
struct RangeTree {
	struct Range {
		Range(T min_, T max_)
			: min(min_), max(max_), single(min_ == max_) {
			assert(min_ <= max_);
		}

		Range(T t)
			: min(t), max(t), single(true)
		{}

		Range& operator=(const Range& v) {
			*this = v;
			return *this;
		}

		bool operator<(const Range& v) const {
			return max < v.min;
		}

		const T min;
		const T max;
		const bool single;
	};

	typedef std::set<Range> Tree;
	typedef typename Tree::iterator TreeIter;
	typedef typename Tree::reverse_iterator TreeIterReverse;
	typedef typename Tree::const_iterator TreeIterConst;

	/* Initialize with a single range from 'min' to 'max', inclusive. */
	RangeTree(T min, T max) {
		tree.insert(Range(min, max));
	}

	/* Initialize with a single range from 0 to 'max', inclusive. */
	RangeTree(T max) {
		tree.insert(Range(0, max));
	}

	RangeTree(const RangeTree<T>& src) {
		tree = src.tree;
	}

	/* Remove 't' from the associated range in the tree. Precondition:
	   a range including 't' must exist in the tree. */
	void take(T t) {
		#if RANGE_TREE_DEBUG_PRINT_FUNCTION
		std::cout << __func__ << "(" << t << ")\n";
		#endif

		/* Find the range that includes 't' and its neighbors */
		TreeIter iter = tree.find(Range(t));
		assert(iter != tree.end());
		Range cur = *iter;

		/* Remove the original range (note that this does not
		   invalidate the prev/next iterators) */
		tree.erase(iter);

		/* Construct two new ranges that together cover the original
		   range, except for 't' */
		if (t > cur.min)
			tree.insert(Range(cur.min, t - 1));
		if (t + 1 <= cur.max)
			tree.insert(Range(t + 1, cur.max));
	}

	/* Take the first element out of the first range in the
	   tree. Precondition: tree must not be empty. */
	T take_any() {
		#if RANGE_TREE_DEBUG_PRINT_FUNCTION
		std::cout << __func__ << "()\n";
		#endif

		/* Find the first element */
		TreeIter iter = tree.begin();
		assert(iter != tree.end());
		T first = iter->min;

		/* Take the first element */
		take(first);
		return first;
	}

	/* Return 't' to the tree, either expanding/merging existing
	   ranges or adding a range to cover it. Precondition: 't' cannot
	   be in an existing range. */
	void release(T t) {
		#if RANGE_TREE_DEBUG_PRINT_FUNCTION
		std::cout << __func__ << "(" << t << ")\n";
		#endif

		/* TODO: these cases should be simplified/unified */

		TreeIter right = tree.upper_bound(t);
		if (right != tree.end()) {
			TreeIter left = right;
			if (left != tree.begin())
				--left;

			if (left == right) {
				/* 't' lies before any existing ranges */
				if (t + 1 == left->min) {
					/* 't' lies directly before the first range,
					   resize and replace that range */
					const Range r(t, left->max);
					tree.erase(left);
					tree.insert(r);
				}
				else {
					/* There's a gap between 't' and the first range,
					   add a new range */
					tree.insert(Range(t));
				}
			}
			else if ((left->max + 1 == t) &&
				(t + 1 == right->min)) {
				/* 't' fills a hole. Remove left and right, and insert a
				   new range that covers both. */
				const Range r(left->min, right->max);
				tree.erase(left);
				tree.erase(right);
				tree.insert(r);
			}
			else if (left->max + 1 == t) {
				/* 't' lies directly after 'left' range, resize and
				   replace that range */
				const Range r(left->min, t);
				tree.erase(left);
				tree.insert(r);
			}
			else if (t + 1 == right->min) {
				/* 't' lies directly before 'right' range, resize and
				   replace that range */
				const Range r(t, right->max);
				tree.erase(right);
				tree.insert(r);
			}
			else {
				/* There's a gap between 't' and both adjacent ranges,
				   add a new range */
				tree.insert(Range(t));
			}
		}
		else {
			/* 't' lies after any existing ranges */
			right = tree.end();
			right--;
			if (right->max + 1 == t) {
				/* 't' lies directly after last range, resize and
				   replace that range */
				const Range r(right->min, t);
				tree.erase(right);
				tree.insert(r);
			}
			else {
				/* There's a gap between the last range and 't', add a
				   new range */
				tree.insert(Range(t));
			}
		}
	}

	bool has(T t) const {
		TreeIterConst iter = tree.find(Range(t));
		return (iter != tree.end()) && (t <= iter->max);
	}

	bool has_range(T min, T max) const {
		TreeIterConst iter = tree.find(Range(min, max));
		return (iter != tree.end()) && (min == iter->min && max == iter->max);
	}

	bool empty() const {
		return tree.empty();
	}

	int size() const {
		return tree.size();
	}

	void print() const {
		std::cout << "RangeTree:\n";
		for (TreeIterConst iter = tree.begin(); iter != tree.end(); ++iter) {
			const Range& r = *iter;
			if (r.single)
				std::cout << "  [" << r.min << "]\n";
			else
				std::cout << "  [" << r.min << ", " << r.max << "]\n";
		}
		if (empty())
			std::cout << "  <empty>";
		std::cout << "\n";
	}

	unsigned int allocation_lower_bound() const {
		return tree.size() * sizeof(Range);
	}

private:
	Tree tree;
};