Skip to content

Instantly share code, notes, and snippets.

@dgodfrey206
dgodfrey206 / tpool.cpp
Last active March 26, 2022 17:37
Thread pool
#include <deque>
#include <stack>
#include <mutex>
#include <thread>
#include <algorithm>
#include <functional>
#include <type_traits>
#include <unordered_map>
#include <condition_variable>
@dgodfrey206
dgodfrey206 / ts-singleton.cpp
Created December 29, 2014 17:15
Thread-safe singleton
#include <mutex>
#include <thread>
#include <iostream>
struct Singleton
{
static Singleton& instance()
{
std::lock_guard<std::mutex> lock(m);
static Singleton s;
@dgodfrey206
dgodfrey206 / bst.cpp
Created January 10, 2015 00:49
A binary search tree
#include <initializer_list>
#include <type_traits>
#include <memory>
#include <iostream>
template <class T>
using ref_ptr = std::decay_t<T>*&;
template <class>
struct Node;
@dgodfrey206
dgodfrey206 / ddl.cpp
Last active March 26, 2022 17:35
New doubly linked list
#include <iostream>
#include <iterator>
#include <algorithm>
#include <initializer_list>
template<typename T>
struct LinkedList;
template<typename T>
struct node
@dgodfrey206
dgodfrey206 / adl_example.cpp
Last active August 29, 2015 14:13
Example on ADL
#include <iostream>
#define LOG_ARG(ARG) \
do { std::cout << __PRETTY_FUNCTION__ << std::endl; } while (0)
#define LOG LOG_ARG(__PRETTY_FUNCTION__)
class Widget {
class Toolbox { };
public:
@dgodfrey206
dgodfrey206 / dumb_array.cpp
Created January 24, 2015 22:59
Dumb array
#include <iostream>
#include <algorithm>
template<class T>
class dumb_array
{
public:
dumb_array(std::size_t size = 0) noexcept(noexcept(T()))
: m_size(size)
, m_array(size ? new T[m_capacity]() : 0)
@dgodfrey206
dgodfrey206 / student.cpp
Created January 25, 2015 04:32
Student with whitespace fmt
#include <iostream>
#include <vector>
#include <iterator>
class student_fmt;
struct student
{
student() = default;
student(std::string const& name, std::string const& a,
@dgodfrey206
dgodfrey206 / cooladl.cpp
Last active August 29, 2015 14:14
ADL and function template instantiation
template<class T>
decltype(f(T())) h() {}
void f(int);
void f(struct X);
struct X {};
int main()
{
@dgodfrey206
dgodfrey206 / nthfromend.cpp
Created January 30, 2015 22:39
Nth from end for singly linked list
/* nth node from end */
void nthNodeFromEnd(node* head, int n)
{
int size = 0;
node* temp = head;
for (; temp; ++size, temp = temp->next)
{ }
if (size < n)
return;
@dgodfrey206
dgodfrey206 / llpalindrome.cpp
Created January 31, 2015 00:13
Palindrome for singly linked list
#include <iostream>
#include <unordered_set>
struct node
{
node(int data, node* next = nullptr)
: data(data)
, next(next)
{ }