Skip to content

Instantly share code, notes, and snippets.

@yohhoy
yohhoy / submdspan-test.cpp
Created January 31, 2024 14:19
submdspan
#include <cassert>
#include <concepts>
#include <span>
#include <tuple>
#include <utility>
#include <ranges>
#if 1
#include <https://raw.githubusercontent.com/kokkos/mdspan/single-header/mdspan.hpp>
#else
#include <mdspan>
@yohhoy
yohhoy / mdspan_hilbert.cpp
Last active November 25, 2023 17:07
Hilbert curve layout for std::mdspan
#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
Last active May 2, 2024 07:22
mdspan constructors & deduction guides
#include <cassert>
#include <array>
#include <span>
#include <type_traits>
#if 0
#include <mdspan>
#else
#include <https://raw.githubusercontent.com/kokkos/mdspan/single-header/mdspan.hpp>
#endif
using namespace std;
@yohhoy
yohhoy / mm.c
Last active September 22, 2022 04:44
// 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);

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>
#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
// 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)
/*
* Convert from OpenCV image and write movie with FFmpeg
*
* Copyright (c) 2021 yohhoy
*/
#include <iostream>
#include <vector>
// FFmpeg
extern "C" {
#include <libavformat/avformat.h>
#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)>);
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)