a few stl container questions: allocators and comparers.

Discussion in 'C++' started by sheam, Jun 11, 2008.

  1. sheam

    sheam Guest

    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, Jun 11, 2008
    #1
    1. Advertising

  2. sheam

    sheam Guest

    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, Jun 11, 2008
    #2
    1. Advertising

  3. sheam

    sheam Guest

    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, Jun 11, 2008
    #3
  4. sheam

    sheam Guest

    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, Jun 11, 2008
    #4
  5. sheam

    sheam Guest

    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
     
    sheam, Jun 11, 2008
    #5
  6. sheam

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Jun 11, 2008
    #6
  7. sheam

    Shea Martin Guest

    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
     
    Shea Martin, Jun 11, 2008
    #7
  8. 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
     
    Frank Birbacher, Jun 12, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Maitre Bart
    Replies:
    2
    Views:
    536
    Maitre Bart
    Feb 11, 2004
  2. mar00ned

    Help with STL allocators

    mar00ned, Sep 24, 2004, in forum: C++
    Replies:
    6
    Views:
    458
    John Harrison
    Sep 24, 2004
  3. Replies:
    4
    Views:
    815
    Daniel T.
    Feb 16, 2006
  4. Murali
    Replies:
    2
    Views:
    593
    Jerry Coffin
    Mar 9, 2006
  5. wolverine
    Replies:
    2
    Views:
    462
    Marcus Kwok
    Jul 24, 2006
Loading...

Share This Page