
      *>----------------------------------------------------------------
      *> An abstract class which contains the common code for
      *> OrderedCollections and SortedCollections.
      *>
      *> Copyright (C) 1991-1999 Micro Focus International Ltd.
      *>
      *>----------------------------------------------------------------
      $set localcount(150) linkcount(150) mfoo
       class-id. dynamicArrayedCollection is abstract data is protected
                     inherits from sequencedcollection with data.


        object section.
        class-control.
           sequencedcollection is class "sequence"
           dynamicArrayedCollection is class "dynarray"
           callback is class "callback".

       class-object.
       object-storage section.
       end class-object.


       Object.

       object-storage section.

      *>>---------------------------------------------------------------
      *>> the first index of the ordered collection
      *>>---------------------------------------------------------------

       01  firstindex              pic x(4) comp-5 value 0.

      *>>---------------------------------------------------------------
      *>> the last index of the ordered collection
      *>>---------------------------------------------------------------
       01  lastindex               pic x(4) comp-5 value 0.
      *>----------------------------------------------------------------
      *> Return a new collection of the same species and size as this
      *> collection, containing fresh copies of the elements.
      *>----------------------------------------------------------------
       method-id. "deepcopy".
       local-storage section.
       01 anobject         usage object reference.
       01 i                pic x(4) comp-5.
       01 lsSize           pic x(4) comp-5.

       linkage section.
       01 anewcollection       usage object reference.

       procedure division returning anewcollection.
           if collectiontemplate = null
               invoke self "copyEmptySameClass" returning anewcollection
               invoke self "size" returning lsSize
               perform varying i from 1 by 1 until i > lsSize
                   invoke self "at" using i returning anobject
                   invoke anobject "copy" returning anobject
                   invoke anewcollection "add" using anobject
               end-perform
           else
               invoke self "copy" returning anewcollection
           end-if
           exit method.
       end method "deepcopy".
      *>---------------------------------------------------------------
      *> PRIVATE. Method used to initialize instance variables of new
      *> OrderedCollections.
      *>---------------------------------------------------------------
       method-id. "initialize".
       linkage section.
       01 lnkTemplate          usage object reference.
       01 lnkSize              pic s9(9) comp-5.
       01 lnkStride             pic s9(9) comp-5.

       procedure division using lnkSize lnkTemplate lnkStride.
           invoke super "initialize" using lnkSize lnkTemplate lnkStride
           move 1 to firstindex
           move zero to lastindex
           exit method.
       end method "initialize".
      *>----------------------------------------------------------------
      *> Return the number of elements within this Collection.
      *>----------------------------------------------------------------
       method-id. "size".
       linkage section.
       01 lnksize              pic s9(9) comp-5.

       procedure division returning lnksize.
           compute lnksize = lastindex - firstindex + 1
           exit method.
       end method "size".
      *>----------------------------------------------------------------
      *> Add anElement to this Collection
      *>----------------------------------------------------------------
       method-id. "add".
       local-storage section.
       01 realIndex pic x(4) comp-5.

       linkage section.
       01 anelement             COLLECTABLE.
       01 instanceData          pic x.
       procedure division using anelement.
           if lastindex = numberelements
               invoke self "makeRoomAtEnd"
           end-if
           add 1 to lastindex

           subtract 1 from lastIndex giving realIndex
           multiply collectionStride by realIndex
           add 1 to realIndex

       *> compute realindex = (lastIndex - 1) * collectionstride + 1

           set address of instancedata to collptr
      $if GC defined
           if collectionTemplate = null
             call "oopsassignobjectreference" using
                by reference instancedata(realindex:objectreferencesize)
                by value anelement(1:objectreferencesize)
           else
             if Storingobjects
                invoke anelement(1:collectionStride)
                         as collectionTemplate "getValue"
                    returning instancedata(realindex:collectionstride)
             else
                move anelement(1:collectionStride)
                  to instancedata(realindex:collectionstride)
             end-if
           end-if
      $else
           move anelement(1:collectionStride)
                  to instancedata(realindex:collectionstride)
      $end  

           exit method.
       end method "add".
      *>----------------------------------------------------------------
      *> Append the contents of aCollection to this Collection.
      *>----------------------------------------------------------------
       method-id. "addAll".
       local-storage section.
       01 anewmessage      usage object reference.
       01 bool             pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.
       01 messageselector  pic x(80).

       linkage section.
       01 acollection          usage object reference.

       procedure division using acollection.
           invoke self "isCompatible" using acollection
                                  returning bool
           if istrue
               move "add" to messageselector
               invoke callback "new" using self messageselector
                                 returning anewmessage
               invoke acollection "do" using anewmessage
               invoke anewmessage "finalize" returning anewMessage
           else
               invoke self "compatibiltyError" returning aNewMessage
           end-if
           exit method.
       end method "addAll".
      *>----------------------------------------------------------------
      *> PRIVATE. Method which will move the contents of this
      *> OrderedCollection around inorder to make room for elements to
      *> be added at the start. If this collection is too small then a
      *> grow message is issued
      *>----------------------------------------------------------------
       method-id. "makeRoomAtStart".
       local-storage section.
       01 k                pic s9(9) comp-5.
       01 i                pic s9(9) comp-5.
       01 startpt          pic s9(9) comp-5.
       01 endpt            pic s9(9) comp-5.
       01 newdelta         pic s9(9) comp-5.
       01 delta            pic s9(9) comp-5.
       01 j                pic s9(9) comp-5.
       linkage section.
       01 instancedata     pic x.

       procedure division.
           subtract lastindex from numberelements giving delta
           if delta < 0
               subtract 1 from delta
           else
               add 1 to delta
           end-if
           divide 2 into delta
           if delta = zero
               invoke self "grow"
               if firstindex = 1
                   invoke self "makeRoomAtStart"
               end-if
               exit method
           end-if
           compute startpt  = (firstindex - 1) * collectionstride + 1
           move lastindex to endpt
           multiply collectionstride by endpt
           move delta to newdelta
           multiply collectionstride by newdelta
           move 1 to k
           set address of instancedata to collptr
           perform varying i from endpt by -1 until i < startpt
               move i to j
               add newdelta to j
               move instancedata(i : k) to instancedata(j : k)
           end-perform
           compute endpt = (firstindex + delta - 1) * collectionstride
           move low-values to instancedata(1:endpt)
           add delta to firstindex, lastindex
           exit method.
       end method "makeRoomAtStart".
      *>----------------------------------------------------------------
      *> PRIVATE. Method which will move the contents of this
      *> OrderedCollection around inorder to make room for elements to
      *> be added at the end. If this collection is too small then a
      *> grow message is issued
      *>----------------------------------------------------------------
       method-id. "makeRoomAtEnd".
       local-storage section.
       01 newpt            pic s9(9) comp-5.
       01 delta            pic s9(9) comp-5.
       01 startpt          pic s9(9) comp-5.
       01 lsNumBytesMoved  pic s9(9) comp-5.

       linkage section.
       01 instancedata     pic x.

       procedure division.
           subtract firstindex from 1 giving delta
           if delta < 0
               subtract 1 from delta
           else
               add 1 to delta
           end-if
           divide 2 into delta
           if delta = zero
               invoke self "grow"
           else
               compute startpt = (firstindex - 1) * collectionstride + 1
               compute newpt = (firstindex + delta - 1)
                              * collectionstride + 1

               set address of instancedata to collptr

               compute lsNumBytesMoved =
                       storagerequirements - startpt + 1

               move instancedata(startpt : lsNumBytesMoved)
                     to instancedata(newpt : lsNumBytesMoved)

         *>>  compute startpt = (lastindex + delta) * collectionstride + 1
         *>>  move low-values to instancedata(startpt:
         *>>       storagerequirements + 1 - startpt)

               add delta to lastindex, firstindex
           end-if
           exit method.
       end method "makeRoomAtEnd".
      *>----------------------------------------------------------------
      *> PRIVATE. Insert the element anElement before the index position
      *> anIndex. Unlike addBeforeIndex this is used by sortedcollections
      *> when inserting items privately
      *>----------------------------------------------------------------

       method-id. "insertBeforeIndex".
       local-storage section.
       01 index1           pic s9(9) comp-5.
       01 thissize         pic s9(9) comp-5.
       01 spotindex        pic s9(9) comp-5.
       01 delta            pic s9(9) comp-5.
       01 ri1              pic s9(9) comp-5.
       01 ri2              pic s9(9) comp-5.
       01 spot             pic s9(9) comp-5.

       linkage section.
       01 anelement        COLLECTABLE.
       01 anindex          pic s9(9) comp-5.
       01 instancedata     pic x.
       procedure division using anelement anindex.
           invoke self "size" returning thissize
           add 1 to thissize
           if anindex < 1 or anindex > thissize
               invoke self "errorInBounds"
           else
               compute spot = anindex + firstindex - 1
               if ((spot + spot) < (firstindex + lastindex)) or
                  (lastindex = numberelements)
             *>>> insert by moving previous elements down
                   subtract 1 from spot giving spotindex
                   if firstindex = 1
                       subtract firstindex from spotindex giving delta
                       invoke self "makeRoomatStart"
                       add delta to firstindex giving spotindex
                   end-if
                   subtract 1 from firstindex giving firstindex, index1

                   set address of instancedata to collptr
                   perform until index1 = spotindex
                       add 1 to index1
                       compute ri1 = (index1 - 1) * collectionstride + 1
                       move ri1 to ri2
                       subtract collectionstride from ri2
                       move instancedata(ri1:collectionstride) to
                            instancedata(ri2:collectionstride)
                   end-perform
               else
             *>>> insert by moving subsequent elements up
                   set address of instancedata to collptr
                   move spot to spotindex
                   add 1 to lastindex giving lastindex, index1
                   perform until index1 = spotindex
                       subtract 1 from index1
                       compute ri1 = (index1 - 1) * collectionstride + 1
                       move ri1 to ri2
                       add collectionstride to ri2
                       move instancedata(ri1:collectionstride) to
                            instancedata(ri2:collectionstride)
                   end-perform
               end-if
               compute ri1 = (index1 - 1) * collectionstride + 1
      $if GC defined
           if collectionTemplate = null
              call "oopsassignobjectreference" using
                by reference instancedata(ri1:objectreferencesize)
                by value anelement(1:objectreferencesize)
           else
              if Storingobjects
                invoke anelement(1:collectionStride)
                         as collectionTemplate "getValue"
                    returning instancedata(ri1:collectionstride)
              else

                move anelement(1:collectionStride)
                         to instancedata(ri1:collectionstride)
              end-if

           end-if
      $else
           move anelement(1:collectionStride)
                         to instancedata(ri1:collectionstride)
      $end
           end-if
           exit method.
       end method "insertBeforeIndex".
      *>----------------------------------------------------------------
      *> Return true if anElement is at index position anIndex.
      *> Test is by value.
      *>----------------------------------------------------------------
       method-id. "isat".
       local-storage section.
       01 lsBuffer         pic x(bufferSize).
       01 aptr             pointer.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 lnkbool                 pic x comp-5.
           88 lnkistrue           value 1.
           88 lnkisfalse          value 0.
       01 anelement2           COLLECTABLE.
       01 anelement            COLLECTABLE.

       procedure division using anelement anindex
                          returning lnkbool.
           if collectionStride > bufferSize
               invoke self "allocateElement" returning aptr
               set address of anelement2 to aptr
           else
               move low-values to lsBuffer
               set address of anElement2 to address of lsBuffer
           end-if

           invoke self "at" using anindex returning anElement2
           if collectiontemplate equal to null
               invoke anelement2 as object "equal" using anelement
                                  returning lnkbool
           else
               if anelement2(1:collectionStride) =
                        anelement(1:collectionStride)
                   set lnkistrue to true
               else
                   set lnkisfalse to true
               end-if
           end-if
      $if GC defined
           if collectionTemplate = null
                call "oopsrelease" using
                        by value 1
                        by value anElement2(1:objectreferencesize)
           else
                if storingObjects
                    invoke anElement2(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"
                end-if

           end-if
      $end
           if collectionStride > bufferSize
               invoke self "dealloc" using aptr
           end-if
           exit method.
       end method "isat".
      *>----------------------------------------------------------------
      *> Return true if anElement is at index position anIndex.
      *> Test is by identity.
      *>----------------------------------------------------------------

       method-id. "isatIdentity".
       local-storage section.
       01 realIndex            pic x(4) comp-5.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 lnkbool                 pic x comp-5.
           88 lnkistrue           value 1.
           88 lnkisfalse          value 0.

       01 anelement            COLLECTABLE.
       01 instanceData         pic x.

       procedure division using anelement anindex
                          returning lnkbool.
           add anIndex to firstIndex giving realIndex
           subtract 1 from realIndex
           if anindex < 1 or realindex > lastindex
               move zero to lnkBool
           else
               subtract 1 from realIndex
               multiply collectionStride by realIndex
               add 1 to realIndex
               set address of instancedata to collptr
               if instancedata(realindex : collectionstride)
                        = anElement(1:collectionStride)
                   move 1 to lnkBool
               else
                   move 0 to lnkBool
               end-if
           end-if
           exit method.
       end method "isatIdentity".

      *>----------------------------------------------------------------
      *> Return the element at the index position anIndex. Raise an
      *> an exception if anIndex is out of bounds.
      *>----------------------------------------------------------------
       method-id. "at".
       local-storage section.
       01 realindex        pic s9(9) comp-5.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 anelement2           COLLECTABLE.
       01 instancedata         pic x.

       procedure division using anindex returning anelement2.
        *>  compute realindex = anindex + firstindex - 1
           add anIndex to firstIndex giving realIndex
           subtract 1 from realIndex
           if anindex < 1 or realindex > lastindex
               invoke self "errorInBounds" returning anelement2
               exit method
           else
         *>      compute realindex = (realindex - 1) * collectionstride
         *>                            + 1 
               subtract 1 from realIndex
               multiply collectionStride by realIndex
               add 1 to realIndex
               set address of instancedata to collptr
      $if GC defined
               if collectionTemplate = null
                   call "oopsassignobjectreference" using
                     by reference anelement2(1:objectreferencesize)
                     by value
                        instancedata(realindex : objectreferencesize)
               else
                   if Storingobjects
                      invoke instancedata(realindex : collectionstride)
                           as collectionTemplate "getValue"
                        returning anelement2(1:collectionStride)
                   
                   else

                       move instancedata(realindex : collectionstride)
                            to anelement2(1:collectionStride)
                   end-if
               end-if
      $else
               move instancedata(realindex : collectionstride)
                 to anelement2(1:collectionStride)
      $end
               exit method
           end-if.
       end method "at".

      *>----------------------------------------------------------------
      *> Return the element at the index position anIndex. Raise an
      *> an exception if anIndex is out of bounds.
      *>----------------------------------------------------------------
       method-id. "atNoGC".
       local-storage section.
       01 realindex        pic s9(9) comp-5.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 anelement2           COLLECTABLE.
       01 instancedata         pic x.

       procedure division using anindex returning anelement2.
           add anIndex to firstIndex giving realIndex
           subtract 2 from realIndex
           multiply collectionStride by realIndex
           add 1 to realIndex
           set address of instancedata to collptr
           move instancedata(realindex : collectionstride)
                 to anelement2(1:collectionStride)
           exit method
       end method "atNoGC".

      *>---------------------------------------------------------------
      *> Store anElement at position anIndex. Raise an
      *> an exception if anIndex is out of bounds.
      *>---------------------------------------------------------------
       method-id. "atPut".
       local-storage section.
       01 realindex        pic s9(9) comp-5.

       linkage section.
       01 anindex          pic s9(9) comp-5.
       01 anelement        COLLECTABLE.
       01 instanceData     pic x.
       procedure division using anindex anelement.
           compute realindex = anindex + firstindex - 1
           if anindex < 1 or realindex > lastindex
               invoke self "errorinBounds"
               exit method
           end-if
           compute realindex = (realindex - 1) * collectionstride + 1
           set address of instancedata to collptr
      $if GC defined
           if collectionTemplate = null
               call "oopsassignobjectreference" using
                by reference instancedata(realindex:objectreferencesize)
                by value anelement(1:objectreferencesize)
           else
               if Storingobjects
                  invoke anelement(1:collectionStride)
                       as collectionTemplate "getValue"
                    returning instancedata(realindex:collectionstride)         
               else
                  move anelement(1:collectionStride)
                        to instancedata(realindex:collectionstride)
               end-if 
           end-if
      $else
           move anelement(1:collectionStride)
                 to instancedata(realindex:collectionstride)
      $end  
           exit method.
       end method "atPut".
      *>----------------------------------------------------------------
      *> PRIVATE. Returns the element at the real index position anIndex.
      *> If anIndex is out of bounds an exception is raised
      *>----------------------------------------------------------------
       method-id. "privateat".
       local-storage section.
       01 realindex        pic s9(9) comp-5.

       linkage section.
       01 anindex          pic s9(9) comp-5.
       01 anelement2       COLLECTABLE.
       01 instanceData     pic x.

       procedure division using anindex returning anelement2.
           if anindex < 1 or anindex > numberelements
               invoke self "errorInBounds" returning anelement2
           else
               compute realindex = (anindex - 1) * collectionstride + 1
               set address of instancedata to collptr
      $if GC defined
               if collectionTemplate = null
                   call "oopsassignobjectreference" using
                     by reference anelement2(1:objectreferencesize)
                     by value
                        instancedata(realindex : objectreferencesize)
               else
                   if Storingobjects
                      invoke instancedata(realindex : collectionstride)
                          as collectionTemplate "getValue"
                       returning anelement2(1:collectionStride)       
                   else
                       move instancedata(realindex : collectionstride)
                                to anelement2(1:collectionStride)
                   end-if
               end-if
      $else
               move instancedata(realindex : collectionstride) to
                                   anelement2(1:collectionStride)
      $end  
           end-if
           exit method
       end method "privateat".

      *>----------------------------------------------------------------
      *> Return the element which appears after anElement. Raise an
      *> exception if this collection does not contain anElement or
      *> if no element occurs after anElement.
      *>----------------------------------------------------------------
       method-id. "after".
       local-storage section.
       01 exceptionnumber      pic s9(9) comp-5.
       01 k                    pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.

       linkage section.
       01 anelement2           COLLECTABLE.
       01 anelement            COLLECTABLE.

       procedure division using anelement returning anelement2.
           move 1 to i
           invoke self "size" returning j
           invoke self "firstIndexOf" using anelement i j
                                       returning k
           if k = zero
               move 12 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
                                           returning anelement2
           else
               if k = j
                   move 13 to exceptionnumber
                   invoke self "raiseException"
                                       using exceptionnumber
                                   returning anelement2
               else
                   add 1 to k
                   invoke self "at" using k returning anelement2
               end-if
           end-if
           exit method.
       end method "after".
      *>----------------------------------------------------------------
      *> Return the element which appears before anElement. Raise an
      *> exception if this collection does not contain anElement or
      *> if no element occurs before anElement.
      *>----------------------------------------------------------------
       method-id. "before".
       local-storage section.
       01 exceptionnumber      pic s9(9) comp-5.
       01 k                    pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.

       linkage section.
       01 anelement2           COLLECTABLE.
       01 anelement            COLLECTABLE.

       procedure division using anelement returning anelement2.
           move 1 to i
           invoke self "size" returning j
           invoke self "firstIndexOf" using anelement i j
                                       returning k
           if k = zero
               move 12 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
                                             returning anelement2
           else
               if k = 1
                   move 14 to exceptionnumber
                   invoke self "raiseException"
                                       using exceptionnumber
                                   returning anelement2
               else
                   subtract 1 from k
                   invoke self "at" using k returning anelement2
               end-if
           end-if
           exit method.
       end method "before".
      *>----------------------------------------------------------------
      *> Return a new collection of the same species as this
      *> OrderedCollection which contains the elements between the
      *> indices fromIndex and toIndex.
      *>----------------------------------------------------------------
       method-id. "copyFromTo".
       local-storage section.
       01 aptr                 pointer.
       01 i                    pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.

       linkage section.
       01 anelement            COLLECTABLE.
       01 toindex              pic s9(9) comp-5.
       01 fromindex            pic s9(9) comp-5.
       01 anewcollection       usage object reference.

       procedure division using fromindex toindex
                          returning anewcollection.
           move toindex to asize
           subtract fromindex from asize
           add 1 to asize
           invoke self "copyEmptyWithSize" using asize
                                         returning anewcollection
           invoke self "allocateElement" returning aptr
           set address of anelement to aptr
           perform varying i from fromindex by 1 until i > toindex
               invoke self "at" using i returning anElement
               invoke anewcollection "add" using anelement
           end-perform
      $if GC defined
           if collectionTemplate = null
                call "oopsrelease" using
                        by value 1
                        by value anElement(1:objectreferencesize)
           else
                if storingObjects
                    invoke anElement(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"
                end-if

           end-if
      $end

           invoke self "dealloc" using aptr
           exit method.
       end method "copyFromTo".
      *>----------------------------------------------------------------
      *> Replace all the occurrences of aCollection within this
      *> OrderedCollection with aCollection2 returning aNewCollection
      *>----------------------------------------------------------------
       method-id. "copyReplaceAll".
       local-storage section.
       01 i                    pic s9(9) comp-5.
       01 m                    pic s9(9) comp-5.
       01 l                    pic s9(9) comp-5.
       01 k                    pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.
       01 delta            pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.

       linkage section.
       01 acollection2         usage object reference.
       01 anewcollection       usage object reference.
       01 acollection          usage object reference.

       procedure division using acollection acollection2
                                 returning anewcollection.
           invoke self "size" returning asize
           move 1 to k
           invoke self "indexOfSubCollection" using acollection
                                                         k
                                               returning j
           multiply 2 by asize
           invoke self "copyEmptyWithSize" using asize
                                        returning anewcollection
           divide 2 into asize
           invoke acollection "size" returning delta
           move 1 to k l m
           perform until j = zero
               subtract 1 from j giving l
               invoke anewcollection "addSubCollectionLast"
                                   using self m l
               add delta to j giving m
               invoke anewcollection "addAll" using acollection2
               add 1 to j giving i
               invoke self "indexOfSubCollection" using acollection
                                                             i
                                                   returning j
           end-perform
           invoke anewcollection "addSubCollectionLast" using self m
                                                               asize
           exit method.
       end method "copyReplaceAll".
      *>----------------------------------------------------------------
      *> Return a copy of the receiver with the elements between
      *> fromIndex and toIndex replaced by the contents of aCollection.
      *>----------------------------------------------------------------
       method-id. "copyReplace".
       local-storage section.
       01 i                   pic s9(9) comp-5.
       01 j                   pic s9(9) comp-5.
       01 asize               pic s9(9) comp-5.
       01 newsize             pic s9(9) comp-5.
       01 thissize            pic s9(9) comp-5.
       01 bool                pic x comp-5.
          88 isfalse          value 0.
          88 istrue           value 1.

       linkage section.
       01 toindex              pic s9(9) comp-5.
       01 fromindex            pic s9(9) comp-5.
       01 anewcollection       usage object reference.
       01 acollection          usage object reference.

       procedure division using fromindex toindex
                                          acollection
                                returning anewcollection.
           invoke self "isCompatible" using acollection
                                       returning bool
           if istrue
               invoke acollection "size" returning asize
               invoke self "size" returning thissize
               compute newsize = thissize + asize -
                                   (toindex - fromindex + 1)
               invoke self "copyEmptyWithSize" using newsize
                               returning anewcollection
               move 1 to i
               subtract 1 from fromindex giving j
               invoke anewcollection "addSubCollectionLast"
                                 using self i j
               invoke anewcollection "addAll" using acollection
               add 1 to toindex giving j
               invoke anewcollection "addSubCollectionLast"
                                 using  self j thissize
           else
               invoke self "compatibiltyError" returning anewcollection
           end-if
           exit method.
       end method "copyReplace".
      *>----------------------------------------------------------------
      *> Return a new collection which is a copy of this ordered
      *> collection with the additional element anElement.
      *>----------------------------------------------------------------
       method-id. "copyWith".
       linkage section.
       01 anewcollection       usage object reference.
       01 anelement            COLLECTABLE.

       procedure division using anelement returning anewcollection.
           invoke self "copy" returning anewcollection
           invoke anewcollection "add" using anelement
           exit method.
       end method "copyWith".
      *>----------------------------------------------------------------
      *> Returns a new collection which is a copy of this sequenced
      *> collection without the elements that are equal to anElement.
      *>----------------------------------------------------------------
       method-id. "copyWithOut".
       local-storage section.
       01 aptr             pointer.
       01 i                    pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.
       01 bool                 pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.

       linkage section.
       01 anelement            COLLECTABLE.
       01 anewcollection       usage object reference.
       01 anelement2           COLLECTABLE.

       procedure division using anelement
                                 returning anewcollection.
           invoke self "copyEmpty" returning anewcollection
           invoke self "size" returning asize
           invoke self "allocateElement" returning aptr
           set address of anelement2 to aptr
           perform varying i from 1 by 1 until i > asize
               invoke self "isAt" using  anelement i returning bool
               if isfalse
                   invoke self "at" using i returning anElement2
                   invoke anewcollection "add" using anelement2
               end-if
           end-perform
      $if GC defined
           if collectionTemplate = null
                call "oopsrelease" using
                        by value 1
                        by value anElement2(1:objectreferencesize)
           else
                if storingObjects
                    invoke anElement2(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"
                end-if

           end-if
      $end

           invoke self "dealloc" using aptr
           exit method.
       end method "copyWithOut".
      *>----------------------------------------------------------------
      *> Remove the first occurrrence of anElement within this
      *> OrderedCollection. Raise an exception if this collection does
      *> not contain anElement.
      *>----------------------------------------------------------------
       method-id. "remove".
       local-storage section.
       01 exceptionnumber      pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.

       linkage section.
       01 anelement            COLLECTABLE.

       procedure division using anelement.
           move 1 to j
           invoke self "size" returning thissize
           invoke self "firstIndexOf" using anelement j thissize
                                       returning i
           if i = 0
               move 12 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           else
               invoke self "removeAtIndex" using i returning anElement

           end-if
           exit method.
       end method "remove".
      *>----------------------------------------------------------------
      *> Remove the first aNumber of elements. Raise an exception if
      *> there are fewer than aNumber of elements.
      *>----------------------------------------------------------------
       method-id. "removeFirst".
       local-storage section.
       01 exceptionnumber      pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 anObject             usage object reference.

       linkage section.
       01 anumber              pic s9(9) comp-5.

       procedure division using anumber.
           invoke self "size" returning i
           if i < anumber
               move 33 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           else
               add aNumber to firstIndex
           end-if
           exit method.
       end method "removeFirst".
      *>----------------------------------------------------------------
      *> Remove and destroy the first aNumber of elements. Raise an
      *> exception if there are fewer than aNumber of elements.
      *>----------------------------------------------------------------
       method-id. "removeFirstandFinalize".
       local-storage section.
       01 exceptionnumber      pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 anobject             usage object reference.

       linkage section.
       01 anumber              pic s9(9) comp-5.

       procedure division using anumber.
           invoke self "size" returning i
           if i < anumber
               move 33 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           else
               move 1 to i
               perform anumber times
                   if collectiontemplate equal null
                       invoke self "at" using i returning anobject
                       invoke anobject "finalize" returning anobject
                   end-if
                   add 1 to firstindex
               end-perform
           end-if
           exit method.
       end method "removeFirstandFinalize".
      *>----------------------------------------------------------------
      *> Remove and destroy the last aNumber of elements. Raise an
      *> exception if there are fewer than aNumber of elements.
      *>----------------------------------------------------------------
       method-id. "removeLastandFinalize".
       local-storage section.
       01 exceptionnumber      pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 anobject             usage object reference.

       linkage section.
       01 anumber              pic s9(9) comp-5.

       procedure division using anumber.
           invoke self "size" returning i
           if i < anumber
               move 33 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           else
               perform anumber times
                   if collectiontemplate equal null
                       invoke self "at" using i returning anobject
                       invoke anobject "finalize" returning anobject
                   end-if
                   subtract 1 from lastindex
                   subtract 1 from i
               end-perform
           end-if
           exit method.
       end method "removeLastandFinalize".
      *>----------------------------------------------------------------
      *> Remove the last aNumber of elements. Raise an exception if
      *> there are fewer than aNumber of elements.
      *>----------------------------------------------------------------
       method-id. "removeLast".
       local-storage section.
       01 exceptionnumber      pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 anObject             usage object reference.

       linkage section.
       01 anumber              pic s9(9) comp-5.

       procedure division using anumber.
           invoke self "size" returning i
           if i < anumber
               move 33 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           else
               subtract anumber from lastindex
           end-if
           exit method.
       end method "removeLast".
      *>----------------------------------------------------------------
      *> Remove the element at index position anIndex.
      *> The element that has just been removed is returned in anElement
      *>----------------------------------------------------------------
       method-id. "removeAtIndex".
       local-storage section.
       01 realindex        pic s9(9) comp-5.
       01 numbytestomove   pic s9(9) comp-5.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 anElement            COLLECTABLE.
       01 instanceData         pic x.
       procedure division using anindex returning anElement.
           add anIndex to firstIndex giving realIndex
           subtract 1 from realIndex
           if anindex < 1 or realindex > lastindex
               invoke self "errorInBounds"
           else

                *>>> Because this method removes the item
                *>>> and returns it there is no need to call
                *>>> any GC calls as the reference count has
                *>>> essentially not changed during execution
                *>>> of this method.

               subtract realIndex from lastIndex giving numBytesToMove
               multiply collectionStride by numBytesToMove

               subtract 1 from realIndex
               multiply collectionStride by realIndex
               add 1 to realIndex
               set address of instancedata to collptr

               move instancedata(realIndex:collectionStride) to
                        anElement(1:collectionStride)

               move instancedata(realindex + collectionstride:
                    numbytestomove)
                 to instancedata(realindex : numbytestomove)
               subtract 1 from lastindex

               multiply collectionStride by lastIndex giving realIndex
               add 1 to realIndex
               move low-values to
                            instancedata(realindex:collectionstride)
           end-if.
           exit method.
       end method "removeAtIndex".
      *>----------------------------------------------------------------
      *> Remove all the elements that cause the message aMessage to
      *> evaluate to true when the element is passed as a parameter to
      *> the CallBack.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter and returns a pic x comp-5.
      *> For more information see the topic Collection Iterator Methods
      *> in the on-line documentation.
      *>----------------------------------------------------------------
       method-id. "removeAllSuchThat".
       local-storage section.
       01 bool                 pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.
       01 aptr                 pointer.
       01 i                    pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.

       linkage section.
       01 anelement            COLLECTABLE.
       01 amessage             usage object reference.

       procedure division using amessage.
           invoke self "size" returning asize
           move 1 to i
           invoke self "allocateElement" returning aptr
           set address of anelement to aptr

           perform until i > asize
               invoke self "at" using i returning anelement
               invoke amessage "invoke" using anelement
                                        returning bool
               if istrue
                   invoke self "removeAtIndex" using i
                           returning anElement
               else
                   add 1 to i
               end-if
               invoke self "size" returning asize
           end-perform
      $if GC defined
           if collectionTemplate = null
                call "oopsrelease" using
                        by value 1
                        by value anElement(1:objectreferencesize)
           else
                if storingObjects
                    invoke anElement(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"
                end-if

           end-if
      $end

           invoke self "dealloc" using aptr
           exit method.
       end method "removeAllSuchThat".
      *>----------------------------------------------------------------
      *> Return a new collection of the same species as this collection.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter and will return an element of
      *> the same type as the elements in this collection.
      *> "collect" passes each element of the collection to the CallBack.
      *> Each element returned by the CallBack is placed in the new
      *> collection.
      *> For more information see the topic Collection Iterator Methods
      *> in the on-line documentation.
      *>----------------------------------------------------------------
       method-id. "collect".
       local-storage section.
       01 aptr                 pointer.
       01 aptr2                pointer.
       01 i                    pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.
       01 aCopy                usage object reference.

       linkage section.
       01 anelement            COLLECTABLE.
       01 amessage             usage object reference.
       01 anewcollection       usage object reference.
       01 anelement2           COLLECTABLE.

       procedure division using amessage returning anewcollection.
           invoke self "copyEmpty" returning anewcollection
           invoke self "size" returning thissize
           invoke self "allocateElement" returning aptr
           set address of anelement to aptr
           invoke self "allocateElement" returning aptr2
           set address of anelement2 to aptr2
           set oonoquit to true
           invoke self "copy" returning aCopy
           perform varying i from 1 by 1 until i > thissize or ooquit
               invoke aCopy "at" using i returning anelement
               invoke amessage "invoke" using anElement
                          returning anElement2
               invoke anewcollection "add" using anelement2
           end-perform
           invoke aCopy "finalize" returning aCopy
           set oonoquit to true
      $if GC defined
           if collectionTemplate = null
                call "oopsrelease" using
                        by value 2
                        by value anElement(1:objectreferencesize)
                        by value anElement2(1:objectreferencesize)
           else
                if storingObjects
                    invoke anElement(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"

                    invoke anElement2(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"
                end-if

           end-if
      $end

           invoke self "dealloc" using aptr
           invoke self "dealloc" using aptr2
           exit method.
       end method "collect".
      *>----------------------------------------------------------------
      *> Return a new collection which is of the same species as this
      *> sequenced collection with the same contents, but in reverse
      *> order.
      *>----------------------------------------------------------------
       method-id. "reverse".
       local-storage section.
       01 aptr                 pointer.
       01 i                    pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.

       linkage section.
       01 anelement            COLLECTABLE.
       01 anewcollection       usage object reference.

       procedure division returning anewcollection.
           invoke self "copyEmpty" returning anewcollection
           invoke self "size" returning thissize
           invoke self "allocateElement" returning aPtr
           set address of anelement to aptr
           perform varying i from thissize by -1 until i = zero
               invoke self "at" using i returning anElement
               invoke anewcollection "add" using anelement
           end-perform
      $if GC defined
           if collectionTemplate = null
                call "oopsrelease" using
                        by value 1
                        by value anElement(1:objectreferencesize)
           else
                if storingObjects
                    invoke anElement(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"
                end-if

           end-if
      $end

           invoke self "dealloc" using aptr
           exit method.
       end method "reverse".
      *>----------------------------------------------------------------
      *> Return a new collection of the same species as this collection.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter and will return a pic x comp-5.
      *> "select" passes each element of the collection to the CallBack.
      *> If the CallBack returns True (that is 1), then the element is
      *> placed in the new collection.
      *> For more information see the topic Collection Iterator Methods
      *> in the on-line documentation.
      *>----------------------------------------------------------------
       method-id. "select".
       local-storage section.
       01 aptr                 pointer.
       01 i                    pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.
       01 aCopy                usage object reference.
       01 bool                 pic x comp-5.
          88 istrue            value 1.
          88 isfalse           value 0.

       linkage section.
       01 anelement            COLLECTABLE.
       01 amessage             usage object reference.
       01 anewcollection       usage object reference.

       procedure division using amessage
                             returning anewcollection.
           invoke self "copyEmpty" returning anewcollection
           invoke self "size" returning thissize
           invoke self "allocateElement" returning aptr
           set address of anelement to aptr
           set oonoquit to true
           invoke self "copy" returning aCopy
           perform varying i from 1 by 1 until i > thissize or ooquit
               invoke self "at" using i returning anelement
               invoke amessage "invoke" using anelement
                                   returning bool
               if istrue
                   invoke anewcollection "add" using anelement
               end-if
           end-perform
      $if GC defined
           if collectionTemplate = null
                call "oopsrelease" using
                        by value 1
                        by value anElement(1:objectreferencesize)
           else
                if storingObjects
                    invoke anElement(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"
                end-if

           end-if
      $end

           invoke aCopy "finalize" returning aCopy
           set oonoquit to true
           invoke self "dealloc" using aptr
           exit method.
       end method "select".
      *>----------------------------------------------------------------
      *> Return a new collection of the same species as this collection.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter and will return a pic x comp-5.
      *> "reject" passes each element of the collection to the CallBack.
      *> If the CallBack returns False (that is 0), then the element is
      *> placed in the new collection.
      *> For more information see the topic Collection Iterator Methods
      *> in the on-line documentation.
      *>----------------------------------------------------------------
       method-id. "reject".
       local-storage section.
       01 bool                 pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.
       01 aptr             pointer.
       01 i                pic s9(9) comp-5.
       01 thissize         pic s9(9) comp-5.
       01 aCopy            usage object reference.

       linkage section.
       01 anelement            COLLECTABLE.
       01 amessage             usage object reference.
       01 anewcollection       usage object reference.

       procedure division using amessage
                             returning anewcollection.
           invoke self "copyEmpty" returning anewcollection
           invoke self "size" returning thissize
           set oonoquit to true
           invoke self "allocateElement" returning aPtr
           set address of anelement to aptr
           invoke self "copy" returning aCopy
           perform varying i from 1 by 1 until i > thissize or ooquit
               invoke aCopy "at" using i returning anelement
               invoke amessage "invoke" using anelement
                                   returning bool
               if isfalse
                   invoke anewcollection "add" using anelement
               end-if
           end-perform
      $if GC defined
           if collectionTemplate = null
                call "oopsrelease" using
                        by value 1
                        by value anElement(1:objectreferencesize)
           else
                if storingObjects
                    invoke anElement(1:collectionStride) AS
                                 collectionTemplate "decreaseReferences"
                end-if

           end-if
      $end

           invoke aCopy "finalize" returning aCopy
           set oonoquit to true
           invoke self "dealloc" using aptr
           exit method.
       end method "reject".
      *>----------------------------------------------------------------
      *> Return the number of elements this collection is capable of
      *> holding.
      *>----------------------------------------------------------------

       method-id. "capacity".
       linkage section.
       01 lnksize              pic s9(9) comp-5.

       procedure division returning lnksize.
           move numberelements to lnksize.
           exit method.
       end method "capacity".
      *>---------------------------------------------------------------
      *> Clears out the contents of the collection, leaving the collection
      *> of the same capacity, but containing no elements
      *>---------------------------------------------------------------
       method-id. "clear".

       procedure division.
           move 1 to firstindex
           move zero to lastindex
           exit method.
       end method "clear".

       end object.
       end class dynamicArrayedCollection.
