/usr/include/mathic/GeoFront.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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | #ifndef MATHIC_GEO_FRONT_GUARD
#define MATHIC_GEO_FRONT_GUARD
#include "stdinc.h"
#include <vector>
#include <utility>
namespace mathic {
/**
To be used by Geobucket class.
Configuration is the Configuration from the owning Geobucket.
Bucket is the Bucket class from the owning Geobucket. Having Bucket be a
parameter allows it to be used before it is declared so that this header
need not include Geobucket's header, which would otherwise be a cyclic
dependency. This also grants access to Geobucket<C>::Bucket which is
otherwise private.
Why not make this an inner template class of Geobucket? Because inner
template classes cannot be specialized without also specializing the
outer class.
*/
template<class Configuration>
class Geobucket;
template<class Configuration,
class Bucket = typename Geobucket<Configuration>::Bucket,
bool Order = Configuration::trackFront>
class GeoFront;
// ********************************************************************
// GeoFront maintaining no special order
template<class C, class Bucket>
class GeoFront<C, Bucket, false> {
public:
/** A reference is kept to entryCountRef so that it can be updated
in the event of a deduplication. */
GeoFront(const C& conf, size_t& entryCountRef):
_cachedMaxBucket(0), _entryCountRef(&entryCountRef), _conf(conf) {
MATHIC_ASSERT(!C::trackFront);
}
void clear() {
_cachedMaxBucket = 0;
}
void reserveCapacity(size_t newCapacity) {}
void bucketsInvalidated(Bucket* oldBegin, Bucket* newBegin) {
_cachedMaxBucket = 0;
}
bool empty() const {return *_entryCountRef == 0;}
void insert(Bucket* bucket) {_cachedMaxBucket = 0;}
void remove(Bucket* bucket) {_cachedMaxBucket = 0;}
void keyIncreased(Bucket* bucket) {_cachedMaxBucket = 0;}
void keyDecreased(Bucket* bucket) {_cachedMaxBucket = 0;}
void swapKeys(Bucket* a, Bucket* b) {}
bool larger(const Bucket* a, const Bucket* b) const {
MATHIC_ASSERT(false);
return false;
}
const Bucket* getMax(Bucket* bucketBegin, Bucket* bucketEnd) const;
#ifdef MATHIC_DEBUG
bool debugIsValid(Bucket* bucketBegin, Bucket* bucketEnd) const;
#endif
size_t getMemoryUse() const;
private:
const Bucket* computeMax(Bucket* bucketBegin, Bucket* bucketEnd) const;
mutable const Bucket* _cachedMaxBucket;
size_t* _entryCountRef;
const C& _conf;
};
template<class C, class B>
size_t GeoFront<C, B, false>::getMemoryUse() const {
return 0;
}
template<class C, class B>
const B* GeoFront<C, B, false>::getMax(B* bucketBegin, B* bucketEnd) const {
// the point is that the compiler is more likely to compile this
// pre-calculation so that computeMax won't get called in cases
// where the maximum has already been computed.
if (_cachedMaxBucket != 0)
return _cachedMaxBucket;
return computeMax(bucketBegin, bucketEnd);
}
template<class C, class B>
const B* GeoFront<C, B, false>::computeMax
(B* bucketBegin, B* bucketEnd) const {
MATHIC_ASSERT(_cachedMaxBucket == 0);
const B* maxBucket = bucketBegin;
while (maxBucket->empty()) {
++maxBucket;
MATHIC_ASSERT(maxBucket != bucketEnd);
}
for (const B* it = maxBucket + 1; it != bucketEnd; ++it) {
if (it->empty())
continue;
typename C::CompareResult cmp =
_conf.compare(it->back(), maxBucket->back());
if (_conf.cmpLessThan(cmp))
continue;
if (C::supportDeduplication && _conf.cmpEqual(cmp)) {
B* mb = const_cast<B*>(maxBucket);
mb->setEntry(mb->end() - 1,
_conf.deduplicate(mb->back(), it->back()));
const_cast<B*>(it)->pop_back();
--*_entryCountRef;
continue;
}
maxBucket = it;
}
MATHIC_ASSERT(maxBucket != bucketEnd);
return _cachedMaxBucket = maxBucket;
}
#ifdef MATHIC_DEBUG
template<class C, class B>
bool GeoFront<C, B, false>::debugIsValid(B* bucketBegin, B* bucketEnd) const {
if (_cachedMaxBucket == 0)
return true;
MATHIC_ASSERT(!_cachedMaxBucket->empty());
for (B* bucket = bucketBegin; bucket != bucketEnd; ++bucket) {
if (!bucket->empty()) {
MATHIC_ASSERT(!_conf.cmpLessThan
(_conf.compare(_cachedMaxBucket->back(), bucket->back())));
}
}
return true;
}
#endif
// ********************************************************************
// GeoFront maintaining an ordered list
template<class C, class Bucket>
class GeoFront<C, Bucket, true> {
public:
GeoFront(const C& conf, size_t& entryCountRef);
~GeoFront() {delete[] _bucketBegin;}
Bucket** begin() {return _bucketBegin;}
Bucket*const* begin() const {return _bucketBegin;}
Bucket** end() {return _bucketEnd;}
Bucket*const* end() const {return _bucketEnd;}
/** Can contain this many */
void reserveCapacity(size_t newCapacity);
void bucketsInvalidated(Bucket* oldBegin, Bucket* newBegin);
bool empty() const {return _bucketBegin == _bucketEnd;}
void clear();
void insert(Bucket* bucket);
void remove(Bucket* bucket);
void keyIncreased(Bucket* bucket);
void keyDecreased(Bucket* bucket);
void swapKeys(Bucket* a, Bucket* b);
bool larger(const Bucket* a, const Bucket* b) const;
const Bucket* getMax(Bucket* bucketBegin, Bucket* bucketEnd) const;
#ifdef MATHIC_DEBUG
bool debugIsValid(Bucket* bucketBegin, Bucket* bucketEnd) const;
#endif
size_t getMemoryUse() const;
private:
size_t size() const {return _bucketEnd - _bucketBegin;}
size_t capacity() const {return _bucketCapacityEnd - _bucketBegin;}
Bucket** _bucketBegin;
Bucket** _bucketEnd;
Bucket** _bucketCapacityEnd;
size_t* _entryCountRef;
const C& _conf;
};
template<class C, class B>
void GeoFront<C, B, true>::clear() {
_bucketEnd = _bucketBegin;
}
template<class C, class B>
size_t GeoFront<C, B, true>::getMemoryUse() const {
return capacity() * sizeof(*_bucketBegin);
}
template<class C, class B>
GeoFront<C, B, true>::GeoFront(const C& conf, size_t& entryCountRef):
_bucketBegin(0),
_bucketEnd(0),
_bucketCapacityEnd(0),
_entryCountRef(&entryCountRef),
_conf(conf) {
MATHIC_ASSERT(C::trackFront);
}
template<class C, class B>
void GeoFront<C, B, true>::reserveCapacity(size_t newCapacity) {
MATHIC_ASSERT(newCapacity >= size());
if (newCapacity == 0)
newCapacity = 1;
size_t const oldSize = size();
B** const oldBegin = _bucketBegin;
B** const oldEnd = _bucketEnd;
_bucketBegin = new B*[newCapacity];
_bucketEnd = _bucketBegin + oldSize;
_bucketCapacityEnd = _bucketBegin + newCapacity;
std::copy(oldBegin, oldEnd, _bucketBegin);
// the tokens point into the old space, so map them to an index
// and then back to a pointer into the newly allocated memory.
for (B** bucket = _bucketBegin; bucket != _bucketEnd; ++bucket) {
B** token = (*bucket)->getFrontToken();
token = (token - oldBegin) + _bucketBegin;
(*bucket)->setFrontToken(token);
}
delete[] oldBegin;
}
template<class C, class B>
void GeoFront<C, B, true>::bucketsInvalidated
(B* oldBegin, B* newBegin) {
// _buckets points into an array of buckets that has been reallocated,
// so we need to go to an index and back to update the pointers
// to point into the new memory area.
if (empty())
return;
for (B** bucket = begin(); bucket != end(); ++bucket)
*bucket = (*bucket - oldBegin) + newBegin;
}
template<class C, class B>
void GeoFront<C, B, true>::insert(B* bucket) {
if (!C::trackFront)
return;
MATHIC_ASSERT(bucket != 0);
MATHIC_ASSERT(bucket->_frontPos == 0);
MATHIC_ASSERT(!bucket->empty());
if (_bucketEnd == _bucketCapacityEnd)
reserveCapacity(size() + 1);
*_bucketEnd = bucket;
++_bucketEnd;
bucket->_frontPos = end() - 1;
keyIncreased(bucket);
}
template<class C, class B>
void GeoFront<C, B, true>::keyIncreased(B* bucket) {
B** pos = bucket->_frontPos;
B** begin = this->begin();
for (; pos != begin; --pos) { // move bucket to lower index
B& shouldBeGreater = **(pos - 1);
typename C::CompareResult cmp =
_conf.compare(shouldBeGreater.back(), bucket->back());
if (!_conf.cmpLessThan(cmp))
break;
// todo: if equal
*pos = *(pos - 1);
shouldBeGreater._frontPos = pos;
}
// We don't have to adjust bucket's position if it never moved, but
// detecting that without extra branches makes the code too complex
// for it to be worth it.
*pos = bucket;
bucket->_frontPos = pos;
}
template<class C, class B>
void GeoFront<C, B, true>::keyDecreased(B* bucket) {
if (bucket->empty()) {
remove(bucket);
return;
}
B** pos = bucket->_frontPos;
B** end = this->end();
for (; pos + 1 != end; ++pos) { // move bucket to higher index
B& otherBucket = **(pos + 1);
typename C::CompareResult cmp =
_conf.compare(bucket->back(), otherBucket.back());
if (!_conf.cmpLessThan(cmp)) {
if (!C::supportDeduplication || !_conf.cmpEqual(cmp))
break; // greater than, found correct position
otherBucket.setEntry(otherBucket.end() - 1,
_conf.deduplicate(bucket->back(), otherBucket.back()));
bucket->pop_back();
--*_entryCountRef;
if (bucket->empty()) {
bucket->_frontPos = pos;
remove(bucket);
return;
}
}
*pos = *(pos + 1);
otherBucket._frontPos = pos;
}
// We don't have to adjust bucket's position if it never moved, but
// detecting that without extra branches makes the code too complex
// for it to be worth it.
*pos = bucket;
bucket->_frontPos = pos;
}
template<class C, class B>
void GeoFront<C, B, true>::remove(B* bucket) {
MATHIC_ASSERT(bucket->_frontPos != 0);
MATHIC_ASSERT(bucket->empty());
B** end = this->end(); // can't dereference end
for (B** i = bucket->_frontPos + 1; i != end; ++i) {
*(i - 1) = *i;
(*i)->_frontPos = i - 1;
}
--_bucketEnd;
bucket->_frontPos = 0;
}
template<class C, class B>
void GeoFront<C, B, true>::swapKeys(B* a, B* b) {
MATHIC_ASSERT(a != 0);
MATHIC_ASSERT(b != 0);
std::swap(a->_frontPos, b->_frontPos);
if (a->_frontPos != 0)
*a->_frontPos = a;
if (b->_frontPos != 0)
*b->_frontPos = b;
}
template<class C, class B>
bool GeoFront<C, B, true>::larger(const B* a, const B* b) const {
MATHIC_ASSERT(a != 0);
MATHIC_ASSERT(b != 0);
return a->_frontPos < b->_frontPos;
}
template<class C, class B>
const B* GeoFront<C, B, true>::getMax(B* bucketBegin, B* bucketEnd) const {
MATHIC_ASSERT(!empty());
return *begin();
}
#ifdef MATHIC_DEBUG
template<class C, class B>
bool GeoFront<C, B, true>::debugIsValid(B* bucketBegin, B* bucketEnd) const {
std::vector<const B*> nonEmpty;
size_t size = bucketEnd - bucketBegin;
for (size_t b = 0; b < size; ++b) {
if (bucketBegin[b].empty()) {
MATHIC_ASSERT(bucketBegin[b]._frontPos == 0);
} else {
MATHIC_ASSERT(bucketBegin[b]._frontPos != 0);
MATHIC_ASSERT(*(bucketBegin[b]._frontPos) == &(bucketBegin[b]));
nonEmpty.push_back(&(bucketBegin[b]));
}
}
std::vector<B*> frontCopy(_bucketBegin, _bucketEnd);
std::sort(nonEmpty.begin(), nonEmpty.end());
std::sort(frontCopy.begin(), frontCopy.end());
MATHIC_ASSERT(std::equal(frontCopy.begin(), frontCopy.end(), nonEmpty.begin()));
if (!empty()) {
for (B** it = _bucketBegin + 1; it != _bucketEnd; ++it)
MATHIC_ASSERT(!_conf.cmpLessThan
(_conf.compare((*(it - 1))->back(), (*it)->back())));
}
return true;
}
#endif
}
#endif
|