Skip to content

Instantly share code, notes, and snippets.

View garymacindoe's full-sized avatar

Gary Macindoe garymacindoe

View GitHub Profile
@garymacindoe
garymacindoe / check-keywords
Created April 15, 2015 12:13
Checks for invalid/obsolete keywords in Gentoo Portage's packages.accept_keywords files
#!/bin/bash
#
# Author: Gary Macindoe
# Date: May 2013
files="/etc/portage/package.accept_keywords /etc/portage/package.keywords"
fs=""
for f in ${files}
do
if [ -d "${f}" ]
@garymacindoe
garymacindoe / check-use
Created April 15, 2015 12:14
Checks for invalid/obsolete entries in Gentoo Portage's package.use files
#!/bin/bash
#
# Author: Gary Macindoe
# Date: May 2013
files="/etc/portage/package.use"
if [ -d "${files}" ]
then
files="${files}/*"
fi
@garymacindoe
garymacindoe / upgrade-kernel
Last active November 25, 2018 19:08
Compiles and installs two versions of a new kernel source - one with an EFI stub loader and one without - using the running kernel's config as a template (requires /proc/config.gz)
#!/bin/bash
#
# Author: Gary Macindoe
# Date: November 2014
set -eu
EFI_KEYS="/root/efi"
LINUX_SOURCE="/usr/src/linux"
@garymacindoe
garymacindoe / check-world
Created May 6, 2015 12:48
Checks for invalid/obsolete entries in Gentoo Portage's world file
#!/bin/bash
#
# Author: Gary Macindoe
# Date: May 2015
WORLD_FILE=/var/lib/portage/world
while read package;
do
DEPS=$(grep -l ${package} /var/db/pkg/*/*/{,R}DEPEND | cut -d'/' -f5,6 | sort -u);
@garymacindoe
garymacindoe / nextpow2.hpp
Created June 18, 2015 14:44
Round up to the next highest power of 2 using C++11 compile-time templates
#include <type_traits>
#include <limits>
template <typename UIntType, UIntType v, unsigned int n>
struct set_lower_bits : public std::integral_constant<UIntType, v | (v >> n) | set_lower_bits<UIntType, v | (v >> n), (n >> 1)>::value> {};
template <typename UIntType, UIntType v>
struct set_lower_bits<UIntType, v, 1> : public std::integral_constant<UIntType, v | (v >> 1)> {};
template <typename UIntType, UIntType v>
@garymacindoe
garymacindoe / vtable.cpp
Created July 29, 2015 13:55
Calling a private method through the vtable
#include <iostream>
class A {
virtual void function() const {
std::cout << "You bastard!" << std::endl;
}
};
int main() {
A a;
@garymacindoe
garymacindoe / woo.cpp
Created August 12, 2015 15:16
Implementation of voxel traversal algorithm from "A Fast Voxel Traversal Algorithm for Ray Tracing" (Amanatides and Woo)
/*
* Implementation of voxel traversal algorithm from "A Fast Voxel Traversal
* Algorithm for Ray Tracing" (Amanatides and Woo).
*/
#include <iostream>
#include <iterator>
#include <tuple>
#include <limits>
#include <algorithm>
#!/bin/bash
#
# Converts mercurial repostories to git repositories.
# Requires git-remote-hg from https://github.com/felipec/git-remote-hg.
#
if [ $# -lt 2 ] || [ $# -gt 3 ]
then
echo "Usage: ${0} <url> <local_directory> [user_map]"
echo "Where"
@garymacindoe
garymacindoe / fibonacci.c
Created September 12, 2015 18:32
Calculation of the nth term of the Fibonacci sequence in constant time
#include <stdio.h>
#include <inttypes.h>
#include <math.h>
uint64_t fibonacci(uint64_t n) {
static const double golden = 1.61803398874989484820;
return (uint64_t)floor((pow(golden, (double)n) / sqrt(5.0)) + 0.5);
}
@garymacindoe
garymacindoe / kadane.cpp
Created September 14, 2015 20:38
O(n) solution to the maximum subarray sum problem using Kadane's algorithm
#include <tuple>
#include <iterator>
#include <algorithm>
template <class InputIterator>
std::tuple<typename std::iterator_traits<InputIterator>::value_type,
InputIterator, InputIterator>
maximum_subarray(InputIterator first, InputIterator last) {
typedef typename std::iterator_traits<InputIterator>::value_type value_type;
using std::max;