#ifndef SDK_TL_MAP_H #define SDK_TL_MAP_H #include "iostream" #include "vector" #include "list" #include "map" #include "mutex" #include "string" #include "shared_mutex" namespace CTL{ template class Map{ private: //-------------------------------------------------------------------------------------------------------------- mutable std::shared_mutex _mutex; std::map _map; //-------------------------------------------------------------------------------------------------------------- public: //-------------------------------------------------------------------------------------------------------------- friend std::ostream& operator<<(std::ostream& os,Map& map) { os << "[\n"; auto items = map.ToStdMap(); auto it = items.begin(); while (it != items.end()) { os << " {\n " << it->first << ": " << it->second << "\n }"; ++it; if (it != items.end()) { os << ",\n"; } } os << "\n]"; return os; } friend std::ostream& operator<<(std::ostream& os,const Map& map) { os << "[\n"; auto items = map.ToStdMap(); auto it = items.begin(); while (it != items.end()) { os << " {\n " << it->first << ": " << it->second << "\n }"; ++it; if (it != items.end()) { os << ",\n"; } } os << "\n]"; return os; } //-------------------------------------------------------------------------------------------------------------- Map() = default; explicit Map(const std::map& map){ std::lock_guard lock(_mutex); _map = map; } Map(const Map& map){ std::lock_guard lock(_mutex); this->_map = map._map; } Map(const std::initializer_list>& list){ std::lock_guard lock(_mutex); for (const auto& item : list) { _map.insert(item); } } ~Map() { Clear(); } //-------------------------------------------------------------------------------------------------------------- void Put(K key, V value){ std::lock_guard lock(_mutex); _map.insert(std::make_pair(key, value)); } void put(K key, V value){ Put(key,value); } void Revise(K key, V value){ std::lock_guard lock(_mutex); _map[key] = value; } void revise(K key, V value){ std::lock_guard lock(_mutex); _map[key] = value; } void Remove(K key){ std::lock_guard lock(_mutex); _map.erase(key); } void remove(K key){ std::lock_guard lock(_mutex); _map.erase(key); } void Clear(){ std::lock_guard lock(_mutex); std::map().swap(this->_map); } void clear(){ std::lock_guard lock(_mutex); std::map().swap(this->_map); } static void swap(Map* map) { std::map().swap(map->_map); } V* Get(const K& key){ std::shared_lock lock(_mutex); const bool F = _map.find(key) != _map.end(); if (!F) { return nullptr; } return &_map[key]; } V* get(const K& key){ return Get(key); } V at(const K& key) const{ std::shared_lock lock(_mutex); return _map.at(key); } V* operator[](const K& key){ std::shared_lock lock(_mutex); const bool F = _map.find(key) != _map.end(); if (!F) { return nullptr; } return &_map[key]; } int Size(){ std::shared_lock lock(_mutex); return _map.size(); } int size(){ std::shared_lock lock(_mutex); return _map.size(); } bool IsContains(const K& key){ std::shared_lock lock(_mutex); return _map.find(key) != _map.end(); } bool IsEmpty(){ std::shared_lock lock(_mutex); return _map.empty(); } bool IsFull(){ std::shared_lock lock(_mutex); return _map.size() == _map.max_size(); } std::list Keys(){ std::shared_lock lock(_mutex); std::list keys; for(auto& item : _map){ keys.push_back(item.first); } return keys; } std::list keys(){ return Keys(); } std::vector Values(){ std::shared_lock lock(_mutex); std::vector values; for(auto& item : _map){ values.push_back(item.second); } return values; } std::vector values(){ return Values(); } std::map ToStdMap(){ return _map; } std::map ToStdMap() const { // 实现处同样添加 const return _map; // 假设内部存储为 _map } void SetStdMap(const Map& map) { std::lock_guard lock(_mutex); _map = map._map; } void SetStdMap(const std::map& map) { std::lock_guard lock(_mutex); _map = map; } typename std::map::iterator begin() { return _map.begin(); } typename std::map::iterator end() { return _map.end(); } Map operator=(const Map& map) { std::lock_guard lock(_mutex); _map = map._map; return map; } Map operator=(const std::map& map) { std::lock_guard lock(_mutex); _map = map; return this; } //-------------------------------------------------------------------------------------------------------------- private: }; template class MapPtr:public Map { public: }; } #endif