Merge branch 'master' into cpp-11
# Conflicts: # config.lib
This commit is contained in:
@@ -25,24 +25,8 @@
|
||||
*/
|
||||
template <typename T, size_t length>
|
||||
struct SmallStackSafeStackAlloc {
|
||||
#if !defined(__NDS__)
|
||||
/** Storing the data on the stack */
|
||||
T data[length];
|
||||
#else
|
||||
/** Storing it on the heap */
|
||||
T *data;
|
||||
/** The length (in elements) of data in this allocator. */
|
||||
size_t len;
|
||||
|
||||
/** Allocating the memory */
|
||||
SmallStackSafeStackAlloc() : data(MallocT<T>(length)), len(length) {}
|
||||
|
||||
/** And freeing when it goes out of scope */
|
||||
~SmallStackSafeStackAlloc()
|
||||
{
|
||||
free(data);
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Gets a pointer to the data stored in this wrapper.
|
||||
@@ -69,11 +53,7 @@ struct SmallStackSafeStackAlloc {
|
||||
*/
|
||||
inline T *EndOf()
|
||||
{
|
||||
#if !defined(__NDS__)
|
||||
return endof(data);
|
||||
#else
|
||||
return &data[len];
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
|
@@ -26,7 +26,7 @@
|
||||
#define TTD_BIG_ENDIAN 1
|
||||
|
||||
/* Windows has always LITTLE_ENDIAN */
|
||||
#if defined(_WIN32) || defined(__OS2__)
|
||||
#if defined(_WIN32) || defined(__OS2__) || defined(__HAIKU__)
|
||||
# define TTD_ENDIAN TTD_LITTLE_ENDIAN
|
||||
#elif defined(OSX)
|
||||
# include <sys/types.h>
|
||||
|
@@ -12,11 +12,6 @@
|
||||
#ifndef GEOMETRY_TYPE_HPP
|
||||
#define GEOMETRY_TYPE_HPP
|
||||
|
||||
#if defined(__AMIGA__)
|
||||
/* AmigaOS already has a Point declared */
|
||||
#define Point OTTD_Point
|
||||
#endif /* __AMIGA__ */
|
||||
|
||||
#if defined(__APPLE__)
|
||||
/* Mac OS X already has both Rect and Point declared */
|
||||
#define Rect OTTD_Rect
|
||||
|
473
src/core/kdtree.hpp
Normal file
473
src/core/kdtree.hpp
Normal file
@@ -0,0 +1,473 @@
|
||||
/*
|
||||
* This file is part of OpenTTD.
|
||||
* OpenTTD 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, version 2.
|
||||
* OpenTTD 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 OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/** @file kdtree.hpp K-d tree template specialised for 2-dimensional Manhattan geometry */
|
||||
|
||||
#ifndef KDTREE_HPP
|
||||
#define KDTREE_HPP
|
||||
|
||||
#include "../stdafx.h"
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
/**
|
||||
* K-dimensional tree, specialised for 2-dimensional space.
|
||||
* This is not intended as a primary storage of data, but as an index into existing data.
|
||||
* Usually the type stored by this tree should be an index into an existing array.
|
||||
*
|
||||
* This implementation assumes Manhattan distances are used.
|
||||
*
|
||||
* Be careful when using this in game code, depending on usage pattern, the tree shape may
|
||||
* end up different for different clients in multiplayer, causing iteration order to differ
|
||||
* and possibly having elements returned in different order. The using code should be designed
|
||||
* to produce the same result regardless of iteration order.
|
||||
*
|
||||
* The element type T must be less-than comparable for FindNearest to work.
|
||||
*
|
||||
* @tparam T Type stored in the tree, should be cheap to copy.
|
||||
* @tparam TxyFunc Functor type to extract coordinate from a T value and dimension index (0 or 1).
|
||||
* @tparam CoordT Type of coordinate values extracted via TxyFunc.
|
||||
* @tparam DistT Type to use for representing distance values.
|
||||
*/
|
||||
template <typename T, typename TxyFunc, typename CoordT, typename DistT>
|
||||
class Kdtree {
|
||||
/** Type of a node in the tree */
|
||||
struct node {
|
||||
T element; ///< Element stored at node
|
||||
size_t left; ///< Index of node to the left, INVALID_NODE if none
|
||||
size_t right; ///< Index of node to the right, INVALID_NODE if none
|
||||
|
||||
node(T element) : element(element), left(INVALID_NODE), right(INVALID_NODE) { }
|
||||
};
|
||||
|
||||
static const size_t INVALID_NODE = SIZE_MAX; ///< Index value indicating no-such-node
|
||||
|
||||
std::vector<node> nodes; ///< Pool of all nodes in the tree
|
||||
std::vector<size_t> free_list; ///< List of dead indices in the nodes vector
|
||||
size_t root; ///< Index of root node
|
||||
TxyFunc xyfunc; ///< Functor to extract a coordinate from an element
|
||||
size_t unbalanced; ///< Number approximating how unbalanced the tree might be
|
||||
|
||||
/** Create one new node in the tree, return its index in the pool */
|
||||
size_t AddNode(const T &element)
|
||||
{
|
||||
if (this->free_list.size() == 0) {
|
||||
this->nodes.emplace_back(element);
|
||||
return this->nodes.size() - 1;
|
||||
} else {
|
||||
size_t newidx = this->free_list.back();
|
||||
this->free_list.pop_back();
|
||||
this->nodes[newidx] = node{ element };
|
||||
return newidx;
|
||||
}
|
||||
}
|
||||
|
||||
/** Find a coordinate value to split a range of elements at */
|
||||
template <typename It>
|
||||
CoordT SelectSplitCoord(It begin, It end, int level)
|
||||
{
|
||||
It mid = begin + (end - begin) / 2;
|
||||
std::nth_element(begin, mid, end, [&](T a, T b) { return this->xyfunc(a, level % 2) < this->xyfunc(b, level % 2); });
|
||||
return this->xyfunc(*mid, level % 2);
|
||||
}
|
||||
|
||||
/** Construct a subtree from elements between begin and end iterators, return index of root */
|
||||
template <typename It>
|
||||
size_t BuildSubtree(It begin, It end, int level)
|
||||
{
|
||||
ptrdiff_t count = end - begin;
|
||||
|
||||
if (count == 0) {
|
||||
return INVALID_NODE;
|
||||
} else if (count == 1) {
|
||||
return this->AddNode(*begin);
|
||||
} else if (count > 1) {
|
||||
CoordT split_coord = SelectSplitCoord(begin, end, level);
|
||||
It split = std::partition(begin, end, [&](T v) { return this->xyfunc(v, level % 2) < split_coord; });
|
||||
size_t newidx = this->AddNode(*split);
|
||||
this->nodes[newidx].left = this->BuildSubtree(begin, split, level + 1);
|
||||
this->nodes[newidx].right = this->BuildSubtree(split + 1, end, level + 1);
|
||||
return newidx;
|
||||
} else {
|
||||
NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
/** Rebuild the tree with all existing elements, optionally adding or removing one more */
|
||||
bool Rebuild(const T *include_element, const T *exclude_element)
|
||||
{
|
||||
size_t initial_count = this->Count();
|
||||
if (initial_count < 8) return false; // arbitrary value for "not worth rebalancing"
|
||||
|
||||
T root_element = this->nodes[this->root].element;
|
||||
std::vector<T> elements = this->FreeSubtree(this->root);
|
||||
elements.push_back(root_element);
|
||||
|
||||
if (include_element != NULL) {
|
||||
elements.push_back(*include_element);
|
||||
initial_count++;
|
||||
}
|
||||
if (exclude_element != NULL) {
|
||||
typename std::vector<T>::iterator removed = std::remove(elements.begin(), elements.end(), *exclude_element);
|
||||
elements.erase(removed, elements.end());
|
||||
initial_count--;
|
||||
}
|
||||
|
||||
this->Build(elements.begin(), elements.end());
|
||||
assert(initial_count == this->Count());
|
||||
return true;
|
||||
}
|
||||
|
||||
/** Insert one element in the tree somewhere below node_idx */
|
||||
void InsertRecursive(const T &element, size_t node_idx, int level)
|
||||
{
|
||||
/* Dimension index of current level */
|
||||
int dim = level % 2;
|
||||
/* Node reference */
|
||||
node &n = this->nodes[node_idx];
|
||||
|
||||
/* Coordinate of element splitting at this node */
|
||||
CoordT nc = this->xyfunc(n.element, dim);
|
||||
/* Coordinate of the new element */
|
||||
CoordT ec = this->xyfunc(element, dim);
|
||||
/* Which side to insert on */
|
||||
size_t &next = (ec < nc) ? n.left : n.right;
|
||||
|
||||
if (next == INVALID_NODE) {
|
||||
/* New leaf */
|
||||
size_t newidx = this->AddNode(element);
|
||||
/* Vector may have been reallocated at this point, n and next are invalid */
|
||||
node &nn = this->nodes[node_idx];
|
||||
if (ec < nc) nn.left = newidx; else nn.right = newidx;
|
||||
} else {
|
||||
this->InsertRecursive(element, next, level + 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Free all children of the given node
|
||||
* @return Collection of elements that were removed from tree.
|
||||
*/
|
||||
std::vector<T> FreeSubtree(size_t node_idx)
|
||||
{
|
||||
std::vector<T> subtree_elements;
|
||||
node &n = this->nodes[node_idx];
|
||||
|
||||
/* We'll be appending items to the free_list, get index of our first item */
|
||||
size_t first_free = this->free_list.size();
|
||||
/* Prepare the descent with our children */
|
||||
if (n.left != INVALID_NODE) this->free_list.push_back(n.left);
|
||||
if (n.right != INVALID_NODE) this->free_list.push_back(n.right);
|
||||
n.left = n.right = INVALID_NODE;
|
||||
|
||||
/* Recursively free the nodes being collected */
|
||||
for (size_t i = first_free; i < this->free_list.size(); i++) {
|
||||
node &fn = this->nodes[this->free_list[i]];
|
||||
subtree_elements.push_back(fn.element);
|
||||
if (fn.left != INVALID_NODE) this->free_list.push_back(fn.left);
|
||||
if (fn.right != INVALID_NODE) this->free_list.push_back(fn.right);
|
||||
fn.left = fn.right = INVALID_NODE;
|
||||
}
|
||||
|
||||
return subtree_elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find and remove one element from the tree.
|
||||
* @param element The element to search for
|
||||
* @param node_idx Sub-tree to search in
|
||||
* @param level Current depth in the tree
|
||||
* @return New root node index of the sub-tree processed
|
||||
*/
|
||||
size_t RemoveRecursive(const T &element, size_t node_idx, int level)
|
||||
{
|
||||
/* Node reference */
|
||||
node &n = this->nodes[node_idx];
|
||||
|
||||
if (n.element == element) {
|
||||
/* Remove this one */
|
||||
this->free_list.push_back(node_idx);
|
||||
if (n.left == INVALID_NODE && n.right == INVALID_NODE) {
|
||||
/* Simple case, leaf, new child node for parent is "none" */
|
||||
return INVALID_NODE;
|
||||
} else {
|
||||
/* Complex case, rebuild the sub-tree */
|
||||
std::vector<T> subtree_elements = this->FreeSubtree(node_idx);
|
||||
return this->BuildSubtree(subtree_elements.begin(), subtree_elements.end(), level);;
|
||||
}
|
||||
} else {
|
||||
/* Search in a sub-tree */
|
||||
/* Dimension index of current level */
|
||||
int dim = level % 2;
|
||||
/* Coordinate of element splitting at this node */
|
||||
CoordT nc = this->xyfunc(n.element, dim);
|
||||
/* Coordinate of the element being removed */
|
||||
CoordT ec = this->xyfunc(element, dim);
|
||||
/* Which side to remove from */
|
||||
size_t next = (ec < nc) ? n.left : n.right;
|
||||
assert(next != INVALID_NODE); // node must exist somewhere and must be found before a leaf is reached
|
||||
/* Descend */
|
||||
size_t new_branch = this->RemoveRecursive(element, next, level + 1);
|
||||
if (new_branch != next) {
|
||||
/* Vector may have been reallocated at this point, n and next are invalid */
|
||||
node &nn = this->nodes[node_idx];
|
||||
if (ec < nc) nn.left = new_branch; else nn.right = new_branch;
|
||||
}
|
||||
return node_idx;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
DistT ManhattanDistance(const T &element, CoordT x, CoordT y) const
|
||||
{
|
||||
return abs((DistT)this->xyfunc(element, 0) - (DistT)x) + abs((DistT)this->xyfunc(element, 1) - (DistT)y);
|
||||
}
|
||||
|
||||
/** A data element and its distance to a searched-for point */
|
||||
using node_distance = std::pair<T, DistT>;
|
||||
/** Ordering function for node_distance objects, elements with equal distance are ordered by less-than comparison */
|
||||
static node_distance SelectNearestNodeDistance(const node_distance &a, const node_distance &b)
|
||||
{
|
||||
if (a.second < b.second) return a;
|
||||
if (b.second < a.second) return b;
|
||||
if (a.first < b.first) return a;
|
||||
if (b.first < a.first) return b;
|
||||
NOT_REACHED(); // a.first == b.first: same element must not be inserted twice
|
||||
}
|
||||
/** Search a sub-tree for the element nearest to a given point */
|
||||
node_distance FindNearestRecursive(CoordT xy[2], size_t node_idx, int level) const
|
||||
{
|
||||
/* Dimension index of current level */
|
||||
int dim = level % 2;
|
||||
/* Node reference */
|
||||
const node &n = this->nodes[node_idx];
|
||||
|
||||
/* Coordinate of element splitting at this node */
|
||||
CoordT c = this->xyfunc(n.element, dim);
|
||||
/* This node's distance to target */
|
||||
DistT thisdist = ManhattanDistance(n.element, xy[0], xy[1]);
|
||||
/* Assume this node is the best choice for now */
|
||||
node_distance best = std::make_pair(n.element, thisdist);
|
||||
|
||||
/* Next node to visit */
|
||||
size_t next = (xy[dim] < c) ? n.left : n.right;
|
||||
if (next != INVALID_NODE) {
|
||||
/* Check if there is a better node down the tree */
|
||||
best = SelectNearestNodeDistance(best, this->FindNearestRecursive(xy, next, level + 1));
|
||||
}
|
||||
|
||||
/* Check if the distance from current best is worse than distance from target to splitting line,
|
||||
* if it is we also need to check the other side of the split. */
|
||||
size_t opposite = (xy[dim] >= c) ? n.left : n.right; // reverse of above
|
||||
if (opposite != INVALID_NODE && best.second >= abs((int)xy[dim] - (int)c)) {
|
||||
node_distance other_candidate = this->FindNearestRecursive(xy, opposite, level + 1);
|
||||
best = SelectNearestNodeDistance(best, other_candidate);
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
template <typename Outputter>
|
||||
void FindContainedRecursive(CoordT p1[2], CoordT p2[2], size_t node_idx, int level, Outputter outputter) const
|
||||
{
|
||||
/* Dimension index of current level */
|
||||
int dim = level % 2;
|
||||
/* Node reference */
|
||||
const node &n = this->nodes[node_idx];
|
||||
|
||||
/* Coordinate of element splitting at this node */
|
||||
CoordT ec = this->xyfunc(n.element, dim);
|
||||
/* Opposite coordinate of element */
|
||||
CoordT oc = this->xyfunc(n.element, 1 - dim);
|
||||
|
||||
/* Test if this element is within rectangle */
|
||||
if (ec >= p1[dim] && ec < p2[dim] && oc >= p1[1 - dim] && oc < p2[1 - dim]) outputter(n.element);
|
||||
|
||||
/* Recurse left if part of rectangle is left of split */
|
||||
if (p1[dim] < ec && n.left != INVALID_NODE) this->FindContainedRecursive(p1, p2, n.left, level + 1, outputter);
|
||||
|
||||
/* Recurse right if part of rectangle is right of split */
|
||||
if (p2[dim] > ec && n.right != INVALID_NODE) this->FindContainedRecursive(p1, p2, n.right, level + 1, outputter);
|
||||
}
|
||||
|
||||
/** Debugging function, counts number of occurrences of an element regardless of its correct position in the tree */
|
||||
size_t CountValue(const T &element, size_t node_idx) const
|
||||
{
|
||||
if (node_idx == INVALID_NODE) return 0;
|
||||
const node &n = this->nodes[node_idx];
|
||||
return CountValue(element, n.left) + CountValue(element, n.right) + ((n.element == element) ? 1 : 0);
|
||||
}
|
||||
|
||||
void IncrementUnbalanced(size_t amount = 1)
|
||||
{
|
||||
this->unbalanced += amount;
|
||||
}
|
||||
|
||||
/** Check if the entire tree is in need of rebuilding */
|
||||
bool IsUnbalanced()
|
||||
{
|
||||
size_t count = this->Count();
|
||||
if (count < 8) return false;
|
||||
return this->unbalanced > this->Count() / 4;
|
||||
}
|
||||
|
||||
/** Verify that the invariant is true for a sub-tree, assert if not */
|
||||
void CheckInvariant(size_t node_idx, int level, CoordT min_x, CoordT max_x, CoordT min_y, CoordT max_y)
|
||||
{
|
||||
if (node_idx == INVALID_NODE) return;
|
||||
|
||||
const node &n = this->nodes[node_idx];
|
||||
CoordT cx = this->xyfunc(n.element, 0);
|
||||
CoordT cy = this->xyfunc(n.element, 1);
|
||||
|
||||
assert(cx >= min_x);
|
||||
assert(cx < max_x);
|
||||
assert(cy >= min_y);
|
||||
assert(cy < max_y);
|
||||
|
||||
if (level % 2 == 0) {
|
||||
// split in dimension 0 = x
|
||||
CheckInvariant(n.left, level + 1, min_x, cx, min_y, max_y);
|
||||
CheckInvariant(n.right, level + 1, cx, max_x, min_y, max_y);
|
||||
} else {
|
||||
// split in dimension 1 = y
|
||||
CheckInvariant(n.left, level + 1, min_x, max_x, min_y, cy);
|
||||
CheckInvariant(n.right, level + 1, min_x, max_x, cy, max_y);
|
||||
}
|
||||
}
|
||||
|
||||
/** Verify the invariant for the entire tree, does nothing unless KDTREE_DEBUG is defined */
|
||||
void CheckInvariant()
|
||||
{
|
||||
#ifdef KDTREE_DEBUG
|
||||
CheckInvariant(this->root, 0, std::numeric_limits<CoordT>::min(), std::numeric_limits<CoordT>::max(), std::numeric_limits<CoordT>::min(), std::numeric_limits<CoordT>::max());
|
||||
#endif
|
||||
}
|
||||
|
||||
public:
|
||||
/** Construct a new Kdtree with the given xyfunc */
|
||||
Kdtree(TxyFunc xyfunc) : root(INVALID_NODE), xyfunc(xyfunc), unbalanced(0) { }
|
||||
|
||||
/**
|
||||
* Clear and rebuild the tree from a new sequence of elements,
|
||||
* @tparam It Iterator type for element sequence.
|
||||
* @param begin First element in sequence.
|
||||
* @param end One past last element in sequence.
|
||||
*/
|
||||
template <typename It>
|
||||
void Build(It begin, It end)
|
||||
{
|
||||
this->nodes.clear();
|
||||
this->free_list.clear();
|
||||
this->unbalanced = 0;
|
||||
if (begin == end) return;
|
||||
this->nodes.reserve(end - begin);
|
||||
|
||||
this->root = this->BuildSubtree(begin, end, 0);
|
||||
CheckInvariant();
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstruct the tree with the same elements, letting it be fully balanced.
|
||||
*/
|
||||
void Rebuild()
|
||||
{
|
||||
this->Rebuild(NULL, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a single element in the tree.
|
||||
* Repeatedly inserting single elements may cause the tree to become unbalanced.
|
||||
* Undefined behaviour if the element already exists in the tree.
|
||||
*/
|
||||
void Insert(const T &element)
|
||||
{
|
||||
if (this->Count() == 0) {
|
||||
this->root = this->AddNode(element);
|
||||
} else {
|
||||
if (!this->IsUnbalanced() || !this->Rebuild(&element, NULL)) {
|
||||
this->InsertRecursive(element, this->root, 0);
|
||||
this->IncrementUnbalanced();
|
||||
}
|
||||
CheckInvariant();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a single element from the tree, if it exists.
|
||||
* Since elements are stored in interior nodes as well as leaf nodes, removing one may
|
||||
* require a larger sub-tree to be re-built. Because of this, worst case run time is
|
||||
* as bad as a full tree rebuild.
|
||||
*/
|
||||
void Remove(const T &element)
|
||||
{
|
||||
size_t count = this->Count();
|
||||
if (count == 0) return;
|
||||
if (!this->IsUnbalanced() || !this->Rebuild(NULL, &element)) {
|
||||
/* If the removed element is the root node, this modifies this->root */
|
||||
this->root = this->RemoveRecursive(element, this->root, 0);
|
||||
this->IncrementUnbalanced();
|
||||
}
|
||||
CheckInvariant();
|
||||
}
|
||||
|
||||
/** Get number of elements stored in tree */
|
||||
size_t Count() const
|
||||
{
|
||||
assert(this->free_list.size() <= this->nodes.size());
|
||||
return this->nodes.size() - this->free_list.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the element closest to given coordinate, in Manhattan distance.
|
||||
* For multiple elements with the same distance, the one comparing smaller with
|
||||
* a less-than comparison is chosen.
|
||||
*/
|
||||
T FindNearest(CoordT x, CoordT y) const
|
||||
{
|
||||
assert(this->Count() > 0);
|
||||
|
||||
CoordT xy[2] = { x, y };
|
||||
return this->FindNearestRecursive(xy, this->root, 0).first;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all items contained within the given rectangle.
|
||||
* @note Start coordinates are inclusive, end coordinates are exclusive. x1<x2 && y1<y2 is a precondition.
|
||||
* @param x1 Start first coordinate, points found are greater or equals to this.
|
||||
* @param y1 Start second coordinate, points found are greater or equals to this.
|
||||
* @param x2 End first coordinate, points found are less than this.
|
||||
* @param y2 End second coordinate, points found are less than this.
|
||||
* @param outputter Callback used to return values from the search.
|
||||
*/
|
||||
template <typename Outputter>
|
||||
void FindContained(CoordT x1, CoordT y1, CoordT x2, CoordT y2, Outputter outputter) const
|
||||
{
|
||||
assert(x1 < x2);
|
||||
assert(y1 < y2);
|
||||
|
||||
if (this->Count() == 0) return;
|
||||
|
||||
CoordT p1[2] = { x1, y1 };
|
||||
CoordT p2[2] = { x2, y2 };
|
||||
this->FindContainedRecursive(p1, p2, this->root, 0, outputter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all items contained within the given rectangle.
|
||||
* @note End coordinates are exclusive, x1<x2 && y1<y2 is a precondition.
|
||||
*/
|
||||
std::vector<T> FindContained(CoordT x1, CoordT y1, CoordT x2, CoordT y2) const
|
||||
{
|
||||
std::vector<T> result;
|
||||
this->FindContained(x1, y1, x2, y2, [&result](T e) {result.push_back(e); });
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
#endif
|
@@ -247,9 +247,9 @@ static inline T Delta(const T a, const T b)
|
||||
* @return True if the value is in the interval, false else.
|
||||
*/
|
||||
template <typename T>
|
||||
static inline bool IsInsideBS(const T x, const uint base, const uint size)
|
||||
static inline bool IsInsideBS(const T x, const size_t base, const size_t size)
|
||||
{
|
||||
return (uint)(x - base) < size;
|
||||
return (size_t)(x - base) < size;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -263,9 +263,9 @@ static inline bool IsInsideBS(const T x, const uint base, const uint size)
|
||||
* @see IsInsideBS()
|
||||
*/
|
||||
template <typename T>
|
||||
static inline bool IsInsideMM(const T x, const uint min, const uint max)
|
||||
static inline bool IsInsideMM(const T x, const size_t min, const size_t max)
|
||||
{
|
||||
return (uint)(x - min) < (max - min);
|
||||
return (size_t)(x - min) < (max - min);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -21,8 +21,8 @@
|
||||
/* virtual */ PoolBase::~PoolBase()
|
||||
{
|
||||
PoolVector *pools = PoolBase::GetPools();
|
||||
pools->Erase(pools->Find(this));
|
||||
if (pools->Length() == 0) delete pools;
|
||||
pools->erase(std::find(pools->begin(), pools->end(), this));
|
||||
if (pools->size() == 0) delete pools;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,10 +31,7 @@
|
||||
*/
|
||||
/* static */ void PoolBase::Clean(PoolType pt)
|
||||
{
|
||||
PoolVector *pools = PoolBase::GetPools();
|
||||
PoolBase **end = pools->End();
|
||||
for (PoolBase **ppool = pools->Begin(); ppool != end; ppool++) {
|
||||
PoolBase *pool = *ppool;
|
||||
for (PoolBase *pool : *PoolBase::GetPools()) {
|
||||
if (pool->type & pt) pool->CleanPool();
|
||||
}
|
||||
}
|
||||
|
@@ -26,7 +26,7 @@ enum PoolType {
|
||||
};
|
||||
DECLARE_ENUM_AS_BIT_SET(PoolType)
|
||||
|
||||
typedef SmallVector<struct PoolBase *, 4> PoolVector; ///< Vector of pointers to PoolBase
|
||||
typedef std::vector<struct PoolBase *> PoolVector; ///< Vector of pointers to PoolBase
|
||||
|
||||
/** Base class for base of all pools. */
|
||||
struct PoolBase {
|
||||
@@ -50,7 +50,7 @@ struct PoolBase {
|
||||
*/
|
||||
PoolBase(PoolType pt) : type(pt)
|
||||
{
|
||||
*PoolBase::GetPools()->Append() = this;
|
||||
PoolBase::GetPools()->push_back(this);
|
||||
}
|
||||
|
||||
virtual ~PoolBase();
|
||||
|
@@ -27,6 +27,7 @@ struct SmallPair {
|
||||
|
||||
/** Initializes this Pair with data */
|
||||
inline SmallPair(const T &first, const U &second) : first(first), second(second) { }
|
||||
SmallPair() = default;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -38,8 +39,8 @@ struct SmallPair {
|
||||
*
|
||||
* @see SmallVector
|
||||
*/
|
||||
template <typename T, typename U, uint S = 16>
|
||||
struct SmallMap : SmallVector<SmallPair<T, U>, S> {
|
||||
template <typename T, typename U>
|
||||
struct SmallMap : std::vector<SmallPair<T, U> > {
|
||||
typedef ::SmallPair<T, U> Pair;
|
||||
typedef Pair *iterator;
|
||||
typedef const Pair *const_iterator;
|
||||
@@ -54,12 +55,13 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
|
||||
* @param key key to find
|
||||
* @return &Pair(key, data) if found, this->End() if not
|
||||
*/
|
||||
inline const Pair *Find(const T &key) const
|
||||
inline typename std::vector<Pair>::const_iterator Find(const T &key) const
|
||||
{
|
||||
for (uint i = 0; i < this->items; i++) {
|
||||
if (key == this->data[i].first) return &this->data[i];
|
||||
typename std::vector<Pair>::const_iterator it;
|
||||
for (it = std::vector<Pair>::begin(); it != std::vector<Pair>::end(); it++) {
|
||||
if (key == it->first) return it;
|
||||
}
|
||||
return this->End();
|
||||
return it;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -69,18 +71,39 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
|
||||
*/
|
||||
inline Pair *Find(const T &key)
|
||||
{
|
||||
for (uint i = 0; i < this->items; i++) {
|
||||
if (key == this->data[i].first) return &this->data[i];
|
||||
for (uint i = 0; i < std::vector<Pair>::size(); i++) {
|
||||
if (key == std::vector<Pair>::operator[](i).first) return &std::vector<Pair>::operator[](i);
|
||||
}
|
||||
return this->End();
|
||||
}
|
||||
|
||||
inline const Pair *End() const
|
||||
{
|
||||
return std::vector<Pair>::data() + std::vector<Pair>::size();
|
||||
}
|
||||
|
||||
inline Pair *End()
|
||||
{
|
||||
return std::vector<Pair>::data() + std::vector<Pair>::size();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests whether a key is assigned in this map.
|
||||
* @param key key to test
|
||||
* @return true iff the item is present
|
||||
*/
|
||||
inline bool Contains(const T &key) const
|
||||
{
|
||||
return this->Find(key) != std::vector<Pair>::end();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether a key is assigned in this map.
|
||||
* @param key key to test
|
||||
* @return true iff the item is present
|
||||
*/
|
||||
inline bool Contains(const T &key)
|
||||
{
|
||||
return this->Find(key) != this->End();
|
||||
}
|
||||
@@ -92,8 +115,9 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
|
||||
*/
|
||||
inline void Erase(Pair *pair)
|
||||
{
|
||||
assert(pair >= this->Begin() && pair < this->End());
|
||||
*pair = this->data[--this->items];
|
||||
assert(pair >= std::vector<Pair>::data() && pair < this->End());
|
||||
auto distance = pair - std::vector<Pair>::data();
|
||||
std::vector<Pair>::erase(std::vector<Pair>::begin() + distance);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -104,13 +128,11 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
|
||||
*/
|
||||
inline bool Erase(const T &key)
|
||||
{
|
||||
for (uint i = 0; i < this->items; i++) {
|
||||
if (key == this->data[i].first) {
|
||||
this->data[i] = this->data[--this->items];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
auto *pair = this->Find(key);
|
||||
if (pair == this->End()) return false;
|
||||
|
||||
this->Erase(pair);
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -122,9 +144,7 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
|
||||
inline bool Insert(const T &key, const U &data)
|
||||
{
|
||||
if (this->Contains(key)) return false;
|
||||
Pair *n = this->Append();
|
||||
n->first = key;
|
||||
n->second = data;
|
||||
std::vector<Pair>::emplace_back(key, data);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -136,17 +156,18 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> {
|
||||
*/
|
||||
inline U &operator[](const T &key)
|
||||
{
|
||||
for (uint i = 0; i < this->items; i++) {
|
||||
if (key == this->data[i].first) return this->data[i].second;
|
||||
for (uint i = 0; i < std::vector<Pair>::size(); i++) {
|
||||
if (key == std::vector<Pair>::operator[](i).first) return std::vector<Pair>::operator[](i).second;
|
||||
}
|
||||
Pair *n = this->Append();
|
||||
n->first = key;
|
||||
return n->second;
|
||||
/*C++17: Pair &n = */ std::vector<Pair>::emplace_back();
|
||||
Pair &n = std::vector<Pair>::back();
|
||||
n.first = key;
|
||||
return n.second;
|
||||
}
|
||||
|
||||
inline void SortByKey()
|
||||
{
|
||||
QSortT(this->Begin(), this->items, KeySorter);
|
||||
QSortT(std::vector<Pair>::data(), std::vector<Pair>::size(), KeySorter);
|
||||
}
|
||||
|
||||
static int CDECL KeySorter(const Pair *a, const Pair *b)
|
||||
|
@@ -13,7 +13,7 @@
|
||||
#define SMALLSTACK_TYPE_HPP
|
||||
|
||||
#include "smallvec_type.hpp"
|
||||
#include "../thread/thread.h"
|
||||
#include <mutex>
|
||||
|
||||
/**
|
||||
* A simplified pool which stores values instead of pointers and doesn't
|
||||
@@ -23,15 +23,14 @@
|
||||
template<typename Titem, typename Tindex, Tindex Tgrowth_step, Tindex Tmax_size>
|
||||
class SimplePool {
|
||||
public:
|
||||
inline SimplePool() : first_unused(0), first_free(0), mutex(ThreadMutex::New()) {}
|
||||
inline ~SimplePool() { delete this->mutex; }
|
||||
inline SimplePool() : first_unused(0), first_free(0) {}
|
||||
|
||||
/**
|
||||
* Get the mutex. We don't lock the mutex in the pool methods as the
|
||||
* SmallStack isn't necessarily in a consistent state after each method.
|
||||
* @return Mutex.
|
||||
*/
|
||||
inline ThreadMutex *GetMutex() { return this->mutex; }
|
||||
inline std::mutex &GetMutex() { return this->mutex; }
|
||||
|
||||
/**
|
||||
* Get the item at position index.
|
||||
@@ -73,8 +72,8 @@ private:
|
||||
if (!this->data[index].valid) return index;
|
||||
}
|
||||
|
||||
if (index >= this->data.Length() && index < Tmax_size) {
|
||||
this->data.Resize(index + 1);
|
||||
if (index >= this->data.size() && index < Tmax_size) {
|
||||
this->data.resize(index + 1);
|
||||
}
|
||||
return index;
|
||||
}
|
||||
@@ -86,8 +85,8 @@ private:
|
||||
Tindex first_unused;
|
||||
Tindex first_free;
|
||||
|
||||
ThreadMutex *mutex;
|
||||
SmallVector<SimplePoolPoolItem, Tgrowth_step> data;
|
||||
std::mutex mutex;
|
||||
std::vector<SimplePoolPoolItem> data;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -106,6 +105,7 @@ struct SmallStackItem {
|
||||
*/
|
||||
inline SmallStackItem(const Titem &value, Tindex next) :
|
||||
next(next), value(value) {}
|
||||
SmallStackItem() = default;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -195,7 +195,7 @@ public:
|
||||
inline void Push(const Titem &item)
|
||||
{
|
||||
if (this->value != Tinvalid) {
|
||||
ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
|
||||
std::lock_guard<std::mutex> lock(SmallStack::GetPool().GetMutex());
|
||||
Tindex new_item = SmallStack::GetPool().Create();
|
||||
if (new_item != Tmax_size) {
|
||||
PooledSmallStack &pushed = SmallStack::GetPool().Get(new_item);
|
||||
@@ -218,7 +218,7 @@ public:
|
||||
if (this->next == Tmax_size) {
|
||||
this->value = Tinvalid;
|
||||
} else {
|
||||
ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
|
||||
std::lock_guard<std::mutex> lock(SmallStack::GetPool().GetMutex());
|
||||
PooledSmallStack &popped = SmallStack::GetPool().Get(this->next);
|
||||
this->value = popped.value;
|
||||
if (popped.branch_count == 0) {
|
||||
@@ -257,7 +257,7 @@ public:
|
||||
{
|
||||
if (item == Tinvalid || item == this->value) return true;
|
||||
if (this->next != Tmax_size) {
|
||||
ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
|
||||
std::lock_guard<std::mutex> lock(SmallStack::GetPool().GetMutex());
|
||||
const SmallStack *in_list = this;
|
||||
do {
|
||||
in_list = static_cast<const SmallStack *>(
|
||||
@@ -281,7 +281,7 @@ protected:
|
||||
inline void Branch()
|
||||
{
|
||||
if (this->next != Tmax_size) {
|
||||
ThreadMutexLocker lock(SmallStack::GetPool().GetMutex());
|
||||
std::lock_guard<std::mutex> lock(SmallStack::GetPool().GetMutex());
|
||||
++(SmallStack::GetPool().Get(this->next).branch_count);
|
||||
}
|
||||
}
|
||||
|
@@ -14,375 +14,60 @@
|
||||
|
||||
#include "alloc_func.hpp"
|
||||
#include "mem_func.hpp"
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
/**
|
||||
* Simple vector template class.
|
||||
* Helper function to append an item to a vector if it is not already contained
|
||||
* Consider using std::set, std::unordered_set or std::flat_set in new code
|
||||
*
|
||||
* @note There are no asserts in the class so you have
|
||||
* to care about that you grab an item which is
|
||||
* inside the list.
|
||||
* @param vec A reference to the vector to be extended
|
||||
* @param item Reference to the item to be copy-constructed if not found
|
||||
*
|
||||
* @tparam T The type of the items stored
|
||||
* @tparam S The steps of allocation
|
||||
* @return Whether the item was already present
|
||||
*/
|
||||
template <typename T, uint S>
|
||||
class SmallVector {
|
||||
protected:
|
||||
T *data; ///< The pointer to the first item
|
||||
uint items; ///< The number of items stored
|
||||
uint capacity; ///< The available space for storing items
|
||||
template <typename T>
|
||||
inline bool include(std::vector<T>& vec, const T &item)
|
||||
{
|
||||
const bool is_member = std::find(vec.begin(), vec.end(), item) != vec.end();
|
||||
if (!is_member) vec.emplace_back(item);
|
||||
return is_member;
|
||||
}
|
||||
|
||||
public:
|
||||
SmallVector() : data(NULL), items(0), capacity(0) { }
|
||||
/**
|
||||
* Helper function to get the index of an item
|
||||
* Consider using std::set, std::unordered_set or std::flat_set in new code
|
||||
*
|
||||
* @param vec A reference to the vector to be extended
|
||||
* @param item Reference to the item to be search for
|
||||
*
|
||||
* @return Index of element if found, otherwise -1
|
||||
*/
|
||||
template <typename T>
|
||||
int find_index(std::vector<T> const& vec, T const& item)
|
||||
{
|
||||
auto const it = std::find(vec.begin(), vec.end(), item);
|
||||
if (it != vec.end()) return it - vec.begin();
|
||||
|
||||
/**
|
||||
* Copy constructor.
|
||||
* @param other The other vector to copy.
|
||||
*/
|
||||
SmallVector(const SmallVector &other) : data(NULL), items(0), capacity(0)
|
||||
{
|
||||
this->Assign(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic copy constructor.
|
||||
* @param other The other vector to copy.
|
||||
*/
|
||||
template <uint X>
|
||||
SmallVector(const SmallVector<T, X> &other) : data(NULL), items(0), capacity(0)
|
||||
{
|
||||
this->Assign(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assignment.
|
||||
* @param other The other vector to assign.
|
||||
*/
|
||||
SmallVector &operator=(const SmallVector &other)
|
||||
{
|
||||
this->Assign(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic assignment.
|
||||
* @param other The other vector to assign.
|
||||
*/
|
||||
template <uint X>
|
||||
SmallVector &operator=(const SmallVector<T, X> &other)
|
||||
{
|
||||
this->Assign(other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
~SmallVector()
|
||||
{
|
||||
free(this->data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assign items from other vector.
|
||||
*/
|
||||
template <uint X>
|
||||
inline void Assign(const SmallVector<T, X> &other)
|
||||
{
|
||||
if ((const void *)&other == (void *)this) return;
|
||||
|
||||
this->Clear();
|
||||
if (other.Length() > 0) MemCpyT<T>(this->Append(other.Length()), other.Begin(), other.Length());
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all items from the list.
|
||||
*/
|
||||
inline void Clear()
|
||||
{
|
||||
/* In fact we just reset the item counter avoiding the need to
|
||||
* probably reallocate the same amount of memory the list was
|
||||
* previously using. */
|
||||
this->items = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all items from the list and free allocated memory.
|
||||
*/
|
||||
inline void Reset()
|
||||
{
|
||||
this->items = 0;
|
||||
this->capacity = 0;
|
||||
free(data);
|
||||
data = NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compact the list down to the smallest block size boundary.
|
||||
*/
|
||||
inline void Compact()
|
||||
{
|
||||
uint capacity = Align(this->items, S);
|
||||
if (capacity >= this->capacity) return;
|
||||
|
||||
this->capacity = capacity;
|
||||
this->data = ReallocT(this->data, this->capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Append an item and return it.
|
||||
* @param to_add the number of items to append
|
||||
* @return pointer to newly allocated item
|
||||
*/
|
||||
inline T *Append(uint to_add = 1)
|
||||
{
|
||||
uint begin = this->items;
|
||||
this->items += to_add;
|
||||
|
||||
if (this->items > this->capacity) {
|
||||
this->capacity = Align(this->items, S);
|
||||
this->data = ReallocT(this->data, this->capacity);
|
||||
}
|
||||
|
||||
return &this->data[begin];
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the size of the vector, effectively truncating items from the end or appending uninitialised ones.
|
||||
* @param num_items Target size.
|
||||
*/
|
||||
inline void Resize(uint num_items)
|
||||
{
|
||||
this->items = num_items;
|
||||
|
||||
if (this->items > this->capacity) {
|
||||
this->capacity = Align(this->items, S);
|
||||
this->data = ReallocT(this->data, this->capacity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a new item at a specific position into the vector, moving all following items.
|
||||
* @param item Position at which the new item should be inserted
|
||||
* @return pointer to the new item
|
||||
*/
|
||||
inline T *Insert(T *item)
|
||||
{
|
||||
assert(item >= this->Begin() && item <= this->End());
|
||||
|
||||
size_t to_move = this->End() - item;
|
||||
size_t start = item - this->Begin();
|
||||
|
||||
this->Append();
|
||||
if (to_move > 0) MemMoveT(this->Begin() + start + 1, this->Begin() + start, to_move);
|
||||
return this->Begin() + start;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the first occurrence of an item.
|
||||
* The '!=' operator of T is used for comparison.
|
||||
* @param item Item to search for
|
||||
* @return The position of the item, or End() when not present
|
||||
*/
|
||||
inline const T *Find(const T &item) const
|
||||
{
|
||||
const T *pos = this->Begin();
|
||||
const T *end = this->End();
|
||||
while (pos != end && *pos != item) pos++;
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the first occurrence of an item.
|
||||
* The '!=' operator of T is used for comparison.
|
||||
* @param item Item to search for
|
||||
* @return The position of the item, or End() when not present
|
||||
*/
|
||||
inline T *Find(const T &item)
|
||||
{
|
||||
T *pos = this->Begin();
|
||||
const T *end = this->End();
|
||||
while (pos != end && *pos != item) pos++;
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for the first occurrence of an item.
|
||||
* The '!=' operator of T is used for comparison.
|
||||
* @param item Item to search for
|
||||
* @return The position of the item, or -1 when not present
|
||||
*/
|
||||
inline int FindIndex(const T &item) const
|
||||
{
|
||||
int index = 0;
|
||||
const T *pos = this->Begin();
|
||||
const T *end = this->End();
|
||||
while (pos != end && *pos != item) {
|
||||
pos++;
|
||||
index++;
|
||||
}
|
||||
return pos == end ? -1 : index;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether a item is present in the vector.
|
||||
* The '!=' operator of T is used for comparison.
|
||||
* @param item Item to test for
|
||||
* @return true iff the item is present
|
||||
*/
|
||||
inline bool Contains(const T &item) const
|
||||
{
|
||||
return this->Find(item) != this->End();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes given item from this vector
|
||||
* @param item item to remove
|
||||
* @note it has to be pointer to item in this map. It is overwritten by the last item.
|
||||
*/
|
||||
inline void Erase(T *item)
|
||||
{
|
||||
assert(item >= this->Begin() && item < this->End());
|
||||
*item = this->data[--this->items];
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove items from the vector while preserving the order of other items.
|
||||
* @param pos First item to remove.
|
||||
* @param count Number of consecutive items to remove.
|
||||
*/
|
||||
void ErasePreservingOrder(uint pos, uint count = 1)
|
||||
{
|
||||
ErasePreservingOrder(this->data + pos, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove items from the vector while preserving the order of other items.
|
||||
* @param item First item to remove.
|
||||
* @param count Number of consecutive items to remove.
|
||||
*/
|
||||
inline void ErasePreservingOrder(T *item, uint count = 1)
|
||||
{
|
||||
if (count == 0) return;
|
||||
assert(item >= this->Begin());
|
||||
assert(item + count <= this->End());
|
||||
|
||||
this->items -= count;
|
||||
ptrdiff_t to_move = this->End() - item;
|
||||
if (to_move > 0) MemMoveT(item, item + count, to_move);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether a item is present in the vector, and appends it to the end if not.
|
||||
* The '!=' operator of T is used for comparison.
|
||||
* @param item Item to test for
|
||||
* @return true iff the item is was already present
|
||||
*/
|
||||
inline bool Include(const T &item)
|
||||
{
|
||||
bool is_member = this->Contains(item);
|
||||
if (!is_member) *this->Append() = item;
|
||||
return is_member;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the number of items in the list.
|
||||
*
|
||||
* @return The number of items in the list.
|
||||
*/
|
||||
inline uint Length() const
|
||||
{
|
||||
return this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pointer to the first item (const)
|
||||
*
|
||||
* @return the pointer to the first item
|
||||
*/
|
||||
inline const T *Begin() const
|
||||
{
|
||||
return this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pointer to the first item
|
||||
*
|
||||
* @return the pointer to the first item
|
||||
*/
|
||||
inline T *Begin()
|
||||
{
|
||||
return this->data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pointer behind the last valid item (const)
|
||||
*
|
||||
* @return the pointer behind the last valid item
|
||||
*/
|
||||
inline const T *End() const
|
||||
{
|
||||
return &this->data[this->items];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pointer behind the last valid item
|
||||
*
|
||||
* @return the pointer behind the last valid item
|
||||
*/
|
||||
inline T *End()
|
||||
{
|
||||
return &this->data[this->items];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pointer to item "number" (const)
|
||||
*
|
||||
* @param index the position of the item
|
||||
* @return the pointer to the item
|
||||
*/
|
||||
inline const T *Get(uint index) const
|
||||
{
|
||||
/* Allow access to the 'first invalid' item */
|
||||
assert(index <= this->items);
|
||||
return &this->data[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pointer to item "number"
|
||||
*
|
||||
* @param index the position of the item
|
||||
* @return the pointer to the item
|
||||
*/
|
||||
inline T *Get(uint index)
|
||||
{
|
||||
/* Allow access to the 'first invalid' item */
|
||||
assert(index <= this->items);
|
||||
return &this->data[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get item "number" (const)
|
||||
*
|
||||
* @param index the position of the item
|
||||
* @return the item
|
||||
*/
|
||||
inline const T &operator[](uint index) const
|
||||
{
|
||||
assert(index < this->items);
|
||||
return this->data[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* Get item "number"
|
||||
*
|
||||
* @param index the position of the item
|
||||
* @return the item
|
||||
*/
|
||||
inline T &operator[](uint index)
|
||||
{
|
||||
assert(index < this->items);
|
||||
return this->data[index];
|
||||
}
|
||||
};
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to append N default-constructed elements and get a pointer to the first new element
|
||||
* Consider using std::back_inserter in new code
|
||||
*
|
||||
* @param vec A reference to the vector to be extended
|
||||
* @param num Number of elements to be default-constructed
|
||||
*
|
||||
* @return Pointer to the first new element
|
||||
*/
|
||||
template <typename T>
|
||||
T* grow(std::vector<T>& vec, std::size_t num)
|
||||
{
|
||||
std::size_t const pos = vec.size();
|
||||
vec.resize(pos + num);
|
||||
return vec.data() + pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple vector template class, with automatic free.
|
||||
@@ -392,10 +77,9 @@ public:
|
||||
* inside the list.
|
||||
*
|
||||
* @param T The type of the items stored, must be a pointer
|
||||
* @param S The steps of allocation
|
||||
*/
|
||||
template <typename T, uint S>
|
||||
class AutoFreeSmallVector : public SmallVector<T, S> {
|
||||
template <typename T>
|
||||
class AutoFreeSmallVector : public std::vector<T> {
|
||||
public:
|
||||
~AutoFreeSmallVector()
|
||||
{
|
||||
@@ -407,11 +91,11 @@ public:
|
||||
*/
|
||||
inline void Clear()
|
||||
{
|
||||
for (uint i = 0; i < this->items; i++) {
|
||||
free(this->data[i]);
|
||||
for (T p : *this) {
|
||||
free(p);
|
||||
}
|
||||
|
||||
this->items = 0;
|
||||
std::vector<T>::clear();
|
||||
}
|
||||
};
|
||||
|
||||
@@ -423,10 +107,9 @@ public:
|
||||
* inside the list.
|
||||
*
|
||||
* @param T The type of the items stored, must be a pointer
|
||||
* @param S The steps of allocation
|
||||
*/
|
||||
template <typename T, uint S>
|
||||
class AutoDeleteSmallVector : public SmallVector<T, S> {
|
||||
template <typename T>
|
||||
class AutoDeleteSmallVector : public std::vector<T> {
|
||||
public:
|
||||
~AutoDeleteSmallVector()
|
||||
{
|
||||
@@ -438,14 +121,14 @@ public:
|
||||
*/
|
||||
inline void Clear()
|
||||
{
|
||||
for (uint i = 0; i < this->items; i++) {
|
||||
delete this->data[i];
|
||||
for (T p : *this) {
|
||||
delete p;
|
||||
}
|
||||
|
||||
this->items = 0;
|
||||
std::vector<T>::clear();
|
||||
}
|
||||
};
|
||||
|
||||
typedef AutoFreeSmallVector<char*, 4> StringList; ///< Type for a list of strings.
|
||||
typedef AutoFreeSmallVector<char*> StringList; ///< Type for a list of strings.
|
||||
|
||||
#endif /* SMALLVEC_TYPE_HPP */
|
||||
|
@@ -25,7 +25,7 @@
|
||||
* @param desc Sort descending.
|
||||
*/
|
||||
template <typename T>
|
||||
static inline void QSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
|
||||
static inline void QSortT(T *base, size_t num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
|
||||
{
|
||||
if (num < 2) return;
|
||||
|
||||
@@ -49,7 +49,7 @@ static inline void QSortT(T *base, uint num, int (CDECL *comparator)(const T*, c
|
||||
* @param desc Sort descending.
|
||||
*/
|
||||
template <typename T>
|
||||
static inline void GSortT(T *base, uint num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
|
||||
static inline void GSortT(T *base, size_t num, int (CDECL *comparator)(const T*, const T*), bool desc = false)
|
||||
{
|
||||
if (num < 2) return;
|
||||
|
||||
|
Reference in New Issue
Block a user