      *>----------------------------------------------------------------
      *> PRIVATE. SequencedCollection is an abstract class which
      *> inherits from Collection. This class represents the common
      *> behavior for all SequencedCollections.
      *>
      *> Copyright (C) 1991-1999 Micro Focus International Ltd.
      *>
      *>----------------------------------------------------------------
      $set localcount(200) linkcount(150) mfoo
       class-id. sequencedcollection is abstract data is protected
                     inherits from collection with data.


        object section.
        class-control.
           collection is class "collectn"
           sequencedcollection is class "sequence"
           orderedcollection is class "ordrdcll"
           array is class "array"
           cobolcomp5 is class "comp5".


       class-object.

       object-storage section.
      *>---------------------------------------------------------------
      *> Return a new instance of SequencedCollection which is
      *> capable of holding lnkSize intrinsics of type defined by the
      *> class lnkTemplate.
      *>---------------------------------------------------------------
       method-id. "ofValues".
       local-storage section.
       01 astride              pic s9(9) comp-5.

       linkage section.
       01 lnkTemplate                usage object reference.
       01 lnksize              pic s9(9) comp-5.
       01 acollection          usage object reference.

       procedure division using lnkTemplate lnksize
                                            returning acollection.
           if lnksize < zero or lnksize >= h"20202020" then            
               invoke self "invalidParameter" returning acollection
           else
               invoke lnktemplate "sizeInBytes" returning astride
               invoke super "new" returning aCollection
               invoke acollection "initialize" using lnksize
                                                     lnktemplate
                                                     aStride

           end-if
           exit method.
       end method "ofValues".
      *>----------------------------------------------------------------
      *> Returns a new instance of SequencedCollection capable of
      *> holding lnkSize objects.
      *>----------------------------------------------------------------
       method-id. "ofReferences".
       local-storage section.
       01 astride              pic s9(9) comp-5.
       01 anObject             usage object reference.

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

       procedure division using lnksize returning acollection.
           if lnksize < zero or lnksize >= h"20202020" then            
               invoke self "invalidParameter" returning acollection
           else
               move objectReferenceSize to aStride
               set anObject to Null
               invoke super "new" returning aCollection
               invoke acollection "initialize" using lnksize
                                                     anObject
                                                     aStride
           end-if
           exit method.
       end method "ofReferences".

       end class-object.

       Object.

       object-storage section.

      *>----------------------------------------------------------------
      *> Return true if this collection is the same species as
      *> aCollection and they store the same type of elements and the
      *> elements are of the same value and in the same
      *> order. If the species or the type of elements are different
      *> raise an incompatible collection exception.
      *>----------------------------------------------------------------
       method-id. "equal".
       local-storage section.
       01 aclass           usage object reference.
       01 aptr             pointer.
       01 i                pic s9(9) comp-5.
       01 asize            pic s9(9) comp-5.
       01 thissize         pic s9(9) comp-5.

       linkage section.
       01 anelement        COLLECTABLE.
       01 lnkbool                 pic x comp-5.
           88 lnkistrue           value 1.
           88 lnkisfalse          value 0.
       01 acollection          usage object reference.

       procedure division using acollection returning lnkbool.
           if acollection equal to self
               set lnkistrue to true
           else
               invoke acollection "class" returning aclass
               set lnkisfalse to true
               if selfClass equal to aclass
                   invoke self "size" returning thissize
                   invoke acollection "size" returning asize
                   if asize = thissize
                       set lnkistrue to true
                       invoke self "allocateElement"
                                        returning aptr
                       set address of anelement to aptr
                       perform varying i from 1 by 1
                                until i > thissize or lnkisfalse
                           invoke self "at" using i returning anElement
                           invoke acollection "isAt" using anelement i
                                   returning lnkbool
                       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
                   end-if
               end-if
           end-if
           exit method.
       end method "equal".
      *>----------------------------------------------------------------
      *> 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.

           if anindex < 1 or anIndex > numberElements
               move zero to lnkBool
           else
               compute realindex = (anindex - 1) * collectionstride + 1
               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".

      *>----------------------------------------------------------------
      *> PRIVATE. Answer true if the receiver is sequenced, else answer
      *> false
      *>----------------------------------------------------------------
       method-id. "isSequenced".
       linkage section.
       01 lnkbool                 pic x comp-5.
           88 lnkistrue           value 1.
           88 lnkisfalse          value 0.

       procedure division returning lnkbool.
           set lnkistrue to true
           exit method.
       end method "isSequenced".
      *>----------------------------------------------------------------
      *> Place anElement at every indexable  position of this
      *> SequencedCollection
      *>----------------------------------------------------------------
       method-id. "atAllPut".
       local-storage section.
       01 thissize             pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.

       linkage section.
       01 anelement            COLLECTABLE.

       procedure division  using anelement.
           invoke self "size" returning thissize
           perform varying i from 1 by 1 until i > thissize
               invoke self "atPut" using i anelement
           end-perform
           exit method.
       end method "atAllPut".
      *>----------------------------------------------------------------
      *> Return the first element in this SequencedCollection
      *>----------------------------------------------------------------
       method-id. "first".
       local-storage section.
       01 i                    pic s9(9) comp-5.

       linkage section.
       01 anelement            COLLECTABLE.

       procedure division returning anelement.
           move 1 to i
           invoke self "at" using i returning anelement
           exit method.
       end method "first".
      *>----------------------------------------------------------------
      *> Return the index of the first occurrence of anElement within
      *> this Sequenced collection between the indices fromIndex and
      *> toIndex. If anElement does not occur within this subcollection,
      *> then return zero
      *>----------------------------------------------------------------
       method-id. "firstIndexOfIdentity".
       local-storage section.
       01 aptr                 pointer.
       01 i                    pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.
       01 lsBuffer             pic x(4).
       01 lsBool               pic x comp-5.
       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 anelement            COLLECTABLE.
       01 toindex              pic x(4) comp-5.
       01 fromindex            pic s9(9) comp-5.
       01 anelement2           COLLECTABLE.

       procedure division using anelement fromindex toindex
                                          returning anindex.
           invoke self "size" returning thissize
           if toindex < thissize
               move toindex to  thissize
           end-if

           perform varying i from fromindex by 1 until i > thissize
               invoke self "isAtIdentity" using anElement i
                               returning lsBool
               if lsBool = 1
                   move i to anindex
                   exit method
               end-if
           end-perform
           move zero to anindex
           exit method.
       end method "firstIndexOfIdentity".
      *>----------------------------------------------------------------
      *> Return the index position of the first occurrence of
      *> aCollection within this SequencedCollection, starting the
      *> search at anIndex.
      *> If the subcollection is not contained within this
      *> SequencedCollection, then return zero.
      *>----------------------------------------------------------------
       method-id. "indexOfSubCollection".
       local-storage section.
       01 exceptionnumber  pic s9(9) comp-5.
       01 bool                 pic x comp-5.
           88 istrue           value 1.
           88 isfalse          value 0.
       01 aptr             pointer.
       01 k                    pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 anelement            COLLECTABLE.
       01 anindex2             pic s9(9) comp-5.
       01 acollection          usage object reference.

       procedure division using acollection anindex
                                  returning anindex2.
           invoke self "isCompatible" using acollection
                                       returning bool
           if istrue
               invoke acollection "size" returning asize
               if asize = zero
                   move 11 to exceptionnumber
                   invoke self "raiseException" using exceptionnumber
                                           returning anindex2
               else
                   invoke self "size" returning thissize
                   invoke aCollection "allocateElement" returning aptr
                   set address of anElement to aptr
                   perform varying i from anindex by 1
                       until i > thissize or (thissize - i + 1) < asize
                       set istrue to true
                       perform varying j from 1 by 1 until j > asize or
                                     isfalse
                           invoke acollection "at" using j
                                               returning anElement
                           compute k = i + j - 1
                           invoke self "isAt" using anelement k
                                          returning bool
                       end-perform
                       if istrue
                           move i to anindex2
                           exit method
                       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 self "dealloc" using aptr
                   move zero to anindex2
               end-if
           else
               invoke self "compatibilityError" returning anIndex2
           end-if
           exit method.
       end method "indexOfSubCollection".
      *>----------------------------------------------------------------
      *> Return the last element in this SequencedCollection.
      *>----------------------------------------------------------------
       method-id. "last".
       local-storage section.
       01 i                    pic s9(9) comp-5.

       linkage section.
       01 anelement            COLLECTABLE.

       procedure division returning anelement.
           invoke self "size" returning i
           invoke self "at" using i returning anelement
           exit method.
       end method "last".
      *>----------------------------------------------------------------
      *> Return the index of the first occurrence of anElement between
      *> the index positions fromIndex and toIndex. If anElement does
      *> not occur between these indices, then return zero.
      *>----------------------------------------------------------------
       method-id. "firstIndexOf".
       local-storage section.
       01 bool                 pic x comp-5.
           88 istrue           value 1.
           88 isfalse          value 0.
       01 i                    pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 anelement            COLLECTABLE.
       01 toindex              pic s9(9) comp-5.
       01 fromindex            pic x(4) comp-5.

       procedure division using anelement fromindex
                                            toindex
                                  returning anindex.
           invoke self "size" returning thissize
           if toindex < thissize
               move toindex to  thissize
           end-if
           perform varying i from fromindex by 1 until i > thissize
               invoke self "isat" using anelement i returning bool
               if istrue
                   move i to anindex
                   exit method
               end-if
           end-perform
           move zero to anindex
           exit method.
       end method "firstIndexOf".
      *>----------------------------------------------------------------
      *> Return the index of the last occurrence of anElement between
      *> the index positions fromIndex and toIndex. This method works
      *> backwards through the the collection. If anElement does not
      *> occur between these indices, then return zero.
      *>----------------------------------------------------------------
       method-id. "lastIndexOf".
       local-storage section.
       01 bool                 pic x comp-5.
           88 istrue           value 1.
           88 isfalse          value 0.
       01 i                    pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 anelement            COLLECTABLE.
       01 toindex              pic s9(9) comp-5.
       01 fromindex            pic s9(9) comp-5.

       procedure division using anelement fromindex toindex
                                 returning anindex.

           invoke self "size" returning thissize
           if  toindex < thissize
               move toindex to thissize
           end-if
           perform varying i from thissize by -1 until i < fromindex
               invoke self "isat" using anelement i returning bool
               if istrue
                   move i to anindex
                   exit method
               end-if
           end-perform
           move zero to anindex
           exit method.
       end method "lastIndexOf".
      *>----------------------------------------------------------------
      *> Replace all the occurrences of anElement between the
      *> indices fromIndex and toIndex with anElement2.
      *>----------------------------------------------------------------
       method-id. "replaceAll".
       local-storage section.
       01 i                    pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.

       linkage section.
       01 toindex              pic s9(9) comp-5.
       01 anelement2           COLLECTABLE.
       01 fromindex            pic s9(9) comp-5.
       01 anelement            COLLECTABLE.

       procedure division using anelement anelement2
                                          fromindex toindex.
           invoke self "firstIndexOf" using anelement
                                                 fromindex
                                                 toindex
                                       returning i
           perform until i = 0
               invoke self "atPut" using i anelement2
               move i to j
               add 1 to j
               invoke self "firstIndexOf" using anelement
                                                     j
                                                     toindex
                                           returning i
           end-perform
           exit method.
       end method "replaceAll".
      *>----------------------------------------------------------------
      *> Replace the contents of this collection between the indices
      *> toIndex and fromIndex with the contents of aCollection
      *> starting at fromSource.
      *>
      *>----------------------------------------------------------------
       method-id. "replaceSubCollection".
       local-storage section.
       01 k                    pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 bool                 pic x comp-5.
           88 istrue           value 1.
           88 isfalse          value 0.
       01 asize                pic s9(9) comp-5.
       01 aptr             pointer.
       01 numtoreplace         pic s9(9) comp-5.
       01 exceptionnumber  pic s9(9) comp-5.
       01 anindex              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 fromSource           pic s9(9) comp-5.
       01 acollection          usage object reference.

       procedure division using fromindex toindex
                                acollection fromSource.
           invoke self "isCompatible" using acollection
                                       returning bool
           if istrue
               move fromSource to anIndex
               invoke acollection "size" returning asize
               if (asize - anindex ) < (toindex - fromindex)
                   move 4 to exceptionnumber
                   invoke self "raiseException" using exceptionnumber
               else
                   if anindex < fromindex and
                      self equal to acollection
                       compute numtoreplace = toindex - fromindex + 1
                       move anindex to k
                       add numtoreplace to k
                       move fromindex to j
                       add numtoreplace to j
                       invoke acollection "allocateElement"
                                   returning aptr
                       set address of anelement to  aptr
                       perform numtoreplace times
                           subtract 1 from k
                           invoke acollection "at" using k
                                               returning anElement
                           subtract 1 from j
                           invoke self "atPut" using j 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
                   else
                       compute numtoreplace = toindex - fromindex + 1
                       invoke acollection "allocateElement"
                                        returning aptr
                       set address of anelement to  aptr
                       perform varying i from 0 by 1
                               until i = numtoreplace
                           move anindex to k
                           add i to k
                           invoke acollection "at" using k
                                               returning anElement
                           move fromindex to j
                           add i to j
                           invoke self "atPut" using j 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
                   end-if
               end-if
           else
               invoke self "compatibilityError" returning i
           end-if
           exit method.
       end method "replaceSubCollection".
      *>----------------------------------------------------------------
      *> Return the number of elements stored within this collection
      *>----------------------------------------------------------------
       method-id. "size".
       linkage section.
       01 acollectionsize      pic s9(9) comp-5.

       procedure division returning acollectionsize.
           move numberelements to acollectionsize
           exit method.
       end method "size".
      *>----------------------------------------------------------------
      *> Return the number of elements anElement is equal to.
      *>----------------------------------------------------------------
       method-id. "occurrencesOf".
       local-storage section.
       01 j                    pic s9(9) comp-5.
       01 k                    pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.

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

       procedure division using anelement
                                       returning anumber.
           invoke self "size" returning asize
           move 1 to i
           invoke self "firstindexOf" using anelement
                                                     i
                                                     asize
                                           returning k
           move zero to j
           perform until k = 0
               add 1 to j
               move k to i
               add 1 to i
               invoke self "firstIndexOf" using anelement
                                                     i
                                                     asize
                                           returning k
           end-perform
           move j to anumber
           exit method.
       end method "occurrencesOf".
      *>----------------------------------------------------------------
      *> Cause this Sequenced collection to become larger.
      *>----------------------------------------------------------------
       method-id. "grow".
       local-storage section.
       01 realindex                pic s9(9) comp-5.
       01 exceptionnumber          pic s9(9) comp-5.
       01 bool                     pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.
       01 oldsize                  pic s9(9) comp-5.
       01 asize                    pic s9(9) comp-5.
       01 numbytestomove           pic s9(9) comp-5.

       procedure division.
           invoke self "capacity" returning oldsize
           add 10 to oldsize giving asize
           multiply 2 by asize
           move asize to numberelements
           multiply collectionstride by asize
           invoke self "basicGrow" using asize
                                    returning bool
           move asize to storagerequirements
           if isfalse
               move 32 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           end-if
           exit method.
       end method "grow".
      *>----------------------------------------------------------------
      *> Cause this Sequenced collection to become larger or smaller
      *> according to the requiredSize parameter.
      *>----------------------------------------------------------------
       method-id. "growTo".
       local-storage section.
       01 realindex            pic s9(9) comp-5.
       01 exceptionnumber  pic s9(9) comp-5.
       01 bool                 pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.
       01 oldsize          pic s9(9) comp-5.
       01 numbytestomove       pic s9(9) comp-5.

       linkage section.
       01 defaultreturn        usage object reference.
       01 requiredSize         pic x(4) comp-5.


       procedure division using requiredSize returning defaultreturn.
           invoke self "capacity" returning oldsize
           move requiredsize to numberelements
           multiply collectionstride by requiredsize
           invoke self "basicGrow" using requiredsize
                                    returning bool
           move requiredsize to storagerequirements
           set defaultreturn to self
           if isfalse
               move 32 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
                                       returning defaultreturn
           end-if
           exit method.
       end method "growTo".
      *>----------------------------------------------------------------
      *> Raise an exception as elements cannot be removed
      *> from Sequenced collections
      *>----------------------------------------------------------------
       method-id. "remove".
       local-storage section.
       01 exceptionnumber  pic s9(9) comp-5.

       linkage section.
       01 anobject             usage object reference.

       procedure division using anobject.
           move 5 to exceptionnumber
           invoke self "raiseException" using exceptionnumber
           exit method.
       end method "remove".
      *>----------------------------------------------------------------
      *> Return a new collection containing the elements of this
      *> collection concatenated with the elements of aCollection
      *>----------------------------------------------------------------
       method-id. "concat".
       local-storage section.
       01 bool                 pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.
       01 i                    pic s9(9) comp-5.

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

       procedure division using acollection
                            returning anewcollection.
           invoke self "isCompatible" using acollection
                                       returning bool
           if istrue
               move zero to i
               invoke acollection "insertAllAfter" using i self
                       returning anewcollection
           else
               invoke self "compatibilityError"
                       returning anewcollection
           end-if
           exit method.
       end method "concat".

      *>----------------------------------------------------------------
      *> Returns a new Sequenced collection which is a subset of this
      *> SequencedCollection determined by the indices fromIndex and
      *> toIndex.
      *>----------------------------------------------------------------
       method-id. "copyFromTo".
       local-storage section.
       01 asize                pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.

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

       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
           move 1 to i
           invoke anewcollection "replaceSubCollection"
                                    using i asize self fromindex
           exit method.
       end method "copyFromTo".
      *>----------------------------------------------------------------
      *> Replaces all the occurrences of aCollection within this
      *> SequencedCollection with aCollection2 returning aNewCollection
      *>----------------------------------------------------------------
       method-id. "copyReplaceAll".

       local-storage section.
       01 atempcollection  usage object reference.
       01 i                    pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.
       01 k                    pic s9(9) comp-5.
       01 l                    pic s9(9) comp-5.
       01 m                    pic s9(9) comp-5.
       01 n                    pic s9(9) comp-5.
       01 numoccurrences   pic s9(9) comp-5.
       01 newsize          pic s9(9) comp-5.
       01 asize2               pic s9(9) comp-5.
       01 asize1               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 acollection "size" returning asize1
           invoke acollection2 "size" returning asize2
           move 4 to j
           invoke orderedcollection "ofValues" using cobolcomp5 j
                                returning atempcollection

           move 1 to j
           invoke self "indexOfSubCollection" using acollection
                                                         j
                                               returning i
           perform until i = 0
               invoke atempcollection "add" using i
               move i to j
               add asize1 to j
               invoke self "indexOfSubCollection" using acollection
                                                             j
                                                   returning i
           end-perform
           invoke atempcollection "size" returning numoccurrences
           invoke self "size" returning newsize
           compute newsize = newsize + numoccurrences *
                                   ( asize2 - asize1 )
           invoke self "copyEmptyWithSize" using newsize
                                        returning anewcollection
           move 1 to k m n l
           perform varying i from 1 by 1 until i > numoccurrences
               invoke atempcollection "at" using i returning j
               compute n = m + j - k
               subtract 1 from n
               invoke anewcollection "replaceSubCollection"
                                   using m n self k
               add 1 to n
               compute k = j + asize1
               compute m = n + asize2
               subtract 1 from m
               invoke anewcollection "replaceSubCollection"
                                   using n m acollection2 l
               add 1 to m
           end-perform
           invoke anewcollection "replaceSubCollection"
                                   using m newsize self k
           invoke atempcollection "finalize" returning atempCollection
           exit method.
       end method "copyReplaceAll".
      *>----------------------------------------------------------------
      *> Return a copy of the receiver with the elements between
      *> fromIndex and toIndex replaced by the contents of a collection.
      *>----------------------------------------------------------------
       method-id. "copyReplace".
       local-storage section.
       01 k                    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.
       01 asize                pic s9(9) comp-5.
       01 newsize          pic s9(9) comp-5.

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

       procedure division using fromindex toindex
                                           acollection
                                 returning anewcollection.
           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 "replaceSubcollection"
                                 using i j self i
           add asize to j
           invoke anewcollection "replaceSubcollection"
                                 using fromindex j acollection i
           add 1 to j
           add 1 to toindex giving k
           invoke anewcollection "replaceSubcollection"
                                 using  j newsize self k
           exit method.
       end method "copyReplace".
      *>----------------------------------------------------------------
      *> Return a new collection which is a copy of this sequenced
      *> collection with the additional element anElement.
      *>----------------------------------------------------------------
       method-id. "copyWith".
       local-storage section.
       01 i                    pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.
       01 newsize          pic s9(9) comp-5.

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

       procedure division using anelement
                              returning anewcollection.
           invoke self "size" returning asize
           move asize to newsize
           add 1 to newsize
           invoke self "copyEmptyWithSize" using newsize
                                            returning anewcollection
           move 1 to i
           invoke anewcollection "replaceSubcollection"
                               using i asize self i
           invoke anewcollection "atPut" using newsize anelement
           exit method.
       end method "copyWith".
      *>----------------------------------------------------------------
      *> Return 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 j                    pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.
       01 newsize          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 "occurrencesOf" using anelement
                                        returning i
           invoke self "size" returning asize
           move asize to newsize
           subtract i from newsize
           invoke self "copyEmptyWithSize" using newsize
                                            returning anewcollection
           move 1 to j
           invoke self "allocateElement" using collectionStride
                            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 "atPut" using j anelement2
                   add 1 to j
               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 self "dealloc" using aptr
           exit method.
       end method "copyWithOut".
      *>----------------------------------------------------------------
      *> 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
           set oonoquit to true
           invoke self "allocateElement"
                            returning aptr
           set address of anelement to aptr
           invoke self "allocateElement"
                            returning aptr2
           set address of anelement2 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 anelement2
               invoke anewcollection "atPut" using i 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 of the same species and size as this
      *> collection, containing fresh copies of its elements.
      *>----------------------------------------------------------------
       method-id. "deepcopy".
       local-storage section.
       01 anobject         usage object reference.
       01 i                pic x(4) comp-5.

       linkage section.
       01 anewcollection       usage object reference.

       procedure division returning anewcollection.
           if collectiontemplate = null
               invoke self "copyEmpty" returning anewcollection
               perform varying i from 1 by 1 until i > numberelements
                   invoke self "at" using i returning anobject
                   invoke anobject "copy" returning anobject
                   invoke anewcollection "atPut" using i anobject
               end-perform
           else
               invoke self "copy" returning anewcollection
           end-if
           exit method.
       end method "deepcopy".
      *>----------------------------------------------------------------
      *> Send aMessage for each element of this sequenced collection in
      *> turn, passing that element as a parameter.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter.
      *> For more information see the topic Collection Iterator Methods
      *> in the on-line documentation.
      *>----------------------------------------------------------------
       method-id. "doWithOutCopy".
       local-storage section.
       01 ooreturnptr          usage pointer.
       01 aptr                 pointer.
       01 i                    pic s9(9) comp-5.
       01 thissize             pic s9(9) comp-5.
       01 lsBuffer.
           03 lsBuffer1            pic x(bufferSize).
           03 lsBuffer2            pic x(bufferSize).

       linkage section.
       01 amessage             usage object reference.
       01 lnkreturnbuffer      COLLECTABLE.
       01 anelement2           COLLECTABLE.

       procedure division using amessage.
           invoke self "size" returning thissize
           set oonoquit to true
           if collectionStride > bufferSize
               move 2 to i
               invoke self "allocateElements" using i
                            returning ooreturnptr
           else
               set ooreturnPtr to address of lsBuffer
               move low-value to lsBuffer
           end-if
           set address of lnkreturnbuffer to ooreturnptr
           set aptr to ooreturnPtr
           set aptr up by collectionStride
           set address of anelement2 to aptr

           perform varying i from 1 by 1 until i > thissize or ooquit
               invoke self "at" using i returning anelement2
               invoke amessage "invoke" using anElement2
                       returning lnkReturnBuffer
           end-perform
           set oonoquit to true
      $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 ooreturnPtr
           end-if
           exit method.
       end method "doWithOutCopy".
      *>----------------------------------------------------------------
      *> Send aMessage for each element of this sequenced collection in
      *> turn, passing that element as a parameter.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter.
      *> For more information see the topic Collection Iterator Methods
      *> in the on-line documentation.
      *>----------------------------------------------------------------
       method-id. "do".
       local-storage section.
       01 lsBuffer.
           03 lsBuffer1            pic x(bufferSize).
           03 lsBuffer2            pic x(bufferSize).
       01 ooreturnptr               usage pointer.
       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 amessage             usage object reference.
       01 lnkreturnbuffer      COLLECTABLE.
       01 anelement2           COLLECTABLE.

       procedure division using amessage.
           invoke self "size" returning thissize
           set oonoquit to true
           if collectionStride > bufferSize
               move 2 to i
               invoke self "allocateElements" using i
                            returning ooreturnptr
           else
               move low-values to lsBuffer
               set ooreturnPtr to address of lsBuffer
           end-if
           set address of lnkreturnbuffer to ooreturnptr
           set aptr to ooreturnPtr
           set aptr up by collectionStride
           set address of anelement2 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 anelement2
               invoke amessage "invoke" using anElement2
                       returning lnkReturnBuffer
           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 aCopy "finalize" returning aCopy
           set oonoquit to true
           if collectionStride > bufferSize
               invoke self "dealloc" using ooreturnPtr
           end-if
           exit method.
       end method "do".
      *>----------------------------------------------------------------
      *> Send aMessage for each element within this collection,
      *> passing the element to the invoked method until one of the
      *> elements causes lnkMessage to return true.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter and will return a pic x
      *> comp-5.
      *> If the CallBack returns True (that is 1), then the index of
      *> the element that caused this is returned in anindex and
      *> lnkMessage is not sent to any more elements.
      *> If no element causes lnkMessage to return true then zero is
      *> returned.
      *>----------------------------------------------------------------
       method-id. "findFirst".
       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.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 amessage             usage object reference.
       01 anelement2           COLLECTABLE.

       procedure division using amessage
                                   returning anindex.
           invoke self "size" returning thissize
           set isfalse to true
           invoke self "allocateElement" returning aptr
           set address of anelement2 to aptr
           perform varying i from 1 by 1 until i > thissize
               invoke self "at" using i returning anelement2
               invoke amessage "invoke" using anelement2
                                        returning bool
               if istrue
                   move i to anindex
                   exit method
               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

           move zero to anindex
           invoke self "dealloc" using aptr
           exit method.
       end method "findFirst".
      *>----------------------------------------------------------------
      *> Send aMessage for each element within this collection, but in
      *> reverse order, passing the element to the invoked method until
      *> one of the elements causes lnkMessage to return true.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter and will return a pic x
      *> comp-5.
      *> If the CallBack returns True (that is 1), then the index of
      *> the element that caused this is returned in anindex and
      *> lnkMessage is not sent to any more elements.
      *> If no element causes lnkMessage to return true then zero is
      *> returned.
      *>----------------------------------------------------------------
       method-id. "findLast".
       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.

       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 amessage             usage object reference.
       01 anelement2           COLLECTABLE.

       procedure division using amessage
                                   returning anindex.
           invoke self "size" returning thissize
           set isfalse to true
           invoke self "allocateElement" returning aptr
           set address of anelement2 to aptr
           perform varying i from thissize by -1 until i = 0
               invoke self "at" using i returning anelement2
               invoke amessage "invoke" using anelement2
                                        returning bool
               if istrue
                   move i to anindex
      $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-if
           end-perform
           move zero to anindex
      $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 "findLast".
      *>----------------------------------------------------------------
      *> Instance method "keysAndDatado" repeatedly sends a message for
      *> each element passing the index and the element as parameters.
      *> This method is similar to the "do" method, except that aMessage
      *> should be a CallBack pointing to a method which expects two
      *> parameters. The second parameter is the element itself.
      *> The first parameter is a pic s9(9) comp-5 item representing its
      *> position within the collection.
      *>----------------------------------------------------------------
       method-id. "keysAndDatado".
       local-storage section.
       01 ooreturnptr          usage pointer.
       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 amessage             usage object reference.
       01 lnkreturnbuffer      COLLECTABLE.
       01 anelement2           COLLECTABLE.

       procedure division using amessage.
           invoke self "size" returning thissize
           set oonoquit to true
           invoke self "allocateElement"
                            returning ooreturnptr
           set address of lnkreturnbuffer to ooreturnptr
           invoke self "allocateElement"
                            returning aptr
           set address of anelement2 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 anelement2
               invoke amessage "invoke" using i anelement2
                                            returning lnkreturnbuffer
           end-perform
           invoke aCopy "finalize" returning aCopy
           set oonoquit to true
      $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
           invoke self "dealloc" using ooreturnptr
           exit method.
       end method "keysAndDatado".
      *>----------------------------------------------------------------
      *> Return a new collection which is of the same species as this
      *> sequenced collection, and with the same contents, but in
      *> reverse order.
      *>----------------------------------------------------------------
       method-id. "reverse".
       local-storage section.
       01 aptr                 pointer.
       01 j                    pic s9(9) comp-5.
       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
           move 1 to j
           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 "atPut" using j anelement
               add 1 to j
           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".
      *>----------------------------------------------------------------
      *> Send a message for each element within this sequenced
      *> collection. The elements are passed as parameters in
      *> reverse order to that of the "do" method.
      *> aMessage should be a CallBack pointing to a method which
      *> expects an element as a parameter.
      *> For more information see the topic Collection Iterator Methods
      *> in the on-line documentation.
      *>----------------------------------------------------------------
       method-id. "reverseDo".
       local-storage section.
       01 ooreturnptr          usage pointer.
       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 amessage             usage object reference.
       01 lnkreturnbuffer      COLLECTABLE.
       01 anelement2           COLLECTABLE.

       procedure division using amessage.
           invoke self "size" returning thissize
           set oonoquit to true
           invoke self "allocateElement" returning ooreturnptr
           set address of lnkreturnbuffer to ooreturnptr
           invoke self "allocateElement" returning aptr
           set address of anelement2 to aptr
           invoke self "copy" returning aCopy
           perform varying i from thissize by -1
                            until i = 0 or ooquit
               invoke aCopy "at" using i returning anelement2
               invoke amessage "invoke" using anelement2
                                            returning lnkreturnbuffer
           end-perform
           invoke aCopy "finalize" returning aCopy
           set oonoquit to true
      $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
           invoke self "dealloc" using ooreturnptr
           exit method.
       end method "reverseDo".
      *>----------------------------------------------------------------
      *> Return a new collection of the same species as this sequenced
      *> 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 answer              usage object reference.
       01 newsize             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 amessage             usage object reference.
       01 anewcollection       usage object reference.
       01 anelement2           COLLECTABLE.

       procedure division using amessage
                             returning anewcollection.
           invoke self "size" returning thissize
           invoke orderedcollection "ofValues" using collectiontemplate
                                            thissize
                                  returning answer
           set oonoquit to true
           invoke self "allocateElement" returning aptr
           set address of anelement2 to aptr
           invoke self "copy" returning aCopy
           perform varying i from 1 by 1 until i > thissize or ooquit
               invoke self "at" using i returning anelement2
               invoke amessage "invoke" using anelement2
                                   returning bool
               if istrue
                   invoke answer "add" using anelement2
               end-if
           end-perform
           invoke aCopy "finalize" returning aCopy
           set oonoquit to true
           invoke answer "size" returning newsize
           invoke self "copyEmptyWithSize" using newsize
                                        returning anewcollection
           move 1 to i
           invoke anewcollection "replaceSubcollection"
                                    using i newsize answer i
           invoke answer "finalize" returning answer

      $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 "select".
      *>----------------------------------------------------------------
      *> Return a new collection of the same species as this sequenced
      *> 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 aptr                 pointer.
       01 i                    pic s9(9) comp-5.
       01 answer               usage object reference.
       01 newsize              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 amessage             usage object reference.
       01 anewcollection       usage object reference.
       01 anelement2           COLLECTABLE.

       procedure division using amessage returning anewcollection.
           invoke self "size" returning thissize
           invoke orderedcollection "ofValues" using
                                                   collectiontemplate
                                                   thissize
                                         returning answer
           set oonoquit to true
           invoke self "allocateElement" returning aptr
           set address of anelement2 to aptr
           invoke self "copy" returning aCopy
           perform varying i from 1 by 1 until i > thissize or ooquit
               invoke self "at" using i returning anelement2
               invoke amessage "invoke" using anelement2
                                   returning bool
               if isfalse
                   invoke answer "add" using anelement2
               end-if
           end-perform
           set oonoquit to true
           invoke aCopy "finalize" returning aCopy
           invoke answer "size" returning newsize
           invoke self "copyEmptyWithSize" using newsize
                                            returning anewcollection
           move 1 to i
           invoke anewcollection "replaceSubcollection"
                                        using i newsize answer i
           invoke answer "finalize" returning answer
      $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 "reject".
      *>----------------------------------------------------------------
      *> Cause the message amessage to be sent for each character of
      *> this collection.
      *> amessage should be a CallBack pointing to a method which
      *> expects two elements as parameters.
      *> Each time the callback is invoked, the first parameter will
      *> be the element from this collection, and the second
      *> parameter will be the corresponding element from the other
      *> collection acollection.
      *> If acollection is of different size to this collection then
      *> raise an exception.
      *>----------------------------------------------------------------
       method-id. "withDo".
       local-storage section.
       01 ooreturnptr         usage pointer.
       01 atemplate2          usage object reference.
       01 i                   pic s9(9) comp-5.
       01 tempsize            pic s9(9) comp-5.
       01 asize               pic s9(9) comp-5.
       01 thissize            pic s9(9) comp-5.
       01 aptr                pointer.
       01 aptr2               pointer.
       01 exceptionnumber     pic s9(9) comp-5.
       01 aCopy               usage object reference.
       01 aCopy2              usage object reference.

       linkage section.
       01 lnkreturnbuffer     COLLECTABLE.
       01 anelement2          COLLECTABLE.
       01 anelement           COLLECTABLE.
       01 amessage             usage object reference.
       01 acollection          usage object reference.

       procedure division using acollection amessage.
           invoke self "size" returning thissize
           invoke acollection "size" returning asize
           if asize not = thissize
               move 6 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           else
               set oonoquit to true
               invoke acollection "template" returning atemplate2
               invoke atemplate2 "sizeInBytes" returning tempSize

               invoke self "allocateElement" returning aptr
               set address of anelement to aptr

               invoke acollection "allocateElement" returning aptr2
               set address of anelement2 to aptr2
               if collectionstride > tempSize
                   invoke self "malloc" using collectionStride
                               returning ooreturnptr
               else
                   invoke self "malloc" using tempSize
                                returning ooreturnptr
               end-if
               set address of lnkreturnbuffer to ooreturnptr
               invoke self "copy" returning aCopy
               invoke aCollection "copy" returning aCopy2
               perform varying i from 1 by 1
                   until i > thissize or ooquit
                   invoke aCopy "at" using i returning anelement
                   invoke aCopy2 "at" using i
                                            returning anelement2
                   invoke amessage "invoke" using anelement
                                                  anelement2
                                       returning lnkreturnbuffer
               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
               if atemplate2 = 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
               invoke self "dealloc" using aptr2
               invoke self "dealloc" using ooreturnPtr
               invoke aCopy "finalize" returning aCopy
               invoke aCopy2 "finalize" returning aCopy2
               set oonoquit to true
           end-if
           exit method.
       end method "withDo".
      *>----------------------------------------------------------------
      *> Return a new collection with the elements of aCollection
      *> inserted after the index position anIndex. If anIndex = zero
      *> then the elements are inserted at the beginning of the
      *> collection.
      *>----------------------------------------------------------------
       method-id. "insertAllAfter".
       local-storage section.
       01 bool                 pic x comp-5.
          88 istrue           value 1.
          88 isfalse          value 0.
       01 thissize             pic s9(9) comp-5.
       01 j                    pic s9(9) comp-5.
       01 i                    pic s9(9) comp-5.
       01 asize                pic s9(9) comp-5.
       01 newsize          pic s9(9) comp-5.

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

       procedure division using anindex acollection
                            returning anewcollection.
           invoke self "iscompatible" using acollection
                                       returning bool
           if istrue
               invoke self "size" returning thissize
               invoke acollection "size" returning asize
               add thissize to asize giving newsize
               invoke self "copyEmptyWithSize" using newsize
                                         returning anewcollection
               move 1 to i
               invoke anewcollection "replaceSubCollection"
                                                    using i anindex
                                                          self i
               move anindex to j
               add j to asize
               add 1 to j
               invoke anewcollection "replaceSubCollection"
                                                   using j asize
                                                         acollection i
               add 1 to asize
               invoke anewcollection "replaceSubCollection"
                                                   using asize newsize
                                                         self j
           else
               invoke self "compatibilityError"
                       returning anewcollection
           end-if
           exit method.
       end method "insertAllAfter".
       end object.
       end class sequencedcollection.
