This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <chrono> | |
std::chrono::high_resolution_clock::time_point t_start = std::chrono::high_resolution_clock::now(); | |
//... | |
float elapsedTime = std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::high_resolution_clock::now() - t_start).count(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
void inmerge(std::vector<T> &target, const std::vector<T> &source) | |
{ | |
target.resize(target.size() + source.size()); | |
auto lit = target.crbegin() + source.size(), rit = source.rbegin(); | |
auto lend = target.crend(), rend = source.rend(); | |
auto out = target.rbegin(); | |
while (rit != rend) { | |
*(out++) = (lit != lend and *rit < *lit) ? *(lit++) : *(rit++); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename T> | |
class SplitIterator | |
{ | |
const float pivot; | |
typename vector<T>::const_iterator left, right, it; | |
const typename vector<T>::const_iterator begin, end; | |
public: | |
SplitIterator(const vector<T> &points, T origin) : pivot{origin.x()}, begin{points.begin()}, end{points.end()} { | |
assert (std::is_sorted(points.begin(), points.end(), [](T a, T b) { return a.x() < b.x(); });) | |
right = std::upper_bound(begin, end, origin, [](T a, T b) { return a.x() < b.x(); }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename Result, typename M, typename A> | |
Result& nonconst_call(const M& m, A& argument) | |
{ | |
return const_cast<Result&>(static_cast<const M&>(m)(argument)); | |
} | |
Value const& Container::operator () (int index) const | |
{ | |
return data[index]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def format_dict(obj, refs=tuple()): | |
return "<< " + "".join("/{} {}\n".format(key, format_value(value, refs)) for (key, value) in obj.items()) + ">>" | |
def format_value(value, refs=tuple()): | |
if value in refs: | |
return "{} 0 R".format(refs.index(value) + 1) | |
elif type(value) is dict: | |
return format_dict(value, refs) | |
elif type(value) is list: | |
return "[ " + " ".join(format_value(item, refs) for item in value) + " ]" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pairs(seq, cyclic=True): | |
it = iter(seq) | |
first = prev = next(it) | |
for here in it: | |
yield prev, here | |
prev = here | |
if cyclic: | |
yield here, first | |
# I use this snippet in almost every script | |
# It is really simple but still I would prefer to import it from itertools if it was there... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import triangle | |
from collections import OrderedDict | |
def triangulate(multipolygon): | |
vertices = OrderedDict() | |
indices = [[vertices.setdefault(co, len(vertices)) for co in poly] for poly in multipolygon] | |
dt = triangle.triangulate({"vertices": list(vertices), "segments": [s for f in indices for s in pairs(f)]}, "p") | |
segments = {s for poly in indices for s in pairs(poly)} | |
triangles = {tuple(tri) for tri in dt["triangles"]} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// setdefault(D, k, v) -> D.at(k), and set D[k]=v if k not in D | |
template<class T, class K, class V> | |
V& setdefault(T &map, const K &key, const V &default_value) | |
{ | |
if (!map.count(key)) { | |
map.insert({key, default_value}); | |
} | |
return map.at(key); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename M> | |
M::mapped_type pop(M &m, M::const_iterator it) | |
{ | |
assert (it != m.end()); | |
M::mapped_type result = *it; | |
m.erase(it); | |
return result; | |
} | |
template<typename M> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename It, typename OutIt, typename UnaryPredicate> | |
It move_if(It begin, It end, OutIt dst, UnaryPredicate pred) | |
{ | |
It last = end; | |
while (begin != last) { | |
if (pred(*begin)) { | |
*dst++ = std::move(*begin); | |
std::swap(*begin, *(--last)); | |
} else { | |
++begin; |
OlderNewer