// Created on: 2002-04-23
// Created by: Alexander KARTOMIN (akm)
// Copyright (c) 2002-2014 OPEN CASCADE SAS
//
// This file is part of Open CASCADE Technology software library.
//
// This library is free software; you can redistribute it and/or modify it under
// the terms of the GNU Lesser General Public License version 2.1 as published
// by the Free Software Foundation, with special exception defined in the file
// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
// distribution for complete text of the license and disclaimer of any warranty.
//
// Alternatively, this file may be used under the terms of Open CASCADE
// commercial license or contractual agreement.

// Purpose:     Implementation of the BaseList class

#include <NCollection_BaseList.hxx>

//=======================================================================
// function : pClear
// purpose  : Deletes all nodes from the list
//=======================================================================

void NCollection_BaseList::pClear(NCollection_DelListNode fDel)
{
  NCollection_ListNode* pCur  = myFirst;
  NCollection_ListNode* pNext = nullptr;
  while (pCur)
  {
    pNext = pCur->Next();
    fDel(pCur, myAllocator);
    pCur = pNext;
  }
  myLength = 0;
  myFirst = myLast = nullptr;
}

//=================================================================================================

void NCollection_BaseList::pAppend(NCollection_ListNode* theNode) noexcept
{
  if (myLength)
  {
    myLast->Next() = theNode;
  }
  else
  {
    myFirst = theNode;
  }
  theNode->Next() = nullptr;
  myLast          = theNode;
  myLength++;
}

//=======================================================================
// function : pAppend
// purpose  : Appends another list at the end
//=======================================================================

void NCollection_BaseList::pAppend(NCollection_BaseList& theOther) noexcept
{
  if (this == &theOther || theOther.IsEmpty())
  {
    return;
  }

  if (IsEmpty())
  {
    myFirst = theOther.myFirst;
  }
  else
  {
    myLast->Next() = theOther.myFirst;
  }
  myLast           = theOther.myLast;
  theOther.myFirst = theOther.myLast = nullptr;

  myLength += theOther.myLength;
  theOther.myLength = 0;
}

//=======================================================================
// function : pPrepend
// purpose  : Prepends one item at the beginning
//=======================================================================

void NCollection_BaseList::pPrepend(NCollection_ListNode* theNode) noexcept
{
  theNode->Next() = myFirst;
  myFirst         = theNode;
  if (myLast == nullptr)
  {
    myLast = myFirst;
  }
  myLength++;
}

//=================================================================================================

void NCollection_BaseList::pPrepend(NCollection_BaseList& theOther) noexcept
{
  if (this == &theOther || theOther.IsEmpty())
  {
    return;
  }

  if (IsEmpty())
  {
    myLast = theOther.myLast;
  }
  else
  {
    theOther.myLast->Next() = myFirst;
  }
  myFirst          = theOther.myFirst;
  theOther.myFirst = theOther.myLast = nullptr;

  myLength += theOther.myLength;
  theOther.myLength = 0;
}

//=================================================================================================

void NCollection_BaseList::pRemoveFirst(NCollection_DelListNode fDel)
{
  Standard_NoSuchObject_Raise_if(IsEmpty(), "NCollection_BaseList::pRemoveFirst");
  NCollection_ListNode* pItem = myFirst;
  myFirst                     = pItem->Next();
  fDel(pItem, myAllocator);
  myLength--;
  if (myLength == 0)
  {
    myLast = nullptr;
  }
}

//=================================================================================================

void NCollection_BaseList::pRemove(Iterator& theIter, NCollection_DelListNode fDel)
{
  Standard_NoSuchObject_Raise_if(!theIter.More(), "NCollection_BaseList::pRemove");
  if (theIter.myPrevious == nullptr)
  {
    pRemoveFirst(fDel);
    theIter.myCurrent = myFirst;
  }
  else
  {
    NCollection_ListNode* pNode  = (theIter.myCurrent)->Next();
    (theIter.myPrevious)->Next() = pNode;
    fDel(theIter.myCurrent, myAllocator);
    theIter.myCurrent = pNode;
    if (pNode == nullptr)
    {
      myLast = theIter.myPrevious;
    }
    myLength--;
  }
}

//=================================================================================================

void NCollection_BaseList::pInsertBefore(NCollection_ListNode* theNode, Iterator& theIter)
{
  Standard_NoSuchObject_Raise_if(!theIter.More(), "NCollection_BaseList::pInsertBefore");
  if (theIter.myPrevious == nullptr)
  {
    pPrepend(theNode);
    theIter.myPrevious = myFirst;
  }
  else
  {
    (theIter.myPrevious)->Next() = theNode;
    theNode->Next()              = theIter.myCurrent;
    theIter.myPrevious           = theNode;
    myLength++;
  }
}

//=================================================================================================

void NCollection_BaseList::pInsertBefore(NCollection_BaseList& theOther, Iterator& theIter)
{
  Standard_NoSuchObject_Raise_if(!theIter.More(), "NCollection_BaseList::pInsertBefore");
  if (theIter.myPrevious == nullptr)
  {
    theIter.myPrevious = theOther.myLast;
    pPrepend(theOther);
  }
  else if (!theOther.IsEmpty())
  {
    myLength += theOther.myLength;
    (theIter.myPrevious)->Next() = theOther.myFirst;
    (theOther.myLast)->Next()    = theIter.myCurrent;
    theIter.myPrevious           = theOther.myLast;
    theOther.myLast = theOther.myFirst = nullptr;
    theOther.myLength                  = 0;
  }
}

//=================================================================================================

void NCollection_BaseList::pInsertAfter(NCollection_ListNode* theNode, Iterator& theIter)
{
  Standard_NoSuchObject_Raise_if(!theIter.More(), "NCollection_BaseList::pInsertAfter");
  if (theIter.myCurrent == myLast)
  {
    pAppend(theNode);
  }
  else
  {
    theNode->Next()             = (theIter.myCurrent)->Next();
    (theIter.myCurrent)->Next() = theNode;
    myLength++;
  }
}

//=================================================================================================

void NCollection_BaseList::pInsertAfter(NCollection_BaseList& theOther, Iterator& theIter)
{
  Standard_NoSuchObject_Raise_if(!theIter.More(), "NCollection_BaseList::pInsertAfter");
  if (theIter.myCurrent == myLast)
  {
    pAppend(theOther);
  }
  else if (!theOther.IsEmpty())
  {
    myLength += theOther.myLength;
    (theOther.myLast)->Next()   = (theIter.myCurrent)->Next();
    (theIter.myCurrent)->Next() = theOther.myFirst;
    theOther.myLast = theOther.myFirst = nullptr;
    theOther.myLength                  = 0;
  }
}

//=================================================================================================

void NCollection_BaseList::pReverse() noexcept
{
  if (myLength > 1)
  {
    NCollection_ListNode* aHead = myFirst->Next();
    NCollection_ListNode* aNeck = myFirst;
    aNeck->Next()               = nullptr;
    while (aHead)
    {
      NCollection_ListNode* aTmp = aHead->Next();
      aHead->Next()              = aNeck;
      aNeck                      = aHead;
      aHead                      = aTmp;
    }
    myLast  = myFirst;
    myFirst = aNeck;
  }
}
