Skip to content

Instantly share code, notes, and snippets.

View petomalina's full-sized avatar
🚀

Peter Malina petomalina

🚀
  • kiwi.com
  • London, UK
View GitHub Profile
/*
* This is the very basic implementation of Singleton
* Since C++11, this implementation is thread-safe
*/
class Singleton {
public:
static Singleton& getInstance() {
static Singleton instance;
return instance;
@petomalina
petomalina / GCC to_string patch
Created July 13, 2014 18:20
Patch for the gcc missing to_string function
#include<sstream>
namespace std {
#ifndef to_string // gcc patch to_string
template <typename T>
string to_string(T value)
{
std::ostringstream os ;
os << value ;
@petomalina
petomalina / Type class
Created July 17, 2014 14:01
Type<T> class that works as Type storage
template <typename T>
class Type {
public:
Type() {}
virtual ~Type() {}
virtual T* allocate() const { return new T; }
virtual T* cast(void *object) const { return static_cast<T*>(object); }
};
@petomalina
petomalina / gist:d80f1a9f47288ed3dd5d
Created December 11, 2014 19:08
IsFibo challenge in C++
/*Author: Gelidus(gelidus@hotmail.sk)*/
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
@petomalina
petomalina / gist:a57a26a10fd751633d41
Last active August 29, 2015 14:12
GSort - simple sorting algorithm
###
Sorting algorithm created by Gelidus January 1st
This algorithms uses two cycles to sort the given array correctly.
First, it will partially sort given sequence to the subsequences.
After that, it will simply compare first element with others to
correct the partial sort
@author: Peter "Gelidus" Malina - gelidus@hotmail.sk
@license: Do what you want
@petomalina
petomalina / gist:0a1ddcfe7ffb095e45f6
Created January 18, 2015 13:49
Simple Selection sort
int *selectionSort(int *array, int count) {
// at is location of first unordered item, minimum is unordered list minimum
int at = 0, minimum = 0;
for(at = 0; at < count-1; at++) {
minimum = at; // set minimum to the current replacer
for(int i = at+1; i < count; i++) { // find minimum
if(array[i] < array[minimum]) {
@petomalina
petomalina / bubblesort.c
Last active August 29, 2015 14:13
Bubble sort with no swap detection
int *bubbleSort(int *array, int count) {
int unsorted = count; // number of unsorted elements in array
bool swapped = false;
while(unsorted > 1) {
swapped = false;
for(int i = 0; i < unsorted-1; i++) {
if(array[i] > array[i+1]) {
// swap
@petomalina
petomalina / insertionsort.c
Created January 18, 2015 14:12
Simple Insertion sort
void swap(int *a, int *b) {
int temp = *a;
*a = *b;
*b = temp;
}
int *insertionSort(int *array, int count) {
for(int i = 1; i < count; i++) { // start at the second element
int replacer = i;
@petomalina
petomalina / parser.coffee
Created January 25, 2015 22:09
Node.js input arguments parsing algorithm
###
Parses given array of input arguments and recognizes commands,
subcommands and switches with their values
@param [argv] argument list to parse
@return [ Array, Object] An array of commands and object of switches
###
parseArguments = (argv) ->
commands = []
switches = { }
@petomalina
petomalina / find_duplicates.h
Last active August 29, 2015 14:14
Find duplicates in the given array
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
template <typename T>
vector<T> *findDuplicates(T *elements, unsigned int length) {
sort(elements, elements + length);