Up to Main Index Up to Journal for August, 2022
JOURNAL FOR WEDNESDAY 31ST AUGUST, 2022
______________________________________________________________________________
SUBJECT: Arrays and bubble sorts
DATE: Wed 31 Aug 21:31:37 BST 2022
Since launching Mere and Mere ICE[1] the response has been quite positive. One
person wanted to know why I was pissing away my time f***ing around with Mere
and not working on WolfMUD. Well it’s my time, you hand me a contact, we
settle on a fee, then I’ll spend time how you want it spent, on your dime :|
In the meantime, I’ve been f***ing around with Mere some more :) Mostly I’ve
been adding array handling. I’ve started small and implemented int arrays:
x:int[] ;// define x as an int array
x = int[] 3 4 5 ;// add initial three elements
x = int[] x 6 ;// append an element
x = int[] 2 x ;// prepend an element
x = int[] 1 x 7 ;// append and prepend
println x ;// prints [1 2 3 4 5 6 7]
y:int[] = x ;// duplicate array x as array y
delete x 3 3 ;// delete 3 elements starting at element 3
delete x 2 ;// delete element 2
delete x ;// delete all elements and clear array
// Pick a random small prime...
x:int = int[] ( 2 3 5 7 11 13 17 19 23 29 ) [ rnd ( 10 ) - 1 ]
// Array splicing...
z:int[] = int[] 1 2 3 4 5 6
z = int[] ( ( delete int[] ( z ) 2 4 ) 9 9 9 ( delete int[] ( z ) 0 4 ) )
println z ;// prints [1 2 9 9 9 5 6]
The first two lines can be combined, as in the array splicing, but the above
wouldn’t look so nice ;)
x:int[] = int[] 3 4 5
As for the array splicing, all I can say is that it works. Butt ugly and needs
sorting out. In case you are wondering the ‘int[] ( z )’ creates an anonymous
array from z.
I’ve update all the operators and built-ins to work with int arrays. As part
of my testing I wrote this little ditty:
# A mere bubble sort...
maxN:int = 15 ; // numer of ints to sort
s:int[] ; // array to be sorted
// fill array with random ints
f:int
fill:
s = int[] s rnd maxN * 10
if f ++ < maxN; goto fill
println " In: " s
// some additional initialisation
swap:bool
x:int
l:int = len ( s ) - 1
end:int
start:int = time
// main bubble sort
swapped:
x = 0
l--
swap = false
xloop:
if s [ x ] <= s [ x + 1 ]; goto next
s [ x ] += s [ x + 1 ]
s [ x + 1 ] = s [ x ] - s [ x + 1 ]
s [ x ] -= s [ x + 1 ]
swap = true
next:
if x ++ < l; goto xloop
if swap; goto swapped
end = time
println ( "Out: " s " " ( end - start ) "μs" )
Running it a few times produces:
>mere bubble
In: [51 31 51 3 133 44 132 149 14 92 71 93 61 145 107]
Out: [3 14 31 44 51 51 61 71 92 93 107 132 133 145 149] 103μs
>mere bubble
In: [72 71 138 14 17 26 140 59 98 43 127 120 136 137 138]
Out: [14 17 26 43 59 71 72 98 120 127 136 137 138 138 140] 150μs
>mere bubble
In: [137 61 135 122 51 89 132 46 137 36 132 133 106 144 34]
Out: [34 36 46 51 61 89 106 122 132 132 133 135 137 137 144] 166μs
>mere bubble
In: [94 109 126 32 6 44 128 8 99 10 113 78 81 78 22]
Out: [6 8 10 22 32 44 78 78 81 94 99 109 113 126 128] 275μs
>mere bubble
In: [134 127 73 35 116 14 144 92 72 40 116 104 143 141 150]
Out: [14 35 40 72 73 92 104 116 116 127 134 141 143 144 150] 223μs
>
Not too shabby, 103-275μs. I’ve not updated Mere ICE yet as I’m still testing
and sorting out a few odd corner cases. Then I need to add float, string and
bool arrays. After that I need to add the uint, rune and byte types and their
arrays. Then do it all over again to add associative arrays / maps :P
There are also a few loose ends I want to tidy up. For example the return
value of the *= /= %= += and -= operators are wrong. I can’t deicide if having
them as expressions rather than statements is useful. Would anyone, apart from
myself, even dare to write code such as:
a = b += 2
a [ x ] = b += 2
if b += 2 > a
I’ll update Mere ICE as soon as the int arrays are finished.
--
Diddymus
[1] ../../../annex
Up to Main Index Up to Journal for August, 2022