How to manage 2d array in Smalltalk?

两盒软妹~` 提交于 2019-12-10 16:11:28

问题


I have a list of point and have to do erosion/dilation operations. I need a kind of 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).


回答1:


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



回答2:


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.




回答3:


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.




回答4:


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!



来源:https://stackoverflow.com/questions/3095907/how-to-manage-2d-array-in-smalltalk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!