#174 Array\\\'s \\\"previous\\\"-method: add multidimensionality support

v3.2.0
closed
David Ashley
Classes (154)
5
2012-08-14
2007-05-29
No

Now that the array class supports a single-dimension object as an index for multidimensional arrays, the method "previous" should support that feature as well.

An implementation to support this feature may look like (tested):

---------------- cut here ---------------
::method previous -- single & multidimensional previous
use strict arg idx

  /* check received index */

if datatype(idx, "Whole") then -- single dimensional array
do
if idx<1 then -- behave as current implementation
raise syntax 93.907 array (1, idx)

 if idx>=self~size then-- at end of array ?
    return .nil

 return idx+1

end

if \idx~isA(.array) then -- not an array object?
raise syntax 93.900 array ("Method argument 1 must be a positive whole number or an integer array representing an existing index.")

if idx~dimension<>1 then -- not a single dimensioned array?
raise syntax 93.939 array (1)

d=self~dimension -- get number of dimensions

idxItems=idx~items -- get # of entries in idx
if idxItems<d then -- too few entries
raise syntax 93.925 array (d)

if idxItems>d then -- too many entries
raise syntax 93.925 array (d)

  /* check received index, if any index is beyond the dimension's size return .nil,
     if any index is smaller than 1, then raise a syntax error
  */

new=idx~allItems -- create a new array object, containing all index values
-- "allItems" makes sure that index entries are consecutively
do i=d to 1 by -1
v=new[i] -- get dimension's index number

 if \datatype(v,"Whole") then   -- indicate we need a whole number
    raise syntax 93.907 array(i, v)

 if v<1 then     -- indicate dimension which needs a positive value
    raise syntax 93.907 array (i, v)

 if v>self~dimension(i) then -- supplied index beyond dimension's size
    return .nil

 v=v-1           -- decrease index value of this dimension by 1
 if v>0 then     -- o.k. within
 do
    new[i]=v
    return new
 end

 if i=1  then    -- moving beyond the current dimension sizes
    return .nil  -- indicate beyond end of array

 new[i]=self~dimension(i) -- set current index to dimension's size

end
return .nil
---------------- cut here ---------------

Discussion

  • Logged In: YES
    user_id=662126
    Originator: YES

    This RFE contains all RFE-request code in one file together with little tests. (Just to make sure that that file is not overseen, if applying the suggested patches.)

     
  • Rick McGuire
    Rick McGuire
    2007-05-29

    Logged In: YES
    user_id=1125291
    Originator: NO

    Committed revision 411.

    Note, this also allows the index to be specified using multiple arguments (x~previous(1,1,1)).

     
  • Rick McGuire
    Rick McGuire
    2007-06-20

    Logged In: YES
    user_id=1125291
    Originator: NO

    There are a whole series of RFEs for this related item. There should probably be a paragraph or two about array indices at the start of the array documentation.

    For methods that accept an array index, the index can be specified either as separate arguments or as a single argument that is an array of the index items. For example,

    say a[1,2,3]

    and

    index = .array~of(1,2,3)
    say a[index]

    will work. For methods that return an index item (for example, NEXT or PREVIOUS), if the array is multidimensional the index will be returned in the array form. Also, the INDEX method for an array supplier will also be returned as an array of index values for a multidimensional array.

    Also, remove the restriction about PREVIOUS only working on single-dimension arrays.

     


Anonymous


Cancel   Add attachments