Skip to Content
Author's profile photo Former Member

ABAP classes for array and matrix of integer

I’m accustomed to use array and matrix, so I implemented a couple of class in ABAP that provide them.

class ZMLA_ARRAY_I definition
   public
   create public .
 public section.
 *"* public components of class ZMLA_ARRAY_I
 *"* do not include other source files here!!!
   methods CONSTRUCTOR
     importing
       value(SIZE) type I .
   methods GET
     importing
       value(IX) type I
     returning
       value(RET) type I .
   methods SIZE
     returning
       value(RET) type I .
   methods SET
     importing
       value(IX) type I
       value(VAL) type I default 0 .
   methods SET_VALUES
     importing
       value(START) type I
       value(VALS) type STRING optional .
   protected section.
 private section.
 *"* private components of class ZMLA_ARRAY_I
 *"* do not include other source files here!!!
   data ARRAY type STANDARD TABLE OF i .
 ENDCLASS.
 CLASS ZMLA_ARRAY_I IMPLEMENTATION.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_ARRAY_I->CONSTRUCTOR
 * +-------------------------------------------------------------------------------------------------+
 * | [--->] SIZE                           TYPE        I
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method CONSTRUCTOR.
     DO SIZE TIMES.
         append 0 to array.
     ENDDO.
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_ARRAY_I->GET
 * +-------------------------------------------------------------------------------------------------+
 * | [--->] IX                             TYPE        I
 * | [<-()] RET                            TYPE        I
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method GET.
     read table array index ix + 1 into ret.
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_ARRAY_I->SET
 * +-------------------------------------------------------------------------------------------------+
 * | [--->] IX                             TYPE        I
 * | [--->] VAL                            TYPE        I (default =0)
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method SET.
     modify array from val index ix + 1.
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_ARRAY_I->SET_VALUES
 * +-------------------------------------------------------------------------------------------------+
 * | [--->] START                          TYPE        I
 * | [--->] VALS                           TYPE        STRING(optional)
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method SET_VALUES.
     DATA: stab TYPE TABLE OF STRING,
           s    TYPE          STRING,
           val  TYPE          i,
           ix   TYPE          i.
     SPLIT VALS AT ' ' INTO TABLE stab.
     ix = start.
     LOOP AT stab INTO s.
         val = s.
         me->set( IX  = ix
                  VAL = VAL ).
         ix = ix + 1.
     ENDLOOP.
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_ARRAY_I->SIZE
 * +-------------------------------------------------------------------------------------------------+
 * | [<-()] RET                            TYPE        I
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method SIZE.
     ret = lines( array ).
 endmethod.
 ENDCLASS.

class ZMLA_MATRIX_I definition
   public
   create public .
 public section.
 *"* public components of class ZMLA_MATRIX_I
 *"* do not include other source files here!!!
   methods CONSTRUCTOR
     importing
       value(XSIZE) type I
       value(YSIZE) type I .
   methods GET
     importing
       value(X) type I
       value(Y) type I
     returning
       value(RET) type I .
   methods GET_XSIZE
     returning
       value(RET) type I .
   methods GET_YSIZE
     returning
       value(RET) type I .
   methods SET
     importing
       value(X) type I
       value(Y) type I
       value(VAL) type I default 0 .
   methods SET_ROW
     importing
       value(ROW) type I
       value(VALS) type STRING optional .
   protected section.
 private section.
 *"* private components of class ZMLA_MATRIX_I
 *"* do not include other source files here!!!
   data XSIZE type I .
   data YSIZE type I .
   data ARRAY type ref to ZMLA_ARRAY_I .
 ENDCLASS.
 CLASS ZMLA_MATRIX_I IMPLEMENTATION.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_MATRIX_I->CONSTRUCTOR
 * +-------------------------------------------------------------------------------------------------+
 * | [--->] XSIZE                          TYPE        I
 * | [--->] YSIZE                          TYPE        I
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method CONSTRUCTOR.
   me->xsize = xsize.
   me->ysize = ysize.
   CREATE OBJECT me->array EXPORTING SIZE = xsize * ysize.
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_MATRIX_I->GET
 * +-------------------------------------------------------------------------------------------------+
 * | [--->] X                              TYPE        I
 * | [--->] Y                              TYPE        I
 * | [<-()] RET                            TYPE        I
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method GET.
     ret = me->array->get( IX = me->xsize * X + Y ).
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_MATRIX_I->GET_XSIZE
 * +-------------------------------------------------------------------------------------------------+
 * | [<-()] RET                            TYPE        I
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method GET_XSIZE.
     ret = me->xsize.
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_MATRIX_I->GET_YSIZE
 * +-------------------------------------------------------------------------------------------------+
 * | [<-()] RET                            TYPE        I
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method GET_YSIZE.
     ret = me->ysize.
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_MATRIX_I->SET
 * +-------------------------------------------------------------------------------------------------+
 * | [--->] X                              TYPE        I
 * | [--->] Y                              TYPE        I
 * | [--->] VAL                            TYPE        I (default =0)
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method SET.
     me->array->set( IX  = me->xsize * X + Y
                     VAL = VAL ).
 endmethod.
 * <SIGNATURE>---------------------------------------------------------------------------------------+
 * | Instance Public Method ZMLA_MATRIX_I->SET_ROW
 * +-------------------------------------------------------------------------------------------------+
 * | [--->] ROW                            TYPE        I
 * | [--->] VALS                           TYPE        STRING(optional)
 * +--------------------------------------------------------------------------------------</SIGNATURE>
 method SET_ROW.
     me->ARRAY->SET_VALUES( START = me->xsize * ROW
                            VALS  = VALS ).  "MLA: check che il numero di elementi sia consistente.
 endmethod.
 ENDCLASS.

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.