Skip to content

Instantly share code, notes, and snippets.

@toboqus
toboqus / Binary Search
Created September 29, 2013 18:19
Binary searching through an array
angular.module('Scope.safeApply', [])
.run(['$rootScope', function ($rootScope) {
$rootScope.$safeApply = function () {
var fn, phase = this.$root.$$phase;
if (arguments.length == 1) {
fn = arguments[0];
} else {
myApp.directive('capitalizeFirst', function($parse) {
return {
require: 'ngModel',
link: function(scope, element, attrs, modelCtrl) {
var capitalize = function(inputValue) {
if (inputValue === undefined) { inputValue = ''; }
var capitalized = inputValue.charAt(0).toUpperCase() +
inputValue.substring(1);
if(capitalized !== inputValue) {
modelCtrl.$setViewValue(capitalized);
@toboqus
toboqus / speedTestJS
Created September 23, 2014 14:44
Performance testing JS
(function(global){
var testFunc = function testFunc(func){
var start = new Date();
func();
return new Date() - start;
},
speedTest = function speedTest(name, func){
var numOfTimes = 1000,
@toboqus
toboqus / loadDependencies.js
Last active August 29, 2015 14:07
Synchronously execute functions/load dependencies
$scope.loadDependencies = function(arr){
var deferred = $q.defer();
var arrLen = arr.length,i = -1;
(function loadNext(){
i++;
(i < arrLen) ? arr[i](loadNext) : deferred.resolve();
})();
return deferred.promise;
};
@toboqus
toboqus / OrderedInsert.js
Last active December 14, 2015 08:43
Ordered insert using binary searching for angularJS
/**
* @name orderedInsert
* @param {Array} collection
* @param {String} key
* @param {Object | array} items - array of objects to insert
* @returns {promise}
* @description will insert an object into a sorted collection using a modified
* form of binary searching. it will also insert an array of objects if that is provided
* instead.
* @example
@toboqus
toboqus / RussianPeasant.java
Created September 14, 2015 18:32
Russian peasant algorithm
public class RussianPeasant {
public Static int product(int a, int b){
int x = a;
int y = b;
int z = 0;
while(x > 0){
if(x%2 == 1) z += y;
x = x >> 1;
@toboqus
toboqus / xorEncryption.c
Created October 31, 2015 17:41
Simple demonstration on using xor to encrypt data with a key, and a demonstration of an attack with both the plaintext and cyphertext
/*
* xorEncryption.c
*
* Created on: 30 Oct 2015
* Author: Alex
*/
#include <stdio.h>
#include <string.h>
int main(void)
@toboqus
toboqus / btree.cpp
Created November 3, 2015 08:53
Binary tree implementation in c++
#include <iostream>
using namespace std;
struct node{
int value;
node *left;
node *right;
};
@toboqus
toboqus / linkedList.cpp
Created November 3, 2015 11:42
Linked List in C++
/*
* linkedList.cpp
*
* Created on: 3 Nov 2015
* Author: Alex
*/
#include <iostream>
#include "linkedList.hpp"
using namespace std;