2015-05-23 01:04:09 +03:00
|
|
|
/*============================================================================
|
|
|
|
CMake - Cross Platform Makefile Generator
|
|
|
|
Copyright 2015 Stephen Kelly <steveire@gmail.com>
|
|
|
|
|
|
|
|
Distributed under the OSI-approved BSD License (the "License");
|
|
|
|
see accompanying file Copyright.txt for details.
|
|
|
|
|
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even the
|
|
|
|
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
See the License for more information.
|
|
|
|
============================================================================*/
|
|
|
|
#ifndef cmLinkedTree_h
|
|
|
|
#define cmLinkedTree_h
|
|
|
|
|
|
|
|
#include "cmStandardIncludes.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
@brief A adaptor for traversing a tree structure in a vector
|
|
|
|
|
|
|
|
This class is not intended to be wholly generic like a standard library
|
|
|
|
container adaptor. Mostly it exists to facilitate code sharing for the
|
|
|
|
needs of the cmState. For example, the Truncate() method is a specific
|
|
|
|
requirement of the cmState.
|
|
|
|
|
2015-11-25 18:23:05 +03:00
|
|
|
An empty cmLinkedTree provides a Root() method, and an Push() method,
|
2015-05-23 01:04:09 +03:00
|
|
|
each of which return iterators. A Tree can be built up by extending
|
|
|
|
from the root, and then extending from any other iterator.
|
|
|
|
|
|
|
|
An iterator resulting from this tree construction can be
|
|
|
|
forward-only-iterated toward the root. Extending the tree never
|
|
|
|
invalidates existing iterators.
|
|
|
|
*/
|
2016-05-16 17:34:04 +03:00
|
|
|
template <typename T>
|
2015-05-23 01:04:09 +03:00
|
|
|
class cmLinkedTree
|
|
|
|
{
|
|
|
|
typedef typename std::vector<T>::size_type PositionType;
|
|
|
|
typedef T* PointerType;
|
|
|
|
typedef T& ReferenceType;
|
2016-05-16 17:34:04 +03:00
|
|
|
|
2015-05-23 01:04:09 +03:00
|
|
|
public:
|
|
|
|
class iterator : public std::iterator<std::forward_iterator_tag, T>
|
|
|
|
{
|
|
|
|
friend class cmLinkedTree;
|
|
|
|
cmLinkedTree* Tree;
|
|
|
|
|
|
|
|
// The Position is always 'one past the end'.
|
|
|
|
PositionType Position;
|
|
|
|
|
|
|
|
iterator(cmLinkedTree* tree, PositionType pos)
|
2016-05-16 17:34:04 +03:00
|
|
|
: Tree(tree)
|
|
|
|
, Position(pos)
|
2015-05-23 01:04:09 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
iterator()
|
2016-06-27 23:44:16 +03:00
|
|
|
: Tree(CM_NULLPTR)
|
2016-05-16 17:34:04 +03:00
|
|
|
, Position(0)
|
2015-05-23 01:04:09 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void operator++()
|
|
|
|
{
|
|
|
|
assert(this->Tree);
|
|
|
|
assert(this->Tree->UpPositions.size() == this->Tree->Data.size());
|
|
|
|
assert(this->Position <= this->Tree->Data.size());
|
|
|
|
assert(this->Position > 0);
|
|
|
|
this->Position = this->Tree->UpPositions[this->Position - 1];
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerType operator->() const
|
|
|
|
{
|
|
|
|
assert(this->Tree);
|
|
|
|
assert(this->Tree->UpPositions.size() == this->Tree->Data.size());
|
|
|
|
assert(this->Position <= this->Tree->Data.size());
|
|
|
|
assert(this->Position > 0);
|
|
|
|
return this->Tree->GetPointer(this->Position - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
PointerType operator->()
|
|
|
|
{
|
|
|
|
assert(this->Tree);
|
|
|
|
assert(this->Tree->UpPositions.size() == this->Tree->Data.size());
|
|
|
|
assert(this->Position <= this->Tree->Data.size());
|
|
|
|
assert(this->Position > 0);
|
|
|
|
return this->Tree->GetPointer(this->Position - 1);
|
|
|
|
}
|
|
|
|
|
2015-06-08 21:09:55 +03:00
|
|
|
ReferenceType operator*() const
|
|
|
|
{
|
|
|
|
assert(this->Tree);
|
|
|
|
assert(this->Tree->UpPositions.size() == this->Tree->Data.size());
|
|
|
|
assert(this->Position <= this->Tree->Data.size());
|
|
|
|
assert(this->Position > 0);
|
|
|
|
return this->Tree->GetReference(this->Position - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
ReferenceType operator*()
|
|
|
|
{
|
|
|
|
assert(this->Tree);
|
|
|
|
assert(this->Tree->UpPositions.size() == this->Tree->Data.size());
|
|
|
|
assert(this->Position <= this->Tree->Data.size());
|
|
|
|
assert(this->Position > 0);
|
|
|
|
return this->Tree->GetReference(this->Position - 1);
|
|
|
|
}
|
|
|
|
|
2015-05-23 01:04:09 +03:00
|
|
|
bool operator==(iterator other) const
|
|
|
|
{
|
|
|
|
assert(this->Tree);
|
|
|
|
assert(this->Tree->UpPositions.size() == this->Tree->Data.size());
|
|
|
|
assert(this->Tree == other.Tree);
|
|
|
|
return this->Position == other.Position;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(iterator other) const
|
|
|
|
{
|
|
|
|
assert(this->Tree);
|
|
|
|
assert(this->Tree->UpPositions.size() == this->Tree->Data.size());
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsValid() const
|
|
|
|
{
|
2016-05-16 17:34:04 +03:00
|
|
|
if (!this->Tree) {
|
2015-05-23 01:04:09 +03:00
|
|
|
return false;
|
2016-05-16 17:34:04 +03:00
|
|
|
}
|
2015-05-23 01:04:09 +03:00
|
|
|
return this->Position <= this->Tree->Data.size();
|
|
|
|
}
|
2015-08-02 11:07:30 +03:00
|
|
|
|
|
|
|
bool StrictWeakOrdered(iterator other) const
|
|
|
|
{
|
|
|
|
assert(this->Tree);
|
|
|
|
assert(this->Tree == other.Tree);
|
|
|
|
return this->Position < other.Position;
|
|
|
|
}
|
2015-05-23 01:04:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
iterator Root() const
|
|
|
|
{
|
|
|
|
return iterator(const_cast<cmLinkedTree*>(this), 0);
|
|
|
|
}
|
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
iterator Push(iterator it) { return Push_impl(it, T()); }
|
2015-05-23 01:04:09 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
iterator Push(iterator it, T t) { return Push_impl(it, t); }
|
2015-05-23 01:04:09 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
bool IsLast(iterator it) { return it.Position == this->Data.size(); }
|
2015-11-24 22:42:20 +03:00
|
|
|
|
|
|
|
iterator Pop(iterator it)
|
2016-05-16 17:34:04 +03:00
|
|
|
{
|
2015-11-24 22:42:20 +03:00
|
|
|
assert(!this->Data.empty());
|
|
|
|
assert(this->UpPositions.size() == this->Data.size());
|
|
|
|
bool const isLast = this->IsLast(it);
|
|
|
|
++it;
|
|
|
|
// If this is the last entry then no other entry can refer
|
|
|
|
// to it so we can drop its storage.
|
2016-05-16 17:34:04 +03:00
|
|
|
if (isLast) {
|
2015-11-24 22:42:20 +03:00
|
|
|
this->Data.pop_back();
|
|
|
|
this->UpPositions.pop_back();
|
|
|
|
}
|
2016-05-16 17:34:04 +03:00
|
|
|
return it;
|
|
|
|
}
|
2015-11-24 22:42:20 +03:00
|
|
|
|
2015-05-23 01:04:09 +03:00
|
|
|
iterator Truncate()
|
|
|
|
{
|
|
|
|
assert(this->UpPositions.size() > 0);
|
|
|
|
this->UpPositions.erase(this->UpPositions.begin() + 1,
|
|
|
|
this->UpPositions.end());
|
|
|
|
assert(this->Data.size() > 0);
|
|
|
|
this->Data.erase(this->Data.begin() + 1, this->Data.end());
|
|
|
|
return iterator(this, 1);
|
|
|
|
}
|
|
|
|
|
2015-06-14 02:38:57 +03:00
|
|
|
void Clear()
|
|
|
|
{
|
|
|
|
this->UpPositions.clear();
|
|
|
|
this->Data.clear();
|
|
|
|
}
|
|
|
|
|
2015-05-23 01:04:09 +03:00
|
|
|
private:
|
2016-05-16 17:34:04 +03:00
|
|
|
T& GetReference(PositionType pos) { return this->Data[pos]; }
|
2015-05-23 01:04:09 +03:00
|
|
|
|
2016-05-16 17:34:04 +03:00
|
|
|
T* GetPointer(PositionType pos) { return &this->Data[pos]; }
|
2015-05-23 01:04:09 +03:00
|
|
|
|
2015-11-25 18:23:05 +03:00
|
|
|
iterator Push_impl(iterator it, T t)
|
2015-05-23 01:04:09 +03:00
|
|
|
{
|
|
|
|
assert(this->UpPositions.size() == this->Data.size());
|
|
|
|
assert(it.Position <= this->UpPositions.size());
|
|
|
|
this->UpPositions.push_back(it.Position);
|
|
|
|
this->Data.push_back(t);
|
|
|
|
return iterator(this, this->UpPositions.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<T> Data;
|
|
|
|
std::vector<PositionType> UpPositions;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|