Skip to content

Instantly share code, notes, and snippets.

View shoooe's full-sized avatar
🏓

Thomas Pigarelli shoooe

🏓
View GitHub Profile
@shoooe
shoooe / bitboard.hpp
Last active December 29, 2015 06:49
Bitboard boilerplate code.
template<std::size_t Cols, std::size_t Rows>
class bitboard {
private:
std::bitset<Cols * Rows> _bitset;
public:
using reference = std::bitboard<Cols, Rows><Cols * Rows>::reference;
reference operator()(std::size_t, std::size_t);
bitboard<Cols, Rows>& operator&=(const bitboard<Cols, Rows>&);
bitboard<Cols, Rows>& operator|=(const bitboard<Cols, Rows>&);
#include <numeric>
namespace std {
template<class T, std::size_t N, std::size_t M>
T* begin(T (&array)[N][M]) {
return &array[0][0];
}
template<class T, std::size_t N, std::size_t M>
@shoooe
shoooe / caesar.hs
Created February 18, 2014 17:42
Caesar's chiper -- Haskell exercise from §5.5 of "Programming in Haskell"
import Data.Char
char2Int :: Char -> Int
char2Int l = ord l - ord 'a'
int2Char :: Int -> Char
int2Char i = chr (ord 'a' + i)
charAdvance :: Int -> Char -> Char
charAdvance i c | isLower c = int2Char (((char2Int c) + i) `mod` 26)
@shoooe
shoooe / plusrow.hs
Last active August 29, 2015 13:56
A simple program in Haskell written while reading "Programming in Haskell". It prints a diagonal line of +es.
makeString :: Char -> Int -> String
makeString _ 0 = []
makeString c i = c : (makeString c (i - 1))
putStringsLn :: [String] -> IO ()
putStringsLn [] = return ()
putStringsLn (s : ss) = do
putStrLn s
putStringsLn ss
@shoooe
shoooe / any.hpp
Last active July 5, 2021 02:19
A simple implementation of Boost's any, as an exercise.
#pragma once
#include <exception>
#include <memory>
#include <typeinfo>
#include <type_traits>
class any;
template<class Type> Type any_cast(any&);
@shoooe
shoooe / bubble_sort.hpp
Last active August 29, 2015 13:56
Implementation of bubble sort in C++ as an exercise.
#pragma once
#include <algorithm>
template<typename IT>
void bubble_sort(IT begin, IT end) {
while (true) {
bool c = false; // changed?
for (IT i = begin; i != end-1; i = i+1) {
if (*i > *(i+1)) {
@shoooe
shoooe / insertion_sort.hpp
Created February 25, 2014 03:46
Implementation of insertion sort as an exercise.
#pragma once
#include <algorithm>
template <typename IT>
void insertion_sort(IT begin, IT end) {
for (IT i = begin; i != end; ++i) {
for (IT j = i; j != begin; --j) {
if (*(j-1) > *j) std::iter_swap(j-1, j);
else break;
@shoooe
shoooe / merge_sort.hpp
Created February 25, 2014 03:47
Implementation of merge sort as an exercise.
#pragma once
#include <algorithm>
#include <vector>
namespace {
template <typename IT>
void inner_merge(IT begin, IT middle, IT end) {
using Container = std::vector<typename std::iterator_traits<IT>::value_type>;
@shoooe
shoooe / round_1a.hs
Created April 14, 2014 20:20
Solution to the Round 1A of Google's Code Jam (http://goo.gl/yX6J8).
import Data.List.Split (splitOn)
import Control.Monad ((>=>))
newtype Circle = Circle { circleRadius :: Double } deriving (Eq, Show)
newtype Paint = Paint { remainingMl :: Double } deriving (Eq, Show)
data Ring = Ring { ringRadius :: Double
, ringThickness :: Double
} deriving (Eq, Show)
@shoooe
shoooe / state_sequences.hs
Last active August 29, 2015 14:01
Playing around with the monad State. The program finds the number of sequences (defined as repetitions of a character) in a string.
import Control.Monad.State
data Sequence = Sequence { sequenceChar :: Char
, sequenceCount :: Integer } deriving (Eq, Show)
-- Without State monad.
-- Unused.
getNextSequence :: String -> (Sequence, String)
getNextSequence (h : []) = (Sequence h 1, [])
getNextSequence (h : ss) =