forked from parallel101/course
-
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.
- Loading branch information
Showing
7 changed files
with
149 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[+] | ||
build_type=Release | ||
build_target=demo0 |
File renamed without changes.
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,14 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class JavaIsTrash { | ||
public static void main(String[] args) { | ||
Map<String, Integer> items = new HashMap<String, Integer>(){{ | ||
put("hello", 1); | ||
put("world", 2); | ||
}}; | ||
items.put("time", 4); | ||
int time = (int)items.get("time"); | ||
System.out.println(time); | ||
} | ||
} |
File renamed without changes.
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,16 @@ | ||
cmake_minimum_required(VERSION 3.15) | ||
|
||
if (NOT CMAKE_BUILD_TYPE) | ||
set(CMAKE_BUILD_TYPE Release) | ||
endif() | ||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
project(MapCourse LANGUAGES CXX) | ||
|
||
set(DEMO_LIST | ||
demo0 | ||
) | ||
|
||
foreach (name IN ITEMS ${DEMO_LIST}) | ||
add_executable(${name} ${name}.cpp) | ||
endforeach() |
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,116 @@ | ||
#include <type_traits> | ||
#include <functional> | ||
#include <utility> | ||
#include <string> | ||
#include <map> | ||
#include <iostream> | ||
|
||
template | ||
< class Map | ||
, class Iterator = std::conditional_t | ||
< std::is_const_v<Map> | ||
, typename Map::const_iterator | ||
, typename Map::iterator | ||
> | ||
> | ||
class MapEntry { | ||
using map_type = std::remove_cv_t<Map>; | ||
using key_type = typename map_type::key_type; | ||
using mapped_type = std::conditional_t | ||
< std::is_const_v<map_type> | ||
, std::add_const_t<typename map_type::mapped_type> | ||
, typename map_type::mapped_type | ||
>; | ||
using iterator = Iterator; | ||
|
||
map_type &m_map; | ||
key_type const &m_key; | ||
|
||
public: | ||
explicit MapEntry | ||
( map_type &map | ||
, key_type const &key | ||
) | ||
: m_map(map) | ||
, m_key(key) | ||
{ | ||
} | ||
|
||
template <class ...Ts> | ||
mapped_type &or_insert(Ts &&...ts) const { | ||
iterator it = m_map.find(m_key); | ||
if (it != m_map.end()) | ||
return it->second; | ||
else | ||
return m_map.try_emplace(it, m_key, std::forward<Ts>(ts)...)->second; | ||
} | ||
|
||
template <class ...Fs> | ||
mapped_type &or_insert_with(Fs &&...fs) const { | ||
iterator it = m_map.find(m_key); | ||
if (it != m_map.end()) | ||
return *this; | ||
else | ||
return m_map.try_emplace(it, m_key, std::invoke(std::forward<Fs>(fs)...))->second; | ||
} | ||
|
||
template <class ...Ts> | ||
mapped_type or_value(Ts &&...ts) const { | ||
iterator it = m_map.find(m_key); | ||
if (it != m_map.end()) | ||
return it->second; | ||
else | ||
return mapped_type(std::forward<Ts>(ts)...); | ||
} | ||
|
||
template <class ...Fs> | ||
mapped_type or_value_with(Fs &&...fs) const { | ||
iterator it = m_map.find(m_key); | ||
if (it != m_map.end()) | ||
return it->second; | ||
else | ||
return std::invoke(std::forward<Fs>(fs)...); | ||
} | ||
|
||
template <class = void> | ||
mapped_type &or_die() const { | ||
iterator it = m_map.find(m_key); | ||
if (it != m_map.end()) | ||
return it->second; | ||
else | ||
throw std::out_of_range("MapEntry"); | ||
} | ||
|
||
template <class ...Ts> | ||
void and_erase(Ts &&...ts) const { | ||
iterator it = m_map.find(m_key); | ||
if (it != m_map.end()) | ||
return m_map.erase(it); | ||
} | ||
|
||
template <class ...Fs> | ||
void and_modify(Fs &&...fs) const { | ||
iterator it = m_map.find(m_key); | ||
if (it != m_map.end()) | ||
std::invoke(std::forward<Fs>(fs)..., it->second); | ||
} | ||
|
||
template <class = void> | ||
bool exists() const { | ||
iterator it = m_map.find(m_key); | ||
return it != m_map.end(); | ||
} | ||
}; | ||
|
||
template <class Map> | ||
MapEntry(Map, typename Map::key_type const &) -> MapEntry<Map>; | ||
|
||
int main() { | ||
std::map<std::string, int> m; | ||
MapEntry(m, "hello").or_insert(1); | ||
std::cout << MapEntry(m, "hello").or_value(3) << std::endl; | ||
std::cout << MapEntry(m, "world").or_value(3) << std::endl; | ||
MapEntry(m, "hello").and_modify([&] (auto &val) { val += 1; }); | ||
std::cout << MapEntry(m, "world").or_insert(4) << std::endl; | ||
return 0; | ||
} |
Binary file not shown.