/usr/include/mathic/TourTree.h is in libmathic-dev 1.0~git20160320-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | #ifndef MATHIC_TOUR_TREE_GUARD
#define MATHIC_TOUR_TREE_GUARD
#include "stdinc.h"
#include "ComTree.h"
#include <string>
#include <vector>
namespace mathic {
class TourTreeSuggestedOptions {
public:
// there are no tournament tree options so far.
};
template<class C>
class TourTree {
public:
typedef C Configuration;
typedef typename Configuration::Entry Entry;
TourTree(const Configuration& configuration);
Configuration& getConfiguration() {return _conf;}
const Configuration& getConfiguration() const {return _conf;}
std::string getName() const;
void push(Entry entry);
template<class It>
void push(It begin, It end);
Entry pop();
Entry top() const;
bool empty() const {return _tree.empty();}
void print(std::ostream& out) const;
void decreaseTop(Entry newEntry);
template<class T>
void forAll(T& t) const {
typedef typename std::vector<Player>::const_iterator Iter;
Iter end = _players.end();
for (Iter it = _players.begin(); it != end; ++it)
if (!t.proceed(it->entry))
return;
}
template<class T>
void forAll(T& t) {
typedef typename std::vector<Player>::iterator Iter;
Iter end = _players.end();
for (Iter it = _players.begin(); it != end; ++it)
if (!t.proceed(it->entry))
return;
}
void clear();
size_t getMemoryUse() const;
private:
class Player;
// Setting fastIndex to true speeds up left/right child
// computations. We only compute parents, so there is no reason
// to enable fastIndex.
typedef ComTree<Player*, false> Tree;
typedef typename Tree::Node Node;
struct Player {
Player(const Entry& entry, Node leaf): entry(entry), leaf(leaf) {}
Entry entry;
Node leaf;
};
void reallocate();
/// Asserts internal invariants if asserts are turned on.
bool isValid() const;
Tree _tree;
std::vector<Player> _players;
Configuration _conf;
};
template<class C>
void TourTree<C>::clear() {
MATHIC_ASSERT(isValid());
_tree.clear();
_players.clear();
MATHIC_ASSERT(isValid());
}
template<class C>
size_t TourTree<C>::getMemoryUse() const {
return _tree.getMemoryUse() +
_players.capacity() * sizeof(_players.front());
}
template<class C>
TourTree<C>::TourTree(const C& configuration): _conf(configuration) {
reallocate();
}
template<class C>
std::string TourTree<C>::getName() const {
return std::string("t-tree (") +
(C::fastIndex ? "fi" : "si") +
')';
}
template<class C>
void TourTree<C>::push(Entry entry) {
MATHIC_SLOW_ASSERT(isValid());
if (!_tree.hasFreeCapacity(2))
reallocate();
if (empty()) {
_players.push_back(Player(entry, Node()));
_tree.pushBackWithCapacity(&_players.back());
MATHIC_SLOW_ASSERT(isValid());
return;
}
// move leaf down as left child
Node posParent = _tree.lastLeaf().next().parent();
Player* moveDown = _tree[posParent];
_tree.pushBackWithCapacity(moveDown);
moveDown->leaf = _tree.lastLeaf();
MATHIC_ASSERT(_tree.lastLeaf().isLeft());
// insert entry as right child
_players.push_back(Player(entry, _tree.lastLeaf().next()));
_tree.pushBackWithCapacity(&_players.back());
MATHIC_ASSERT(_tree.lastLeaf().isRight());
Node pos = _tree.lastLeaf();
do {
MATHIC_ASSERT(!pos.isRoot());
MATHIC_ASSERT(posParent == pos.parent());
typename C::CompareResult cmp = _conf.compare
(_tree[posParent]->entry, _tree[pos]->entry);
if (!_conf.cmpLessThan(cmp))
break;
_tree[posParent] = _tree[pos];
pos = posParent;
posParent = pos.parent();
} while (!pos.isRoot());
MATHIC_SLOW_ASSERT(isValid());
}
template<class C>
template<class It>
void TourTree<C>::push(It begin, It end) {
for (; begin != end; ++begin)
push(*begin);
}
template<class C>
void TourTree<C>::decreaseTop(Entry newEntry) {
MATHIC_ASSERT(!empty());
Player* player = _tree[Node()];
player->entry = newEntry;
for (Node pos = player->leaf; !pos.isRoot(); pos = pos.parent()) {
Player* opponent = _tree[pos.sibling()];
if (_conf.cmpLessThan(_conf.compare(player->entry, opponent->entry)))
player = opponent;
_tree[pos.parent()] = player;
}
MATHIC_SLOW_ASSERT(isValid());
}
template<class C>
typename TourTree<C>::Entry TourTree<C>::pop() {
MATHIC_ASSERT(!empty());
Entry top = _tree[Node()]->entry;
if (_tree.lastLeaf().isRoot()) {
_tree.popBack();
_players.pop_back();
MATHIC_SLOW_ASSERT(isValid());
return top;
}
Node parentPos = _tree.lastLeaf().parent();
Player* left = _tree[_tree.lastLeaf().prev()];
Player* right = _tree[_tree.lastLeaf()];
if (right == _tree[parentPos]) {
// we want right to be the smaller entry so that it can be
// removed without that having an impact further up the tree.
std::swap(left->entry, right->entry);
for (Node pos = parentPos; _tree[pos] == right; pos = pos.parent()) {
_tree[pos] = left;
if (pos.isRoot())
break;
}
}
Player* player = _tree[Node()];
player->entry = right->entry; // let right take the winner's place
MATHIC_ASSERT(right == &_players.back());
_players.pop_back(); // remove right
left->leaf = parentPos; // move up left
_tree.popBack();
_tree.popBack();
for (Node pos = player->leaf; !pos.isRoot();) {
Player* opponent = _tree[pos.sibling()];
typename C::CompareResult cmp =
_conf.compare(player->entry, opponent->entry);
if (_conf.cmpLessThan(cmp))
player = opponent;
pos = pos.parent();
_tree[pos] = player;
}
MATHIC_SLOW_ASSERT(isValid());
return top;
}
template<class C>
typename TourTree<C>::Entry TourTree<C>::top() const {
MATHIC_ASSERT(!empty());
return _tree[Node()]->entry;
}
template<class C>
void TourTree<C>::print(std::ostream& out) const {
out << getName() << ": {\n" << _tree << "}\n";
}
template<class C>
void TourTree<C>::reallocate() {
MATHIC_ASSERT(isValid());
_tree.increaseCapacity();
const size_t newCapacity = _tree.capacity();
if (_players.empty())
_players.reserve(newCapacity / 2 + 2);
else {
Player* oldBegin = &_players.front();
_players.reserve(newCapacity / 2 + 2);
Player* newBegin = &_players.front();
for (Node pos; pos <= _tree.lastLeaf(); ++pos)
_tree[pos] = newBegin + (_tree[pos] - oldBegin);
}
MATHIC_ASSERT(isValid());
}
template<class C>
bool TourTree<C>::isValid() const {
#ifndef MATHIC_DEBUG
return true;
#else
MATHIC_ASSERT
((_tree.empty() && _players.empty()) || // just constructed
(_tree.capacity() + 1 <= 2 * _players.capacity()));
MATHIC_ASSERT((empty() && _players.empty()) ||
(_tree.size() + 1 == 2 * _players.size()));
// _tree points into _players
for (Node pos; pos <= _tree.lastLeaf(); ++pos) {
size_t index = _tree[pos] - &(_players.front());
MATHIC_ASSERT(index < _players.size());
}
for (Node pos; pos <= _tree.lastLeaf(); ++pos) {
if (pos.left() >= _tree.lastLeaf()) { // leaf or two children
MATHIC_ASSERT(pos.right() > _tree.lastLeaf()); // pos is a leaf
MATHIC_ASSERT(_tree[pos]->leaf == pos);
} else {
MATHIC_ASSERT(pos.right() <= _tree.lastLeaf()); // pos has two children
// exactly one child wins
MATHIC_ASSERT(_tree[pos.left()] != _tree[pos.right()]);
MATHIC_ASSERT(_tree[pos] == _tree[pos.left()] ||
_tree[pos] == _tree[pos.right()]);
MATHIC_ASSERT(!_conf.cmpLessThan(
_conf.compare(_tree[pos]->entry, _tree[pos.left()]->entry)));
MATHIC_ASSERT(!_conf.cmpLessThan(
_conf.compare(_tree[pos]->entry, _tree[pos.right()]->entry)));
}
}
return true;
#endif
}
}
#endif
|