Tkrzw
tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE > Class Template Referencefinal

Doubly-linked hash map. More...

#include <tkrzw_containers.h>

Classes

class  ConstIterator
 Const iterator of records. More...
 
class  Iterator
 Iterator of records. More...
 
struct  Record
 Record data. More...
 

Public Types

enum  MoveMode : int32_t { MOVE_CURRENT = 0 , MOVE_FIRST = 1 , MOVE_LAST = 2 }
 Enumeration for move modes. More...
 

Public Member Functions

 LinkedHashMap ()
 Default constructor.
 
 LinkedHashMap (size_t num_buckets)
 Constructor.
 
 ~LinkedHashMap ()
 Destructor.
 
 LinkedHashMap (const LinkedHashMap &rhs)=delete
 Copy and assignment are disabled.
 
LinkedHashMapoperator= (const LinkedHashMap &rhs)=delete
 
RecordGet (const KEYTYPE &key, MoveMode mode=MOVE_CURRENT)
 Retrieves a record.
 
VALUETYPE GetSimple (const KEYTYPE &key, VALUETYPE default_value=VALUETYPE(), MoveMode mode=MOVE_CURRENT)
 Retrieves a record.
 
RecordSet (const KEYTYPE &key, const VALUETYPE &value, bool overwrite=true, MoveMode mode=MOVE_CURRENT)
 Stores a record.
 
bool Remove (const KEYTYPE &key)
 Removes a record.
 
RecordMigrate (const KEYTYPE &key, LinkedHashMap *dest, MoveMode mode)
 Migrates a record to another map.
 
void clear ()
 Removes all records.
 
size_t size () const
 Gets the number of records.
 
bool empty () const
 Checks whether no records exist.
 
Iterator begin ()
 Gets an iterator at the first record.
 
ConstIterator begin () const
 Gets a const iterator at the first record.
 
Iterator end ()
 Gets an iterator of the end sentry.
 
ConstIterator end () const
 Gets a const iterator of the end sentry.
 
Iterator find (const KEYTYPE &key)
 Gets an iterator at a record.
 
ConstIterator find (const KEYTYPE &key) const
 Gets an iterator at a record.
 
VALUETYPE & operator[] (const KEYTYPE &key)
 Refers to a record.
 
std::pair< Iterator, bool > insert (const Record &record)
 Insert a record without overwriting an existing record.
 
size_t erase (const KEYTYPE &key)
 Removes a record.
 
Recordfront ()
 Gets the reference of the first record.
 
const Recordfront () const
 Gets the reference of the first record.
 
Recordback ()
 Gets the reference of the last record.
 
const Recordback () const
 Gets the reference of the last record.
 
void rehash (size_t num_buckets)
 Rehashes all records to a new number of records.
 
float load_factor () const
 Gets average number of elements per bucket.
 

Static Public Attributes

static constexpr size_t DEFAULT_NUM_BUCKETS = 101
 The default value of the number of buckets.
 

Detailed Description

template<typename KEYTYPE, typename VALUETYPE, typename HASHTYPE = std::hash<KEYTYPE>, typename EQUALTOTYPE = std::equal_to<KEYTYPE>>
class tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >

Doubly-linked hash map.

Parameters
KEYTYPEthe key type.
VALUETYPEthe value type.
HASHTYPEthe hash functor.
EQUALTOTYPEthe equality checking functor.

Member Enumeration Documentation

◆ MoveMode

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE = std::hash<KEYTYPE>, typename EQUALTOTYPE = std::equal_to<KEYTYPE>>
enum tkrzw::LinkedHashMap::MoveMode : int32_t

Enumeration for move modes.

Enumerator
MOVE_CURRENT 

To keep the current position.

MOVE_FIRST 

To move to the first.

MOVE_LAST 

To move to the last.

Constructor & Destructor Documentation

◆ LinkedHashMap() [1/3]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::LinkedHashMap ( )

Default constructor.

◆ LinkedHashMap() [2/3]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::LinkedHashMap ( size_t num_buckets)
explicit

Constructor.

Parameters
num_bucketsThe number of buckets of the hash table.

◆ ~LinkedHashMap()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::~LinkedHashMap ( )

Destructor.

◆ LinkedHashMap() [3/3]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE = std::hash<KEYTYPE>, typename EQUALTOTYPE = std::equal_to<KEYTYPE>>
tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::LinkedHashMap ( const LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE > & rhs)
explicitdelete

Copy and assignment are disabled.

Member Function Documentation

◆ Get()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Record * tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Get ( const KEYTYPE & key,
MoveMode mode = MOVE_CURRENT )

Retrieves a record.

Parameters
keyThe key.
modeThe moving mode.
Returns
The pointer to the corresponding record, or nullptr on failure.

◆ GetSimple()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
VALUETYPE tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::GetSimple ( const KEYTYPE & key,
VALUETYPE default_value = VALUETYPE(),
MoveMode mode = MOVE_CURRENT )

Retrieves a record.

Parameters
keyThe key.
default_valueThe value to be returned on failure.
modeThe moving mode.
Returns
The value of the record or the default value on failure.

◆ Set()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Record * tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Set ( const KEYTYPE & key,
const VALUETYPE & value,
bool overwrite = true,
MoveMode mode = MOVE_CURRENT )

Stores a record.

Parameters
keyThe key.
valueThe value.
modeThe moving mode.
overwriteWhether to overwrite the existing value.
Returns
The pointer to the stored record or the existing record.

◆ Remove()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
bool tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Remove ( const KEYTYPE & key)

Removes a record.

Parameters
keyThe key.
Returns
True on success, or false on failure.

◆ Migrate()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Record * tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Migrate ( const KEYTYPE & key,
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE > * dest,
MoveMode mode )

Migrates a record to another map.

Parameters
keyThe key.
destThe destination map.
modeThe moving mode.
Returns
The pointer to the migrated record, or nullptr on failure.

◆ clear()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
void tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::clear ( )

Removes all records.

◆ size()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
size_t tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::size ( ) const

Gets the number of records.

Returns
The number of records.

◆ empty()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
bool tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::empty ( ) const

Checks whether no records exist.

Returns
True if there's no record or false if there are one or more records.

◆ begin() [1/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Iterator tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::begin ( )

Gets an iterator at the first record.

Returns
The iterator at the first record.

◆ begin() [2/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::ConstIterator tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::begin ( ) const

Gets a const iterator at the first record.

Returns
The const iterator at the first record.

◆ end() [1/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Iterator tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::end ( )

Gets an iterator of the end sentry.

Returns
The iterator at the end sentry.

◆ end() [2/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::ConstIterator tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::end ( ) const

Gets a const iterator of the end sentry.

Returns
The const iterator at the end sentry.

◆ find() [1/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Iterator tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::find ( const KEYTYPE & key)

Gets an iterator at a record.

Parameters
keyThe key of the record to find.
Returns
The pointer to the value of the corresponding record, or nullptr on failure.

◆ find() [2/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::ConstIterator tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::find ( const KEYTYPE & key) const

Gets an iterator at a record.

Parameters
keyThe key of the record to find.
Returns
The pointer to the value of the corresponding record, or nullptr on failure.

◆ operator[]()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
VALUETYPE & tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::operator[] ( const KEYTYPE & key)

Refers to a record.

Parameters
keyThe key of the record to find.
Returns
The reference to the record.

If there's no matching record. a new record is created.

◆ insert()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
std::pair< typename LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Iterator, bool > tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::insert ( const Record & record)

Insert a record without overwriting an existing record.

Parameters
recordThe record to insert.
Returns
A pair. The first element is an iterator to the inserted or existing element. The second element is a boolean of whether the insertion is successful.

◆ erase()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
size_t tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::erase ( const KEYTYPE & key)

Removes a record.

Parameters
keyThe key.
Returns
1 on success or 0 if there's no matching key.

◆ front() [1/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Record & tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::front ( )

Gets the reference of the first record.

Returns
The reference of the first record.

◆ front() [2/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
const LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Record & tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::front ( ) const

Gets the reference of the first record.

Returns
The reference of the first record.

◆ back() [1/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Record & tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::back ( )

Gets the reference of the last record.

Returns
The reference of the last record.

◆ back() [2/2]

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
const LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::Record & tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::back ( ) const

Gets the reference of the last record.

Returns
The reference of the last record.

◆ rehash()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
void tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::rehash ( size_t num_buckets)

Rehashes all records to a new number of records.

Parameters
num_bucketsThe new number of buckets.

◆ load_factor()

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE , typename EQUALTOTYPE >
float tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::load_factor ( ) const

Gets average number of elements per bucket.

Returns
The average number of elements per bucket.

Member Data Documentation

◆ DEFAULT_NUM_BUCKETS

template<typename KEYTYPE , typename VALUETYPE , typename HASHTYPE = std::hash<KEYTYPE>, typename EQUALTOTYPE = std::equal_to<KEYTYPE>>
size_t tkrzw::LinkedHashMap< KEYTYPE, VALUETYPE, HASHTYPE, EQUALTOTYPE >::DEFAULT_NUM_BUCKETS = 101
staticconstexpr

The default value of the number of buckets.