Paste: my version of multi dimensional arrays - two variants
Author: | kobi (randy7) |
Mode: | factor |
Date: | Thu, 3 Jun 2010 21:37:14 |
Plain Text |
USING: accessors arrays assocs fry hashtables kernel math
math.order math.vectors sequences ;
IN: mdarray
TUPLE: multi1
default-value
{ planes sequence }
{ underlying array } ;
: <multi1>
2dup
[ product ] dip <array>
multi1 new
swap >>underlying
swap >>default-value
swap >>planes ;
TUPLE: multi2
default-value
{ planes sequence }
{ cells hashtable } ;
: <multi2>
multi2 new
swap >>default-value
swap >>planes
H{ } clone >>cells ;
: within?
[ 1 - 0 swap between? ] 2all? ;
ERROR: not-within! positions planes ;
: ensure-within
2dup planes>> within? [ planes>> not-within! ] unless ;
M: multi2 set-nth
ensure-within cells>> set-at ;
M: multi2 nth
ensure-within
[ cells>> at* ] [ default-value>> ] bi
'[ drop _ ] unless ;
: muls-seq
{ 1 } swap rest [ over first * prefix ] each ;
: coord>index
muls-seq v* sum ;
: (positions>index)
[ planes>> coord>index ] [ underlying>> ] bi ;
M: multi1 nth
ensure-within (positions>index) nth ;
M: multi1 set-nth
ensure-within (positions>index) set-nth ;
New Annotation