Trade-off, option, choice.



I've got a two dimensional grid of characters.

At one stage, I need to extract strings from the grid,
either on a row by row basis, or on a column by column
basis.

I started writing the accessors, but I'm worried about
the amount of duplicate code. To wit:

(defun get-grid-row (grid row)
"Returns the given row of the grid as a string"
(let* ((length (grid-cols grid))
(s (make-array length :fill-pointer 0 :adjustable t :element-type 'character)))
(dotimes (col length s)
(vector-push-extend (grid@ grid col row) s))))


(defun get-grid-col (grid col)
"Returns the given column of the grid as a string"
(let* ((length (grid-rows grid))
(s (make-array length :fill-pointer 0 :adjustable t :element-type 'character)))
(dotimes (row length s)
(vector-push-extend (grid@ grid col row) s))))


This piece of code is exactly the same one, save for the swaping
of row & cols.

And that's a simple example. I've got a much more involved piece of code,
which I already wrote, debugged and tested for the row case, and I really
don't feel like copy-paste-patch a second instance.

The choices I see are:

1. have the coordinate include its orientation. Like > 0 for cols
  and < 0 for rows.
  -> this wouldn't easily scale to 3 dimensions(but I don't care)
  -> this pushes an explicit test (against 0) at the lowest level

2. use an array of two elements, and transform all references
  of the form (foo col), (foo row) into (foo index direction)
  -> this would increase the argument list by one everywhere.
  -> replacing a pair of (row, col) slots with an array of two
     elements is going to be more expensive both in time and
     space, but not emphatically so,
  -> I can defer chosing between the :by-column and :by-row
     directions until the highest level.

3. use copy-paste-patch as I've started doing. But I'm not pleased
  at all. this smells.

Anyone see any other options I missed?

Many Thanks
--
JFB

.



Relevant Pages

  • Re: How do I populate a DataGridView with arrays containing strings? (I get convertion error C2664)
    ... maybe it can't be a 'string' data type. ... grid, but that is probably not how it works. ... Adds a new row to the collection, and populates the cells with the ...
    (microsoft.public.vc.mfc)
  • Re: I want to a event method to be called in the a form.
    ... Even before you have to write your own dervied grid... ... private void MyGrid_selectedIndexChanged ... private string _myArg; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Referencing an object from a string within an array
    ... you could use the first element to reference the columns and the ... For-Next loops to address every cell in the grid of data. ... Almost everybody in this newsgroup is using VB6 or lower. ... > string) by referring to it with a string. ...
    (microsoft.public.vb.syntax)
  • Referencing an object from a string within an array
    ... string) by referring to it with a string. ... I have a 9x9 grid that I have created strings to represent the data. ... I create an array for each object within the grid. ... Then I can then create a loop to parse each of the arrays and run the ...
    (microsoft.public.vb.syntax)
  • Re: TStringGrid question
    ... > ability to associate an object with each string in the grid. ... > I interpret this to mean that grid array elements can be associated ... customary paint and fill in the cell. ...
    (alt.comp.lang.borland-delphi)