Looking for a special <map>

P

Pierre Couderc

I am looking for a "special" kind of map :

- it is read like a map
- if the searched element exists, it is given back imediately
- if the searched element does not exist, an initialise() is called to
do the job.
- anyway the map has a limited size, when it is full, the oldest element
is dropped ( if recalled later, will need again an initialise())

Is there some STL (or not) to do that?

My idea is to store in memory big data only if there is enough physical
memory available.

Thank you in advance

Pierre Couderc
 
P

Peter Koch Larsen

Pierre Couderc said:
I am looking for a "special" kind of map :

- it is read like a map This is ok
- if the searched element exists, it is given back imediately
This is ok
- if the searched element does not exist, an initialise() is called to do
the job.
This is ok - assuming you can use the default constructor to do the job.
- anyway the map has a limited size, when it is full, the oldest element
is dropped ( if recalled later, will need again an initialise())
You can't do that. The map knows about order of elements, but does not
remember when they were inserted.
Is there some STL (or not) to do that?
Nope. It would not be a big problem creating one such structure. You would
need two structures: one to keep elements according to their "age" and one
to keep the elements sorted.
My idea is to store in memory big data only if there is enough physical
memory available.
This is more difficult to do portable. What do you mean by physical memory?
Thank you in advance

Pierre Couderc
/Peter
 
W

Wiseguy

Pierre Couderc said:
I am looking for a "special" kind of map :

- it is read like a map
- if the searched element exists, it is given back imediately
- if the searched element does not exist, an initialise() is called to
do the job.
- anyway the map has a limited size, when it is full, the oldest element
is dropped ( if recalled later, will need again an initialise())


subclass a traditional map to do what you want...that's the beauty of
c++.


--
dual 2.8Ghz Xeon; 2GB RAM; 500GB ATA-133; nVidia powered
Linux 2.6.10; glibc-2.3.5; vendor neutral home-brewed installation

----anything after this line is ANNOYING CRAP that the newsserver adds-----
---directly contact newsfeeds and ISPs that piggy back them to complain----
 
M

msalters

Wiseguy schreef:
subclass a traditional map to do what you want...that's the beauty of
c++.

Why? It's not a std::map in the OO sense. Remember, in OO "IS-A" means
is-a substitute. You can't use such a special map where you would
otherwise
use a std::map. E.g. the dtor is not virtual, nor is the operator[].

Of course, this fixedSizeMap can probably use a std::map member.

HTH,
Michiel Salters
 
C

Calum Grant

Pierre said:
I am looking for a "special" kind of map :

- it is read like a map
- if the searched element exists, it is given back imediately
- if the searched element does not exist, an initialise() is called to
do the job.
- anyway the map has a limited size, when it is full, the oldest element
is dropped ( if recalled later, will need again an initialise())

Is there some STL (or not) to do that?

My idea is to store in memory big data only if there is enough physical
memory available.

Thank you in advance

Pierre Couderc

Check out Boost.MultiIndex.

You could build something like that on top of this container - it is
more efficient than maintaining several STL containers.

For example, you could make a composite index of a linked list and a
balanced tree. Your linked list would be used to find the oldest
member, and the tree would be used for indexed lookups.

I don't know if there's something "off the shelf" to do what you want.

Calum
 
W

Wiseguy

msalters said:
subclass a traditional map to do what you want...that's the beauty of
c++.

Why? It's not a std::map in the OO sense. Remember, in OO "IS-A" means
is-a substitute. You can't use such a special map where you would
otherwise
use a std::map. E.g. the dtor is not virtual, nor is the operator[].

Of course, this fixedSizeMap can probably use a std::map member.

shouldn't matter whether members are virtual or not. std::map give a good
starting point to CREATE a class that does what the OP wants. whether
the map is subclassed or is merely a member is merely a point of coding
preference.


--
dual 2.8Ghz Xeon; 2GB RAM; 500GB ATA-133; nVidia powered
Linux 2.6.10; glibc-2.3.5; vendor neutral home-brewed installation

----anything after this line is ANNOYING CRAP that the newsserver adds-----
---directly contact newsfeeds and ISPs that piggy back them to complain----
 
S

Shezan Baig

Wiseguy said:
msalters said:
subclass a traditional map to do what you want...that's the beauty of
c++.

Why? It's not a std::map in the OO sense. Remember, in OO "IS-A" means
is-a substitute. You can't use such a special map where you would
otherwise
use a std::map. E.g. the dtor is not virtual, nor is the operator[].

Of course, this fixedSizeMap can probably use a std::map member.

shouldn't matter whether members are virtual or not. std::map give a good
starting point to CREATE a class that does what the OP wants. whether
the map is subclassed or is merely a member is merely a point of coding
preference.


IMO, a problem like this is better solved through containment rather
than structural inheritance. If it means creating 'forwarding'
functions, then so be it. Let's not be lazy.

-shez-
 
C

Calum Grant

Shezan said:
msalters said:
subclass a traditional map to do what you want...that's the beauty of
c++.

Why? It's not a std::map in the OO sense. Remember, in OO "IS-A" means
is-a substitute. You can't use such a special map where you would
otherwise
use a std::map. E.g. the dtor is not virtual, nor is the operator[].

Of course, this fixedSizeMap can probably use a std::map member.

shouldn't matter whether members are virtual or not. std::map give a good
starting point to CREATE a class that does what the OP wants. whether
the map is subclassed or is merely a member is merely a point of coding
preference.



IMO, a problem like this is better solved through containment rather
than structural inheritance. If it means creating 'forwarding'
functions, then so be it. Let's not be lazy.

Good point. You don't want to violate your class invariant by exposing
the base class. For example, if someone called std::map::insert,
instead of mymap::insert, then your invariant (that you don't exceed a
certain size) could be violated. So you'd probably be looking at
protected inheritance anyway, in which case you may as well just use a
member.

Calum
 
R

red floyd

Calum said:
Good point. You don't want to violate your class invariant by exposing
the base class. For example, if someone called std::map::insert,
instead of mymap::insert, then your invariant (that you don't exceed a
certain size) could be violated. So you'd probably be looking at
protected inheritance anyway, in which case you may as well just use a
member.

Calum

Use private inheritance. Some of the stock STL classes have protected
members for just this reason (see std::priority_queue for an example).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top