Program Listing for File map.hpp#
↰ Return to documentation for file (librapid/include/librapid/datastructures/map.hpp)
#ifndef LIBRAPID_UTILS_MAP_HPP
#define LIBRAPID_UTILS_MAP_HPP
namespace librapid {
template<typename Key, typename Value>
class Map : public std::map<Key, Value> {
public:
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE bool contains(const Key &key) const {
return this->find(key) != this->end();
}
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE bool contains(const Key &key,
Value &value) const {
auto it = this->find(key);
if (it != this->end()) {
value = it->second;
return true;
}
return false;
}
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto get(const Key &key) const {
return (*this)[key];
}
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto get(const Key &key,
const Value &defaultValue) const {
auto it = this->find(key);
if (it != this->end()) { return it->second; }
return defaultValue;
}
LIBRAPID_NODISCARD std::string str(const std::string &keyFormat = "{}",
const std::string &valueFormat = "{}") const {
std::string str = "[\n";
for (const auto &pair : *this) {
str += " " + fmt::format(keyFormat, pair.first);
str += ": ";
str += fmt::format(valueFormat, pair.second);
str += "\n";
}
str += "]";
return str;
}
};
template<typename Key, typename Value>
class UnorderedMap : public std::unordered_map<Key, Value> {
public:
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE bool contains(const Key &key) const {
return this->find(key) != this->end();
}
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE bool contains(const Key &key,
Value &value) const {
auto it = this->find(key);
if (it != this->end()) {
value = it->second;
return true;
}
return false;
}
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto get(const Key &key) const {
return (*this)[key];
}
LIBRAPID_NODISCARD LIBRAPID_ALWAYS_INLINE auto get(const Key &key,
const Value &defaultValue) const {
auto it = this->find(key);
if (it != this->end()) { return it->second; }
return defaultValue;
}
LIBRAPID_NODISCARD std::string str(const std::string &keyFormat = "{}",
const std::string &valueFormat = "{}") const {
std::string str = "[\n";
for (const auto &pair : *this) {
str += " " + fmt::format(keyFormat, pair.first);
str += ": ";
str += fmt::format(valueFormat, pair.second);
str += "\n";
}
str += "]";
return str;
}
};
} // namespace librapid
LIBRAPID_SIMPLE_IO_IMPL(typename Key COMMA typename Value, librapid::Map<Key COMMA Value>)
LIBRAPID_SIMPLE_IO_NORANGE(typename Key COMMA typename Value, librapid::Map<Key COMMA Value>)
LIBRAPID_SIMPLE_IO_IMPL(typename Key COMMA typename Value, librapid::UnorderedMap<Key COMMA Value>)
LIBRAPID_SIMPLE_IO_NORANGE(typename Key COMMA typename Value,
librapid::UnorderedMap<Key COMMA Value>)
#endif // LIBRAPID_UTILS_MAP_HPP