2011-01-18 9 views
1

私のアプリケーションでは、マップ構造内で特定の数のエントリしか処理できません。私のコードでは制限がないので、コードを圧倒することはできません。型マップの変数を定義する際の最大限度を指定する方法はありますか?STL :: mapコンテナの最大サイズを制限する方法はありますか?

おかげ

+1

N個以上の要素をマップに入れない以外は、 –

答えて

3

マップをインスタンス化する際に制限を設定する方法はありませんが、アクセスする際には自分自身で安全なガードを持つことができます。例:

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

これらのチェックをカプセル化する独自のマップクラスを作成できます。

1

STLコンテナは、(債務不履行)をパラメータとして 'allocator' を取ります。このアロケータは、データのために新しい領域を割り当てるコンテナの手段です。

「キャップ付き」アロケータを定義している場合は、そこにいます。

EDIT - いくつかのforaでは、アロケータは当初はステートレスであるとは言えますが、ほとんどの(現代の)コンパイラではステートフルであることがわかりました。それが私がこれを続ける理由です。ただし、これを行うにはかなり面倒です。また、マップド・マップ・アダプタでマップ・タイプを集計する方が簡単で手ごろな方法です。

それはここにある私の瞬間の多くを取ったが、ここで私がコンパイル、蓋をし、一例を得た:

// 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; 

allocatorメンバーに頂いたアロケータの代表者の実際のコード:

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; 
    }; 

}; 

// 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; 
} 

注:

このアロケータは、このように使用することができないトール徹底的にテストされています。それは単なるアイデアです。

+0

興味深い。ユーザーは 'max_size()'をオーバーライドする必要がありますか? – chrisaycock

+2

残念なことに、アロケータはコンテナ単位ではありません。そのため、サイズを制限したいマップインスタンスごとに別々のアロケータ型を持たない限り、このアプローチは動作しません。 –

+0

@James:特別なアロケータで 'map'を' typedef'することができます。 –

1

キャップ付きアロケータのアイデアを試した後、std::map(注:継承元ではありません!少なくとも公開しないでください)を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(); 
    } 
    } 
    ... 
}; 
関連する問題