
      *>----------------------------------------------------------------
      *> An OrderedCollection manages the storage of elements in the
      *> order in which they were added to the collection. One of the
      *> main advantages of OrderedCollections over Arrays is that they
      *> can grow dynamically as more elements are added.
      *> You can use OrderedCollections to implement stacks and queues.
      *>
      *>#See the section Using Collections.
      *>
      *> Copyright (C) 1991-1999 Micro Focus International Ltd.
      *>
      *>----------------------------------------------------------------
      $set localcount(150) linkcount(150) mfoo
       class-id. orderedcollection data is protected
                     inherits from dynamicArrayedcollection with data.


        object section.
        class-control.
           dynamicarrayedcollection is class "dynarray"
           orderedcollection is class "ordrdcll"
           callback is class "callback".

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


       Object.

       object-storage section.
      *>----------------------------------------------------------------
      *> Store anElement at the beginning of this OrderedCollection.
      *>----------------------------------------------------------------
       method-id. "addFirst".
       local-storage section.
       01 realIndex        pic x(4) comp-5.
       linkage section.
       01 anelement        COLLECTABLE.
       01 instanceData     pic x.
       procedure division using anelement.
           if firstindex = 1
               invoke self "makeRoomAtStart"
           end-if
           subtract 1 from firstindex
           compute realindex = (firstIndex - 1) * collectionstride + 1
           set address of instancedata to collptr
           move anelement(1:collectionStride)
                 to instancedata(realindex:collectionstride)

           exit method.
       end method "addFirst".
      *>----------------------------------------------------------------
      *> Store anElement at the end of this OrderedCollection.
      *>----------------------------------------------------------------
       method-id. "addLast".
       linkage section.
       01 anelement             COLLECTABLE.

       procedure division using anelement.
           invoke self "add" using anelement
           exit method.
       end method "addLast".
      *>----------------------------------------------------------------
      *> Store anElement within this OrderedCollection immediately
      *> after the first occurrence of anElement2. Raise an
      *> exception if this collection does not contain anElement.
      *>----------------------------------------------------------------
       method-id. "addAfter".
       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 anelement2            COLLECTABLE.
       01 anelement             COLLECTABLE.

       procedure division using anelement anelement2.
           invoke self "size" returning thissize
           move 1 to j
           invoke self "firstIndexOf" using anelement2 j thissize
                                       returning i
           if i not = 0
               add 1 to i
               invoke self "insertBeforeIndex" using anelement i
           else
               move 12 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           end-if
           exit method.
       end method "addAfter".
      *>----------------------------------------------------------------
      *> Store anElement within this OrderedCollection immediately
      *> before the first occurrence of anElement2.  Raise an
      *> exception if this collection does not contain anElement.
      *>----------------------------------------------------------------
       method-id. "addBefore".
       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 anelement2           COLLECTABLE.
       01 anelement            COLLECTABLE.

       procedure division using anelement anelement2.
           invoke self "size" returning thissize
           move 1 to j
           invoke self "firstIndexOf" using anelement2 j thissize
                                       returning i
           if i not = 0
               invoke self "insertBeforeIndex" using anelement i
           else
               move 12 to exceptionnumber
               invoke self "raiseException" using exceptionnumber
           end-if
           exit method.
       end method "addBefore".
      *>----------------------------------------------------------------
      *> Stores anElement within this OrderedCollection immediately
      *> before the index position anIndex. Raise an exception if the
      *> anIndex is out of bounds.
      *>----------------------------------------------------------------
       method-id. "addBeforeIndex".
       linkage section.
       01 anindex              pic s9(9) comp-5.
       01 anelement            COLLECTABLE.

       procedure division using anelement anindex.
           invoke self "insertBeforeIndex" using anelement anindex
           exit method.
       end method "addBeforeIndex".
      *>----------------------------------------------------------------
      *> Stores anElement within this OrderedCollection immediately
      *> after the index position anIndex. Raise an exception if the
      *> anIndex is out of bounds.
      *>----------------------------------------------------------------
       method-id. "addAfterIndex".
       local-storage section.

       01 i                    pic s9(9) comp-5.

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

       procedure division using anelement anindex.
           add 1 to anindex giving i
           invoke self "insertBeforeIndex" using anelement i
           exit method.
       end method "addAfterIndex".
      *>----------------------------------------------------------------
      *> Append the contents of aCollection to this OrderedCollection.
      *>----------------------------------------------------------------
       method-id. "addAllLast".
       linkage section.
       01 acollection          usage object reference.

       procedure division using acollection.
           invoke self "addAll" using acollection
           exit method.
       end method "addAllLast".
      *>----------------------------------------------------------------
      *> Prefix the contents of this OrderedCollection with the contents
      *> of aCollection.
      *>----------------------------------------------------------------
       method-id. "addAllFirst".
       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 "addFirst" to messageselector
               invoke callback "new" using self messageselector
                                 returning anewmessage
               invoke acollection "reversedo" using anewmessage
               invoke anewmessage "finalize" returning anewmessage
           else
               invoke self "compatibilityError" returning aNewMessage
           end-if
           exit method.
       end method "addAllFirst".
      *>----------------------------------------------------------------
      *> Append the contents of the subcollection indicated by the
      *> collection aCollection and the indices fromIndex and toIndex to
      *> this ordered collection. aCollection must be a kind of
      *> sequenced collection otherwise an exception will be raised.
      *>----------------------------------------------------------------
       method-id. "addSubCollectionLast".
       local-storage section.

       01 exceptionnumber      pic s9(9) comp-5.
       01 ooreturnptr          usage pointer.
       01 aptr             pointer.
       01 i                    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 toindex              pic s9(9) comp-5.
       01 fromindex            pic s9(9) comp-5.
       01 acollection          usage object reference.
       procedure division using acollection fromindex toindex.
           invoke self "isCompatible" using acollection
                                       returning bool
           if istrue
               invoke acollection "isSequenced" returning bool
               if istrue
                   invoke aCollection "allocateElement" returning aptr
                   set address of anelement to aptr
                   perform varying i from fromindex by 1
                           until i > toindex
                       invoke acollection "at" using i
                                           returning anElement
                       invoke self "add" using anelement
                   end-perform
                   invoke self "dealloc" using aptr
               else
                   move 15 to exceptionnumber
                   invoke self "raiseException" using exceptionnumber
               end-if
           else
               invoke self "compatibilityError"
           end-if
           exit method.
       end method "addSubCollectionLast".
      *>----------------------------------------------------------------
      *> Prefix the contents  of this ordered collection with the
      *> subcollection indicated by the collection aCollection and the
      *> indices fromIndex and toIndex. ACollection must be a kind of
      *> sequenced collection otherwise an exception will be raised
      *>----------------------------------------------------------------
       method-id. "addSubCollectionFirst".
       local-storage section.

       01 exceptionnumber      pic s9(9) comp-5.
       01 aptr             pointer.
       01 i                    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 toindex              pic s9(9) comp-5.
       01 fromindex            pic s9(9) comp-5.
       01 acollection          usage object reference.

       procedure division using acollection fromindex toindex.
           invoke self "isCompatible" using acollection
                                       returning bool
           if istrue
               invoke acollection "isSequenced" returning bool
               if istrue
                   invoke aCollection "allocateElement" returning aptr
                   set address of anelement to aptr
                   perform varying i from toindex by -1
                           until i < fromindex
                       invoke acollection "at" using i
                                           returning anElement
                       invoke self "addFirst" using anelement
                   end-perform
                   invoke self "dealloc" using aptr
               else
                   move 15 to exceptionnumber
                   invoke self "raiseException"
                                           using exceptionnumber
               end-if
           else
               invoke self "compatibilityError"
           end-if
           exit method.
       end method "addSubCollectionFirst".
      *>----------------------------------------------------------------
      *> 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 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 acollection "size" returning asize
               invoke self "size" returning thissize
               add thissize to asize giving newsize
               invoke self "copyEmptyWithSize" using newsize
                                                returning anewcollection
               move 1 to i
               invoke anewcollection "addSubCollectionLast"
                                   using self i anindex
               invoke anewcollection "addSubCollectionLast"
                                    using acollection i asize
               add anindex to i
               invoke anewcollection "addSubCollectionLast"
                                   using self i thissize
           else
               invoke self "compatibilityError"
                        returning aNewCollection
           end-if
           exit method.
       end method "insertAllAfter".
       end object.
       end class orderedcollection.
