#ifndef STAR_MAP_HPP #define STAR_MAP_HPP #include #include #include "StarFlatHashMap.hpp" #include "StarList.hpp" namespace Star { STAR_EXCEPTION(MapException, StarException); template class MapMixin : public BaseMap { public: typedef BaseMap Base; typedef typename Base::iterator iterator; typedef typename Base::const_iterator const_iterator; typedef typename Base::key_type key_type; typedef typename Base::mapped_type mapped_type; typedef typename Base::value_type value_type; typedef typename std::decay::type* mapped_ptr; typedef typename std::decay::type const* mapped_const_ptr; template static MapMixin from(MapType const& m); using Base::Base; List keys() const; List values() const; List> pairs() const; bool contains(key_type const& k) const; // Removes the item with key k and returns true if contains(k) is true, // false otherwise. bool remove(key_type const& k); // Removes *all* items that have a value matching the given one. Returns // true if any elements were removed. bool removeValues(mapped_type const& v); // Throws exception if key not found mapped_type take(key_type const& k); Maybe maybeTake(key_type const& k); // Throws exception if key not found mapped_type& get(key_type const& k); mapped_type const& get(key_type const& k) const; // Return d if key not found mapped_type value(key_type const& k, mapped_type d = mapped_type()) const; Maybe maybe(key_type const& k) const; mapped_const_ptr ptr(key_type const& k) const; mapped_ptr ptr(key_type const& k); // Finds first value matching the given value and returns its key. key_type keyOf(mapped_type const& v) const; // Finds all of the values matching the given value and returns their keys. List keysOf(mapped_type const& v) const; bool hasValue(mapped_type const& v) const; using Base::insert; // Same as insert(value_type), returns the iterator to either the newly // inserted value or the existing value, and then a bool that is true if the // new element was inserted. pair insert(key_type k, mapped_type v); // Add a key / value pair, throw if the key already exists mapped_type& add(key_type k, mapped_type v); // Set a key to a value, always override if it already exists mapped_type& set(key_type k, mapped_type v); // Appends all values of given map into this map. If overwite is false, then // skips values that already exist in this map. Returns false if any keys // previously existed. template bool merge(MapType const& m, bool overwrite = false); bool operator==(MapMixin const& m) const; }; template std::ostream& operator<<(std::ostream& os, MapMixin const& m); template , typename Allocator = std::allocator>> using Map = MapMixin>; template , typename Equals = std::equal_to, typename Allocator = std::allocator>> using HashMap = MapMixin>; template , typename Equals = std::equal_to, typename Allocator = std::allocator>> using StableHashMap = MapMixin>; template template auto MapMixin::from(MapType const& m) -> MapMixin { return MapMixin(m.begin(), m.end()); } template auto MapMixin::keys() const -> List { List klist; for (const_iterator i = Base::begin(); i != Base::end(); ++i) klist.push_back(i->first); return klist; } template auto MapMixin::values() const -> List { List vlist; for (const_iterator i = Base::begin(); i != Base::end(); ++i) vlist.push_back(i->second); return vlist; } template auto MapMixin::pairs() const -> List> { List> plist; for (const_iterator i = Base::begin(); i != Base::end(); ++i) plist.push_back(*i); return plist; } template bool MapMixin::contains(key_type const& k) const { return Base::find(k) != Base::end(); } template bool MapMixin::remove(key_type const& k) { return Base::erase(k) != 0; } template bool MapMixin::removeValues(mapped_type const& v) { bool removed = false; const_iterator i = Base::begin(); while (i != Base::end()) { if (i->second == v) { Base::erase(i++); removed = true; } else { ++i; } } return removed; } template auto MapMixin::take(key_type const& k) -> mapped_type { if (auto v = maybeTake(k)) return v.take(); throw MapException(strf("Key '%s' not found in Map::take()", outputAny(k))); } template auto MapMixin::maybeTake(key_type const& k) -> Maybe { const_iterator i = Base::find(k); if (i != Base::end()) { mapped_type v = std::move(i->second); Base::erase(i); return move(v); } return {}; } template auto MapMixin::get(key_type const& k) -> mapped_type& { iterator i = Base::find(k); if (i == Base::end()) throw MapException(strf("Key '%s' not found in Map::get()", outputAny(k))); return i->second; } template auto MapMixin::get(key_type const& k) const -> mapped_type const& { const_iterator i = Base::find(k); if (i == Base::end()) throw MapException(strf("Key '%s' not found in Map::get()", outputAny(k))); return i->second; } template auto MapMixin::value(key_type const& k, mapped_type d) const -> mapped_type { const_iterator i = Base::find(k); if (i == Base::end()) return std::move(d); else return i->second; } template auto MapMixin::maybe(key_type const& k) const -> Maybe { auto i = Base::find(k); if (i == Base::end()) return {}; else return i->second; } template auto MapMixin::ptr(key_type const& k) const -> mapped_const_ptr { auto i = Base::find(k); if (i == Base::end()) return nullptr; else return &i->second; } template auto MapMixin::ptr(key_type const& k) -> mapped_ptr { auto i = Base::find(k); if (i == Base::end()) return nullptr; else return &i->second; } template auto MapMixin::keyOf(mapped_type const& v) const -> key_type { for (const_iterator i = Base::begin(); i != Base::end(); ++i) { if (i->second == v) return i->first; } throw MapException(strf("Value '%s' not found in Map::keyOf()", outputAny(v))); } template auto MapMixin::keysOf(mapped_type const& v) const -> List { List keys; for (const_iterator i = Base::begin(); i != Base::end(); ++i) { if (i->second == v) keys.append(i->first); } return keys; } template auto MapMixin::hasValue(mapped_type const& v) const -> bool { for (const_iterator i = Base::begin(); i != Base::end(); ++i) { if (i->second == v) return true; } return false; } template auto MapMixin::insert(key_type k, mapped_type v) -> pair { return Base::insert(value_type(move(k), move(v))); } template auto MapMixin::add(key_type k, mapped_type v) -> mapped_type& { auto pair = Base::insert(value_type(move(k), move(v))); if (!pair.second) throw MapException(strf("Entry with key '%s' already present.", outputAny(k))); else return pair.first->second; } template auto MapMixin::set(key_type k, mapped_type v) -> mapped_type& { auto i = Base::find(k); if (i != Base::end()) { i->second = move(v); return i->second; } else { return Base::insert(value_type(move(k), move(v))).first->second; } } template template bool MapMixin::merge(OtherMapType const& m, bool overwrite) { return mapMerge(*this, m, overwrite); } template bool MapMixin::operator==(MapMixin const& m) const { return this == &m || mapsEqual(*this, m); } template void printMap(std::ostream& os, MapType const& m) { os << "{ "; for (auto i = m.begin(); i != m.end(); ++i) { if (m.begin() == i) os << "\""; else os << ", \""; os << i->first << "\" : \"" << i->second << "\""; } os << " }"; } template std::ostream& operator<<(std::ostream& os, MapMixin const& m) { printMap(os, m); return os; } } #endif