\ Toolset for one- and two-dimensional arrays in ANS Forth \ --------------------------------------------------- \ (c) Copyright 2001 Julian V. Noble. \ \ Permission is granted by the author to \ \ use this software for any application pro- \ \ vided this copyright notice is preserved. \ \ --------------------------------------------------- \ Requires CORE and CORE EXT wordsets : [undefined] BL WORD FIND NIP 0= ; \ non-Standard word: [undefined] CELL- [IF] : cell- [ -1 CELLS ] LITERAL + ; [THEN] \ words for 1-dimensional arrays : long ; : 1array ( len #bytes/datum --) \ ( #b len data ...) CREATE 2DUP , , * ALLOT ; : _len ( base_addr -- len) \ determine length of an array CELL+ @ ; : } ( base_adr indx -- adr[indx] ) OVER _len OVER <= OVER 0< OR ABORT" Index out of range" OVER @ * + CELL+ CELL+ ; \ words for 2-dimensional arrays : wide ; : 2array ( hgt wid data_size --) \ ( wid #b len data ...) CREATE >R TUCK , ( wid hgt) R@ , * DUP , R> * ALLOT ; : }} ( base_adr m n -- adr[m,n] ) \ data stored row-wise 2>R CELL+ DUP cell- @ R> * R> + ( base_adr+cell m+n*w) } ; FALSE [IF] Usage examples: 20 long 2 FLOATS 1array a{ \ complex vector 20 long 20 wide 1 FLOATS 2array M{{ \ real matrix 20 long 1 CELLS 1array Irow{ \ single-length, integer- \ valued vector say M{{ I J }} ( -- adr[m_ij] ) to dereference. [THEN]