From bde7cc58c7d82df7a168d3798851ed7857c6142f Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Tue, 4 Jun 2019 17:42:59 +1000 Subject: [PATCH] map/fixed: add a find function, and use it internally --- map/fixed.hpp | 66 +++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 28 deletions(-) diff --git a/map/fixed.hpp b/map/fixed.hpp index 8c4fd166..413ed2fc 100644 --- a/map/fixed.hpp +++ b/map/fixed.hpp @@ -35,6 +35,7 @@ namespace cruft::map { using value_type = std::pair; using iterator = value_type*; + using const_iterator = value_type const*; /////////////////////////////////////////////////////////////////////// @@ -68,13 +69,31 @@ namespace cruft::map { /////////////////////////////////////////////////////////////////////// - mapped_type& at (KeyT const &key) & + iterator find (KeyT const &key)& { ComparatorT cmp {}; - for (auto &i: *this) - if (cmp (i.first, key)) - return i.second; + for (auto cursor = begin (), last = end (); cursor != last; ++cursor) + if (cmp (cursor->first, key)) + return cursor; + return end (); + } + //--------------------------------------------------------------------- + const_iterator find (KeyT const &key) const& + { + ComparatorT cmp {}; + for (auto cursor = begin (), last = end (); cursor != last; ++cursor) + if (cmp (cursor->first, key)) + return cursor; + return end (); + } + + + /////////////////////////////////////////////////////////////////////// + mapped_type& at (KeyT const &key) & + { + if (auto pos = find (key); pos != end ()) + return pos->second; throw std::out_of_range ("Element out of range"); } @@ -82,11 +101,8 @@ namespace cruft::map { //--------------------------------------------------------------------- mapped_type const& at (KeyT const &key) const& { - ComparatorT cmp {}; - for (auto &i: *this) - if (cmp (i.first, key)) - return i.second; - + if (auto pos = find (key); pos != end ()) + return pos->second; throw std::out_of_range ("Element out of range"); } @@ -94,12 +110,8 @@ namespace cruft::map { //--------------------------------------------------------------------- std::pair insert (value_type const &keyval) { - ComparatorT cmp {}; - for (auto &i: *this) { - if (cmp (i.first, keyval.first)) { - return { &i, false }; - } - } + if (auto pos = find (keyval.first); pos != end ()) + return { pos, false }; if (m_size >= capacity ()) throw std::bad_alloc (); @@ -115,22 +127,18 @@ namespace cruft::map { //--------------------------------------------------------------------- mapped_type const& operator[] (KeyT const &key) const { - ComparatorT cmp {}; - for (auto &i: *this) - if (cmp (i.first, key)) - return i.second; - throw std::out_of_range ("Invalid key"); + return at (key); } //--------------------------------------------------------------------- mapped_type& operator[] (KeyT const &key) { - ComparatorT cmp {}; - for (auto &i: *this) - if (cmp (i.first, key)) - return i.second; + // If it's present, then return a reference to the value + if (auto pos = find (key); pos != end ()) + return pos->second; + // Make space for a new entry, initialise it, and return that. if (m_size >= capacity ()) throw std::bad_alloc (); @@ -152,12 +160,14 @@ namespace cruft::map { //--------------------------------------------------------------------- - auto begin (void)& { return std::begin (m_store.data); } - auto end (void)& { return begin () + m_size; } + iterator begin (void)& { return std::begin (m_store.data); } + iterator end (void)& { return begin () + m_size; } - auto begin (void) const& { return std::begin (m_store.data); } - auto end (void) const& { return begin () + m_size; } + const_iterator begin (void) const& { return std::begin (m_store.data); } + const_iterator end (void) const& { return begin () + m_size; } + const_iterator cbegin (void) const& { return begin (); } + const_iterator cend (void) const& { return end (); } //--------------------------------------------------------------------- auto size (void) const { return m_size; }