Skip to content

Instantly share code, notes, and snippets.

View smspillaz's full-sized avatar
💥
mindblown

Sam Spilsbury smspillaz

💥
mindblown
View GitHub Profile
void inplace_merge(int *first, int *mid, int *end) {
int *datacopy[size of data];
int *left = first;
int *right = end;
int next = 0;
int n = end - first;
while (next < n) {
if (left < mid && *left < *right) {
datacopy[next++] = *left;
} else if (right < end) {
for (int size = 1; size < n; size = 2 * size) {
int nSegments = n / size;
#pragma omp for
for(int segment = 0; segment < nSegments; ++segment) {
int leftStart = segment * size;
int mid = min(leftStart + (size / 2), n-1);
int rightEnd = min(leftStart + size, n-1);
merge(data, leftStart, mid, rightEnd);
}
import fs from 'fs';
function oldStyleAsynchronousFunction(callback) {
fs.readFile('somefile.txt', 'utf8', function(err, result) {
if (err) {
callback(err, null);
return;
}
callback(result);
def both_objectives(layout, columns, *args):
return (frequency_objective(layout,*args),
similarity_objective(layout,*args))
def random_evaluated_design(seed_design, objective_function, *args):
"""Return a new random design, evaluated by objective_function"""
candidate = random.sample(seed_design, len(seed_design))
return (candidate, *objective_function(candidate, *args))
def both_objectives(layout, columns, *args):
return (frequency_objective(layout,*args),
similarity_objective(layout,*args))
def random_evaluated_design(seed_design, objective_function, *args):
"""Return a new random design, evaluated by objective_function"""
candidate = random.sample(seed_design, len(seed_design))
return (candidate, *objective_function(candidate, *args))
def both_objectives(layout, columns, *args):
return (frequency_objective(layout,*args),
similarity_objective(layout,*args))
def random_evaluated_design(seed_design, objective_function, *args):
"""Return a new random design, evaluated by objective_function"""
candidate = random.sample(seed_design, len(seed_design))
return (candidate, *objective_function(candidate, *args))
@smspillaz
smspillaz / random.py
Created October 31, 2018 17:42
Random Search in Pyton
import random
def random_search(array, iterations, objective, *args):
"""Do a random search over the array for :iterations:."""
best = array[0]
best_value = objective(best, *args)
for i in range(iterations):
candidate = random.sample(best, len(best))
value = objective(candidate, *args)
@smspillaz
smspillaz / foo.py
Created October 1, 2018 15:35
Opening files the right way
# So generally speaking, if you want to be opening files with Python
# its better to observe the "Easier to ask for forgiveness than permission"
# principle (EAFP) where just blindly try to open the file as though it
# existed and then handle exceptions where it does not:
#
# The reason why you should do this is two fold:
# (1) Checking first always imposes a performance cost due
# to the extra system call overhead.
# (1.1) Unlike languages like C++, you always pay the
# cost of exception paths in Python, so it makes
M /metadata
M /files/manifest.json
M /files/lib/libcontentfeed-0.so.0.0.0
M /files/lib/libeknr-0.so.0.0.0
M /files/lib/libendless-0.so.0.500.0
M /files/lib/libeoscompanion-1.so.0.0.0
M /files/lib/python3.5/site-packages/eoscompanion/__pycache__/__init__.cpython-35.opt-1.pyc
M /files/lib/python3.5/site-packages/eoscompanion/__pycache__/applications_query.cpython-35.opt-1.pyc
M /files/lib/python3.5/site-packages/eoscompanion/__pycache__/constants.cpython-35.opt-1.pyc
M /files/lib/python3.5/site-packages/eoscompanion/__pycache__/content_streaming.cpython-35.opt-1.pyc
@smspillaz
smspillaz / matapp.cpp
Created August 19, 2018 15:06
These two should be equivalent....
auto translationMat = glm::translate (
glm::mat4 (1.0),
glm::vec3 (agd::get <0> (translation),
agd::get <1> (translation),
0.0)
);
auto invCenterMat = glm::translate (
glm::mat4 (1.0),
glm::vec3 (-1 * agd::get <0> (intendedCenter),
-1 * agd::get <1> (intendedCenter),