Files CCore/inc/CompactNodeAllocator.h CCore/src/CompactNodeAllocator.cpp
Compact node allocator is used to allocate and destroy list and tree nodes in compact lists and tree maps. It is similar to node allocators, but have different node deletion methods. This allocator is efficient both in the performance and the memory utilization.
template <class Node>
class CompactNodeAllocator : NoCopy
{
....
ulen count;
public:
// constructors
static const ulen DefaultCount = 100 ;
explicit CompactNodeAllocator(ulen block_len_=DefaultCount);
~CompactNodeAllocator();
// std move
CompactNodeAllocator(CompactNodeAllocator<Node> &&obj) noexcept;
CompactNodeAllocator<Node> & operator = (CompactNodeAllocator<Node> &&obj) noexcept;
// props
ulen operator + () const { return count; }
bool operator ! () const { return !count; }
ulen getCount() const { return count; }
// methods
template <class ... SS>
Node * alloc(SS && ... ss);
ulen erase();
Node * todel();
void del();
// swap/move objects
void objSwap(CompactNodeAllocator<Node> &obj);
explicit CompactNodeAllocator(ToMoveCtor<CompactNodeAllocator<Node> > obj);
};
Constructor takes a node block length as the argument, it is 100 by default. Memory for nodes is allocated in blocks of this capacity. Destructor calls abort, if not all nodes are destroyed.
CompactNodeAllocator is not copyable, but std movable. The original object is nullified during the move.
operator + and operator ! can be used to check, if there are allocated nodes.
getCount() returns the number of allocated nodes.
alloc() allocates a node. Arguments are forwarder to the node constructor.
erase() destroys all allocated nodes. The number of nodes is returned.
todel() points to the "special" node, which can be deleted. If there are no allocated nodes, null is returned. Unlike other node allocators, compact variant can delete only one special node from the set of allocated nodes.
del() deletes the special node, if any.
CompactNodeAllocator is swappable and movable.