简体   繁体   中英

Is there a way to cap the max size on a STL::map container?

My application can only process up to a certain number of entry in the map structure, how do I specify that limit in my code so that my code does not get overwhelmed (for lack of better term). Is there a way to specify the max limit when defining the variable of type map?

Thanks

There's no way to set a limit when instantiating the map, though I supposed you could have your own safe guard when accessing it. For example:

if (mymap.find(a) == mymap.end() and mymap.size() >= MAX_MAP_ALLOWED) {
    throw (runtime_error("map limit exceeded"));
} else {
    mymap[a] = b;
}

You could possibly create your own map class that encapsulates these checks.

The stl containers also take an ' allocator ' as a (defaulted) parameter. This allocator is the container's means to allocate new space for it's data.

If you define a 'capped' allocator (sounds simple, hey?), you're there.

EDIT - In some fora, I found that the allocators, though initially intended stateless, can be statefull on most (modern) compilers. That's why I carry on on this. It's quite cumbersome, though, to do it this way, and probably way more easy and comprenensible to aggregate your map type in a cappedmap adapter.

It took me a lot of moments here and there, but here I got a compiling, capped, example:

// an allocator with maximally MAX elements.
template< typename T, size_t MAX = 5 >
struct AllocateCapped {

    // reuses an existing allocator
    typedef std::allocator<T> tallocator;

    typedef typename tallocator::value_type value_type;
    typedef typename tallocator::pointer pointer;
    typedef typename tallocator::reference reference;
    typedef typename tallocator::const_pointer const_pointer;
    typedef typename tallocator::const_reference const_reference;
    typedef typename tallocator::size_type size_type;
    typedef typename tallocator::difference_type difference_type;

The actual code of the capped allocator delegates to the allocator member:

    size_t free;
    tallocator allocator;

    AllocateCapped():free(MAX){
        printf("capped");
    }

template<typename T2>
    AllocateCapped( const AllocateCapped<T2>& other ){}

    pointer allocate( size_type n, const_pointer hint = 0) {
        if( !free ) throw std::bad_alloc();
        free-=n;
        return allocator.allocate( n, hint );
    }

    void deallocate( pointer p, size_type n ) {
        free+=n;
        allocator.deallocate(p,n);
    }

    size_type max_size() const { return free; }
    void construct( pointer p, const_reference val ) {
        return allocator.construct(p,val);
    }
    void destroy( pointer p ) { allocator.destroy(p); }

    template<class _Other>
    struct rebind 
    {   // convert this type to _ALLOCATOR<_Other>
        typedef typename AllocateCapped<_Other> other;
    };

};

This allocator can be used like this:

// example structure
struct s {
    int i;
    s():i(){}
    s(int i):i(i){}
};

int main(int argc, char* argv[]) {
typedef AllocateCapped< std::pair<const int, s> > talloc;
talloc a;
talloc::pointer p = reinterpret_cast<talloc::pointer>( a.allocate(1,0) );
a.construct(p, talloc::value_type() );
a.destroy(p);
a.deallocate(p, 1 );

std::map<int , s, std::less<int>, talloc > m;
std::vector<int, AllocateCapped<int> > v;
for( int i = 0; i != 4; ++i ) {
    m[i]=s(i);
    v.push_back(i);
}
m[5]=s(5); // throws
v.push_back(5); // throws
return 0;
}

Note: not thoroughly tested. It's just an idea.

After trying out the idea of a capped allocator, I think it's way more straightforward to aggregate an std::map (note: not inherit-from! At least not publicly) in a cappedadaptor .

template<typename tKey, typename tVal> class cappedmap {
   typedef std::map<tKey,tVal> tmap;
   tmap mymap;

   cappedmap(size_t amax):mymax(amax){}

   // adapt the map interface
   pair<tmap::iterator,bool> insert( tmap::value_type kv ) { 
        if( mymap.size() > mymax ) throw myexcept();
        return mymap.insert(kv);
   }

   tVal operator[]( tKey k ) { 
     tVal v = mymap[k];
     if( mymap.size() > mymax ) { 
        mymap.remove(k)
        throw myexcept();
     }
   }
   ...
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM