DGtal
1.5.beta
|
Aim: Represents a map label -> data, where the label is an integer between 0 and a constant L-1. It is based on a binary coding of labels and a mixed list/array structure. The assumption is that the number of used labels is much less than L. The objective is to minimize the memory usage. More...
#include <DGtal/base/LabelledMap.h>
Data Structures | |
struct | __AnyBlock |
struct | __FirstBlock |
class | BlockConstIterator |
class | BlockIterator |
union | BlockPointer |
Forward declaration. More... | |
class | ConstIterator |
union | DataOrBlockPointer |
Used in first block to finish it or to point to the next block. More... | |
class | KeyCompare |
Key comparator class. Always natural ordering. More... | |
class | ValueCompare |
Value comparator class. Always natural ordering between keys. More... | |
Public Types | |
typedef LabelledMap< TData, L, TWord, N, M > | Self |
typedef TData | Data |
typedef TWord | Word |
typedef Labels< L, Word > | LabelsType |
typedef LabelsType::Label | Label |
typedef Label | Key |
typedef std::pair< const Key, Data > | Value |
typedef LabelsType::ConstIterator | LabelsConstIterator |
typedef std::ptrdiff_t | DifferenceType |
typedef std::size_t | SizeType |
typedef Value & | Reference |
typedef Value * | Pointer |
typedef const Value & | ConstReference |
typedef const Value * | ConstPointer |
typedef Key | key_type |
Forward declaration. More... | |
typedef Value | value_type |
typedef Data | data_type |
typedef Data | mapped_type |
typedef DifferenceType | difference_type |
typedef Reference | reference |
typedef Pointer | pointer |
typedef ConstReference | const_reference |
typedef ConstPointer | const_pointer |
typedef SizeType | size_type |
typedef ConstIterator | iterator |
typedef ConstIterator | const_iterator |
typedef KeyCompare | key_compare |
typedef ValueCompare | value_compare |
typedef ConstIterator | Iterator |
non-mutable class via iterators. More... | |
Public Member Functions | |
LabelledMap () | |
LabelledMap (const LabelledMap &other) | |
template<typename InputIterator > | |
LabelledMap (InputIterator first, InputIterator last) | |
LabelledMap & | operator= (const LabelledMap &other) |
~LabelledMap () | |
const LabelsType & | labels () const |
SizeType | size () const |
bool | empty () const |
SizeType | max_size () const |
SizeType | capacity () const |
KeyCompare | key_comp () const |
ValueCompare | value_comp () const |
void | swap (Self &other) |
void | clear () |
SizeType | count (const Key &key) const |
Data & | operator[] (const Key &key) |
const Data & | operator[] (const Key &key) const |
Data & | fastAt (const Key &key) |
const Data & | fastAt (const Key &key) const |
std::pair< Iterator, bool > | insert (const Value &val) |
Iterator | insert (Iterator position, const Value &val) |
template<typename InputIterator > | |
void | insert (InputIterator first, InputIterator last) |
void | erase (Iterator position) |
SizeType | erase (Key key) |
void | erase (Iterator first, Iterator last) |
ConstIterator | begin () const |
ConstIterator | end () const |
Iterator | begin () |
Iterator | end () |
std::pair< Iterator, Iterator > | equal_range (const Key &x) |
std::pair< ConstIterator, ConstIterator > | equal_range (const Key &x) const |
Iterator | find (const Key &x) |
ConstIterator | find (const Key &x) const |
Iterator | lower_bound (const Key &x) |
ConstIterator | lower_bound (const Key &x) const |
Iterator | upper_bound (const Key &x) |
ConstIterator | upper_bound (const Key &x) const |
void | blockClear (size_t size) |
Data & | blockAt (size_t idx) |
const Data & | blockAt (size_t idx) const |
Data & | blockInsert (size_t idx, size_t block_size, const Data &data) |
void | blockErase (size_t idx) |
BlockIterator | blockBegin () |
BlockIterator | blockEnd () |
BlockConstIterator | blockBegin () const |
BlockConstIterator | blockEnd () const |
void | selfDisplay (std::ostream &out) const |
bool | isValid () const |
Private Member Functions | |
BOOST_STATIC_ASSERT (L >=1) | |
BOOST_STATIC_ASSERT (N >=0) | |
BOOST_STATIC_ASSERT (M >=2) | |
Private Attributes | |
LabelsType | myLabels |
Stores the labels for this sequence of datas. More... | |
__FirstBlock | myFirstBlock |
Aim: Represents a map label -> data, where the label is an integer between 0 and a constant L-1. It is based on a binary coding of labels and a mixed list/array structure. The assumption is that the number of used labels is much less than L. The objective is to minimize the memory usage.
Description of template class 'LabelledMap'
Model of boost::AssociativeContainer, boost::PairAssociativeContainer, boost::UniqueAssociativeContainer. As such, it is refinement of boost::ForwardContainer and boost::Container. It is also a model of boost::Assignable, boost::CopyConstructible.
V[ 0 ] is the data of the first set label. V[ 1 ] is the data of the second set label. ... if less than 4 datas and N = 3 +------+------+------+------+------+ |labels| V[0] | V[1] | ... | 0 | +------+------+------+------+------+ if only 4 datas and N = 3 +------+------+------+------+------+ |labels| V[0] | V[1] | V[2] | V[3] | +------+------+------+------+------+ if more than 4 datas and N = 3, M = 4 +------+------+------+------+------+ +------+------+------+------+------+ |labels| V[0] | V[1] | V[2] | ptr --------> | V[3] | V[4] | V[5] | V[6] | ptr --------> ... +------+------+------+------+------+ +------+------+------+------+------+
This structure is related to the IndexedListWithBlocks, except that it stores the mapping label -> index. The (maximum) number of possible labels is fixed at instantiation for optimisation purposes.
Such a structure is useful when:
Model of boost::PairAssociativeContainer and boost::SimpleAssociativeContainer.
TData | the type for the datas stored in the list. |
L | the maximum number of labels. |
TWord | the integer used to store the labels (if L >= log_2( digits( TWord ) ) then several consecutive words are stored.), e.g. DGtal::uint8_t. |
N | the number of data stored in the first block. |
M | the number of data stored in the further blocks. |
NB: In the following, we use the notations
Definition at line 119 of file LabelledMap.h.
typedef ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::const_iterator |
Definition at line 159 of file LabelledMap.h.
typedef ConstPointer DGtal::LabelledMap< TData, L, TWord, N, M >::const_pointer |
Definition at line 156 of file LabelledMap.h.
typedef ConstReference DGtal::LabelledMap< TData, L, TWord, N, M >::const_reference |
Definition at line 155 of file LabelledMap.h.
typedef const Value* DGtal::LabelledMap< TData, L, TWord, N, M >::ConstPointer |
Definition at line 141 of file LabelledMap.h.
typedef const Value& DGtal::LabelledMap< TData, L, TWord, N, M >::ConstReference |
Definition at line 140 of file LabelledMap.h.
typedef TData DGtal::LabelledMap< TData, L, TWord, N, M >::Data |
Definition at line 127 of file LabelledMap.h.
typedef Data DGtal::LabelledMap< TData, L, TWord, N, M >::data_type |
Definition at line 150 of file LabelledMap.h.
typedef DifferenceType DGtal::LabelledMap< TData, L, TWord, N, M >::difference_type |
Definition at line 152 of file LabelledMap.h.
typedef std::ptrdiff_t DGtal::LabelledMap< TData, L, TWord, N, M >::DifferenceType |
Definition at line 136 of file LabelledMap.h.
typedef ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::iterator |
Definition at line 158 of file LabelledMap.h.
typedef ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::Iterator |
non-mutable class via iterators.
Definition at line 730 of file LabelledMap.h.
typedef Label DGtal::LabelledMap< TData, L, TWord, N, M >::Key |
Definition at line 131 of file LabelledMap.h.
typedef KeyCompare DGtal::LabelledMap< TData, L, TWord, N, M >::key_compare |
Definition at line 160 of file LabelledMap.h.
typedef Key DGtal::LabelledMap< TData, L, TWord, N, M >::key_type |
Forward declaration.
Definition at line 148 of file LabelledMap.h.
typedef LabelsType::Label DGtal::LabelledMap< TData, L, TWord, N, M >::Label |
Definition at line 130 of file LabelledMap.h.
typedef LabelsType::ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::LabelsConstIterator |
Definition at line 135 of file LabelledMap.h.
typedef Labels<L, Word> DGtal::LabelledMap< TData, L, TWord, N, M >::LabelsType |
Definition at line 129 of file LabelledMap.h.
typedef Data DGtal::LabelledMap< TData, L, TWord, N, M >::mapped_type |
Definition at line 151 of file LabelledMap.h.
typedef Value* DGtal::LabelledMap< TData, L, TWord, N, M >::Pointer |
Definition at line 139 of file LabelledMap.h.
typedef Pointer DGtal::LabelledMap< TData, L, TWord, N, M >::pointer |
Definition at line 154 of file LabelledMap.h.
typedef Value& DGtal::LabelledMap< TData, L, TWord, N, M >::Reference |
Definition at line 138 of file LabelledMap.h.
typedef Reference DGtal::LabelledMap< TData, L, TWord, N, M >::reference |
Definition at line 153 of file LabelledMap.h.
typedef LabelledMap<TData, L, TWord, N, M> DGtal::LabelledMap< TData, L, TWord, N, M >::Self |
Definition at line 126 of file LabelledMap.h.
typedef SizeType DGtal::LabelledMap< TData, L, TWord, N, M >::size_type |
Definition at line 157 of file LabelledMap.h.
typedef std::size_t DGtal::LabelledMap< TData, L, TWord, N, M >::SizeType |
Definition at line 137 of file LabelledMap.h.
typedef std::pair<const Key, Data> DGtal::LabelledMap< TData, L, TWord, N, M >::Value |
Definition at line 132 of file LabelledMap.h.
typedef ValueCompare DGtal::LabelledMap< TData, L, TWord, N, M >::value_compare |
Definition at line 161 of file LabelledMap.h.
typedef Value DGtal::LabelledMap< TData, L, TWord, N, M >::value_type |
Definition at line 149 of file LabelledMap.h.
typedef TWord DGtal::LabelledMap< TData, L, TWord, N, M >::Word |
Definition at line 128 of file LabelledMap.h.
DGtal::LabelledMap< TData, L, TWord, N, M >::LabelledMap | ( | ) |
Constructor.
DGtal::LabelledMap< TData, L, TWord, N, M >::LabelledMap | ( | const LabelledMap< TData, L, TWord, N, M > & | other | ) |
Copy constructor.
other | the object to clone. |
DGtal::LabelledMap< TData, L, TWord, N, M >::LabelledMap | ( | InputIterator | first, |
InputIterator | last | ||
) |
Constructor from range.
InputIterator | model of boost::InputIterator whose value type is convertible to Value. |
first | an iterator on the first value of the range. |
last | an iterator after the last value of the range. |
DGtal::LabelledMap< TData, L, TWord, N, M >::~LabelledMap | ( | ) |
Destructor.
Iterator DGtal::LabelledMap< TData, L, TWord, N, M >::begin | ( | ) |
ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::begin | ( | ) | const |
Data& DGtal::LabelledMap< TData, L, TWord, N, M >::blockAt | ( | size_t | idx | ) |
Random unprotected read-write access to data at position idx
idx | the index of the data in the container. |
const Data& DGtal::LabelledMap< TData, L, TWord, N, M >::blockAt | ( | size_t | idx | ) | const |
Random unprotected read access to data at position idx
idx | the index of the data in the container. |
BlockIterator DGtal::LabelledMap< TData, L, TWord, N, M >::blockBegin | ( | ) |
BlockConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::blockBegin | ( | ) | const |
void DGtal::LabelledMap< TData, L, TWord, N, M >::blockClear | ( | size_t | size | ) |
Removes all the datas stored in the block structure.
size | must be the current size of the block structure. |
BlockIterator DGtal::LabelledMap< TData, L, TWord, N, M >::blockEnd | ( | ) |
BlockConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::blockEnd | ( | ) | const |
void DGtal::LabelledMap< TData, L, TWord, N, M >::blockErase | ( | size_t | idx | ) |
Removal of a data at a given position. Following datas are shifted.
idx | the index of the data in the container. |
Data& DGtal::LabelledMap< TData, L, TWord, N, M >::blockInsert | ( | size_t | idx, |
size_t | block_size, | ||
const Data & | data | ||
) |
Insertion of a new data at given position. The former data at this position and the next ones are shifted.
idx | the index of the data in the container. |
block_size | the block size. |
data | the data to insert. NB: O( n ), E = O( n - idx ) |
|
private |
|
private |
|
private |
SizeType DGtal::LabelledMap< TData, L, TWord, N, M >::capacity | ( | ) | const |
The number of datas currently allocated in the structure.
void DGtal::LabelledMap< TData, L, TWord, N, M >::clear | ( | ) |
Removes all the datas stored in the structure.
SizeType DGtal::LabelledMap< TData, L, TWord, N, M >::count | ( | const Key & | key | ) | const |
Follows std::count.
key | any label |
bool DGtal::LabelledMap< TData, L, TWord, N, M >::empty | ( | ) | const |
Iterator DGtal::LabelledMap< TData, L, TWord, N, M >::end | ( | ) |
ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::end | ( | ) | const |
std::pair<Iterator, Iterator> DGtal::LabelledMap< TData, L, TWord, N, M >::equal_range | ( | const Key & | x | ) |
Get range of equal elements.
Returns the bounds of a range that includes all the elements in the container with a key that compares equal to x. Here, the range will include one element at most.
If x does not match any key in the container, the range returned has a length of zero, with both iterators pointing to the element with nearest key greater than x, if any, or to map::end if x is greater than all the elements in the container.
x | any key in 0..L-1 |
std::pair<ConstIterator, ConstIterator> DGtal::LabelledMap< TData, L, TWord, N, M >::equal_range | ( | const Key & | x | ) | const |
Get range of equal elements.
Returns the bounds of a range that includes all the elements in the container with a key that compares equal to x. Here, the range will include one element at most.
If x does not match any key in the container, the range returned has a length of zero, with both iterators pointing to the element with nearest key greater than x, if any, or to map::end if x is greater than all the elements in the container.
x | any key in 0..L-1 |
void DGtal::LabelledMap< TData, L, TWord, N, M >::erase | ( | Iterator | first, |
Iterator | last | ||
) |
void DGtal::LabelledMap< TData, L, TWord, N, M >::erase | ( | Iterator | position | ) |
Erases the pair (key,data) pointed by iterator.
position | any valid iterator in the container. |
SizeType DGtal::LabelledMap< TData, L, TWord, N, M >::erase | ( | Key | key | ) |
Erases the element of key key.
key | any key (in 0..L-1) |
Data& DGtal::LabelledMap< TData, L, TWord, N, M >::fastAt | ( | const Key & | key | ) |
A read-write accessor to the data associated to an existing key.
key | any label (such that count(key)==1) |
const Data& DGtal::LabelledMap< TData, L, TWord, N, M >::fastAt | ( | const Key & | key | ) | const |
A read-only accessor to the data associated to an existing key.
key | any label (such that count(key)==1) |
Iterator DGtal::LabelledMap< TData, L, TWord, N, M >::find | ( | const Key & | x | ) |
Get iterator to element.
Searches the container for an element with x as key and returns an iterator to it if found, otherwise it returns an iterator to end() (the element past the end of the container).
NB: Another member function, count(), can be used to just check whether a particular key exists. 'count( x ) == 1' is faster than 'find( x ) != end()'.
x | Key to be searched for (in 0..L-1) |
ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::find | ( | const Key & | x | ) | const |
Get iterator to element.
Searches the container for an element with x as key and returns an iterator to it if found, otherwise it returns an iterator to end() (the element past the end of the container).
NB: Another member function, count(), can be used to just check whether a particular key exists. 'count( x ) == 1' is faster than 'find( x ) != end()'.
x | Key to be searched for (in 0..L-1) |
std::pair<Iterator, bool> DGtal::LabelledMap< TData, L, TWord, N, M >::insert | ( | const Value & | val | ) |
Insertion of a new data at given label. Follows std::insert return a pair<iterator,bool>). Note that the data is associated to key only if key was not present in the container.
val | a pair<key,data>. |
NB: This method is provided to follow the std::AssociativeContainer concept. You are discourage to use this functions since the correct iterator must be recomputed at each insert. Prefer operator[] or fastAt.
void DGtal::LabelledMap< TData, L, TWord, N, M >::insert | ( | InputIterator | first, |
InputIterator | last | ||
) |
Insertion from range. Insert all values in the range. Be careful that if a value in the container has the same key as a value in the range, then the mapped data is not changed.
InputIterator | model of boost::InputIterator whose value type is convertible to Value. |
first | an iterator on the first value of the range. |
last | an iterator after the last value of the range. |
Iterator DGtal::LabelledMap< TData, L, TWord, N, M >::insert | ( | Iterator | position, |
const Value & | val | ||
) |
Inserts the pair val (key,data) in the container, where position is a hint
position | an iterator used as a hint to find the good place. Unused here. |
val | a pair (key,data) |
NB: This method is provided to follow the std::AssociativeContainer concept. You are discourage to use this functions since the correct iterator must be recomputed at each insert. Prefer operator[] or fastAt.
bool DGtal::LabelledMap< TData, L, TWord, N, M >::isValid | ( | ) | const |
Checks the validity/consistency of the object.
KeyCompare DGtal::LabelledMap< TData, L, TWord, N, M >::key_comp | ( | ) | const |
const LabelsType& DGtal::LabelledMap< TData, L, TWord, N, M >::labels | ( | ) | const |
Iterator DGtal::LabelledMap< TData, L, TWord, N, M >::lower_bound | ( | const Key & | x | ) |
Return iterator to lower bound.
Returns an iterator pointing to the first element in the container whose key does not compare less than x (using the container's comparison object), i.e. it is either equal or greater.
Unlike upper_bound(), this member function returns an iterator to the element also if it compares equal to x and not only if it compares greater.
Notice that, internally, all the elements in this container are always ordered by their keys, therefore all the elements that follow the one returned by this function will have a key that compares greater than x.
x | Key to be searched for (in 0..L-1) |
ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::lower_bound | ( | const Key & | x | ) | const |
Return iterator to lower bound.
Returns an iterator pointing to the first element in the container whose key does not compare less than x, i.e. it is either equal or greater.
Unlike upper_bound(), this member function returns an iterator to the element also if it compares equal to x and not only if it compares greater.
Notice that, internally, all the elements in this container are always ordered by their keys, therefore all the elements that follow the one returned by this function will have a key that compares greater than x.
x | Key to be searched for (in 0..L-1) |
SizeType DGtal::LabelledMap< TData, L, TWord, N, M >::max_size | ( | ) | const |
The maximum number of datas storable in the structure.
LabelledMap& DGtal::LabelledMap< TData, L, TWord, N, M >::operator= | ( | const LabelledMap< TData, L, TWord, N, M > & | other | ) |
Assignment.
other | the object to copy. |
Data& DGtal::LabelledMap< TData, L, TWord, N, M >::operator[] | ( | const Key & | key | ) |
Follows std::operator[]. Given a key key, returns a reference to the associated data.
key | any label |
const Data& DGtal::LabelledMap< TData, L, TWord, N, M >::operator[] | ( | const Key & | key | ) | const |
Read-only version. Follows std::operator[]. Given a key key, returns a reference to the associated data.
key | any label |
void DGtal::LabelledMap< TData, L, TWord, N, M >::selfDisplay | ( | std::ostream & | out | ) | const |
Writes/Displays the object on an output stream.
out | the output stream where the object is written. |
SizeType DGtal::LabelledMap< TData, L, TWord, N, M >::size | ( | ) | const |
The number of datas stored in the structure. O(1) complexity.
void DGtal::LabelledMap< TData, L, TWord, N, M >::swap | ( | Self & | other | ) |
Swap content. Exchanges the content of the container with the content of mp, which is another map object containing elements of the same type. Sizes may differ.
After the call to this member function, the elements in this container are those which were in mp before the call, and the elements of mp are those which were in this.
NB: not exactly standard ! The iterators pointing on the first block change roles ! The other references and pointers remain valid for the swapped objects.
Iterator DGtal::LabelledMap< TData, L, TWord, N, M >::upper_bound | ( | const Key & | x | ) |
Return iterator to upper bound.
Returns an iterator pointing to the first element in the container whose key compares greater than x. (>x)
Unlike lower_bound(), this member function does not return an iterator to the element if its key compares equal to x, but only if it compares strictly greater.
Notice that, internally, all the elements in this container are always ordered by their keys, therefore all the elements that follow the one returned by this function will have a key that compares greater than x.
x | Key to be searched for (in 0..L-1) |
ConstIterator DGtal::LabelledMap< TData, L, TWord, N, M >::upper_bound | ( | const Key & | x | ) | const |
Return iterator to upper bound.
Returns an iterator pointing to the first element in the container whose key compares greater than x. (>x)
Unlike lower_bound(), this member function does not return an iterator to the element if its key compares equal to x, but only if it compares strictly greater.
Notice that, internally, all the elements in this container are always ordered by their keys, therefore all the elements that follow the one returned by this function will have a key that compares greater than x.
x | Key to be searched for (in 0..L-1) |
ValueCompare DGtal::LabelledMap< TData, L, TWord, N, M >::value_comp | ( | ) | const |
|
private |
Stores the first block of datas.
Definition at line 1234 of file LabelledMap.h.
|
private |
Stores the labels for this sequence of datas.
Definition at line 1229 of file LabelledMap.h.