I am looking for a data structures that have the next features:
- have key and value
- can find the value in O(n) > t > O(logn ) or O(1)
- can pull the first element and the last element I insert.
TreeMep is not good, because if I insert key (as string) "b" and then key "a" if I will pull the first one I will get "a" instead of "b"
ConcurrentSkipListMap is not good because I can't rely on size func'
will appreciate any help
thanks
You can use a deque
(double ended queue) cross-referenced with a multimap
(tipically a binary search tree), which allows duplicate keys.
Every element of the queue would have a reference (an iterator) to the corresponding element of the map and vice-versa.
This way, you can search in the map in O(log N) and you can push/pull at either ends of the sequence in O(log N).
You can decide to store the strings in the map or in the queue.
Here is an implementation in C++:
#include <string>
#include <map>
#include <deque>
typedef int my_key_type; // Key type
typedef std::string my_value_type; // Value type
struct queueitem; // Queue element
typedef std::deque<queueitem> my_queue; // Queue
typedef std::multimap <my_key_type,
my_queue::iterator> my_map; // Map
typedef std::pair <my_key_type,
my_queue::iterator> my_map_pair; // Map element
struct queueitem
{
my_value_type value;
my_map::iterator mapitem;
queueitem (const my_value_type & val) : value(val) {}
};
class mapqueue
{
public:
mapqueue () {}
my_value_type find (my_key_type key) const;
size_t index_of (my_key_type key) const;
my_value_type front_value () const { return Q.front().value; }
my_value_type back_value () const { return Q.back().value; }
my_key_type front_key () const
{ return Q.front().mapitem->first; }
my_key_type back_key () const
{ return Q.back().mapitem->first; }
void push_front (my_key_type key,
const my_value_type & value);
void push_back (my_key_type key,
const my_value_type & value);
void pop_front ();
void pop_back ();
private:
my_queue Q;
my_map M;
mapqueue (const mapqueue &) {}
mapqueue & operator= (const mapqueue &) { return *this; }
};
using namespace std;
my_value_type mapqueue::find (my_key_type key) const
{
my_map::const_iterator it = M.find (key);
if (it==M.end())
throw "Not found";
return it->second->value;
}
size_t mapqueue::index_of (my_key_type key) const
{
my_map::const_iterator it = M.find (key);
if (it==M.end())
throw "Not found";
return it->second - Q.begin();
}
void mapqueue::push_front (my_key_type key,
const my_value_type & value)
{
Q.push_front (queueitem(value));
my_queue::iterator qit = Q.begin ();
qit->mapitem = M.insert (my_map_pair(key,qit));
}
void mapqueue::push_back (my_key_type key,
const my_value_type & value)
{
Q.push_back (queueitem(value));
my_queue::iterator qit = Q.end () - 1;
qit->mapitem = M.insert (my_map_pair(key,qit));
}
void mapqueue::pop_front ()
{
M.erase (Q.front().mapitem);
Q.pop_front ();
}
void mapqueue::pop_back ()
{
M.erase (Q.back().mapitem);
Q.pop_back ();
}
This also supports finding the index in the queue of any given key in O(log N). If you don't need this, you can simplify the design storing the strings in the map and removing the references from the map to the queue.
Update: The key and value types are now specified via typedef
s. This way it's easy to change them. It would be interesting to convert the whole thing in a template parametrized by these two types. This way, the same code would be reusable for different pairs of key-value types even in the same program.
Update: There is a tool for the general case: the Boost Multi-index Containers Library. See this example in the documentation.