Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Jan 24, 2023
1 parent 8e1a189 commit 0c32ec6
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 0 deletions.
3 changes: 3 additions & 0 deletions 17/01/.tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[+]
build_type=Release
build_target=demo0
File renamed without changes.
14 changes: 14 additions & 0 deletions 17/01/00.java
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.
16 changes: 16 additions & 0 deletions 17/01/CMakeLists.txt
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()
116 changes: 116 additions & 0 deletions 17/01/demo0.cpp
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 modified 17/slides.pptx
Binary file not shown.

0 comments on commit 0c32ec6

Please sign in to comment.