Panda3D
Public Types | Public Member Functions | Public Attributes | List of all members
SimpleHashMap< Key, Value, Compare > Class Template Reference

This template class implements an unordered map of keys to data, implemented as a hashtable. More...

#include "simpleHashMap.h"

Public Types

typedef SimpleKeyValuePair< Key, Value > TableEntry
 

Public Member Functions

constexpr SimpleHashMap (const Compare &comp=Compare())
 
 SimpleHashMap (const SimpleHashMap &copy)
 
 SimpleHashMap (SimpleHashMap &&from) noexcept
 
void clear ()
 Completely empties the table. More...
 
bool consider_shrink_table ()
 Shrinks the table if the allocated storage is significantly larger than the number of elements in it. More...
 
int find (const Key &key) const
 Searches for the indicated key in the table. More...
 
const Value & get_data (size_t n) const
 Returns the data in the nth entry of the table. More...
 
const Key & get_key (size_t n) const
 Returns the key in the nth entry of the table. More...
 
size_t get_num_entries () const
 Returns the number of active entries in the table. More...
 
bool is_empty () const
 Returns true if the table is empty; i.e. More...
 
Value & modify_data (size_t n)
 Returns a modifiable reference to the data in the nth entry of the table. More...
 
Value & operator [] (const Key &key)
 Returns a modifiable reference to the data associated with the indicated key, or creates a new data entry and returns its reference. More...
 
SimpleHashMapoperator= (const SimpleHashMap &copy)
 
SimpleHashMapoperator= (SimpleHashMap &&from) noexcept
 
void output (std::ostream &out) const
 
bool remove (const Key &key)
 Removes the indicated key and its associated data from the table. More...
 
void remove_element (size_t n)
 Removes the nth entry from the table. More...
 
void set_data (size_t n, const Value &data)
 Changes the data for the nth entry of the table. More...
 
void set_data (size_t n, Value &&data)
 Changes the data for the nth entry of the table. More...
 
constexpr size_t size () const
 Returns the total number of entries in the table. More...
 
int store (const Key &key, const Value &data)
 Records the indicated key/data pair in the map. More...
 
void swap (SimpleHashMap &other)
 Quickly exchanges the contents of this map and the other map. More...
 
bool validate () const
 Returns true if the internal table appears to be consistent, false if there are some internal errors. More...
 
void write (std::ostream &out) const
 

Public Attributes

Compare _comp
 
DeletedBufferChain_deleted_chain
 
size_t _num_entries
 
TableEntry_table
 
size_t _table_size
 

Detailed Description

template<class Key, class Value, class Compare = method_hash<Key, std::less<Key> >>
class SimpleHashMap< Key, Value, Compare >

This template class implements an unordered map of keys to data, implemented as a hashtable.

It is similar to STL's hash_map, but (a) it has a simpler interface (we don't mess around with iterators), (b) it wants an additional method on the Compare object, Compare::is_equal(a, b), (c) it doesn't depend on the system STL providing hash_map, (d) it allows for efficient iteration over the entries, (e) permits removal and resizing during forward iteration, and (f) it has a constexpr constructor.

It can also be used as a set, by using nullptr_t as Value typename.

Definition at line 81 of file simpleHashMap.h.

Member Function Documentation

◆ clear()

template<class Key , class Value , class Compare >
void SimpleHashMap< Key, Value, Compare >::clear ( )

Completely empties the table.

Definition at line 331 of file simpleHashMap.I.

Referenced by RenderState::clear_munger_cache().

◆ consider_shrink_table()

template<class Key , class Value , class Compare >
bool SimpleHashMap< Key, Value, Compare >::consider_shrink_table ( )
inline

Shrinks the table if the allocated storage is significantly larger than the number of elements in it.

Returns true if shrunk, false otherwise.

Definition at line 693 of file simpleHashMap.I.

◆ find()

template<class Key, class Value , class Compare >
int SimpleHashMap< Key, Value, Compare >::find ( const Key &  key) const

Searches for the indicated key in the table.

Returns its index number if it is found, or -1 if it is not present in the table.

Definition at line 156 of file simpleHashMap.I.

Referenced by CPT(), and PT().

◆ get_data()

template<class Key , class Value , class Compare >
const Value & SimpleHashMap< Key, Value, Compare >::get_data ( size_t  n) const
inline

Returns the data in the nth entry of the table.

Parameters
nshould be in the range 0 <= n < size().

Definition at line 387 of file simpleHashMap.I.

Referenced by CPT(), and PT().

◆ get_key()

template<class Key , class Value , class Compare >
const Key & SimpleHashMap< Key, Value, Compare >::get_key ( size_t  n) const
inline

◆ get_num_entries()

template<class Key , class Value , class Compare >
size_t SimpleHashMap< Key, Value, Compare >::get_num_entries ( ) const
inline

◆ is_empty()

template<class Key , class Value , class Compare >
bool SimpleHashMap< Key, Value, Compare >::is_empty ( ) const
inline

Returns true if the table is empty; i.e.

get_num_entries() == 0.

Definition at line 454 of file simpleHashMap.I.

Referenced by PT(), RenderAttrib::validate_attribs(), and RenderState::validate_states().

◆ modify_data()

template<class Key , class Value , class Compare >
Value & SimpleHashMap< Key, Value, Compare >::modify_data ( size_t  n)
inline

Returns a modifiable reference to the data in the nth entry of the table.

Parameters
nshould be in the range 0 <= n < size().

Definition at line 399 of file simpleHashMap.I.

◆ operator []()

template<class Key, class Value , class Compare >
Value & SimpleHashMap< Key, Value, Compare >::operator [] ( const Key &  key)
inline

Returns a modifiable reference to the data associated with the indicated key, or creates a new data entry and returns its reference.

Definition at line 351 of file simpleHashMap.I.

◆ remove()

template<class Key, class Value , class Compare >
bool SimpleHashMap< Key, Value, Compare >::remove ( const Key &  key)
inline

Removes the indicated key and its associated data from the table.

Returns true if the key was removed, false if it was not present.

Iterator safety: To perform removal during iteration, revisit the element at the current index if removal succeeds, keeping in mind that the number of elements has now shrunk by one.

Definition at line 254 of file simpleHashMap.I.

◆ remove_element()

template<class Key , class Value , class Compare >
void SimpleHashMap< Key, Value, Compare >::remove_element ( size_t  n)

Removes the nth entry from the table.

Parameters
nshould be in the range 0 <= n < size().

Definition at line 435 of file simpleHashMap.I.

Referenced by CPT(), and PT().

◆ set_data() [1/2]

template<class Key , class Value, class Compare >
void SimpleHashMap< Key, Value, Compare >::set_data ( size_t  n,
const Value &  data 
)
inline

Changes the data for the nth entry of the table.

Parameters
nshould be in the range 0 <= n < size().

Definition at line 411 of file simpleHashMap.I.

◆ set_data() [2/2]

template<class Key , class Value, class Compare >
void SimpleHashMap< Key, Value, Compare >::set_data ( size_t  n,
Value &&  data 
)
inline

Changes the data for the nth entry of the table.

Parameters
nshould be in the range 0 <= n < size().

Definition at line 423 of file simpleHashMap.I.

◆ size()

template<class Key , class Value , class Compare >
constexpr size_t SimpleHashMap< Key, Value, Compare >::size ( ) const

Returns the total number of entries in the table.

Same as get_num_entries.

Definition at line 364 of file simpleHashMap.I.

Referenced by PandaNode::compare_tags().

◆ store()

template<class Key, class Value, class Compare >
int SimpleHashMap< Key, Value, Compare >::store ( const Key &  key,
const Value &  data 
)

Records the indicated key/data pair in the map.

If the key was already present, silently replaces it. Returns the index at which it was stored.

Definition at line 177 of file simpleHashMap.I.

Referenced by CPT().

◆ swap()

template<class Key , class Value , class Compare >
void SimpleHashMap< Key, Value, Compare >::swap ( SimpleHashMap< Key, Value, Compare > &  other)
inline

Quickly exchanges the contents of this map and the other map.

Definition at line 132 of file simpleHashMap.I.

◆ validate()

template<class Key , class Value , class Compare >
bool SimpleHashMap< Key, Value, Compare >::validate ( ) const

Returns true if the internal table appears to be consistent, false if there are some internal errors.

Definition at line 504 of file simpleHashMap.I.

Referenced by RenderAttrib::validate_attribs(), and RenderState::validate_states().


The documentation for this class was generated from the following files: