-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add defaultdict data structure for c++ && use SFINAE for get color
- Loading branch information
Showing
3 changed files
with
145 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
#include "cw_logic.h" | ||
#include "cw_saver.h" | ||
#include "cw_string.h" | ||
#include "cw_datastructure.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#pragma once | ||
|
||
#include <unordered_map> | ||
#include <utility> // for std::move | ||
|
||
namespace cw { | ||
|
||
template<typename Key, typename Value, typename Hash = std::hash<Key>> | ||
class DefaultDict { | ||
public: | ||
// Default constructor | ||
DefaultDict() : default_value(Value()) {} | ||
|
||
// Constructor with default value | ||
DefaultDict(const Value& default_value) : default_value(default_value) {} | ||
|
||
// Copy constructor | ||
DefaultDict(const DefaultDict& other) = default; | ||
|
||
// Move constructor | ||
DefaultDict(DefaultDict&& other) noexcept = default; | ||
|
||
// Copy assignment | ||
DefaultDict& operator=(const DefaultDict& other) = default; | ||
|
||
// Move assignment | ||
DefaultDict& operator=(DefaultDict&& other) noexcept = default; | ||
|
||
// Operator [] | ||
Value& operator[](const Key& key) { | ||
return data[key]; | ||
} | ||
|
||
const Value& operator[](const Key& key) const { | ||
auto it = data.find(key); | ||
if (it != data.end()) { | ||
return it->second; | ||
} else { | ||
return default_value; | ||
} | ||
} | ||
|
||
std::size_t size() const { | ||
return data.size(); | ||
} | ||
|
||
void clear() { | ||
data.clear(); | ||
} | ||
|
||
// Iterator support | ||
auto begin() { return data.begin(); } | ||
auto end() { return data.end(); } | ||
auto begin() const { return data.begin(); } | ||
auto end() const { return data.end(); } | ||
|
||
private: | ||
std::unordered_map<Key, Value, Hash> data; | ||
Value default_value; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters