Skip to content

Instantly share code, notes, and snippets.

@jmhertlein
jmhertlein / removefirstocc.cpp
Created February 27, 2012 23:39
Removing the first occurrence of a generic element x in a node-based linked list.
//assume we're looking for x.
bool found = false;
Node* temp = head, rm = NULL;
//now we loop through our list looking for a node that holds something == to x
while(temp != null && !found) {
if(temp->data == x) { //if we find it, set found to true and rm to the node
found = true;
rm = temp;
@jmhertlein
jmhertlein / listeners.java
Created June 29, 2012 23:41
Listeners (CraftBukkit)
//let's say we're replacing this:
public MyChatListener extends ChatListener {
public void onPlayerChat(PlayerChatEvent event) {
//do something
}
}
//We'll change it to this:
@jmhertlein
jmhertlein / gist:3327775
Created August 11, 2012 23:20
procedure: serverCountdownTask
var remainingTime := 5
procedure run()
if(remainingTime <= 0) then
Bukkit.shutdown()
else
print("Halt in " + remainingTime)
remainingTime--
end
buildMaxHeap(data)
//Precondition: data is an ordered set that contains n elements
data.heapSize = data.length
//Loop invariant: the trees rooted at indices in the range [i,n] are max-heaps
for i=FLOOR(data.length/2) downto 1
maxHeapify(data, I)
//Postcondition: data[1] is the root of a tree that is a max-heap
mergeSort(data)
//Precondition: data is an ordered set of n elements
@jmhertlein
jmhertlein / gist:4123956
Created November 21, 2012 09:23
Create a binary search tree from a list of numbers sorted in increasing order
//Precondition: A is a list of elements, sorted in increasing order
// P is the root of the binary tree to be created
//Postcondition: P is the root of a binary tree containing all elements in A
procedure mktree(list A, node P)
if A is empty,
return
middle = A.length/2
@jmhertlein
jmhertlein / gist:5471873
Created April 27, 2013 04:20
pseudocode for authenticating a user with rsa keys
Server has client's public key already, in a list of trusted public keys
keys are RSA key pairs
client connects
server sends its pubkey
client now using server's pubkey to encrypt all outgoing communications, incoming are still unencrypted
client sends username it wants to authenticate as
server loads trusted pubkey for username
encrypts n securely-randomly-generated bytes, sends over unencrypted connection to client
client uses private key to decrypt
private void deleteTown(ObjectOutputStream oos, ObjectInputStream ois) throws IOException, ClassNotFoundException {
//receive arguments over socket
final String townName = (String) ois.readObject();
final FutureBoolean result = new FutureBoolean();
final Object callback = this;
//prepare a Runnable object to delete the town from the server's main thread
Runnable r = new Runnable() {
@Override
client connects to server
<authentication happens>
client writes a RemoteAction enum to signal what it wants to do (ADD_PLAYER, REMOVE_PLAYER, etc)
server reads enum, uses a big switch-case to direct flow to appropriate method
in the appropriate method, server reads in "arguments" for action one by one, as client sends them
server executes the action once it has all its input
server communicates back exit status
@jmhertlein
jmhertlein / MuhJFrame.java
Last active October 12, 2020 19:59
JFrame boilerplate
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.SwingWorker;
public class MuhJFrame extends JFrame {
private SwingWorker gameLooper;
private boolean stop;
private int seconds;
/*
* Copyright (C) 2013 everdras@gmail.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of