GitLab will continue to be upgraded from 11.4.5-ce.0 on November 25th 2019 at 4.00pm (AEDT) to 5.00pm (AEDT) due to Critical Security Patch Availability. During the update, GitLab and Mattermost services will not be available.

Commit 7d9edb43 authored by Stefan Marr's avatar Stefan Marr

Added SOM standard library, tests, and benchmarks

Signed-off-by: 's avatarStefan Marr <git@stefan-marr.de>
parent bd6e8abb
"
$Id: All.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
All = (
run = (
| total |
total := 0.
Fibonacci, Dispatch, Bounce, Loop, Permute, Queens, List, Recurse,
Storage, Sieve, BubbleSort, QuickSort, Sum, Towers, TreeSort,
IntegerLoop, FieldLoop
do: [ :benchmark | total := total + benchmark new run ].
('All tests completed (' + total + ' ms)') println.
)
)
"
$Id: Ball.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
Ball = (
| x y xVel yVel |
bounce = (
| xLimit yLimit bounced |
xLimit := yLimit := 500.
bounced := false.
x := x + xVel.
y := y + yVel.
(x > xLimit)
ifTrue: [ x := xLimit. xVel := 0 - xVel abs. bounced := true ].
(x < 0)
ifTrue: [ x := 0. xVel := xVel abs. bounced := true ].
(y > yLimit)
ifTrue: [ y := yLimit. yVel := 0 - yVel abs. bounced := true ].
(y < 0)
ifTrue: [ y := 0. yVel := yVel abs. bounced := true ].
^bounced
)
initialize = (
x := Random next % 500.
y := Random next % 500.
xVel := (Random next % 300) - 150.
yVel := (Random next % 300) - 150.
)
-----------------
new = ( ^super new initialize )
)
"
$Id: Benchmark.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
Benchmark = (
run = (
| startTime elapsedTime measuredTime minTime iterations |
(('Starting ' + (self name)) + ' benchmark ... ') println.
iterations := elapsedTime := 0.
minTime := 100000.
"warm up"
[ iterations < 10 ] whileTrue: [
system fullGC.
self benchmark.
iterations := iterations + 1.].
iterations := 0.
[ elapsedTime < 3000 or: [ iterations < 30 ] ]
whileTrue: [
system fullGC.
startTime := system time.
self benchmark.
measuredTime := system time - startTime.
elapsedTime := elapsedTime + measuredTime.
minTime > measuredTime ifTrue: [ minTime := measuredTime ].
iterations := iterations + 1. ].
(' Iterations: ' + iterations + ' (elapsed time ' + elapsedTime
+ ' ms)') println.
(' RESULT: ' + minTime + ' ms') println.
'' println.
^minTime
)
benchmark = ( self subclassResponsibility )
name = ( ^self class name asString )
)
"
$Id: Bounce.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
Bounce = Benchmark (
benchmark = (
| ballCount balls bounces |
Random initialize.
ballCount := 100.
bounces := 0.
balls := Array new: ballCount withAll: [ Ball new ].
1 to: 50 do: [ :i |
balls do: [ :ball |
(ball bounce) ifTrue: [ bounces := bounces + 1 ] ] ].
(bounces = 1331)
ifFalse: [
self error: 'Wrong result: ' + bounces + ' should be: 1331' ]
)
)
"
$Id: BubbleSort.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
BubbleSort = Sort (
sort: array = (
| top |
"array length - 1 downTo: 0 do: [ :i |
0 to: i - 1 do: [ :j |"
array length downTo: 1 do: [ :i |
1 to: i - 1 do: [ :j |
| current next |
current := array at: j.
next := array at: j + 1.
(current > next)
ifTrue: [
array at: j put: next.
array at: j + 1 put: current ] ] ]
)
dataSize = ( ^130 )
)
"
$Id: Dispatch.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
Dispatch = Benchmark (
benchmark = (
1 to: 20000 do: [ :i | self method: i ]
)
method: argument = ( ^argument )
)
"
$Id: Fibonacci.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
Fibonacci = Benchmark (
benchmark = ( | result |
result := self fibonacci: 20.
(result = 10946)
ifFalse: [
self error: 'Wrong result: ' + result + ' should be: 10946' ]
)
fibonacci: n = (
^(n <= 1)
ifTrue: 1
ifFalse: [ (self fibonacci: (n - 1)) + (self fibonacci: (n - 2)) ]
)
)
"
Copyright (c) 2001-2013 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
FieldLoop = Benchmark (
| counter |
benchmark = ( | iter |
counter := 0.
iter := 20000.
[ iter > 0 ] whileTrue: [
iter := iter - 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
counter := counter + 1.
]
)
)
"
$Id: IntegerLoop.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
IntegerLoop = Benchmark (
benchmark = ( | bounds |
bounds := 20000.
bounds negated to: bounds by: 1 do: [:value | |a| a := value-value].
)
)
"
$Id: List.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
List = Benchmark (
benchmark = ( | result |
result := self
taklWithX: (self makeList: 15)
withY: (self makeList: 10)
withZ: (self makeList: 6).
(result length = 10)
ifFalse: [
self error:
'Wrong result: ' + result length asString + ' should be: 10' ].
)
makeList: length = (
(length = 0)
ifTrue: [ ^nil ]
ifFalse: [
^(ListElement new: length)
next: (self makeList: (length - 1)) ]
)
isShorter: x than: y = (
| xTail yTail |
xTail := x. yTail := y.
[ yTail isNil ]
whileFalse: [
xTail isNil ifTrue: [ ^true ].
xTail := xTail next.
yTail := yTail next ].
^false
)
taklWithX: x withY: y withZ: z = (
(self isShorter: y than: x)
ifTrue: [
^(self
taklWithX: (self taklWithX: x next withY: y withZ: z)
withY: (self taklWithX: y next withY: z withZ: x)
withZ: (self taklWithX: z next withY: x withZ: y)) ]
ifFalse: [ ^z ].
)
)
"
$Id: ListElement.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
ListElement = (
| val next |
length = ( next isNil ifTrue: [ ^1 ] ifFalse: [ ^(1 + next length) ] )
val = ( ^val )
val: n = ( val := n )
next = ( ^next )
next: element = ( next := element )
------------------
new: n = ( ^super new val: n )
)
"
$Id: Loop.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
Loop = Benchmark (
singleRun = (
| sum |
sum := 0.
1 to: 100 do: [ :j | sum := sum + 1 ].
(sum = 100)
ifFalse: [
self error: 'Wrong result: ' + sum + ' should be: 100' ].
^ sum
)
benchmark = (
| sum |
sum := 0.
1 to: 200 do: [ :i | sum := sum + self singleRun ].
(sum = 20000)
ifFalse: [
self error: 'Wrong result: ' + sum + ' should be: 20000' ]
)
)
"
$Id: Permute.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
Permute = Benchmark (
| count v |
benchmark = (
count := 0.
v := Array new: 7.
self permute: 6.
(count = 8660)
ifFalse: [
self error: 'Wrong result: ' + count + ' should be: 8660' ]
)
permute: n = (
count := count + 1.
(n <> 0)
ifTrue: [
self permute: n - 1.
n downTo: 1 do: [ :i |
self swap: n with: i.
self permute: n - 1.
self swap: n with: i ] ]
)
swap: i with: j = (
| tmp |
tmp := v at: i.
v at: i put: (v at: j).
v at: j put: tmp
)
)
"
$Id: Queens.som 31 2009-07-31 12:25:18Z michael.haupt $
Copyright (c) 2001-2007 see AUTHORS file
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the 'Software'), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"
Queens = Benchmark (
| freeMajs freeRows freeMins queenRows |
benchmark = (
1 to: 10 do: [ :i | self queens ]
)
queens = (
freeRows := Array new: 8 withAll: true.
freeMajs := Array new: 16 withAll: true.
freeMins := Array new: 16 withAll: true.
queenRows := Array new: 8 withAll: -1.
(self placeQueen: 1) ifFalse: [ self error: 'Wrong result' ]
)
placeQueen: c = (
1 to: 8 do: [ :r |
(self row: r column: c)
ifTrue: [
queenRows at: r put: c.
self row: r column: c put: false.
(c = 8) ifTrue: [ ^true ].
(self placeQueen: c + 1) ifTrue: [ ^true ].
self row: r column: c put: true ] ].
^false
)
row: r column: c = (
^(freeRows at: r) && (freeMajs at: c + r) && (freeMins at: c - r + 8)
)
row: r column: c put: v = (
freeRows at: r put: v.
freeMajs at: c + r put: v.
freeMins at: c - r + 8 put: v.
)
)