Skip to content

Instantly share code, notes, and snippets.

View ctreffs's full-sized avatar
💭
🏊 🚴‍♂️ 🏃

Christian Treffs ctreffs

💭
🏊 🚴‍♂️ 🏃
View GitHub Profile
@ctreffs
ctreffs / EnumCollection.swift
Last active June 22, 2017 08:22
EnumCollection Swift 3.1+
protocol EnumCollection {}
extension EnumCollection where Self: Hashable {
static func sequence() -> AnySequence<Self> {
return AnySequence { () -> AnyIterator<Self> in
var raw = 0
return AnyIterator {
let `case` : Self = withUnsafePointer(to: &raw) { $0.withMemoryRebound(to: Self.self, capacity: 1) { $0.pointee } }
guard `case`.hashValue == raw else { return nil }
raw += 1
return `case`
@ctreffs
ctreffs / speedup.sh
Created November 29, 2016 11:56 — forked from madson/speedup.sh
Script to mount directories DerivedData and iPhoneSimulator in the RAM
#!/bin/bash
# set disk size for 512 MB
disksize=$((512*(512*4)))
# mounting DerivedData virtual disk
disknum=$(hdid -nomount ram://$disksize | tr -cd '[0-9]')
newfs_hfs -v DerivedData /dev/rdisk$disknum
diskutil mount -mountPoint ~/Library/Developer/Xcode/DerivedData /dev/disk$disknum
@ctreffs
ctreffs / clearDerivedData.sh
Created November 29, 2016 12:13
clearDerivedData
#!/bin/bash
# Script to remove all Xcode DerivedData,
# provided Xcode is set to default folder locations
printf "clearing DerivedData..."
rm -rfv ~/Library/Developer/Xcode/DerivedData/*
echo "done"
exit 0
@ctreffs
ctreffs / clearDeveloperCache.sh
Created November 29, 2016 12:13
clearDeveloperCache
#!/bin/bash
# Script to remove all Xcode DeveloperTools Cache files,
# provided Xcode is set to default folder locations
printf "clearing Xcode DeveloperTools cache..."
rm -rfv /var/folders/**/**/**/com.apple.DeveloperTools/*
rm -rfv ~/Library/Caches/com.apple.dt.Xcode/*
echo "done"
exit 0
@ctreffs
ctreffs / sdl-metal-example.m
Created August 5, 2017 11:06 — forked from slime73/sdl-metal-example.m
SDL + Metal example
/**
* This software is in the public domain. Where that dedication is not recognized,
* you are granted a perpetual, irrevokable license to copy and modify this file
* as you see fit.
*
* Requires SDL 2.0.4.
* Devices that do not support Metal are not handled currently.
**/
#import <UIKit/UIKit.h>
@ctreffs
ctreffs / imgui_memory_editor.h
Created August 6, 2017 21:12 — forked from ocornut/(Archived) imgui_memory_editor.h
Mini memory editor for ImGui (to embed in your game/tools)
// Mini memory editor for ImGui (to embed in your game/tools)
// Animated GIF: https://cloud.githubusercontent.com/assets/8225057/9028162/3047ef88-392c-11e5-8270-a54f8354b208.gif
//
// You can adjust the keyboard repeat delay/rate in ImGuiIO.
// The code assume a mono-space font for simplicity! If you don't use the default font, use ImGui::PushFont()/PopFont() to switch to a mono-space font before caling this.
//
// Usage:
// static MemoryEditor mem_edit_1; // store your state somewhere
// mem_edit_1.Draw("Memory Editor", mem_block, mem_block_size, 0x0000); // run
//
@ctreffs
ctreffs / ArrayFromTuple.swift
Last active September 16, 2021 14:16
Array from Tuple [Swift]
/// Generates array form a tuple. Given tuple's elements must have homogenous type.
///
/// - Parameter tuple: a (homogenous) tuple
/// - Returns: array of tuple elements
func makeArray<Tuple, Value>(from tuple: Tuple) -> [Value] {
let tupleMirror = Mirror(reflecting: tuple)
assert(tupleMirror.displayStyle == .tuple, "Given argument is no tuple")
assert(tupleMirror.superclassMirror == nil, "Given tuple argument must not have a superclass (is: \(tupleMirror.superclassMirror!)")
assert(!tupleMirror.children.isEmpty, "Given tuple argument has no value elements")
func convert(child: Mirror.Child) -> Value? {
@ctreffs
ctreffs / DictionaryFromTuple.swift
Created October 5, 2017 09:28
Dictionary from Tuple [Swift]
/// Generates dictionary from a tuple. Given tuple's elements must have homogenous type.
///
/// - Parameter tuple: a (homogenous) tuple
/// - Returns: dict of tuple elements
func makeDictionary<Tuple, Value>(from tuple: Tuple) -> [String:Value] {
let tupleMirror = Mirror(reflecting: tuple)
assert(tupleMirror.displayStyle == .tuple, "Given argument is no tuple")
assert(tupleMirror.superclassMirror == nil, "Given tuple argument must not have a superclass (is: \(tupleMirror.superclassMirror!)")
assert(!tupleMirror.children.isEmpty, "Given tuple argument has no value elements")
@ctreffs
ctreffs / HashCombine.swift
Created October 18, 2017 06:48
[Swift] hash combine and hash range
/// Calculates the combined hash of two values. This implementation is based on boost::hash_combine.
/// Will always produce the same result for the same combination of seed and value during the single run of a program.
///
/// - Parameters:
/// - seed: seed hash.
/// - value: value to be combined with seed hash.
/// - Returns: combined hash value.
func hash(combine seed: Int, _ value: Int) -> Int {
/// http://www.boost.org/doc/libs/1_65_1/doc/html/hash/combine.html
/// http://www.boost.org/doc/libs/1_65_1/doc/html/hash/reference.html#boost.hash_combine
/** A pthread-based recursive mutex lock. */
public class Mutex {
private var mutex: pthread_mutex_t = pthread_mutex_t()
public init() {
var attr: pthread_mutexattr_t = pthread_mutexattr_t()
pthread_mutexattr_init(&attr)
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE)
let err = pthread_mutex_init(&self.mutex, &attr)