Skip to content

Commit

Permalink
1
Browse files Browse the repository at this point in the history
  • Loading branch information
archibate committed Feb 25, 2023
1 parent 0c3a59d commit 913374c
Show file tree
Hide file tree
Showing 28 changed files with 10,917 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ GNUmakefile
r000*/
.wip/
*.exe
.cache
7 changes: 7 additions & 0 deletions 1001/stl_map/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules
.DS_Store
dist
*.local
index.html
.remote-assets
components.d.ts
3 changes: 3 additions & 0 deletions 1001/stl_map/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# for pnpm
shamefully-hoist=true
auto-install-peers=true
11 changes: 11 additions & 0 deletions 1001/stl_map/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Welcome to [Slidev](https://github.com/slidevjs/slidev)!

To start the slide show:

- `npm install`
- `npm run dev`
- visit http://localhost:3030

Edit the [slides.md](./slides.md) to see the changes.

Learn more about Slidev on [documentations](https://sli.dev/).
37 changes: 37 additions & 0 deletions 1001/stl_map/components/Counter.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<script setup lang="ts">
import { ref } from 'vue'
const props = defineProps({
count: {
default: 0,
},
})
const counter = ref(props.count)
</script>

<template>
<div flex="~" w="min" border="~ gray-400 opacity-50 rounded-md">
<button
border="r gray-400 opacity-50"
p="2"
font="mono"
outline="!none"
hover:bg="gray-400 opacity-20"
@click="counter -= 1"
>
-
</button>
<span m="auto" p="2">{{ counter }}</span>
<button
border="l gray-400 opacity-50"
p="2"
font="mono"
outline="!none"
hover:bg="gray-400 opacity-20"
@click="counter += 1"
>
+
</button>
</div>
</template>
10 changes: 10 additions & 0 deletions 1001/stl_map/experiment/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cmake_minimum_required(VERSION 3.18)

if (NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
set(CMAKE_CXX_STANDARD 20)

project(main LANGUAGES CXX)

add_executable(main main.cpp)
11 changes: 11 additions & 0 deletions 1001/stl_map/experiment/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <bits/stdc++.h>
#include "print.h"
using namespace std;

int main() {
const map<string, int> config = {
{"timeout", 985},
{"delay", 211},
};
print(config["timeout"]); // 编译出错
}
161 changes: 161 additions & 0 deletions 1001/stl_map/experiment/print.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#include <type_traits>
#include <iostream>
#include <iomanip>
#include <string>
#include <string_view>

namespace _print_details {
template <class T, class = void>
struct _printer {
static void print(T const &t) {
std::cout << t;
}
};

template <class T>
using _rmcvref_t = std::remove_cv_t<std::remove_reference_t<T>>;

template <class T, class U = void, class = void>
struct _enable_if_tuple {
using not_type = U;
};

template <class T, class U>
struct _enable_if_tuple<T, U, std::void_t<decltype(std::tuple_size<T>::value)>> {
using type = U;
};

template <class T, class U = void, class = void>
struct _enable_if_map {
using not_type = U;
};

template <class T, class U>
struct _enable_if_map<T, U, std::enable_if_t<std::is_same_v<typename T::value_type, std::pair<typename T::key_type const, typename T::mapped_type>>>> {
using type = U;
};

template <class T, class U = void, class = void>
struct _enable_if_iterable {
using not_type = U;
};

template <class T, class U>
struct _enable_if_iterable<T, U, std::void_t<typename std::iterator_traits<decltype(std::begin(std::declval<T const &>()))>::value_type>> {
using type = U;
};

/* template <class T, class U> */
/* struct _enable_if_iterable<T, U, std::void_t<typename std::iterator_traits<typename T::const_iterator>::value_type>> { */
/* using type = U; */
/* }; */

template <class T, class U = void, class = void>
struct _enable_if_char {
using not_type = U;
};

template <class U>
struct _enable_if_char<char, U, void> {
using not_type = U;
};

template <class U>
struct _enable_if_char<wchar_t, U, void> {
using not_type = U;
};

template <class T, class U = void, class = void>
struct _enable_if_string {
using not_type = U;
};

template <class T, class Alloc, class Traits, class U>
struct _enable_if_string<std::basic_string<T, Alloc, Traits>, U, void> {
using type = U;
};

template <class T, class Traits, class U>
struct _enable_if_string<std::basic_string_view<T, Traits>, U, void> {
using type = U;
};

template <class T, class U>
struct _enable_if_string<T, U, std::enable_if_t<std::is_pointer_v<std::decay_t<T>> && std::is_same_v<std::remove_const_t<std::remove_pointer_t<std::decay_t<T>>>, char>>> {
using type = U;
};

template <class T>
struct _printer<T, typename _enable_if_iterable<T, typename _enable_if_string<T, typename _enable_if_tuple<T, typename _enable_if_map<T>::not_type>::not_type>::not_type>::type> {
static void print(T const &t) {
std::cout << "{";
bool once = false;
for (auto const &v: t) {
if (once) {
std::cout << ", ";
} else {
once = true;
}
_printer<_rmcvref_t<decltype(v)>>::print(v);
}
std::cout << "}";
}
};

template <class T>
struct _printer<T, typename _enable_if_tuple<T, typename _enable_if_iterable<T>::not_type>::type> {
template <std::size_t ...Is>
static void _unrolled_print(T const &t, std::index_sequence<Is...>) {
std::cout << "{";
((_printer<_rmcvref_t<std::tuple_element_t<Is, T>>>::print(std::get<Is>(t)), std::cout << ", "), ...);
if constexpr (sizeof...(Is) != 0) _printer<_rmcvref_t<std::tuple_element_t<sizeof...(Is), T>>>::print(std::get<sizeof...(Is)>(t));
std::cout << "}";
}

static void print(T const &t) {
_unrolled_print(t, std::make_index_sequence<std::max(static_cast<std::size_t>(1), std::tuple_size_v<T>) - 1>{});
}
};

template <class T>
struct _printer<T, typename _enable_if_map<T>::type> {
static void print(T const &t) {
std::cout << "{";
bool once = false;
for (auto const &[k, v]: t) {
if (once) {
std::cout << ", ";
} else {
once = true;
}
_printer<_rmcvref_t<decltype(k)>>::print(k);
std::cout << ": ";
_printer<_rmcvref_t<decltype(v)>>::print(v);
}
std::cout << "}";
}
};

template <class T>
struct _printer<T, typename _enable_if_string<T>::type> {
static void print(T const &t) {
std::cout << std::quoted(t);
}
};

template <class T>
struct _printer<T, typename _enable_if_char<T>::type> {
static void print(T const &t) {
std::cout << std::quoted(t, T('\''));
}
};

template <class T0, class ...Ts>
void print(T0 const &t0, Ts const &...ts) {
_printer<_rmcvref_t<T0>>::print(t0);
((std::cout << " " << _printer<_rmcvref_t<Ts>>::print(ts)), ...);
std::cout << "\n";
}
}

using _print_details::print;
4 changes: 4 additions & 0 deletions 1001/stl_map/experiment/tmp.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
std::string s = "I heard it even works if you don't believe";
s.replace(0,8,"").replace(s.find("even"),4,"always")
.replace(s.find("don't believe"),13,"use C++17");
// it always works if you use C++17
Binary file added 1001/stl_map/images/bg.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions 1001/stl_map/netlify.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[build.environment]
NODE_VERSION = "14"

[build]
publish = "dist"
command = "npm run build"

[[redirects]]
from = "/*"
to = "/index.html"
status = 200
Loading

0 comments on commit 913374c

Please sign in to comment.