wxWidgetsツリーコントロールを使用して同様の問題を解決しました。私はコントロールに入れていたオブジェクトを追跡するためにシングルトンリファレンスカウンタを使用し、それらをトラバースするイテレータを使用しました。ここに例があります。
class ReferenceCounter
{
public:
// Singleton pattern. Implementation left up to you.
static ReferenceCounter& get();
void add(const TreeData& data) {
mCounter[data.getId()].push_back(&data);
}
void remove(const TreeData& data) {
const CounterType::const_iterator itr = mCounter.find(data.getId());
if (itr != mCounter.end()) {
ItemType& items = itr->second;
items.erase(std::remove(items.begin(), items.end(), &data), items.end());
if (items.empty()) {
mCounter.erase(itr);
}
}
}
typedef std::vector<TreeData*> ItemType;
ItemType::iterator begin(const TreeData& data) {
const CounterType::const_iterator itr = mCounter.find(data.getId());
if (itr != mCounter.end()) {
return itr->second.begin();
}
// Else condition handling left up to you.
}
ItemType::iterator end(const TreeData& data) {
const CounterType::const_iterator itr = mCounter.find(data.getId());
if (itr != mCounter.end()) {
return itr->second.end();
}
// Else condition handling left up to you.
}
private:
typedef std::map<int, ItemType> CounterType;
CounterType mCounter;
};
class TreeData
{
public:
TreeData() { ReferenceCounter::get().add(*this); }
~TreeData() { ReferenceCounter::get().remove(*this); }
// Get database rows or whatever your tree is tracking.
int getId() const;
};
したがって、任意のTreeDataを指定すると、参照カウンタ内の一致するidsを持つ他のすべてのTreeDataを検索できます。これにより、名前と物事を最新の状態に保ちながら、簡単かつ迅速に行うことができます。私たちのツリーは問題なく1,000,000以上のノードを処理します。私の実装では、より簡単に使用できるように、反復処理をboost::iterator_facade
クラスにラップしました。