Skip to content

Instantly share code, notes, and snippets.

View goldsborough's full-sized avatar
🔨
Fixing things

Peter Goldsborough goldsborough

🔨
Fixing things
View GitHub Profile
@goldsborough
goldsborough / HashSet.cpp
Created September 22, 2015 22:23
A hash-set implementation.
template<typename T>
class HashSet
{
private:
using size_t = std::size_t;
using hash_function_t = std::function<size_t(const T&)>;
static const size_t minimum_capacity = 16;
@goldsborough
goldsborough / HashSet.cpp
Created September 22, 2015 23:34
A hash-set implementation with iterator support.
template<typename T>
class HashSet
{
private:
struct Node
{
Node(const T& t = T(),
Node* following = nullptr,
Node* prev = nullptr)
@goldsborough
goldsborough / HashMap.cpp
Created September 23, 2015 00:13
A hash-map implementation with iterator support.
template<typename Key, typename Value>
class HashMap
{
private:
struct Node
{
Node(const Key& k = Key(),
const Value& v = Value(),
Node* following = nullptr,
@goldsborough
goldsborough / HashMap.cpp
Created September 23, 2015 01:07
A hash-map implementation with iterator support and multiple hashing options.
template<typename Key, typename Value>
class HashMap
{
private:
struct Node
{
Node(const Key& k = Key(),
const Value& v = Value(),
Node* following = nullptr,
@goldsborough
goldsborough / HashMap2.cpp
Last active September 25, 2015 18:19
A hash-map implementation using open-addressing with various probe sequence patterns.
template<typename Key, typename Value>
class HashMap2
{
public:
using size_t = std::size_t;
using hash_function_t = std::function<size_t(const Key& key)>;
@goldsborough
goldsborough / HashMap2.cpp
Last active September 25, 2015 19:14
A hash-map implementation using an open-addressing scheme, with iterator support.
template<typename Key, typename Value>
class HashMap2
{
public:
using size_t = std::size_t;
using hash_function_t = std::function<size_t(const Key& key)>;
@goldsborough
goldsborough / print.cpp
Created September 25, 2015 18:05
A tiny namespace for a more intuitive interface to printing to stdout in C++.
namespace print
{
std::string final = "\n";
std::string delimiter = ", ";
void booleans()
{
std::cout << std::boolalpha;
}
@goldsborough
goldsborough / CuckooHashMap.cpp
Created September 26, 2015 23:12
A hash-map implementation using cuckoo-hashing.
template<typename Key, typename Value>
class Table
{
public:
using size_t = std::size_t;
struct Item
{
using hashes_t = std::pair<size_t, size_t>;
@goldsborough
goldsborough / CuckooHashMap.cpp
Created September 27, 2015 02:15
A hash-map implementation using cuckoo-hashing with iterator support.
template<typename Key, typename Value>
class Table
{
public:
using size_t = std::size_t;
struct Item
{
using hashes_t = std::pair<size_t, size_t>;
@goldsborough
goldsborough / rotate.cpp
Created September 30, 2015 17:26
Matrix rotation
template<std::size_t N>
void rotate_out_of_place(std::size_t (&matrix) [N][N])
{
std::size_t copy [N][N];
for (std::size_t i = 0, end = N/2; i < end; ++i)
{
for (std::size_t j = 0; j < N; ++j)
{
copy[i][j] = matrix[j][N - 1];