Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to limit the size of an STL Map?

Tags:

c++

map

stl

I want to implement some sort of lookup table in C++ that will act as a cache. It is meant to emulate a piece of hardware I'm simulating.

The keys are non-integer, so I'm guessing a hash is in order. I have no intention of inventing the wheel so I intend to use std::map for this (though suggestions for alternatives are welcome).

The question is, is there any way to limit the size of the hash to emulate the fact that my hardware is of finite size? I'd expect the hash's insert method to return an error message or throw an exception if the limit is reached.

If there is no such way, I'll simply check its size before trying to insert, but that seems like an inelegant way to do it.

like image 546
Nathan Fellman Avatar asked Apr 26 '10 07:04

Nathan Fellman


People also ask

How do you set the size of a map in C++?

map::size() In C++, size() function is used to return the total number of elements present in the map. Return Value: It returns the number of elements present in the map.

Can we define size of map in C++?

map::size() function is an inbuilt function in C++ STL, which is defined in header file. size() is used to check the size of the map container. This function gives size or we can say gives us the number of elements in the map container associated.

What is the maximum size of a map in C++?

In C++, map::max_size returns the max. number of elements. In a vanilla map , there's an upper bound of at most SIZE_T_MAX elements, which is 2^64-1 on modern hardware.

Is used to reduce the size of map?

The square method is the most common and simplest method for enlargement and reduction of maps. In order to enlarge a map, cover the original map with a set of squares of equal sides. The side of the squares has to be enlarged proportionally to that the original map.


3 Answers

First thing is that a map structure is not a hash table, but rather a balanced binary tree. This has an impact in the lookup times O(log N) instead of O(1) for an optimal hash implementation. This may or not affect your problem (if the number of actual keys and operations is small it can suffice, but the emulation of the cache can be somehow suboptimal.

The simplest solution that you can do is encapsulate the actual data structure into a class that checks the size in each insertion (size lookups should be implemented in constant time in all STL implementations) and fails if you are trying to add one too many elements.

class cache {
public:
   static const int max_size = 100;
   typedef std::map<key_t, value_t> cache_map_t;
   void add( key_t key, value_t value ) {
      cache_map_t::const_iterator it = m_table.find( key );
      if ( it != m_table.end() && m_table.size() == max_size ) { 
         // handle error, throw exception...
      } else {
         m_table.insert( it, std::make_pair(key,value) );
      }
   }
private:
   cache_map_t m_table;
};
// Don't forget, in just one translation unit:
const int cache::max_size;
like image 99
David Rodríguez - dribeas Avatar answered Oct 25 '22 13:10

David Rodríguez - dribeas


There is no way to "automatically" limit the size of an std::map, simply because a "limited std::map" would not be an std::map anymore.

Wrap the std::map in a class of your own which just checks the size of the std::map against a constant before inserting, and does whatever you want in case the maximum size has been reached.

like image 40
Daniel Daranas Avatar answered Oct 25 '22 14:10

Daniel Daranas


If you're implementing LRU caches or similar, I've found boost::bimap to be invaluable. Use one index to be the primary "object ID" key (which can be an efficient vector view if you have a simple enumeration of objects), and the other index can be an ordered timestamp.

like image 39
timday Avatar answered Oct 25 '22 14:10

timday