开发者

How to manage 2d array in Smalltalk?

I have a list of point and have to do erosion/dilation operations. I need a kind of开发者_StackOverflow社区 2d-array but can't find how to do in VisualWorks (I know there is a Array2d class in Squeak, but I must use VW).


Many Smalltalk implementation will have some kind of Matrix class, sometimes optimized, that will have methods such as #rowAt:columnAt: (or for brevity #at:at:).

In GNU Smalltalk this is in package DhbNumericalMethods. Right now it is not optimized though.


Use simply a generic way: array of arrays:

(Array new: xSize)
    at: 1 put: ((Array new: ySize) at: 1 put: aValue; at: 2 put: aValue; ...);
    at: 2 put: ((Array new: ySize) at: 1 put: aValue; at: 2 put: aValue; ...);
    ...


If you want to the operations to be efficient, study the VisualWorks Image class, protocol "image processing" and "bit processing". Build your own erosion/dilation operations based on primitives there.


Here is another way to deal with a two dimensional array in Squeak (I'm using version 4.2).

test := Matrix new: 3.     "this defines a 3 x 3 array"
test at: 1 at: 1 put: 5.
test at: 1 at: 2 put: 6.
test at: 1 at: 3 put: 7.

etc, etc. AFAIK you can only do 2D arrays this way, and they must be a square matrix. This worked well for a project that my son and I are working on to make a Sudoku game, ymmv. CHEERS!


Array subclass: Array2D

instanceVariableNames: 'myRows myColumns'

classVariableNames: ''
poolDictionaries: ''

category: 'Basic Data Structures'

"I am a two-dimensional array of arbitrary objects.

[Privately, I am really a linear (one-dimensional) array.
I locate my elements internally by index arithmetic on their
(two-dimensional) coordinates.]"


Instance creation (class)
-------------------------

new: nRows by: nColumns
   "Create a new instance of me with nRows rows and nColumns
    columns."
   
   ^(super new: (nRows * nColumns))
       withRows: nRows withColumns: nColumns
   
   "exampleArray := Array2D new: 10 by: 5"


Initialization
--------------

withRows: nRows withColumns: nColumns
   "Set my number of rows and columns to nRows and nColumns,
   respectively.
"
   
   myRows    := nRows.
   myColumns := nColumns


Properties
----------

rows
   "My number of rows."
   
   ^myRows


columns
   "My number of columns."
   
   ^myColumns


Element access
--------------

atRow: whichRow atColumn: whichColumn
   "My element at row whichRow and column whichColumn."
   
   ^super at: (self indexAtRow: whichRow
                      atColumn: whichColumn)
   
   "exampleValue := exampleArray atRow: 6 atColumn: 4"


atRow: whichRow atColumn: whichColumn put: newValue
   "Store value newValue as my element at row whichRow and
    column whichColumn."
   
   super at: (self indexAtRow: whichRow
                     atColumn: whichColumn)
        put: newValue
   
   "exampleArray atRow: 6 atColumn: 4 put: exampleValue"


Private
-------

indexAtRow: whichRow atColumn: whichColumn
   "The internal index at which I store my element at row
    whichRow and column whichColumn.
"
   
   ^((whichRow - 1) * myColumns) + whichColumn
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜