From a8d1cd9c8b2cbf1bb906b50317909bb7795f201e Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 4 Jan 2015 14:01:07 +0100 Subject: [PATCH] trivial implementation of index / snapshot table lots of room for improvement here :) --- src/lib/diff/index-table.hpp | 42 ++++++++++++++++---- tests/library/diff/diff-index-table-test.cpp | 2 +- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/lib/diff/index-table.hpp b/src/lib/diff/index-table.hpp index 1c57f5b2f..292584fd3 100644 --- a/src/lib/diff/index-table.hpp +++ b/src/lib/diff/index-table.hpp @@ -40,6 +40,8 @@ #include "lib/error.hpp" +#include "lib/util.hpp" +#include "lib/format-string.hpp" #include #include @@ -48,42 +50,68 @@ namespace lib { namespace diff{ - using std::vector; - using std::map; + namespace error = lumiera::error; + + using util::_Fmt; + + /** data snapshot and lookup table */ template class IndexTable { + std::vector data_; + std::map idx_; + public: template IndexTable(SEQ const& seq) { - UNIMPLEMENTED("build index"); + size_t i = 0; + for (auto const& elm : seq) + { + __rejectDuplicate(elm); + data_.push_back (elm); + idx_[elm] = i++; + } } size_t size() const { - UNIMPLEMENTED("sequence size"); + return data_.size(); } + VAL const& getElement (size_t i) const { - UNIMPLEMENTED("indexed value access"); + REQUIRE (i < size()); + return data_[i]; } + bool contains (VAL const& elm) const { - return size() == pos(elm); + return pos(elm) != size(); } + size_t pos (VAL const& elm) const { - UNIMPLEMENTED("index lookup"); + auto entry = idx_.find (elm); + return entry==idx_.end()? size() + : entry->second; + } + + private: + void + __rejectDuplicate (VAL const& elm) + { + if (util::contains (idx_, elm)) + throw error::Logic(_Fmt("Attempt to add duplicate %s to index table") % elm); } }; diff --git a/tests/library/diff/diff-index-table-test.cpp b/tests/library/diff/diff-index-table-test.cpp index ad6c65471..bb1139e8f 100644 --- a/tests/library/diff/diff-index-table-test.cpp +++ b/tests/library/diff/diff-index-table-test.cpp @@ -176,7 +176,7 @@ namespace test{ CHECK ( idxB.contains(a1)); CHECK (!idxB.contains(b1)); - idxA = idxB; + idxB = idxA; CHECK (4 == idxA.size()); CHECK (4 == idxB.size());