Looking for a special <map> 
July 23rd, 2005, 05:58 AM
| | | |
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 | 
July 23rd, 2005, 05:58 AM
| | | | re: Looking for a special <map>
"Pierre Couderc" <pierre@couderc.ccNOSPAM> skrev i en meddelelse
news:d7kvpt$2kaj$1@biggoron.nerim.net...[color=blue]
>I am looking for a "special" kind of map :
>
> - it is read like a map[/color]
This is ok[color=blue]
> - if the searched element exists, it is given back imediately[/color]
This is ok
[color=blue]
> - if the searched element does not exist, an initialise() is called to do
> the job.[/color]
This is ok - assuming you can use the default constructor to do the job.[color=blue]
> - anyway the map has a limited size, when it is full, the oldest element
> is dropped ( if recalled later, will need again an initialise())[/color]
You can't do that. The map knows about order of elements, but does not
remember when they were inserted.[color=blue]
>
> Is there some STL (or not) to do that?[/color]
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.[color=blue]
>
> My idea is to store in memory big data only if there is enough physical
> memory available.[/color]
This is more difficult to do portable. What do you mean by physical memory?
[color=blue]
>
> Thank you in advance
>
> Pierre Couderc[/color]
/Peter | 
July 23rd, 2005, 05:58 AM
| | | | re: Looking for a special <map>
Pierre Couderc <pierre@couderc.ccNOSPAM> scribbled on the stall wall:[color=blue]
> 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())
>[/color]
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----
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =---- | 
July 23rd, 2005, 05:58 AM
| | | | re: Looking for a special <map>
Wiseguy schreef:[color=blue]
> Pierre Couderc <pierre@couderc.ccNOSPAM> scribbled on the stall wall:[color=green]
> > 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())
> >[/color]
>
> subclass a traditional map to do what you want...that's the beauty of
> c++.[/color]
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 | 
July 23rd, 2005, 05:58 AM
| | | | re: Looking for a special <map>
Pierre Couderc wrote:[color=blue]
> 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[/color]
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 | 
July 23rd, 2005, 05:59 AM
| | | | re: Looking for a special <map>
"msalters" <Michiel.Salters@logicacmg.com> scribbled on the stall wall:[color=blue][color=green]
>>
>> subclass a traditional map to do what you want...that's the beauty of
>> c++.[/color]
>
> 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.
>[/color]
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----
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =---- | 
July 23rd, 2005, 05:59 AM
| | | | re: Looking for a special <map>
Wiseguy wrote:[color=blue]
> "msalters" <Michiel.Salters@logicacmg.com> scribbled on the stall wall:[color=green][color=darkred]
> >>
> >> subclass a traditional map to do what you want...that's the beauty of
> >> c++.[/color]
> >
> > 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.
> >[/color]
>
> 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.
>[/color]
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- | 
July 23rd, 2005, 05:59 AM
| | | | re: Looking for a special <map>
Shezan Baig wrote:[color=blue]
>
> Wiseguy wrote:
>[color=green]
>>"msalters" <Michiel.Salters@logicacmg.com> scribbled on the stall wall:
>>[color=darkred]
>>>>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.
>>>[/color]
>>
>>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.
>>[/color]
>
>
>
> 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.[/color]
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
[color=blue]
> -shez-
>[/color] | 
July 23rd, 2005, 06:00 AM
| | | | re: Looking for a special <map>
Calum Grant wrote:
[color=blue]
> 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
>[color=green]
>> -shez-
>>[/color][/color]
Use private inheritance. Some of the stock STL classes have protected
members for just this reason (see std::priority_queue for an example). |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,662 network members.
|