Skip to content

Instantly share code, notes, and snippets.

@dcousens
Last active June 26, 2017 15:34
Show Gist options
  • Save dcousens/1f2fef5c14d68e5302fc to your computer and use it in GitHub Desktop.
Save dcousens/1f2fef5c14d68e5302fc to your computer and use it in GitHub Desktop.
Various C++/D snippets
import std.stdio;
import std.algorithm;
import std.range;
import std.array;
void main() {
// initial data
int[] x = [10, 20, 30], y = [40, 50, 60, 70, 80], z = [90, 100, 110, 120];
auto linked = [x, y, z];
// pre-calculations
size_t accum = 0;
auto mlengths = linked.map!((x) {
auto a = accum;
accum += x.length;
return a;
});
auto slengths = assumeSorted(mlengths.array);
// lookup function/method
auto lookup(size_t i) {
auto bound = slengths.lowerBound(i + 1);
return linked[bound.length - 1][i - bound.back];
}
// test output
foreach (i; 0 .. 12) writeln(lookup(i));
}
#include <algorithm>
#include <array>
#include <iostream>
template <typename T, size_t N> struct Vector {
private:
void assign(const size_t i, const T v) {
this->elems[i] = v;
}
template <typename ... Arguments>
void assign(const size_t i, const T v, Arguments... args) {
this->assign(i, v);
this->assign(i + 1, args...);
}
template <typename F, typename U>
U vfold(const F f, const U u) const {
auto result = f(u, this->elems[0]);
for (auto i = 1; i < N; ++i) result = f(result, this->elems[i]);
return result;
}
template <typename F>
Vector vmap(const F f) const {
Vector result;
for (auto i = 0; i < N; ++i) result[i] = f(this->elems[i]);
return result;
}
template <typename F>
Vector vzip(const F f, const Vector b) const {
Vector result;
for (auto i = 0; i < N; ++i) result[i] = f(this->elems[i], b[i]);
return result;
}
public:
std::array<T, N> elems;
// Constructors
Vector() {}
Vector(const T a) {
this->elems.fill(a);
}
template <typename ... Arguments>
Vector(const T a, Arguments... args) {
static_assert(sizeof...(Arguments) == N - 1, "Invalid number of arguments for Vector constructor");
this->assign(0, a, args...);
}
// Accessors
template <typename U=T> typename std::enable_if<N >= 1, U >::type x() const { return this->elems[0]; }
template <typename U=T> typename std::enable_if<N >= 2, U >::type y() const { return this->elems[1]; }
template <typename U=T> typename std::enable_if<N >= 3, U >::type z() const { return this->elems[2]; }
template <typename U=T> typename std::enable_if<N >= 4, U >::type w() const { return this->elems[3]; }
template <typename U=T> typename std::enable_if<N >= 1, U&>::type x() { return this->elems[0]; }
template <typename U=T> typename std::enable_if<N >= 2, U&>::type y() { return this->elems[1]; }
template <typename U=T> typename std::enable_if<N >= 3, U&>::type z() { return this->elems[2]; }
template <typename U=T> typename std::enable_if<N >= 4, U&>::type w() { return this->elems[3]; }
// Vector operations
Vector operator-() const { return this->vmap([](T x){ return -x; }); }
Vector operator+(const Vector b) const { return this->vzip([](T a, T b) { return a + b; }, b); }
Vector operator-(const Vector b) const { return this->vzip([](T a, T b) { return a - b; }, b); }
Vector operator*(const Vector b) const { return this->vzip([](T a, T b) { return a * b; }, b); }
Vector operator/(const Vector b) const { return this->vzip([](T a, T b) { return a / b; }, b); }
Vector& operator+=(const Vector b) { *this = *this + b; return *this; }
Vector& operator-=(const Vector b) { *this = *this - b; return *this; }
Vector& operator*=(const Vector b) { *this = *this * b; return *this; }
Vector& operator/=(const Vector b) { *this = *this / b; return *this; }
// Scalar operations
Vector operator*(const T s) const { return *this * Vector(s); }
Vector operator/(const T s) const { return *this / Vector(s); }
Vector& operator*=(const T s) { *this = *this * s; return *this; }
Vector& operator/=(const T s) { *this = *this / s; return *this; }
// Index operations
T& operator[](const size_t i) { return this->elems[i]; }
T operator[](const size_t i) const { return this->elems[i]; }
// Comparison operations (Lexicographical)
bool operator==(const Vector b) const { return this->elems == b.elems; }
bool operator!=(const Vector b) const { return this->elems != b.elems; }
bool operator>(const Vector b) const { return this->elems > b.elems; }
bool operator<(const Vector b) const { return this->elems < b.elems; }
};
typedef Vector<float, 3> vec3;
int main () {
vec3().z();
return 0;
}
struct PrefixTree(T) {
PrefixTree[dchar] branches;
T value;
void add(in string akey, in T avalue) pure {
const head = akey[0];
const tail = akey[1 .. $];
auto child = this.branches.get(head, PrefixTree());
if (tail.length > 0) {
child.add(tail, avalue);
} else {
child.value = avalue;
}
this.branches[head] = child;
}
// returns T.init on failure to find
T find(in string fkey) pure const {
const head = fkey[0];
const tail = fkey[1 .. $];
auto child = this.branches.get(head, PrefixTree());
if (tail.length > 0) {
return child.find(tail);
} else {
return child.value;
}
}
}
import std.stdio : writeln;
void main() {
PrefixTree!int pt;
pt.add("aaa", 1);
pt.add("aab", 2);
pt.add("aac", 3);
pt.add("abc", 4);
writeln(pt);
auto v = pt.find("abc");
writeln(v);
}
import std.algorithm;
import std.range;
import std.stdio;
void main() {
const d = [1,2,3,4,8,3,4,2,12,45,3,2];
int[] rA = d.dup;
int[] rB = d.dup;
int[] rC = d.dup;
int[] rD = d.dup;
// option 1 -- allocating
rA = d.filter!(x => x > 6).array;
// option 3 -- non non-allocating
{
auto t = d.filter!(x => x > 6).copy(rD);
rD = rD[0 .. $ - t.length];
}
writeln(rA, rB, rC); // [45, 12, 8][8, 12, 45][8, 12, 45]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment