 |
Open Graph Drawing Framework |
v. 2023.09 (Elderberry)
|
|
|
Go to the documentation of this file.
45 #include <type_traits>
49 #ifndef OGDF_MEMORY_POOL_NTS
57 template<
typename Registry>
60 template<
typename Key,
typename Registry,
typename Iterator =
void>
75 template<
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;
95 virtual void keysSwapped(
int index1,
int index2) = 0;
98 virtual void keysCopied(
int toIndex,
int fromIndex) = 0;
138 template<
typename Key,
typename Registry,
typename Iterator>
139 class RegistryBase :
public Observable<RegisteredObserver<Registry>, Registry> {
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) {
207 for (
auto& ob : getObservers()) {
217 for (
auto& ob : getObservers()) {
225 for (
auto& ob : getObservers()) {
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);
258 for (
auto& ob : getObservers()) {
259 ob->keysSwapped(index1, index2);
266 ab->copyEntry(toIndex, fromIndex);
268 for (
auto& ob : getObservers()) {
269 ob->keysCopied(fromIndex, toIndex);
307 template<
class Registry>
308 class RegisteredArrayBase {
347 virtual void resize(
int size,
bool shrink) = 0;
350 virtual void swapEntries(
int index1,
int index2) = 0;
353 virtual void copyEntry(
int newIndex,
int oldIndex) = 0;
382 move_from.m_pRegistry =
nullptr;
403 template<
class ArrayType,
class KeyIterator,
bool isConst = false>
408 using value_type =
typename std::conditional<isConst,
const typename ArrayType::value_type,
487 template<
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 {
646 template<
class Registry,
class Value>
657 using RA::operator[];
679 template<
class Registry,
class Value>
681 typename std::conditional_t<std::is_integral_v<typename Registry::key_type>,
682 RegisteredArrayWithoutDefaultOrIndexAccess<Registry, Value>,
698 template<
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();
868 template<
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);
920 void init(
const Base& base,
const Value& new_default) {
921 RA::setDefault(new_default);
925 void init(
const Base* base,
const Value& new_default) {
926 RA::setDefault(new_default);
932 template<
class Registry,
bool WithDefault,
class Base>
1010 template<
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>;
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.
const Registry * getObserved() const
#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
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
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
virtual void keyAdded(typename Registry::key_type v)=0
Called by watched registry after a key has been added.
std::list< registered_array_type *, OGDFAllocator< registered_array_type * > > registration_list_type
Simple, safe base classes for C++ observables and observers.
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
void reregister(const Registry *obs)
Associates observer instance with instance obs.
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)
virtual void keysCopied(int toIndex, int fromIndex)=0
Called when an entry is copied from fromIndex to toIndex in all registered arrays.
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
Base class for an observable object that can be tracked by multiple Observer objects.
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.
const ListPure< TObserver * > & getObservers() const
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.
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
const Registry * getRegistry() const
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
Abstract Base class for registry observers.
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.
Registered arrays without default values or by-index access to values.
Base class for an observer for a single Observable object.
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
virtual void keysSwapped(int index1, int index2)=0
Called when an entry is swapped between index1 and index2 in all registered arrays.
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)
RegisteredObserver()=default
Constructs instance of RegisteredObserver class.
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.
virtual void keysCleared()=0
Called by watched registry when its clear function is called, just before things are removed.
virtual void keyRemoved(typename Registry::key_type v)=0
Called by watched registry just before a key is deleted.
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.