如何在Smalltalk中管理2d阵列?

如何在Smalltalk中管理2d阵列?,smalltalk,multidimensional-array,visualworks,Smalltalk,Multidimensional Array,Visualworks,我有一个点列表,必须进行侵蚀/扩张操作。我需要一种2d数组,但在VisualWorks中找不到该怎么做(我知道Squeak中有一个Array2d类,但我必须使用VW)。使用一种通用方法:数组数组: (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;

我有一个点列表,必须进行侵蚀/扩张操作。我需要一种2d数组,但在VisualWorks中找不到该怎么做(我知道Squeak中有一个Array2d类,但我必须使用VW)。

使用一种通用方法:数组数组:

(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; ...);
    ...

如果您希望操作更高效,请学习VisualWorks图像类、协议“图像处理”和“位处理”。基于原语构建您自己的侵蚀/扩张操作

许多Smalltalk实现都会有某种矩阵类,有时会进行优化,这些矩阵类会有一些方法,如#rowAt:columnAt:(或为简洁起见#at:at:)


在GNU Smalltalk中,这是DHB数值方法包。现在它还没有优化。

这里是处理Squeak中二维数组的另一种方法(我使用的是4.2版)


等等,等等。好吧,你只能用这种方式做二维阵列,而且它们必须是一个方阵。这对于我和儿子正在制作数独游戏ymmv的项目来说效果很好。干杯

这是我的第一个想法,但我觉得这很难看,不知道是否有更好的办法。但是谢谢你的回答,它告诉我这是可以接受的方式。这是:新的矩阵不能在吱吱声中工作,请确保你给出一个工作的建议。除此之外,在“新的”修复之前,比类名更重要的是,它也不起作用。
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
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