Skip to content

Commit

Permalink
Few fixes + beginning of the sorting example
Browse files Browse the repository at this point in the history
  • Loading branch information
Amos Wenger committed Jun 8, 2012
1 parent 695c3a3 commit 3f65028
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
3 changes: 1 addition & 2 deletions samples/Makefile
Expand Up @@ -2,5 +2,4 @@
all: all:
rm -rf snowflake rm -rf snowflake
rock rock
#(cd snowflake && make) (cd snowflake && make)
colordiff snowflake snowflake-reference
2 changes: 1 addition & 1 deletion samples/sample.use
@@ -1 +1 @@
Main: util.ooc Main: sorting.ooc
60 changes: 60 additions & 0 deletions samples/sorting.ooc
@@ -0,0 +1,60 @@
import math/Random

List: class <X> {
data: X*
size: SizeT

init: func (=size) {
data = gc_malloc(X size * size)
}

get: func (index: Int) -> X {
data[index]
}

set: func (index: Int, element: X) {
data[index] = element
}

swap: func (i, j: Int) {
tmp := get(i)
(data[i], data[j]) = (get(j), tmp)
}

print: func (f: Func (X) -> String) {
"(" print()
for (i in 0..size) {
f(get(i)) print()
if (i < size - 1) ", " print()
}
")" println()
}

bubbleSort!: func (compare: Func (X, X) -> Int) {
sorted := false
while (!sorted) {
sorted = true
for (i in 0..size - 1) {
if (compare(get(i), get(i + 1)) > 0) {
sorted = false
swap(i, i + 1)
}
}
}
}
}

test1: func {
l := List<Int> new(10)
for (i in 0..l size) {
l set(i, Random randInt(0, 200))
}
l print(|i| "%d" format(i))
l bubbleSort!(|a, b| a <=> b)
l print(|i| "%d" format(i))
}

main: func {
test1()
}

0 comments on commit 3f65028

Please sign in to comment.