Skip to content

Instantly share code, notes, and snippets.

Avatar
🐢
on going

yoh yohhoy

🐢
on going
View GitHub Profile
@yohhoy
yohhoy / mdspan_hilbert.cpp
Last active March 15, 2023 00:55
Hilbert curve layout for std::mdspan
View mdspan_hilbert.cpp
#include <cassert>
#include <bit>
#include <iostream>
#include <utility>
#if 1
#include <https://raw.githubusercontent.com/kokkos/mdspan/single-header/mdspan.hpp>
using std::experimental::mdspan;
using std::experimental::dextents;
#else
#include <mdspan>
@yohhoy
yohhoy / mdspan-test.cpp
Created March 4, 2023 14:38
mdspan constructors & deduction guides
View mdspan-test.cpp
#include <cassert>
#include <array>
#include <span>
#if 1
#include <https://raw.githubusercontent.com/kokkos/mdspan/single-header/mdspan.hpp>
using namespace std::experimental;
#else
#include <mdspan>
using namespace std;
#endif
@yohhoy
yohhoy / mm.c
Last active September 22, 2022 04:44
View mm.c
// https://groups.google.com/g/golang-nuts/c/Gze1TRtdLdc/m/RoD2AxssDgAJ
// http://svr-pes20-cppmem.cl.cam.ac.uk/cppmem/
int main() {
int x = 0;
atomic_int int y = 0;
{{{ { x = 1;
y.store(1);
x = 1;
y.store(1); }
||| { y.load().readsvalue(0);
View barrier.md

Full barrier

P: =p0=\ /----\ /=p1=\ /----\ /=
        *      *      *      *
C: ----/ \=c0=/ \----/ \=c1=/ \-

Half barrier

P: p0=\--------/=p1=\--------/=
@yohhoy
yohhoy / chrono_io.hpp
Last active December 15, 2021 04:38
Ad-hoc std::ostream support for <chrono>
View chrono_io.hpp
#include <ctime>
#include <chrono>
#include <iostream>
#include <iomanip>
namespace std {
// https://timsong-cpp.github.io/cppwp/n4868/time.cal.ymd.nonmembers#14
// "yyyy-mm-dd"
inline ostream& operator<<(ostream& os, const chrono::year_month_day& ymd) {
@yohhoy
yohhoy / endian.hpp
Last active October 28, 2021 10:16
endian conversion
View endian.hpp
// requires C++23 or later
#include <bit>
#include <concepts>
template <std::integral T>
constexpr T hton(T value) noexcept
{
if constexpr (std::endian::native == std::endian::little) {
return std::byteswap(value);
} else {
@yohhoy
yohhoy / cv2ff.cpp
Created August 4, 2021 16:27
Convert from OpenCV image and write movie with FFmpeg (OpenCV 4.5, FFmpeg 4.4)
View cv2ff.cpp
/*
* Convert from OpenCV image and write movie with FFmpeg
*
* Copyright (c) 2021 yohhoy
*/
#include <iostream>
#include <vector>
// FFmpeg
extern "C" {
#include <libavformat/avformat.h>
View pre-p2210r2.cpp
#include <algorithm>
#include <iostream>
#include <string>
#include <string_view>
#include <ranges>
int main() {
std::string_view str = "apple,banana,cinnamon";
for (auto rev_item: str | std::views::reverse | std::views::split(',')) {
static_assert( std::ranges::forward_range<decltype(rev_item)>);
View error-concepts-diagnostics.log
prog.cc: In function 'int main()':
prog.cc:6:19: error: no match for 'operator|' (operand types are 'std::forward_list<int, std::allocator<int> >' and 'const std::ranges::views::_Reverse')
6 | for (int _: lst | std::views::reverse) {}
| ~~~ ^ ~~~~~~~~~~~~~~~~~~~
| | |
| | const std::ranges::views::_Reverse
| std::forward_list<int, std::allocator<int> >
In file included from prog.cc:2:
/opt/wandbox/gcc-head/include/c++/12.0.0/ranges:818:7: note: candidate: 'template<class _Lhs, class _Rhs> requires (derived_from<_Lhs, std::ranges::views::__adaptor::_RangeAdaptorClosure>) && (derived_from<_Rhs, std::ranges::views::__adaptor::_RangeAdaptorClosure>) constexpr auto std::ranges::views::__adaptor::operator|(_Lhs, _Rhs)'
818 | operator|(_Lhs __lhs, _Rhs __rhs)
@yohhoy
yohhoy / mock-string.cpp
Last active July 2, 2021 02:12
Mock class template
View mock-string.cpp
#include <initializer_list>
namespace mock {
template <class T>
struct allocator {};
template <class T>
struct char_traits {};
// C++20 https://timsong-cpp.github.io/cppwp/n4861/basic.string
template<class charT, class traits = char_traits<charT>,