mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-03-22 23:05:36 +08:00
refs #290 using a CRange of iterator adaptors to return containers of keys and values from CDictionary without copying the elements
This commit is contained in:
@@ -7,8 +7,7 @@
|
||||
#define BLACKMISC_DICTIONARY_H
|
||||
|
||||
#include "valueobject.h"
|
||||
#include "sequence.h"
|
||||
#include "collection.h"
|
||||
#include "iterator.h"
|
||||
#include <QHash>
|
||||
|
||||
namespace BlackMisc
|
||||
@@ -85,52 +84,6 @@ namespace BlackMisc
|
||||
typedef typename Impl<Key,Value>::const_iterator const_iterator;
|
||||
//! @}
|
||||
|
||||
|
||||
//! Return keys as collection
|
||||
CCollection<Key> keysCollection() const
|
||||
{
|
||||
CCollection<Key> collection;
|
||||
for (auto it = m_impl.begin(); it != m_impl.end(); ++it)
|
||||
{
|
||||
collection.push_back(it.key());
|
||||
}
|
||||
return collection;
|
||||
}
|
||||
|
||||
//! Return values as collection
|
||||
CCollection<Value> valuesCollection() const
|
||||
{
|
||||
CCollection<Value> collection;
|
||||
for (auto it = m_impl.begin(); it != m_impl.end(); ++it)
|
||||
{
|
||||
collection.push_back(it.value());
|
||||
}
|
||||
return collection;
|
||||
}
|
||||
|
||||
//! Return keys as sequence
|
||||
CSequence<Key> keysSequence() const
|
||||
{
|
||||
CSequence<Key> sequence;
|
||||
for (auto it = m_impl.begin(); it != m_impl.end(); ++it)
|
||||
{
|
||||
sequence.push_back(it.key());
|
||||
}
|
||||
return sequence;
|
||||
}
|
||||
|
||||
//! Return values as sequence
|
||||
CSequence<Value> valuesSequence() const
|
||||
{
|
||||
CSequence<Value> sequence;
|
||||
for (auto it = m_impl.begin(); it != m_impl.end(); ++it)
|
||||
{
|
||||
sequence.push_back(it.value());
|
||||
}
|
||||
return sequence;
|
||||
}
|
||||
|
||||
|
||||
//! Return a copy containing only those elements for which the dictionary keys return true for a given predicate.
|
||||
template <class Predicate>
|
||||
CDictionary findKeyBy(Predicate p) const
|
||||
@@ -367,8 +320,8 @@ namespace BlackMisc
|
||||
//! Return key assigned to value or if key is not found defaultKey
|
||||
const Key key(const Value &value, const Key & defaultKey) const { return m_impl.key(value, defaultKey); }
|
||||
|
||||
//! Return a collection of all keys
|
||||
CCollection<Key> keys() const { return this->keysCollection(); }
|
||||
//! Return a range of all keys
|
||||
CRange<Iterators::KeyIterator<const_iterator>> keys() const { return makeRange(Iterators::makeKeyIterator(begin()), end()); }
|
||||
|
||||
//! Remove all items with key from the dictionary
|
||||
int remove(const Key &key) { return m_impl.remove(key); }
|
||||
@@ -385,8 +338,8 @@ namespace BlackMisc
|
||||
//! Returns the value associated with the key or if key is not found defaultValue
|
||||
const Value value(const Key &key, const Value &defaultValue) const { return m_impl.value(key); }
|
||||
|
||||
//! Return a collection of all values
|
||||
CCollection<Value> values() const { return this->valuesCollection(); }
|
||||
//! Return a range of all values
|
||||
CRange<const_iterator> values() const { return makeRange(begin(), end()); }
|
||||
|
||||
//! Copy assignment.
|
||||
CDictionary &operator =(const CDictionary &other) { m_impl = other.m_impl; return *this; }
|
||||
|
||||
@@ -158,13 +158,17 @@ namespace BlackMiscTest
|
||||
d1.insert(key1, value1);
|
||||
d1.insert(key2, value2);
|
||||
|
||||
// keysCollection
|
||||
CCollection<CTestValueObject> keyCollection = d1.keysCollection();
|
||||
QVERIFY2(keyCollection.size() == 2, "keysCollection size wrong");
|
||||
// keys range
|
||||
auto keys = d1.keys();
|
||||
QVERIFY2(std::distance(keys.begin(), keys.end()) == 2, "keys range size wrong");
|
||||
|
||||
// keysSequence
|
||||
CSequence<CTestValueObject> keySequence = d1.keysSequence();
|
||||
QVERIFY2(keySequence.size() == 2, "keysSequence size wrong");
|
||||
// keys collection
|
||||
CCollection<CTestValueObject> keyCollection = d1.keys();
|
||||
QVERIFY2(keyCollection.size() == 2, "keys collection size wrong");
|
||||
|
||||
// keys sequence
|
||||
CSequence<CTestValueObject> keySequence = d1.keys();
|
||||
QVERIFY2(keySequence.size() == 2, "keys sequence size wrong");
|
||||
|
||||
// findKeyBy
|
||||
d2 = d1.findKeyBy(&CTestValueObject::getName, QString("Key1"));
|
||||
|
||||
Reference in New Issue
Block a user