Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   a few stl container questions: allocators and comparers. (http://www.velocityreviews.com/forums/t619570-a-few-stl-container-questions-allocators-and-comparers.html)

sheam 06-11-2008 12:44 AM

a few stl container questions: allocators and comparers.
 
I am using a custom allocator with std::map. Is there anyway to access
the allocator instance that belongs to my instance of map?

i.e.,

std::map<int,int,MyIntComparer,MyCustomAllocator> m;
.....
.....
.....
m.allocator()->printUsageStats();

Also, I don't really need a specialized comparerer, but I had to provide
on, in order to use MyCustomAllocator. Is there a way to specify an
allocator, but not a comparator?

Thanks,
~S

sheam 06-11-2008 12:51 AM

Re: a few stl container questions: allocators and comparers.
 
sheam wrote:
> Also, I don't really need a specialized comparerer, but I had to provide
> on, in order to use MyCustomAllocator. Is there a way to specify an
> allocator, but not a comparator?


Got it, std::less<const int> did it.

~S

sheam 06-11-2008 01:02 AM

Re: a few stl container questions: allocators and comparers.
 
sheam wrote:
> std::map<int,int,MyIntComparer,MyCustomAllocator> m;
> ....
> ....
> ....
> m.allocator()->printUsageStats();


Ok, it looks like there is a "get_allocator() const" function, but it
returns a copy of the allocator. So doing something like this would not
work:

enum Pool { PoolA, PoolB, PoolC };

m.get_allocator().SetPoolID( PoolB );

I guess, I will have to do it with a template parameter for teh custom
allocator:

std::map< int, int, std::less<const int>, MyCustomAllocator<PoolB> > m;

If anyone has a better idea, let me know.

~S

sheam 06-11-2008 02:34 AM

Re: a few stl container questions: allocators and comparers.
 
sheam wrote:
Ok one last question on using my custom allocator with std::map:

should the allocator's value_type template argument be of type
pair<mykey,myval> or just myval type? Both seem to compile and run fine.

i.e.,

map< int,
Test,
less<const int>,
CustomAllocator<Test> > my_map;

or

map< int,
Test,
less<const int>,
CustomAllocator<pair<const int,Test> > > my_map;


Is the 'rebind' struct making them functionally equivilant?

Thanks.

~S

sheam 06-11-2008 02:42 AM

Re: a few stl container questions: allocators and comparers.
 
sheam wrote:
> sheam wrote:
> Ok one last question on using my custom allocator with std::map:
>
> should the allocator's value_type template argument be of type
> pair<mykey,myval> or just myval type? Both seem to compile and run fine.
>
> i.e.,
>
> map< int,
> Test,
> less<const int>,
> CustomAllocator<Test> > my_map;
>
> or
>
> map< int,
> Test,
> less<const int>,
> CustomAllocator<pair<const int,Test> > > my_map;
>
>
> Is the 'rebind' struct making them functionally equivilant?
>
> Thanks.
>
> ~S



In fact, I get same results if I do this:
map< int,
Test,
less<const int>,
CustomAllocator<float> > > my_map;

????

~S

Kai-Uwe Bux 06-11-2008 09:49 AM

Re: a few stl container questions: allocators and comparers.
 
sheam wrote:

> sheam wrote:
>> sheam wrote:
>> Ok one last question on using my custom allocator with std::map:
>>
>> should the allocator's value_type template argument be of type
>> pair<mykey,myval> or just myval type? Both seem to compile and run fine.
>>
>> i.e.,
>>
>> map< int,
>> Test,
>> less<const int>,
>> CustomAllocator<Test> > my_map;
>>
>> or
>>
>> map< int,
>> Test,
>> less<const int>,
>> CustomAllocator<pair<const int,Test> > > my_map;


This would be the usual choice.


>> Is the 'rebind' struct making them functionally equivilant?
>>
>> Thanks.
>>
>> ~S

>
>
> In fact, I get same results if I do this:
> map< int,
> Test,
> less<const int>,
> CustomAllocator<float> > > my_map;
>


The default allocator type is std::alloctor< value_type >. Internally,
std::map has to use nodes that are not of type value_type. Therefore, it ha
to use a rebind trick to get the right allocator type. That is the reason
why it could compile with float.

However, the standard says that

std::map< Key, T, C, A >::reference

is a typedef for A::reference. The same goes for pointer, const_reference,
and const_pointer. As soon as you use these, the first and last version
should fail.


Best

Kai-Uwe Bux

Shea Martin 06-11-2008 05:01 PM

Re: a few stl container questions: allocators and comparers.
 
Kai-Uwe Bux wrote:
>
> However, the standard says that
>
> std::map< Key, T, C, A >::reference
>
> is a typedef for A::reference. The same goes for pointer, const_reference,
> and const_pointer. As soon as you use these, the first and last version
> should fail.


Thanks. I noticed that it was essentially resulting an allocation of
std::pair, so that is what I was using.

~S

Frank Birbacher 06-12-2008 10:06 AM

Re: a few stl container questions: allocators and comparers.
 
Hi!

sheam schrieb:
> Ok, it looks like there is a "get_allocator() const" function, but it
> returns a copy of the allocator. So doing something like this would not
> work:

[snip]
> If anyone has a better idea, let me know.


Make the allocator stateless like a functor.

struct MyAllocState
{
Pool pool; //your enum
};

template<typename T>
struct MyAlloc
{
const shared_ptr<MyAllocState> mystate;
//...
};

use whatever pointer you like and have the state be shared by all copies
of a MyAlloc. That way you can change the state through the
get_allocator() function of a map.

The state could be helb by shared_ptr. Or by regular pointer if you
maintain an instance of the state somewhere else.

Frank


All times are GMT. The time now is 04:55 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.