代码之家  ›  专栏  ›  技术社区  ›  Serty Oan

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

  •  4
  • Serty Oan  · 技术社区  · 15 年前

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

    4 回复  |  直到 15 年前
        1
  •  3
  •   Janko MivÅ¡ek    15 年前

    简单地使用一种通用方法:数组:

    (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
  •  4
  •   Paolo Bonzini    15 年前

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

        3
  •  2
  •   Alan Wostenberg    15 年前

    如果您想使操作更高效,请学习VisualWorks图像类、协议“图像处理”和“位处理”。在原语的基础上建立你自己的侵蚀/扩张操作。

        4
  •  2
  •   Tobias    10 年前

    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.
    

    等等,等等,你只能这样做二维数组,它们必须是一个方阵。我和我儿子正在做一个数独游戏ymmv,这个项目做得很好。干杯!

        5
  •  0
  •   David Oznot    4 年前
    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
    
    推荐文章