Skip to content

Instantly share code, notes, and snippets.

View superwills's full-sized avatar

William superwills

View GitHub Profile
@superwills
superwills / CCF.hpp
Created April 19, 2014 09:51
CCFSet and CCFArray template wrappers
template <typename T>
struct CCFArray
{
CFArrayRef array;
CCFArray()
{
array = CFArrayCreate( kCFAllocatorDefault, 0, 0, 0 );
}
CCFArray(CFArrayRef cf):array(cf)
{
string CFGetType( long typeId )
{
static map<long,string> types = {
{CFArrayGetTypeID(),"CFArray"},
{CFBooleanGetTypeID(),"CFBoolean"},
{CFDataGetTypeID(),"CFData"},
{CFDateGetTypeID(),"CFDate"},
{CFDictionaryGetTypeID(),"CFDictionary"},
{CFNumberGetTypeID(),"CFNumber"},
{CFStringGetTypeID(),"CFString"},
map<IOReturn,string> ioMakeErrorMap()
{
map<IOReturn,string> map;
map[ kIOReturnSuccess ] = "kIOReturnSuccess KERN_SUCCESS // OK";
map[ kIOReturnError ] = " // general error";
map[ kIOReturnNoMemory ] = " // can't allocate memory ";
map[ kIOReturnNoResources ] = "resource shortage ";
map[ kIOReturnIPCError ] = "error during IPC ";
map[ kIOReturnNoDevice ] = "no such device ";
map[ kIOReturnNotPrivileged ] = "privilege violation ";
@superwills
superwills / 2dmatrix
Created January 16, 2015 03:12
2D matrix
// For allocating a 2D vector
template <typename T> vector< vector<T> > twoD( size_t rows, size_t cols )
{
vector< vector<T> > vv( rows );
for( int i = 0; i < vv.size(); i++ )
vv[i].resize( cols );
return vv;
}
@superwills
superwills / contains.hpp
Last active August 29, 2015 14:13
contains
// for a vector
template <typename T> bool contains( const vector<T> &vvector, const T& item )
{
return vvector.find( item ) != vvector.end() ;
}
// for a set
template <typename T> bool contains( const set<T> &sset, const T& item )
{
return sset.find( item ) != sset.end() ;
@superwills
superwills / MersenneTwister.hpp
Last active August 29, 2015 14:26
C++ Class For Mersenne Twister
#ifndef MERSENNETWISTER_H
#define MERSENNETWISTER_H
/*
A C-program for MT19937, with initialization improved 2002/1/26.
Coded by Takuji Nishimura and Makoto Matsumoto.
Before using, initialize the state by using init_genrand(seed)
or init_by_array(init_key, key_length).
Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
All rights reserved.
@superwills
superwills / info.h
Last active August 29, 2015 14:26
Info,warning,error functions
#include <stdio.h>
#include <stdarg.h>
// bitfield because some error messages can
// be categorized as Info | Warning | Error
enum ErrorLevel{
Info = 1 << 0, Warning = 1 << 1, Error = 1 << 2
};
const char* ErrorLevelName[] = {
"None", //0
@superwills
superwills / OpenSSL to OpenSSH public key format
Last active December 16, 2015 10:29
Mounir IDRASSI's conversion from OpenSSL to OpenSSH public key format http://www.idrix.fr/Root/Samples/pubkey2ssh.c
/*
* An implementation of convertion from OpenSSL to OpenSSH public key format
*
* Copyright (c) 2008 Mounir IDRASSI <mounir.idrassi@idrix.fr>. All rights reserved.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE.
*
*/
@superwills
superwills / map reverse
Last active December 16, 2015 15:39
Reverses a mapping of map: T -> vector<S> to map: S -> vector<T>
// Reverses a mapping of map: T -> vector<S> to map: S -> vector<T>
template<typename T, typename S>
void reverseMapping( map< T, vector<S> >& oMapping, map< S, vector<T> >& revMapping )
{
for( typename map< T, vector<S> >::iterator iter = oMapping.begin() ; iter != oMapping.end() ; ++iter )
{
for( int i = 0 ; i < iter->second.size() ; i++ )
{
pair< typename map< S, vector<T> >::iterator, bool > res =
revMapping.insert( make_pair( iter->second[i], vector<T>() ) ) ;
@superwills
superwills / XCodeC++Theme
Created April 25, 2013 19:12
XCode C++ color theme
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>DVTConsoleDebuggerInputTextColor</key>
<string>0 0 0 1</string>
<key>DVTConsoleDebuggerInputTextFont</key>
<string>Menlo-Regular - 12.0</string>
<key>DVTConsoleDebuggerOutputTextColor</key>
<string>0 0 0 1</string>