Skip to content

Instantly share code, notes, and snippets.

View NicMcPhee's full-sized avatar

Nic McPhee NicMcPhee

View GitHub Profile
@NicMcPhee
NicMcPhee / Mergesort.java
Created September 28, 2021 01:58
Java implementation of Mergesort and Quicksort. It's the allocation of the array destination on line 29 in Mergesort.java that creates the memory management complications. In Java we don't have to worry about freeing up that memory because the garbage collector takes care of it for us, but in C we'll have to look after that ourselves.
package umm.systems;
public class Mergesort implements Sorter {
@Override
public void sort(int[] values) {
mergesortRange(values, 0, values.length);
}
private void mergesortRange(int[] values, int startIndex, int endIndex) {
@NicMcPhee
NicMcPhee / docker-compose.yml
Created February 7, 2021 21:23
The `docker-compose.yml` file for the Cloud Computing class Docker lab
services:
server:
container_name: server
build: ./server # Location of the Dockerfile for the client
restart: always
environment:
MONGO_ADDR: mongo # hostname of the mongo container
MONGO_DB: prod
WAIT_HOSTS: mongo:27017 # wait for mongo to start up before starting the server
depends_on:
@NicMcPhee
NicMcPhee / Main.java
Created February 5, 2021 18:54
Java Hello World!
public class Main {
public static void main(String[] args) {
System.out.println("Hello from inside a Java container!");
}
}
@NicMcPhee
NicMcPhee / ThreadDemo.java
Created October 13, 2020 14:42
A simple example of threading in Java
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadDemo {
private int count = 0;
private AtomicInteger atomicCount = new AtomicInteger(0);
public static void main(String[] args) throws InterruptedException {
ThreadDemo demo = new ThreadDemo();
demo.go();
}
@NicMcPhee
NicMcPhee / _README_replacing_temporary_files_with_pipes.md
Last active September 4, 2020 17:22
Using pipes to eliminate temporary files

Using pipes to eliminate temporary files

This illustrates using pipes to eliminate temporary files. We start with a bash script that takes some demographic data (see MOCK_DATA.csv) specified as a file name as a command line argument. The script then outputs a count of how many people come from different states. The output on the included data file is:

 49 MN
@NicMcPhee
NicMcPhee / Employees.xml
Created October 24, 2013 04:40
A simple example of reading and parsing an XML document in Java. This is based substantially on http://www.java-tips.org/java-se-tips/javax.xml.parsers/how-to-read-xml-file-in-java.html but cleaned up some so there's less duplicate logic. It assumes the existence of a file called "Employees.xml" which is also included here.
<?xml version="1.0"?>
<company>
<employee>
<firstname>Tom</firstname>
<lastname>Cruise</lastname>
</employee>
<employee>
<firstname>Paul</firstname>
<lastname>Enderson</lastname>
</employee>
@NicMcPhee
NicMcPhee / counters.clj
Last active September 16, 2019 10:59
Three different ways of doing counters in Clojure, one of which doesn't work and thereby illustrates the scope of atomicity and a difference between atom and ref.
;;;;;;;;;;;;;;;;;;
;; The first way
;;;;;;;;;;;;;;;;;;
;; This use of atom is simple and works fine.
(def counter (atom -1))
(defn next-value []
(swap! counter inc))
@NicMcPhee
NicMcPhee / Counter.java
Last active July 14, 2019 14:53
A simple example of a successful use of Java synchronization to eliminate race conditions. The "synchronized" keyword on Counter.incrementCount() is crucial here; removing it will lead to serious race conditions on multiple-core computers. Having it ensures that two threads can't enter this method at the same time, making sure that only one thre…
public class Counter {
private int count = 0;
public int getCount() {
return count ;
}
/**
* The "synchronized" keyword is crucial here; removing it will lead to serious
@NicMcPhee
NicMcPhee / DateClient.java
Last active October 15, 2018 04:05
Simple Date client/server example. Start the server, and then running the client gets the date from the server and prints it.
import java.net.*;
import java.io.*;
public class DateClient {
public static final int portNumber = 6013;
public static void main(String[] args) throws IOException {
String server;
// Use "127.0.0.1", i.e., localhost, if no server is specified.
if (args.length == 0) {
@NicMcPhee
NicMcPhee / GECCO_tutorial_graph_database_demo.cypher
Created July 15, 2018 23:26
GECCO tutorial graph database demo code
// Clear the DB for a clean start
MATCH (n) DETACH DELETE n;
CREATE CONSTRAINT ON (i:Individual) ASSERT i.uuid IS UNIQUE;
CREATE CONSTRAINT ON (e:Errors) ASSERT e.Errors_vector IS UNIQUE;
CREATE INDEX ON :Individual(generation);
CREATE INDEX ON :Errors(total_error);
USING PERIODIC COMMIT