37#include <ogdf/basic/internal/config_autogen.h>
49#ifndef OGDF_MEMORY_POOL_NTS
57template<
typename Registry>
58class RegisteredArrayBase;
60template<
typename Key,
typename Registry,
typename Iterator =
void>
75template<
typename Registry>
83 OGDF_DEPRECATED(
"calls registrationChanged with only partially-constructed child classes, "
84 "see copy constructor of Observer for fix")
89 virtual void keyRemoved(
typename Registry::key_type v) = 0;
92 virtual void keyAdded(
typename Registry::key_type v) = 0;
138template<
typename Key,
typename Registry,
typename Iterator>
146 std::list<registered_array_type*, OGDFAllocator<registered_array_type*>>;
156#ifndef OGDF_MEMORY_POOL_NTS
177#ifndef OGDF_MEMORY_POOL_NTS
188#ifndef OGDF_MEMORY_POOL_NTS
196#ifndef OGDF_MEMORY_POOL_NTS
204 if (
static_cast<Registry*
>(
this)->keyToIndex(key) >=
m_size) {
242 m_size = size = max(size, 0);
244 ab->resize(size, shrink);
250 resizeArrays(
static_cast<Registry*
>(
this)->calculateArraySize(new_keys));
256 ab->swapEntries(index1, index2);
259 ob->keysSwapped(index1, index2);
266 ab->copyEntry(toIndex, fromIndex);
269 ob->keysCopied(fromIndex, toIndex);
307template<
class Registry>
347 virtual void resize(
int size,
bool shrink) = 0;
403template<
class ArrayType,
class KeyIterator,
bool isConst = false>
408 using value_type =
typename std::conditional<isConst,
const typename ArrayType::value_type,
409 typename ArrayType::value_type>::type;
487template<
class Registry,
class Value>
520 void init(
const Registry* registry =
nullptr) {
521 if (registry ==
nullptr) {
524 OGDF_ASSERT(registry->maxKeyIndex() < registry->getArraySize());
526 resize(registry->getArraySize(),
true);
627 void resize(
int size,
bool shrink)
override {
646template<
class Registry,
class Value>
657 using RA::operator[];
679template<
class Registry,
class Value>
681 typename std::conditional_t<std::is_integral_v<typename Registry::key_type>,
698template<
class Registry,
class Value>
703 static_assert(std::is_copy_constructible_v<Value>,
704 "This RegisteredArrayWithDefault<Value> instantiation (e.g. NodeArray<Graph>) is "
705 "invalid because Value is not copy-constructible! "
706 "Use, e.g., NodeArrayP<Graph> or NodeArray<unique_ptr<Graph>, false> instead.");
756 RA::m_data.at(toIndex) = RA::m_data.at(fromIndex);
759 void resize(
int size,
bool shrink)
override {
762 RA::m_data.shrink_to_fit();
868template<
class Registry,
class Value,
bool WithDefault = true,
class Base = Registry>
870 :
public std::conditional<WithDefault, internal::RegisteredArrayWithDefault<Registry, Value>,
871 internal::RegisteredArrayWithoutDefault<Registry, Value>>::type {
873 typename std::conditional<WithDefault, internal::RegisteredArrayWithDefault<Registry, Value>,
876 static inline const Registry*
cast(
const Base* base) {
877 if (base !=
nullptr) {
879 return &((
const Registry&)*base);
910 void init(
const Base* base =
nullptr) { RA::init(
cast(base)); }
913 void init(
const Base& base) { RA::init(
cast(&base)); }
920 void init(
const Base& base,
const Value& new_default) {
921 RA::setDefault(new_default);
922 RA::init(
cast(&base));
925 void init(
const Base* base,
const Value& new_default) {
926 RA::setDefault(new_default);
927 RA::init(
cast(base));
932template<
class Registry,
bool WithDefault,
class Base>
1010template<
typename RA1,
typename RA2>
1014 for (
const auto& key : *from.registeredAt()) {
1015 to[from[key]] = key;
1021#define OGDF_DECL_REG_ARRAY(NAME) \
1022 template<typename Value, bool WithDefault = true> \
1023 using NAME = OGDF_DECL_REG_ARRAY_TYPE(Value, WithDefault); \
\
1028 template<typename Value> \
1029 using NAME##P = NAME<std::unique_ptr<Value>, false>;
Simple, safe base classes for C++ observables and observers.
void invertRegisteredArray(const RA1 &from, RA2 &to)
Copy data from a ABCArray<XYZ> to an XYZArray<ABC>
Basic declarations, included by all source files.
Base class for an observable object that can be tracked by multiple Observer objects.
const ListPure< RegisteredObserver< Registry > * > & getObservers() const
Base class for an observer for a single Observable object.
void reregister(const Registry *obs)
Associates observer instance with instance obs.
const Registry * getObserved() const
Specialization to work around vector<bool>.
value_const_ref_type operator[](key_type key) const
const_iterator cend() const
const_iterator end() const
typename RA::key_type key_type
const_iterator cbegin() const
value_ref_type operator[](int idx)
const_iterator begin() const
value_ref_type operator[](key_type key)
const bool & value_const_ref_type
value_const_ref_type operator[](int idx) const
value_ref_type getDefault()
value_ref_type operator()(key_type key)
value_const_ref_type getDefault() const
value_const_ref_type operator()(key_type key) const
Dynamic arrays indexed with arbitrary keys.
typename std::conditional< WithDefault, internal::RegisteredArrayWithDefault< Registry, Value >, internal::RegisteredArrayWithoutDefault< Registry, Value > >::type RA
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 Base &base)
Reinitializes the array. Associates the array with the matching registry of base.
RegisteredArray()
Creates a new registered array associated with no registry.
RegisteredArray(const Base *base)
Creates a new registered array associated with the matching registry of base.
void init(const Base *base=nullptr)
Reinitializes the array. Associates the array with the matching registry of base.
void init(const Base &base, const Value &new_default)
Reinitializes the array with default value new_default.
static const Registry * cast(const Base *base)
void init(const Base *base, const Value &new_default)
RegisteredArray(const Base *base, const Value &def)
Creates a new registered array associated with the matching registry of base and initializes all valu...
RegisteredArray(const Base &base)
Creates a new registered array associated with the matching registry of base.
Iterator for registered arrays.
RegisteredArrayIterator(KeyIterator mIt, array_pointer_type mArray)
Creates a new iterator.
value_type & value() const
Returns the value of key() in the registered array.
array_pointer_type m_array
bool operator==(const RegisteredArrayIterator< ArrayType, KeyIterator, isConst > &iter) const
Equality operator.
RegisteredArrayIterator< ArrayType, KeyIterator, isConst > operator++(int)
Increment operator (postfix).
typename std::conditional< isConst, const ArrayType *, ArrayType * >::type array_pointer_type
typename ArrayType::key_type key_type
typename std::conditional< isConst, const typename ArrayType::value_type, typename ArrayType::value_type >::type value_type
RegisteredArrayIterator< ArrayType, KeyIterator, isConst > operator--(int)
Decrement operator (postfix).
key_type key() const
Returns the current key.
RegisteredArrayIterator< ArrayType, KeyIterator, isConst > & operator++()
Increment operator (prefix).
bool operator!=(const RegisteredArrayIterator< ArrayType, KeyIterator, isConst > &iter) const
Inequality operator.
value_type & operator*() const
Returns the value of key() in the registered array.
RegisteredArrayIterator()
Creates a new iterator associated with no array.
RegisteredArrayIterator< ArrayType, KeyIterator, isConst > & operator--()
Decrement operator (prefix).
typename ArrayType::registry_type registry_type
Abstract Base class for registry observers.
virtual void keyRemoved(typename Registry::key_type v)=0
Called by watched registry just before a key is deleted.
virtual void keyAdded(typename Registry::key_type v)=0
Called by watched registry after a key has been added.
const Registry * getRegistry() const
virtual void keysSwapped(int index1, int index2)=0
Called when an entry is swapped between index1 and index2 in all registered arrays.
virtual void keysCopied(int toIndex, int fromIndex)=0
Called when an entry is copied from fromIndex to toIndex in all registered arrays.
virtual void keysCleared()=0
Called by watched registry when its clear function is called, just before things are removed.
RegisteredObserver()=default
Constructs instance of RegisteredObserver class.
Abstract base class for registries.
typename registration_list_type::iterator registration_iterator_type
void copyArrayEntries(int toIndex, int fromIndex)
Copies the entry from fromIndex to toIndex in all registered arrays.
const registration_list_type & getRegisteredArrays() const
Returns a reference to the list of all registered arrays.
void resizeArrays(int size, bool shrink)
Resizes all arrays to size. If shrink is true, the arrays may also shrink.
void resizeArrays(int size)
Resizes all arrays to size. Only shrinks the arrays if auto shrink is enabled.
void keyAdded(Key key)
Records the addition of a new key and resizes all registered arrays if necessary.
bool isAutoShrink() const
Returns whether the registry allows arrays to shrink when keys are removed.
virtual ~RegistryBase() noexcept
Destructor. Unregisters all associated arrays.
std::mutex m_mutexRegArrays
void reserveSpace(int new_keys)
Resizes all arrays to make space of new_keys new keys.
void resizeArrays()
Resizes all arrays to the size requested by calculateArraySize(). Only shrinks the arrays if auto shr...
int getArraySize() const
Returns the current size of all registered arrays.
registration_list_type m_registeredArrays
void keysCleared()
Records that all keys have been cleared. If auto shrink is enabled, all arrays are cleared and resize...
void moveRegisterArray(registration_iterator_type it, registered_array_type *pArray) const
Stores array pArray at position it in the list of registered arrays.
void swapArrayEntries(int index1, int index2)
Swaps the entries at index1 and index2 in all registered arrays.
OGDF_NODISCARD registration_iterator_type registerArray(registered_array_type *pArray) const
Registers a new array with this registry.
void unregisterArray(registration_iterator_type it) const noexcept
Unregisters an array associated with this registry.
std::list< registered_array_type *, OGDFAllocator< registered_array_type * > > registration_list_type
void keyRemoved(Key key)
Records the deletion of a key and resizes all registered arrays if auto shrink is enabled.
void setAutoShrink(bool mAutoShrink)
Specifies whether the registry allows arrays to shrink when keys are removed.
void unregisterArrays() noexcept
Unregister all associated arrays.
Abstract base class for registered arrays.
const Registry * registeredAt() const
Returns a pointer to the associated registry.
RegisteredArrayBase & operator=(const RegisteredArrayBase< Registry > ©)
Assignment operator.
virtual void swapEntries(int index1, int index2)=0
Swaps the entries stored at index1 and index2.
virtual void copyEntry(int newIndex, int oldIndex)=0
Copies the entry stored at oldIndex to newIndex.
RegisteredArrayBase(const RegisteredArrayBase< Registry > ©)
Creates a registered array associated with the same registry as copy.
typename Registry::registration_iterator_type registration_iterator_type
void moveRegister(RegisteredArrayBase< Registry > &move_from)
Moves array registration from move_from to this array.
RegisteredArrayBase(RegisteredArrayBase< Registry > &&move_from) noexcept
Moves the registration of move_from to this registered array.
RegisteredArrayBase & operator=(RegisteredArrayBase< Registry > &&move_from) noexcept
Assignment operator (move semantics).
void unregister() noexcept
Clears the array and associates it with no registry.
virtual ~RegisteredArrayBase() noexcept
Destructor.
registration_iterator_type m_registration
RegisteredArrayBase()=default
Creates a registered array associated with no registry.
const Registry * m_pRegistry
void reregister(const Registry *registry)
Associates the array with a new registry.
virtual void resize(int size, bool shrink)=0
Resizes the registered array to size. The array will only shrink if shrink is true.
Registered arrays with default values.
RegisteredArrayWithDefault(const Value &def)
Creates a new registered array associated with no registry and default value def.
RegisteredArrayWithoutDefault< Registry, Value > RA
RegisteredArrayWithDefault()
Creates a new registered array associated with no registry and a default-constructed default value.
void setDefault(Value &&def)
Sets a new default value for new keys.
RegisteredArrayWithDefault(const Registry *registry, Value &&def)
Creates a new registered array associated with registry and default value def.
RegisteredArrayWithDefault(Value &&def)
Creates a new registered array associated with no registry and default value def.
void fillWithDefault()
Overwrites all values with the current default value.
void copyEntry(int toIndex, int fromIndex) override
Copies the entry stored at oldIndex to newIndex.
const Value & getDefault() const
Returns the current default value for new keys.
void resize(int size, bool shrink) override
void setDefault(const Value &def)
Sets a new default value for new keys.
RegisteredArrayWithDefault(const Registry *registry)
Creates a new registered array associated with registry and a default-constructed default value.
Value & getDefault()
Returns the current default value for new keys.
RegisteredArrayWithDefault(const Registry *registry, const Value &def)
Creates a new registered array associated with registry and default value def.
Registered arrays without default values or by-index access to values.
RegisteredArrayIterator< registered_array, key_iterator, false > iterator
const Registry * registeredAt() const
Returns a pointer to the associated registry.
typename Registry::key_type key_type
value_ref_type operator()(key_type key)
Returns a reference to the element associated with key.
bool valid() const
Returns true iff the array is associated with a registry.
typename vector_type::const_reference value_const_ref_type
const_iterator cbegin() const
Returns a const iterator to the first key-value pair in the array.
typename vector_type::value_type value_type
void resize(int size, bool shrink) override
Resizes the registered array to size. The array will only shrink if shrink is true.
const_iterator begin() const
Returns a const iterator to the first key-value pair in the array.
typename Registry::iterator_type key_iterator
value_const_ref_type operator[](key_type key) const
Returns a const reference to the element associated with key.
iterator begin()
Returns an iterator to the first key-value pair in the array.
const_iterator end() const
Returns the const past-the-end iterator of the array.
value_const_ref_type operator()(key_type key) const
Returns a const reference to the element associated with key.
iterator end()
Returns the past-the-end iterator of the array.
void init(const Registry *registry=nullptr)
Associates the array with registry. All entries are initialized using the default constructor of Valu...
const Registry & getRegistry() const
Returns a reference to the associated registry.
RegisteredArrayIterator< registered_array, key_iterator, true > const_iterator
void fill(value_const_ref_type x)
Fills all entries with value x.
RegisteredArrayWithoutDefaultOrIndexAccess()=default
Creates a new registered array associated with no registry.
void swapEntries(int index1, int index2) override
Swaps the entries stored at index1 and index2.
const_iterator cend() const
Returns the const past-the-end iterator of the array.
void copyEntry(int toIndex, int fromIndex) override
This operation is not supported for registered arrays without default.
RegisteredArrayWithoutDefaultOrIndexAccess(const Registry *registry)
Creates a new registered array associated with registry.
value_ref_type operator[](key_type key)
Returns a reference to the element associated with key.
std::vector< Value, OGDFAllocator< Value > > vector_type
typename vector_type::reference value_ref_type
RegisteredArrayWithoutDefaultOrIndexAccess that also allows accessing its values directly by their in...
RegisteredArrayWithoutDefaultWithIndexAccess(const Registry *registry)
RA::value_const_ref_type operator[](int idx) const
Returns a const reference to the element with index idx.
RA::value_ref_type operator[](int idx)
Returns a reference to the element with index idx.
RegisteredArrayWithoutDefaultWithIndexAccess()=default
#define OGDF_NODISCARD
Indicate that the result of a function call should not be discarded.
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Declaration of memory manager for allocating small pieces of memory.
T nextPower2(T x)
Returns the smallest power of 2 that is no less than the given (integral) argument.
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...
The namespace for all OGDF objects.
HypergraphRegistry< HypernodeElement >::iterator begin(const HypergraphRegistry< HypernodeElement > &self)
int calculateTableSize(int actualCount)
The default growth function for registered arrays.
static constexpr int MIN_TABLE_SIZE
The default minimum table size for registered arrays.
HypergraphRegistry< HypernodeElement >::iterator end(const HypergraphRegistry< HypernodeElement > &self)