//====START_GENERATED_PROLOG======================================
//
//
//   COMPONENT_NAME: odutils
//
//   CLASSES: none
//
//   ORIGINS: 81,82,27
//
//
//   (C) COPYRIGHT International Business Machines Corp. 1995,1996
//   All Rights Reserved
//   Licensed Materials - Property of IBM
//   US Government Users Restricted Rights - Use, duplication or
//   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
//   	
//   IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
//   ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//   PURPOSE. IN NO EVENT SHALL IBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR
//   CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
//   USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
//   OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE
//   OR PERFORMANCE OF THIS SOFTWARE.
//
//====END_GENERATED_PROLOG========================================
//
// @(#) 1.2 nt/src/utils/tpll.cpp, odutils, od96nt, odnt9712d 11/1/96 19:08:01 [ 3/21/97 17:37:34 ]
//
//   Copyright © 1988 Apple Computer, Inc. All rights reserved.

/* WIN16PORT ADD3 */
#ifdef _WIN16
#pragma data_seg("_DATA")
#endif


/* Defines to take the place of PrimitiveTypes.h */
#define somf_TPrimitiveLinkedList_Class_Source
#define SOMF_NIL 0
#define Boolean boolean
#include "tpll.xih"

/*
 * -- Original Instance Data (this is a comment -- actual defn in TPLL.xih)
 */
/*
typedef struct {
	somf_MLinkable *fSentinel;
} somf_TPrimitiveLinkedListData;
*/

/*
 * -- Method Procedures for somf_TPrimitiveLinkedList
 */

/*
 *  This is the closest SOM has to a constructor, the original pink reference
 *  is below:
 *    somf_TPrimitiveLinkedList();
 */

SOM_Scope void   SOMLINK somInit(somf_TPrimitiveLinkedList *somSelf)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somInit");

    parent_SOMObject_somInit(somSelf);

   Environment *ev = somGetGlobalEnvironment();
   somThis->fSentinel = new somf_MLinkable;
   (somThis->fSentinel)->somfSetNext(ev, somThis->fSentinel);
   (somThis->fSentinel)->somfSetPrevious(ev, somThis->fSentinel);
}

/*
 *  This is the closest SOM has to a constructor, the original pink reference
 *  is below:
 *    ~somf_TPrimitiveLinkedList();
 */

SOM_Scope void   SOMLINK somUninit(somf_TPrimitiveLinkedList *somSelf)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somUninit");

    parent_SOMObject_somUninit(somSelf);

   delete somThis->fSentinel;
}

/*
 *    unsigned long somfCount() const;
 */

/*
 *  Return the number of objects in this.
 */


/*
 * SOM_Scope unsigned long   SOMLINK somfCount(somf_TPrimitiveLinkedList *somSelf)
 */

/*
 * The prototype for somfCount was replaced by the following prototype:
 */
SOM_Scope unsigned long  SOMLINK somfCount(somf_TPrimitiveLinkedList *somSelf, Environment *ev)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfCount");

   somf_MLinkable* l = (somThis->fSentinel)->somfGetNext(ev);
   unsigned long count = 0;
   while (l != somThis->fSentinel)
   {
      count++;
      l = l->somfGetNext(ev);
   }
   return count;
//    return (unsigned long) 0;
}

/*
 *    void somfRemove(somf_MLinkable&);
 */

/*
 *  Remove the somf_MLinkable object from the List.
 */


/*
 * SOM_Scope void   SOMLINK somfRemove(somf_TPrimitiveLinkedList *somSelf,
 *              somf_MLinkable *aLink)
 */

/*
 * The prototype for somfRemove was replaced by the following prototype:
 */
SOM_Scope void  SOMLINK somfRemove(somf_TPrimitiveLinkedList *somSelf, Environment *ev,
		somf_MLinkable* aLink)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfRemove");

   somf_MLinkable* oldPrev = aLink->somfGetPrevious(ev);
   somf_MLinkable* oldNext = aLink->somfGetNext(ev);

/* bjk 6369.4 fix */
   if (!(oldPrev == SOMF_NIL))
   {
       aLink->somfSetPrevious(ev, SOMF_NIL);
       oldPrev->somfSetNext(ev, oldNext);
   }
   if (!(oldNext == SOMF_NIL))
   {
       aLink->somfSetNext(ev, SOMF_NIL);
       oldNext->somfSetPrevious(ev, oldPrev);
   }
}

/*
 *    void somfRemoveAll();
 */

/*
 *  Remove all of the objects from this.
 */


/*
 * SOM_Scope void   SOMLINK somfRemoveAll(somf_TPrimitiveLinkedList *somSelf)
 */

/*
 * The prototype for somfRemoveAll was replaced by the following prototype:
 */
SOM_Scope void  SOMLINK somfRemoveAll(somf_TPrimitiveLinkedList *somSelf, Environment *ev)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfRemoveAll");

   somf_MLinkable* l = (somThis->fSentinel)->somfGetNext(ev);
   somf_MLinkable* n = l;
   while (n != somThis->fSentinel)
   {
      n = l->somfGetNext(ev);
      l->somfSetNext(ev, SOMF_NIL);
      l->somfSetPrevious(ev, SOMF_NIL);
      l = n;
   }
   (somThis->fSentinel)->somfSetNext(ev, somThis->fSentinel);
   (somThis->fSentinel)->somfSetPrevious(ev, somThis->fSentinel);
}

/*
 *    somf_MLinkable* somfRemoveFirst();
 */

/*
 *  Remove the first object in the List.
 */


/*
 * SOM_Scope somf_MLinkable *  SOMLINK somfRemoveFirst(somf_TPrimitiveLinkedList *somSelf)
 */

/*
 * The prototype for somfRemoveFirst was replaced by the following prototype:
 */
SOM_Scope somf_MLinkable*  SOMLINK somfRemoveFirst(somf_TPrimitiveLinkedList *somSelf, Environment *ev)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfRemoveFirst");

   somf_MLinkable* old = (somThis->fSentinel)->somfGetNext(ev);
   if (old != somThis->fSentinel)
   {
      somf_MLinkable* newFirst = old->somfGetNext(ev);
      old->somfSetNext(ev, SOMF_NIL);
      (somThis->fSentinel)->somfSetNext(ev, newFirst);
      newFirst->somfSetPrevious(ev, somThis->fSentinel);
      return old;
   }
   else
   {
      return SOMF_NIL;
   }
//    return (somf_MLinkable*) 0;
}

/*
 *    somf_MLinkable* somfRemoveLast();
 */

/*
 *  Remove the last object in the List.
 */


/*
 * SOM_Scope somf_MLinkable *  SOMLINK somfRemoveLast(somf_TPrimitiveLinkedList *somSelf)
 */

/*
 * The prototype for somfRemoveLast was replaced by the following prototype:
 */
SOM_Scope somf_MLinkable*  SOMLINK somfRemoveLast(somf_TPrimitiveLinkedList *somSelf, Environment *ev)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfRemoveLast");

   somf_MLinkable* old = (somThis->fSentinel)->somfGetPrevious(ev);
   if (old != somThis->fSentinel)
   {
      somf_MLinkable* newLast = old->somfGetPrevious(ev);
      old->somfSetPrevious(ev, SOMF_NIL);
      (somThis->fSentinel)->somfSetPrevious(ev, newLast);
      newLast->somfSetNext(ev, somThis->fSentinel);
      return old;
   }
   else
   {
      return SOMF_NIL;
   }
//    return (somf_MLinkable*) 0;
}

/*
 *    void somfAddBefore(somf_MLinkable& existing, somf_MLinkable* obj);
 */

/*
 *  Add the obj object into the List before the existing object.
 */


/*
 * SOM_Scope void   SOMLINK somfAddBefore(somf_TPrimitiveLinkedList *somSelf,
 *              somf_MLinkable *existing,
 *              somf_MLinkable *obj)
 */

/*
 * The prototype for somfAddBefore was replaced by the following prototype:
 */
SOM_Scope void  SOMLINK somfAddBefore(somf_TPrimitiveLinkedList *somSelf, Environment *ev,
		somf_MLinkable* existing,
		somf_MLinkable* obj)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfAddBefore");

   somf_MLinkable* oldPrev = existing->somfGetPrevious(ev);

   oldPrev->somfSetNext(ev, obj);
   obj->somfSetPrevious(ev, oldPrev);

   existing->somfSetPrevious(ev, obj);
   obj->somfSetNext(ev, existing);
}

/*
 *    void somfAddAfter(somf_MLinkable& existing, somf_MLinkable* obj);
 */

/*
 *  Add the obj object into the List after the existing object.
 */


/*
 * SOM_Scope void   SOMLINK somfAddAfter(somf_TPrimitiveLinkedList *somSelf,
 *              somf_MLinkable *existing,
 *              somf_MLinkable *obj)
 */

/*
 * The prototype for somfAddAfter was replaced by the following prototype:
 */
SOM_Scope void  SOMLINK somfAddAfter(somf_TPrimitiveLinkedList *somSelf, Environment *ev,
		somf_MLinkable* existing,
		somf_MLinkable* obj)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfAddAfter");

   somf_MLinkable* oldNext = existing->somfGetNext(ev);

   oldNext->somfSetPrevious(ev, obj);
   obj->somfSetNext(ev, oldNext);

   existing->somfSetNext(ev, obj);
   obj->somfSetPrevious(ev, existing);
}

/*
 *    void somfAddFirst(somf_MLinkable* obj);
 */

/*
 *  Add the obj object as the first object in the List.
 */


/*
 * SOM_Scope void   SOMLINK somfAddFirst(somf_TPrimitiveLinkedList *somSelf,
 *              somf_MLinkable *obj)
 */

/*
 * The prototype for somfAddFirst was replaced by the following prototype:
 */
SOM_Scope void  SOMLINK somfAddFirst(somf_TPrimitiveLinkedList *somSelf, Environment *ev,
		somf_MLinkable* obj)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfAddFirst");

   somf_MLinkable* second = (somThis->fSentinel)->somfGetNext(ev);

   (somThis->fSentinel)->somfSetNext(ev, obj);
   obj->somfSetPrevious(ev, somThis->fSentinel);

   second->somfSetPrevious(ev, obj);
   obj->somfSetNext(ev, second);
}

/*
 *    void somfAddLast(somf_MLinkable* obj);
 */

/*
 *  Add the obj object as the last object in the List.
 */


/*
 * SOM_Scope void   SOMLINK somfAddLast(somf_TPrimitiveLinkedList *somSelf,
 *              somf_MLinkable *obj)
 */

/*
 * The prototype for somfAddLast was replaced by the following prototype:
 */
SOM_Scope void  SOMLINK somfAddLast(somf_TPrimitiveLinkedList *somSelf, Environment *ev,
		somf_MLinkable* obj)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfAddLast");

   somf_MLinkable* second = (somThis->fSentinel)->somfGetPrevious(ev);

   (somThis->fSentinel)->somfSetPrevious(ev, obj);
   obj->somfSetNext(ev, somThis->fSentinel);

   second->somfSetNext(ev, obj);
   obj->somfSetPrevious(ev, second);
}

/*
 *    somf_MLinkable* somfAfter(const somf_MLinkable&) const;
 */

/*
 *  Return the object that comes after the existingobj object in the List.
 */


/*
 * SOM_Scope somf_MLinkable *  SOMLINK somfAfter(somf_TPrimitiveLinkedList *somSelf,
 *              somf_MLinkable *existingobj)
 */

/*
 * The prototype for somfAfter was replaced by the following prototype:
 */
SOM_Scope somf_MLinkable*  SOMLINK somfAfter(somf_TPrimitiveLinkedList *somSelf, Environment *ev,
		somf_MLinkable* existingobj)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfAfter");

   return (existingobj->somfGetNext(ev) != somThis->fSentinel) ? existingobj->somfGetNext(ev) : SOMF_NIL;
//    return (somf_MLinkable*) 0;
}

/*
 *    somf_MLinkable* somfBefore(const somf_MLinkable&) const;
 */

/*
 *  Return the object that comes before the existingobj object in the List.
 */


/*
 * SOM_Scope somf_MLinkable *  SOMLINK somfBefore(somf_TPrimitiveLinkedList *somSelf,
 *              somf_MLinkable *existingobj)
 */

/*
 * The prototype for somfBefore was replaced by the following prototype:
 */
SOM_Scope somf_MLinkable*  SOMLINK somfBefore(somf_TPrimitiveLinkedList *somSelf, Environment *ev,
		somf_MLinkable* existingobj)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfBefore");

   return (existingobj->somfGetPrevious(ev) != somThis->fSentinel) ? existingobj->somfGetPrevious(ev) : SOMF_NIL;
//    return (somf_MLinkable*) 0;
}

/*
 *    somf_MLinkable* somfFirst() const;
 */

/*
 *  Return the first object in the List.
 */


/*
 * SOM_Scope somf_MLinkable *  SOMLINK somfFirst(somf_TPrimitiveLinkedList *somSelf)
 */

/*
 * The prototype for somfFirst was replaced by the following prototype:
 */
SOM_Scope somf_MLinkable*  SOMLINK somfFirst(somf_TPrimitiveLinkedList *somSelf, Environment *ev)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfFirst");

   return ((somThis->fSentinel)->somfGetNext(ev) != somThis->fSentinel) ? (somThis->fSentinel)->somfGetNext(ev) : SOMF_NIL;
//    return (somf_MLinkable*) 0;
}

/*
 *    somf_MLinkable* somfLast() const;
 */

/*
 *  Return the last object in the List.
 */


/*
 * SOM_Scope somf_MLinkable *  SOMLINK somfLast(somf_TPrimitiveLinkedList *somSelf)
 */

/*
 * The prototype for somfLast was replaced by the following prototype:
 */
SOM_Scope somf_MLinkable*  SOMLINK somfLast(somf_TPrimitiveLinkedList *somSelf, Environment *ev)
{
    somf_TPrimitiveLinkedListData *somThis = somf_TPrimitiveLinkedListGetData(somSelf);
    somf_TPrimitiveLinkedListMethodDebug("somf_TPrimitiveLinkedList","somfLast");

   return ((somThis->fSentinel)->somfGetPrevious(ev) != somThis->fSentinel) ? (somThis->fSentinel)->somfGetPrevious(ev) : SOMF_NIL;
//    return (somf_MLinkable*) 0;
}
