programming

Max-heap in Go

tung's picture

I was helping somebody on a forum a few days ago with a C++ max-heap implementation. I thought it might be fun to try it out in Go.

package main
 
import (
    "container/vector";
    "flag";
    "rand";
    "time";
)
 
var num = flag.Int("n", 10, "number of elements to heap sort")
 
func main() {
    flag.Parse();
    rand.Seed(time.Nanoseconds());
    v := vector.NewIntVector(*num);
    for i := 1; i <= *num; i++ {
        v.Set(i - 1, rand.Int() % 100)
    }
    heapSort(v);
    printHeap(v, 0, 0);
}
 
func left(i int) int {
    return i * 2 + 1
}
 
func right(i int) int {
    return i * 2 + 2
}
 
func parent(i int) int {
    return (i - 1) / 2
}
 
// Swap vec[i] with its parent if its larger. Stop at the top.
func maxHeapify(vec *vector.IntVector, i int) {
    if i <= 0 {
        return
    }
    p := parent(i);
    if vec.At(p) < vec.At(i) {
        temp := vec.At(p);
        vec.Set(p, vec.At(i));
        vec.Set(i, temp);
        maxHeapify(vec, p);
    }
}
 
// Given a list of numbers in any order, max heap sort it.
func heapSort(vec *vector.IntVector) {
    for i := 0; i < vec.Len(); i++ {
        maxHeapify(vec, i)
    }
}
 
func printHeap(vec *vector.IntVector, i int, nest int) {
    if i >= vec.Len() {
        return
    }
    for n := 0; n < nest; n++ {
        print("    ")
    }
    print(vec.At(i));
    print("\n");
    printHeap(vec, left(i), nest + 1);
    printHeap(vec, right(i), nest + 1);
}
Read more »

goblog: A proof-of-concept blog in Go

tung's picture

As if I wasn't bored enough, I've written a proof-of-concept blog using Go, that new language out of Google. I blogged about this language before, but I wanted to give it a real test drive, so I came up with this.

goblog on github

Pointers ahoy

The first thing I noticed when I got started was pointer trouble on my part. Go still has pointers like in C, but some of the syntax smooths over the differences.

Read more »

Impressions of Go: From the guy who learned it in 2 days

tung's picture

Go is that new-fangled language out of Google, and though I won't describe it (been done much better by somebody else), I will give a few opinions from the point of view of somebody who's written some code in it.

C-like syntax

It looks like C. Braces, parens and semi-colons all make yet another cameo in a programming language.

Read more »

Tea: Simple 2D Ruby game dev

tung's picture

For simpler games from a simpler age.

Tea is a simple 2D game development library for Ruby. It’s designed with these things in mind:

  • 0 is better than 1, and 1 is better than 2.
  • Simplicity beats speed.
  • Value and convenience can sometimes beat simplicity.
  • Procedural beats object-oriented in a dead-heat.

The aim of Tea is to bring back some of the grass roots game development that things like QBASIC fostered. By staying unobtrusive and out of the way, Tea lets you focus on your game or demo, and not the pointy bits that are part of many game engines and APIs.

Read more »

Dirty Coding Tricks

tung's picture

Caught this on Hacker News:

Gamasutra - Dirty Coding Tricks

Hacker News discussion

Nine real-world stories of the horrible hacks that games programmers had to use in order to meet their deadlines. Here's one from the comments on the page by Ken Demarest:

Back on Wing Commander 1 we were getting an exception from our EMM386 memory manager when we exited the game. We'd clear the screen and a single line would print out, something like "EMM386 Memory manager error. Blah blah blah." We had to ship ASAP. So I hex edited the error in the memory manager itself to read "Thank you for playing Wing Commander."
Read more »

Status: Tea and other thoughts

tung's picture

I haven't written here often, because it's stunningly difficult to write something worth reading; only more so when your thoughts are fleeting and your Internet access is sparse. However, just to affirm that I'm still alive and kicking, I'd like to share some stuff I've been up to and thinking about.

First is my new project: Tea. Tea is a simple 2D game development library for Ruby. It's made for the sort of grass-roots development that was popular with QBASIC back in its era. Tea is designed with simplicity in mind: 0 is better than 1, and 1 is better than 2, but at the same time, it won't avoid a valuable, convenient feature just because it can be built from other features. Graphics, input, sound and text are the pillars of Tea.

Tea is making slow but steady progress. Events have been blasted right through. Sound and text are waiting on graphics, and speaking of graphics, that has been a royal pain to deal with so far. Why? In the words of Joel Spolsky:

All non-trivial abstractions, to some degree, are leaky.

To demonstrate, I'll describe what I'm dealing with in the graphics subsystem of Tea. Tea is built largely on Ruby/SDL, which, surprise surprise, provides a Ruby-like flavouring to SDL.

Read more »

Bare-bones embedding Ruby 1.9 in C++

tung's picture

So I never have waste time trying to figure this out again, here's how to embed the current Ruby 1.9 into a basic C++ program.

1. Install Ruby 1.9

I'm running Arch Linux, so I can install the ruby1.9 package from AUR.

yaourt -S ruby1.9
Read more »

Compiler censorship? In my Drupal?

tung's picture

I don't know what the hell is going on, but if I enter "g plus plus" (no quotes, no spaces, plus replaced with plus sign), followed by a space followed by anything, my own blog gives me a 403 permission denied error!

I'd lodge a bug report, but to whom? Drupal, which has a million layers which may or may not be responsible for this? The GeSHi plugin for Drupal? GeSHi itself? The old, outdated PHP 4 installation? I don't have the will to debug yet more web crap, so I'll just post "around" it, if that's even possible.

Incidentally, that's why I haven't posted in a while: this error in a long blog post fooled me into thinking I couldn't post to my own blog. I'll post something I've kept on the back-burner right away.

Read more »

Well that was short-lived

tung's picture

SomeScript
2009 - 2009

Official reason for this halting of development is this bug: many JS-enabled sites won't work, even when whitelisted. I have a vague idea of how to fix it, and it ain't pretty: recreate a large amount of Firefox's own JavaScript blocking. It's a show-stopper bug I can't fix, so I'm going to have to admit defeat here. Sorry for all you hangers-on.

Well, it was a fun excuse to learn how to work with GitHub: that site's awesome. Meanwhile, SomeScript's GitHub project will continue to be hosted there until it gets in my way, so if somebody wants to take it up and run with it, they can.

On the side, I never thought I'd meet something I'd hate more than web development. Firefox extension development isn't as bad, but it's awful close.

Read more »

Building Redcar 0.3 under Arch Linux

tung's picture

According to its website, Redcar is "a programmer's editor for Gnome, written in Ruby and Vala. Redcar is designed to be compatible with Textmate bundles, including syntax highlighting, commands and snippets, and most keybindings are the same."

There are build instructions on GitHub, but they're skewed heavily towards the Debian-based Ubuntu distro. I use Arch, so here's what I did.

Read more »
Syndicate content