A value is a power of 2 if you can AND
it with itself - 1 and have the result be 0
.
Example: 16
16: 10000 16 - 1: 01111
template<typename Function, typename... Args> | |
auto benchmark(const std::size_t runs, Function function, Args&&... args) | |
{ | |
using duration_t = std::chrono::duration<double, std::milli>; | |
using clock = std::chrono::high_resolution_clock; | |
duration_t duration(0); | |
template<typename Key, typename Value> | |
class RedBlackTree | |
{ | |
public: | |
using size_t = std::size_t; | |
RedBlackTree() | |
: _root(nullptr) | |
, _size(0) |
def memoize(cache_index=0): | |
def decorator(function): | |
cache = {} | |
def proxy(*args, **kwargs): | |
key = args[cache_index] | |
if key not in cache: | |
cache[key] = function(*args, **kwargs) | |
return cache[key] | |
return proxy | |
return decorator |
def memoize(*indices): | |
def decorator(function): | |
cache = {} | |
def proxy(*args, **kwargs): | |
key_args = [args[i] for i in indices] if indices else args | |
key = tuple(key_args) | |
if key not in cache: | |
cache[key] = function(*args, **kwargs) | |
return cache[key] | |
return proxy |
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import re | |
def parse(expression): | |
pattern = re.compile(r'(\d+)\s*(\W)\s*(\d+)') | |
values = [] | |
operators = [] |
class Graph | |
{ | |
public: | |
using size_t = std::size_t; | |
using vertex_t = std::size_t; | |
struct Edge { vertex_t vertex; size_t id; }; | |
class DirectedGraph | |
{ | |
public: | |
using size_t = std::size_t; | |
using vertex_t = std::size_t; | |
using adjacent_t = std::vector<vertex_t>; | |
template<typename T> | |
std::size_t find_lsb(T value) | |
{ | |
T less = value - 1; | |
value = (less | value) ^ less; | |
return std::log2(value + 1); | |
} |
class SortedInsert | |
{ | |
public: | |
template<typename Iterator1, typename Iterator2> | |
void operator()(Iterator1 first_begin, | |
Iterator1 first_end, | |
Iterator2 second_begin, | |
Iterator2 second_end) | |
{ |