|
Open Graph Drawing Framework |
v. 2023.09 (Elderberry)
|
|
|
Go to the documentation of this file.
44 #include <type_traits>
48 #ifndef OGDF_MEMORY_POOL_NTS
56 template<
typename Registry>
103 template<
typename Key,
typename Registry,
typename Iterator =
void>
111 std::list<registered_array_type*, OGDFAllocator<registered_array_type*>>;
119 #ifndef OGDF_MEMORY_POOL_NTS
145 #ifndef OGDF_MEMORY_POOL_NTS
156 #ifndef OGDF_MEMORY_POOL_NTS
164 #ifndef OGDF_MEMORY_POOL_NTS
172 if (
static_cast<Registry*
>(
this)->keyToIndex(key) >=
m_size) {
199 m_size = size = max(size, 0);
201 ab->resize(size, shrink);
207 resizeArrays(
static_cast<Registry*
>(
this)->calculateArraySize(new_keys));
213 ab->swapEntries(index1, index2);
220 ab->copyEntry(toIndex, fromIndex);
256 template<
class Registry>
257 class RegisteredArrayBase {
296 virtual void resize(
int size,
bool shrink) = 0;
299 virtual void swapEntries(
int index1,
int index2) = 0;
302 virtual void copyEntry(
int newIndex,
int oldIndex) = 0;
331 move_from.m_pRegistry =
nullptr;
352 template<
class ArrayType,
class KeyIterator,
bool isConst = false>
357 using value_type =
typename std::conditional<isConst,
const typename ArrayType::value_type,
436 template<
class Registry,
class Value>
469 void init(
const Registry* registry =
nullptr) {
470 if (registry ==
nullptr) {
473 OGDF_ASSERT(registry->maxKeyIndex() < registry->getArraySize());
475 resize(registry->getArraySize(),
true);
576 void resize(
int size,
bool shrink)
override {
594 template<
class Registry,
class Value>
605 using RA::operator[];
627 template<
class Registry,
class Value>
629 typename std::conditional_t<std::is_integral_v<typename Registry::key_type>,
630 RegisteredArrayWithoutDefaultOrIndexAccess<Registry, Value>,
646 template<
class Registry,
class Value>
651 static_assert(std::is_copy_constructible_v<Value>,
652 "This RegisteredArrayWithDefault<Value> instantiation (e.g. NodeArray<Graph>) is "
653 "invalid because Value is not copy-constructible! "
654 "Use, e.g., NodeArrayP<Graph> or NodeArray<unique_ptr<Graph>, false> instead.");
704 RA::m_data.at(toIndex) = RA::m_data.at(fromIndex);
707 void resize(
int size,
bool shrink)
override {
710 RA::m_data.shrink_to_fit();
816 template<
class Registry,
class Value,
bool WithDefault = true,
class Base = Registry>
818 :
public std::conditional<WithDefault, internal::RegisteredArrayWithDefault<Registry, Value>,
819 internal::RegisteredArrayWithoutDefault<Registry, Value>>
::type {
821 typename std::conditional<WithDefault, internal::RegisteredArrayWithDefault<Registry, Value>,
824 static inline const Registry*
cast(
const Base* base) {
825 if (base !=
nullptr) {
827 return &((
const Registry&)*base);
868 void init(
const Base& base,
const Value& new_default) {
869 RA::setDefault(new_default);
873 void init(
const Base* base,
const Value& new_default) {
874 RA::setDefault(new_default);
880 template<
class Registry,
bool WithDefault,
class Base>
958 template<
typename RA1,
typename RA2>
962 for (
const auto& key : *from.registeredAt()) {
969 #define OGDF_DECL_REG_ARRAY(NAME) \
970 template<typename Value, bool WithDefault = true> \
971 using NAME = OGDF_DECL_REG_ARRAY_TYPE(Value, WithDefault); \
976 template<typename Value> \
977 using NAME##P = NAME<std::unique_ptr<Value>, false>;
typename ArrayType::key_type key_type
Abstract base class for registries.
The namespace for all OGDF objects.
RegisteredArrayWithoutDefaultOrIndexAccess(const Registry *registry)
Creates a new registered array associated with registry.
typename Registry::key_type key_type
Dynamic arrays indexed with arbitrary keys.
bool operator!=(const RegisteredArrayIterator< ArrayType, KeyIterator, isConst > &iter) const
Inequality operator.
void copyArrayEntries(int toIndex, int fromIndex)
Copies the entry from fromIndex to toIndex in all registered arrays.
static constexpr int MIN_TABLE_SIZE
The default minimum table size for registered arrays.
RegisteredArrayIterator()
Creates a new iterator associated with no array.
typename vector_type::reference value_ref_type
virtual ~RegistryBase() noexcept
Destructor. Unregisters all associated arrays.
void swapEntries(int index1, int index2) override
Swaps the entries stored at index1 and index2.
void reregister(const Registry *registry)
Associates the array with a new registry.
void resizeArrays(int size)
Resizes all arrays to size. Only shrinks the arrays if auto shrink is enabled.
RegisteredArray(const Base &base)
Creates a new registered array associated with the matching registry of base.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
bool operator==(const RegisteredArrayIterator< ArrayType, KeyIterator, isConst > &iter) const
Equality operator.
std::mutex m_mutexRegArrays
RegisteredArrayIterator< ArrayType, KeyIterator, isConst > & operator++()
Increment operator (prefix).
value_const_ref_type operator[](int idx) const
void keyRemoved(Key key)
Records the deletion of a key and resizes all registered arrays if auto shrink is enabled.
typename std::conditional< WithDefault, internal::RegisteredArrayWithDefault< Registry, Value >, internal::RegisteredArrayWithoutDefault< Registry, Value > >::type RA
HypergraphRegistry< HypernodeElement >::iterator begin(const HypergraphRegistry< HypernodeElement > &self)
value_ref_type operator[](key_type key)
Returns a reference to the element associated with key.
void resizeArrays(int size, bool shrink)
Resizes all arrays to size. If shrink is true, the arrays may also shrink.
OGDF_NODISCARD registration_iterator_type registerArray(registered_array_type *pArray) const
Registers a new array with this registry.
value_type & value() const
Returns the value of key() in the registered array.
RegisteredArrayIterator< ArrayType, KeyIterator, isConst > & operator--()
Decrement operator (prefix).
value_const_ref_type operator()(key_type key) const
std::list< registered_array_type *, OGDFAllocator< registered_array_type * > > registration_list_type
virtual ~RegisteredArrayBase() noexcept
Destructor.
RegisteredArrayWithoutDefaultOrIndexAccess that also allows accessing its values directly by their in...
RegisteredArrayBase & operator=(RegisteredArrayBase< Registry > &&move_from) noexcept
Assignment operator (move semantics).
RegisteredArrayWithDefault(const Value &def)
Creates a new registered array associated with no registry and default value def.
RegisteredArrayWithoutDefault< Registry, Value > RA
Abstract base class for registered arrays.
RegisteredArrayWithDefault(const Registry *registry, Value &&def)
Creates a new registered array associated with registry and default value def.
bool isAutoShrink() const
Returns whether the registry allows arrays to shrink when keys are removed.
void moveRegisterArray(registration_iterator_type it, registered_array_type *pArray) const
Stores array pArray at position it in the list of registered arrays.
void resize(int size, bool shrink) override
std::vector< Value, OGDFAllocator< Value > > vector_type
value_ref_type operator[](int idx)
RegisteredArrayIterator< registered_array, key_iterator, false > iterator
RegisteredArrayIterator< ArrayType, KeyIterator, isConst > operator++(int)
Increment operator (postfix).
const registration_list_type & getRegisteredArrays() const
Returns a reference to the list of all registered arrays.
const_iterator cend() const
virtual void swapEntries(int index1, int index2)=0
Swaps the entries stored at index1 and index2.
RegisteredArrayWithDefault(const Registry *registry)
Creates a new registered array associated with registry and a default-constructed default value.
void setAutoShrink(bool mAutoShrink)
Specifies whether the registry allows arrays to shrink when keys are removed.
void fill(value_const_ref_type x)
Fills all entries with value x.
registration_iterator_type m_registration
value_ref_type operator()(key_type key)
Returns a reference to the element associated with key.
typename std::conditional< isConst, const typename ArrayType::value_type, typename ArrayType::value_type >::type value_type
Specialization to work around vector<bool>.
RegisteredArrayWithoutDefaultWithIndexAccess(const Registry *registry)
const Registry & getRegistry() const
Returns a reference to the associated registry.
typename ArrayType::registry_type registry_type
value_const_ref_type operator[](key_type key) const
Returns a const reference to the element associated with key.
static void copy(const T &from, T &to)
void setDefault(const Value &def)
Sets a new default value for new keys.
RegisteredArrayBase()=default
Creates a registered array associated with no registry.
value_type & operator*() const
Returns the value of key() in the registered array.
void invertRegisteredArray(const RA1 &from, RA2 &to)
Copy data from a ABCArray<XYZ> to an XYZArray<ABC>
void unregisterArray(registration_iterator_type it) const noexcept
Unregisters an array associated with this registry.
#define OGDF_NODISCARD
Indicate that the result of a function call should not be discarded.
RA::value_const_ref_type operator[](int idx) const
Returns a const reference to the element with index idx.
const T & move(const T &v)
RegisteredArrayWithDefault(const Registry *registry, const Value &def)
Creates a new registered array associated with registry and default value def.
const Value & getDefault() const
Returns the current default value for new keys.
const_iterator end() const
const_iterator cbegin() const
Returns a const iterator to the first key-value pair in the array.
RegisteredArrayWithoutDefaultWithIndexAccess()=default
void unregister() noexcept
Clears the array and associates it with no registry.
int calculateTableSize(int actualCount)
The default growth function for registered arrays.
RegisteredArrayWithoutDefaultOrIndexAccess()=default
Creates a new registered array associated with no registry.
void moveRegister(RegisteredArrayBase< Registry > &move_from)
Moves array registration from move_from to this array.
key_type key() const
Returns the current key.
void fillWithDefault()
Overwrites all values with the current default value.
RegisteredArrayBase & operator=(const RegisteredArrayBase< Registry > ©)
Assignment operator.
void copyEntry(int toIndex, int fromIndex) override
This operation is not supported for registered arrays without default.
Registered arrays with default values.
value_const_ref_type getDefault() const
const_iterator cend() const
Returns the const past-the-end iterator of the array.
GraphRegistry< Key, GraphObjectContainer< Key >, 1 > registry_type
void keyAdded(Key key)
Records the addition of a new key and resizes all registered arrays if necessary.
void swapArrayEntries(int index1, int index2)
Swaps the entries at index1 and index2 in all registered arrays.
iterator begin()
Returns an iterator to the first key-value pair in the array.
typename RA::key_type key_type
void init(const Base &base)
Reinitializes the array. Associates the array with the matching registry of base.
void resize(int size, bool shrink) override
Resizes the registered array to size. The array will only shrink if shrink is true.
value_ref_type operator[](key_type key)
const_iterator begin() const
Returns a const iterator to the first key-value pair in the array.
RegisteredArrayIterator< ArrayType, KeyIterator, isConst > operator--(int)
Decrement operator (postfix).
const Registry * m_pRegistry
const bool & value_const_ref_type
RegisteredArrayBase(RegisteredArrayBase< Registry > &&move_from) noexcept
Moves the registration of move_from to this registered array.
typename Registry::iterator_type key_iterator
typename std::conditional_t< std::is_integral_v< typename Registry::key_type >, RegisteredArrayWithoutDefaultOrIndexAccess< Registry, Value >, RegisteredArrayWithoutDefaultWithIndexAccess< Registry, Value > > RegisteredArrayWithoutDefault
Registered arrays without default values that automatically allows by-index access to values if Regis...
Value & getDefault()
Returns the current default value for new keys.
void copyEntry(int toIndex, int fromIndex) override
Copies the entry stored at oldIndex to newIndex.
RegistryBase & operator=(const RegistryBase &other)=delete
Registered arrays without default values or by-index access to values.
value_const_ref_type operator[](key_type key) const
RegisteredArrayBase(const RegisteredArrayBase< Registry > ©)
Creates a registered array associated with the same registry as copy.
value_const_ref_type operator()(key_type key) const
Returns a const reference to the element associated with key.
typename vector_type::value_type value_type
RegisteredArray(const Base *base, const Value &def)
Creates a new registered array associated with the matching registry of base and initializes all valu...
array_pointer_type m_array
Basic declarations, included by all source files.
RegisteredArrayIterator(KeyIterator mIt, array_pointer_type mArray)
Creates a new iterator.
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)
static const Registry * cast(const Base *base)
virtual void copyEntry(int newIndex, int oldIndex)=0
Copies the entry stored at oldIndex to newIndex.
const_iterator end() const
Returns the const past-the-end iterator of the array.
const Registry * registeredAt() const
Returns a pointer to the associated registry.
Iterator for registered arrays.
typename vector_type::const_reference value_const_ref_type
RegisteredArray(const Base *base)
Creates a new registered array associated with the matching registry of base.
GraphObjectContainer< Key > ::iterator iterator_type
value_ref_type operator()(key_type key)
void resizeArrays()
Resizes all arrays to the size requested by calculateArraySize(). Only shrinks the arrays if auto shr...
void setDefault(Value &&def)
Sets a new default value for new keys.
T nextPower2(T x)
Returns the smallest power of 2 that is no less than the given (integral) argument.
void init(const Base *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
typename registration_list_type::iterator registration_iterator_type
void keysCleared()
Records that all keys have been cleared. If auto shrink is enabled, all arrays are cleared and resize...
typename Registry::registration_iterator_type registration_iterator_type
const_iterator cbegin() const
const_iterator begin() const
int getArraySize() const
Returns the current size of all registered arrays.
RegisteredArray()
Creates a new registered array associated with no registry.
registration_list_type m_registeredArrays
virtual void resize(int size, bool shrink)=0
Resizes the registered array to size. The array will only shrink if shrink is true.
RegisteredArrayWithDefault()
Creates a new registered array associated with no registry and a default-constructed default value.
void init(const Base *base, const Value &new_default)
value_ref_type getDefault()
Declaration of memory manager for allocating small pieces of memory.
iterator end()
Returns the past-the-end iterator of the array.
void unregisterArrays() noexcept
Unregister all associated arrays.
typename std::conditional< isConst, const ArrayType *, ArrayType * >::type array_pointer_type
RegisteredArray(const Base &base, const Value &def)
Creates a new registered array associated with the matching registry of base and initializes all valu...
void init(const Registry *registry=nullptr)
Associates the array with registry. All entries are initialized using the default constructor of Valu...
RegisteredArrayIterator< registered_array, key_iterator, true > const_iterator
void reserveSpace(int new_keys)
Resizes all arrays to make space of new_keys new keys.
void init(const Base &base, const Value &new_default)
Reinitializes the array with default value new_default.
RegisteredArrayWithDefault(Value &&def)
Creates a new registered array associated with no registry and default value def.
RA::value_ref_type operator[](int idx)
Returns a reference to the element with index idx.
bool valid() const
Returns true iff the array is associated with a registry.