From bd02c74ea8beb96d35c289f66c25477f4b6f2cf0 Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Thu, 3 Jul 2014 19:05:52 +0100 Subject: [PATCH] refs #290 using a CRange of iterator adaptors to return containers of keys and values from CDictionary without copying the elements --- src/blackmisc/dictionary.h | 57 +++--------------------------- tests/blackmisc/testcontainers.cpp | 16 +++++---- 2 files changed, 15 insertions(+), 58 deletions(-) diff --git a/src/blackmisc/dictionary.h b/src/blackmisc/dictionary.h index 5e0ea8314..7ad30eb75 100644 --- a/src/blackmisc/dictionary.h +++ b/src/blackmisc/dictionary.h @@ -7,8 +7,7 @@ #define BLACKMISC_DICTIONARY_H #include "valueobject.h" -#include "sequence.h" -#include "collection.h" +#include "iterator.h" #include namespace BlackMisc @@ -85,52 +84,6 @@ namespace BlackMisc typedef typename Impl::const_iterator const_iterator; //! @} - - //! Return keys as collection - CCollection keysCollection() const - { - CCollection collection; - for (auto it = m_impl.begin(); it != m_impl.end(); ++it) - { - collection.push_back(it.key()); - } - return collection; - } - - //! Return values as collection - CCollection valuesCollection() const - { - CCollection collection; - for (auto it = m_impl.begin(); it != m_impl.end(); ++it) - { - collection.push_back(it.value()); - } - return collection; - } - - //! Return keys as sequence - CSequence keysSequence() const - { - CSequence sequence; - for (auto it = m_impl.begin(); it != m_impl.end(); ++it) - { - sequence.push_back(it.key()); - } - return sequence; - } - - //! Return values as sequence - CSequence valuesSequence() const - { - CSequence 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 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 keys() const { return this->keysCollection(); } + //! Return a range of all keys + CRange> 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 values() const { return this->valuesCollection(); } + //! Return a range of all values + CRange values() const { return makeRange(begin(), end()); } //! Copy assignment. CDictionary &operator =(const CDictionary &other) { m_impl = other.m_impl; return *this; } diff --git a/tests/blackmisc/testcontainers.cpp b/tests/blackmisc/testcontainers.cpp index e89bd05d0..75924f14a 100644 --- a/tests/blackmisc/testcontainers.cpp +++ b/tests/blackmisc/testcontainers.cpp @@ -158,13 +158,17 @@ namespace BlackMiscTest d1.insert(key1, value1); d1.insert(key2, value2); - // keysCollection - CCollection 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 keySequence = d1.keysSequence(); - QVERIFY2(keySequence.size() == 2, "keysSequence size wrong"); + // keys collection + CCollection keyCollection = d1.keys(); + QVERIFY2(keyCollection.size() == 2, "keys collection size wrong"); + + // keys sequence + CSequence keySequence = d1.keys(); + QVERIFY2(keySequence.size() == 2, "keys sequence size wrong"); // findKeyBy d2 = d1.findKeyBy(&CTestValueObject::getName, QString("Key1"));