Skip to content

Instantly share code, notes, and snippets.

View wdanxna's full-sized avatar

DanDrake wdanxna

  • Guangzhou/China
View GitHub Profile
@wdanxna
wdanxna / reshape.kt
Created January 14, 2019 12:08
reshape
fun<T> reShape(list: List<T>, vararg dims: Int): List<Any> {
fun groupBy(list: List<Any>, by: Int): List<Any> {
assert(list.size % by == 0)
val ret: MutableList<List<T>> = mutableListOf()
var j = 0
for (i in list.size downTo 1 step by) {
val o = mutableListOf<T>()
for (k in 1..by) {
o.add(list[j++] as T)
}
@wdanxna
wdanxna / UniqueIDGenerator.cpp
Created December 14, 2018 09:11
Thread safe Unique ID Generator in c++ using std::atomic
template <typename T>
struct UniqueIDGenerator {
std::atomic<T> _id;
std::function<const T(const T&)> _nexter;
template <typename NEXT>
UniqueIDGenerator(const T& init, const NEXT& nexter) {
_id.store(init, std::memory_order::memory_order_release);
_nexter = nexter;
}
@wdanxna
wdanxna / FileAppender.cpp
Created December 14, 2018 09:10
create & append string into a file
struct FileAppender {
std::ofstream _file;
std::mutex _lock;
explicit FileAppend(const std::string& filename) {
_file.open(filename.c_str(), std::ios_base::app);
}
virtual ~FileAppend() {
_file.flush();
@wdanxna
wdanxna / scalePoints.cpp
Last active December 3, 2018 01:57
Scale a set of Points about a scale center using template c++
#include <iostream>
#include <vector>
using namespace std;
template <class PT, class Iterable, class xGetter, class yGetter, class LAMBDA>
static void scalePoints(Iterable& points, PT& center, float x, float y, xGetter gx, yGetter gy, LAMBDA cb) {
int i = 0;
for (auto& p : points) {
float px = gx(p) - gx(center);
@wdanxna
wdanxna / mapzip.cpp
Created November 20, 2018 03:57
map and zip function in c++
// Example program
#include <iostream>
#include <string>
#include <vector>
using std::vector;
template<typename P, typename Iterator, typename Mapper>
void map(Iterator current, Iterator end, Mapper mapper, vector<P>& ret) {
if (current == end) return;