Skip to content

Commit

Permalink
libcore: Taking pointers from PointerSet
Browse files Browse the repository at this point in the history
  • Loading branch information
skyjake committed Sep 1, 2019
1 parent 7728dd1 commit e47ac72
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions doomsday/libs/core/include/de/data/pointerset.h
Expand Up @@ -63,6 +63,7 @@ class DE_PUBLIC PointerSet
void remove(Pointer ptr);
bool contains(Pointer ptr) const;
void clear();
Pointer take();

PointerSet &operator = (PointerSet const &other);
PointerSet &operator = (PointerSet &&moved);
Expand Down Expand Up @@ -146,6 +147,9 @@ class PointerSetT : public PointerSet
inline const_iterator end() const {
return reinterpret_cast<const_iterator>(PointerSet::end());
}
inline Pointer take() {
return reinterpret_cast<Pointer>(PointerSet::take());
}
};

} // namespace de
Expand Down
16 changes: 16 additions & 0 deletions doomsday/libs/core/src/data/pointerset.cpp
Expand Up @@ -209,6 +209,22 @@ void PointerSet::clear()
_range = Rangeui16(_range.end, _range.end);
}
}

PointerSet::Pointer PointerSet::take()
{
if (isEmpty()) return nullptr;
Pointer ptr = nullptr;
// Make room at both ends.
if (_size - _range.end < _range.start)
{
std::swap(ptr, _pointers[--_range.end]);
}
else
{
std::swap(ptr, _pointers[_range.start++]);
}
return ptr;
}

PointerSet &PointerSet::operator = (PointerSet const &other)
{
Expand Down
4 changes: 4 additions & 0 deletions doomsday/tests/test_pointerset/main.cpp
Expand Up @@ -100,6 +100,10 @@ int main(int, char **)
pset.insert(b);
pset.insert(a);
printSet(pset);

cout << "Taking one:" << endl;
pset.take();
printSet(pset);

cout << "Removing everything:" << endl;
pset.remove(d);
Expand Down

0 comments on commit e47ac72

Please sign in to comment.