して、別の項目へのアクセス権を与え、リストを返す必要はありません。 Andyのソリューションは、効率的な方法でアイテムを繰り返し処理する必要がない場合に便利です。しかし、私はconst std :: listと意味的に同等なものを求めていました。おそらくそれは過剰ですが、パフォーマンスは最適化後にほとんど違いはありません:
ConstListという名前のクラスでプライベートに拡張するのは、BOOST_FOREACHを反復させても何も変更しないようにするだけです誰でも。私はアイテムから子クラスにリストフックを移動したので、アイテムオブジェクトを使ってリストを変更することはできません。フックを子クラスに格納していますが、イテレータはアイテムクラスへの参照を返しています。私はこのソリューションを2つのテンプレート付きクラスにコーディングして、どのアイテムクラスにも簡単に適用できます。
私は、ConstListクラスとHookedItemクラスの後にtests.cppというヘッダーファイルを作成し、テストとベンチマークに使用しました。 ConstListクラスは反復処理中に同等のパフォーマンスを持つことがわかります。
非常にきれいに動作し、ユーザーコードもきれいに保たれます。それで、これは質問をする:どうしてこれはすでにブーストになっていないのですか?!?!?
は、いかなる目的のために、次のコードを使用してお気軽に:)
をPS:この解決策を考え出すながら、私は啓示の瞬間を持っていた:「constが」特殊なケースのためのシンタックスシュガーにすぎません適切なクラス階層で既に達成できるものの中から選択します。それは本当ですか、私は過度に一般化していますか?
------------------ ConstList.h -----------------------
#include <boost/intrusive/list_hook.hpp>
template < typename T>
struct type_wrapper{ typedef T type;};
template<class listType, class owner, class item>
class ConstList: private listType {
friend class type_wrapper<owner>::type;
public:
class iterator {
typename listType::iterator it;
public:
typedef std::forward_iterator_tag iterator_category;
typedef item value_type;
typedef int difference_type;
typedef item* pointer;
typedef item& reference;
template<class T>
iterator(const T it): it(it){}
bool operator==(iterator & otherIt) {return it==otherIt.it;}
iterator & operator++() {
it++;
return *this;
}
item & operator*() {
return *it;
}
};
iterator begin() {
return iterator(listType::begin());
}
iterator end() {
return iterator(listType::end());
}
};
template<class item, class owner, class hooktype>
class HookedItem: public item {
friend class type_wrapper<owner>::type;
public:
hooktype hook_;
typedef boost::intrusive::member_hook<HookedItem, hooktype, &HookedItem::hook_> MemberHookOption;
private:
template<class Arg1, class Arg2>
HookedItem(Arg1 &arg1, Arg2 &arg2): item(arg1, arg2){}
};
------------------テスト。cpp -----------------------
#include<cstdio>
#include<boost/checked_delete.hpp>
#include<ConstList.h>
#include<boost/intrusive/list.hpp>
#include<boost/foreach.hpp>
using namespace boost::intrusive;
class myOwner;
class myItem {
public:
int a,b; //arbitrary members
myItem(int a, int b): a(a), b(b){};
};
typedef HookedItem<myItem,myOwner,list_member_hook<> > myHookedItem;
typedef list<myHookedItem, typename myHookedItem::MemberHookOption> myItemList;
typedef ConstList<myItemList,myOwner,myItem> constItemList;
class myOwner {
public:
constItemList constList;
myItemList & nonConstList;
myOwner(): nonConstList(constList) {}
constItemList & getItems() { return constList;}
myItem * generateItem(int a, int b) {
myHookedItem * newItem = new myHookedItem(a,b);
nonConstList.push_back(*newItem);
return newItem;
}
~myOwner() {nonConstList.clear_and_dispose(boost::checked_delete<myHookedItem>);}
};
int main(int argc, char **argv) {
myOwner owner;
int avoidOptimization=0;
for(int i=0; i<1000000; i++) {
owner.generateItem(i,i);
}
clock_t start = clock();
for(int i=0; i<1000; i++)
BOOST_FOREACH(myItem & item, owner.constList)
avoidOptimization+=item.a;
printf ("%f\n", ((double)clock() - start)/CLOCKS_PER_SEC);
start = clock();
for(int i=0; i<1000; i++)
BOOST_FOREACH(myHookedItem & item, owner.nonConstList)
avoidOptimization+=item.a;
printf ("%f\n", ((double)clock() - start)/CLOCKS_PER_SEC);
printf ("%d",avoidOptimization);
return 0;
}
------------コンソール出力----- ------------
4.690000
4.700000
1764472320
これは本当に良い答えです。私は侵略的なリストへのポインタのstlリストから切り替えているので、私の心はリストを返すことに固執しました。この解決策から生じる1つの問題は、ユーザが非const項目自体を使用してリストを修正できることである。そこに解決策はありますか? – enobayram
OK、私は項目を介してリストを操作する可能性について、次の解決策について考えました。リストフックを持たない「itemBase」クラスを持ち、そのインスタンスをユーザーに渡して渡します。私のクラスのリストは、itemBaseとリストフックから継承して何もしないクラス "item"のオブジェクトを保持します。それは意味をなさないか、何か見落としていますか? – enobayram
はい、問題はありません。 '' list_base_hook'からプライベートに派生し、 'boost :: intrusive :: list- 'を 'Item'のフレンドとして宣言するという別のオプションもあります(' base hooksを使うと仮定します)。 –