From 75c7859ab1d103e21dde80f2be8d65cc8e1a3888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Sat, 21 Sep 2024 00:25:18 +0200 Subject: [PATCH] Use size_t instead of iterators inside Object; addresses #194 (#198) --- include/rfl/Object.hpp | 80 +++++++++++++++----------------------- tests/json/test_object.cpp | 17 ++++++++ 2 files changed, 49 insertions(+), 48 deletions(-) create mode 100644 tests/json/test_object.cpp diff --git a/include/rfl/Object.hpp b/include/rfl/Object.hpp index c6e5aa4a..0053b502 100644 --- a/include/rfl/Object.hpp +++ b/include/rfl/Object.hpp @@ -36,7 +36,7 @@ class Object { using reverse_iterator = typename DataType::reverse_iterator; using const_reverse_iterator = typename DataType::const_reverse_iterator; - Object() : data_(), it_(data_.begin()) {} + Object() : data_(), i_(0) {} Object(const Object& _f) = default; @@ -96,13 +96,13 @@ class Object { /// Inserts a new element at the end. void insert(const value_type& _value) { data_.push_back(_value); - it_ = data_.begin(); + i_ = 0; } /// Inserts a new element at the end. void insert(value_type&& _value) { data_.emplace_back(std::move(_value)); - it_ = data_.begin(); + i_ = 0; } /// Inserts several new elements at the end. @@ -144,97 +144,81 @@ class Object { /// Returns the element signified by the key or creates a new one. T& operator[](const std::string& _key) { - const auto it = find(_key); - if (it != end()) { - return it->second; + const auto i = find(_key); + if (i != size()) { + return data_[i].second; } data_.emplace_back(std::make_pair(_key, T())); - it_ = data_.begin(); + i_ = 0; return data_.back().second; } /// Returns the element signified by the key or creates a new one. T& operator[](std::string&& _key) { - const auto it = find(_key); - if (it != end()) { - return it->second; + const auto i = find(_key); + if (i != size()) { + return data_[i].second; } data_.emplace_back(std::make_pair(std::move(_key), T())); - it_ = data_.begin(); + i_ = 0; return data_.back().second; } /// Deletes all elements. void clear() { data_.clear(); - it_ = data_.begin(); + i_ = 0; } /// Returns the element signified by the key or throws an exception. T& at(const std::string& _key) { - const auto it = find(_key); - if (it == end()) { + const auto i = find(_key); + if (i == size()) { throw std::runtime_error("Key named '" + _key + "' not found."); } - return it->second; + return data_[i].second; } /// Returns the element signified by the key or throws an exception. const T& at(const std::string& _key) const { - const auto it = find(_key); - if (it == end()) { + const auto i = find(_key); + if (i == size()) { throw std::runtime_error("Key named '" + _key + "' not found."); } - return it->second; + return data_[i].second; } /// Returns a result wrapping the element signified by the key. Result get(const std::string& _key) const noexcept { - const auto it = find(_key); - if (it == end()) { + const auto i = find(_key); + if (i == size()) { return Error("Key named '" + _key + "' not found."); } - return it->second; + return data_[i].second; } private: - iterator find(const std::string& _key) { - for (auto it = it_; it != end(); ++it) { - if (it->first == _key) { - it_ = it + 1; - return it; + size_t find(const std::string& _key) const { + for (auto i = i_; i < size(); ++i) { + if (data_[i].first == _key) { + i_ = i + 1; + return i; } } - for (auto it = begin(); it != it_; ++it) { - if (it->first == _key) { - it_ = it + 1; - return it; + for (auto i = 0; i < i_; ++i) { + if (data_[i].first == _key) { + i_ = i + 1; + return i; } } - return end(); - } - - const_iterator find(const std::string& _key) const { - for (auto it = it_; it != end(); ++it) { - if (it->first == _key) { - it_ = it + 1; - return it; - } - } - for (auto it = begin(); it != it_; ++it) { - if (it->first == _key) { - it_ = it + 1; - return it; - } - } - return end(); + return size(); } private: DataType data_; /// Allows faster access - mutable iterator it_; + mutable size_t i_; }; } // namespace rfl diff --git a/tests/json/test_object.cpp b/tests/json/test_object.cpp new file mode 100644 index 00000000..c40bbfd4 --- /dev/null +++ b/tests/json/test_object.cpp @@ -0,0 +1,17 @@ +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_object { + +TEST(json, test_object) { + rfl::Object o; + o["hello"] = "world"; + EXPECT_EQ(o.get("hello").value().to_string().value(), "world"); + EXPECT_EQ(o.at("hello").to_string().value(), "world"); +} + +} // namespace test_object