// Created on: 1993-01-11
// Created by: Remi LEQUETTE
// Copyright (c) 1993-1999 Matra Datavision
// Copyright (c) 1999-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.

#include "Standard_HashUtils.hxx"
#include "TopLoc_SListOfItemLocation.hxx"
#include "TopLoc_Datum3D.hxx"
#include "TopLoc_ItemLocation.hxx"

//=================================================================================================

inline bool TopLoc_Location::IsIdentity() const noexcept
{
  return myItems.IsEmpty();
}

//=================================================================================================

inline void TopLoc_Location::Identity() noexcept
{
  myItems.Clear();
}

//=================================================================================================

inline const occ::handle<TopLoc_Datum3D>& TopLoc_Location::FirstDatum() const
{
  return myItems.Value().myDatum;
}

//=================================================================================================

inline int TopLoc_Location::FirstPower() const
{
  return myItems.Value().myPower;
}

//=================================================================================================

inline const TopLoc_Location& TopLoc_Location::NextLocation() const
{
  return (*(TopLoc_Location*)&(myItems.Tail()));
}

//=================================================================================================

inline size_t TopLoc_Location::HashCode() const noexcept
{
  if (myItems.IsEmpty())
  {
    return 0;
  }
  // Hash all items using pointer-based iteration to avoid Handle copy overhead.
  size_t aHash = opencascade::MurmurHash::optimalSeed<size_t>();
  size_t aCombined[3];
  for (const TopLoc_SListOfItemLocation* pIter = &myItems; pIter->More(); pIter = &pIter->Tail())
  {
    aCombined[0] = std::hash<occ::handle<TopLoc_Datum3D>>{}(pIter->Value().myDatum);
    aCombined[1] = opencascade::hash(pIter->Value().myPower);
    aCombined[2] = aHash;
    aHash        = opencascade::hashBytes(aCombined, sizeof(aCombined));
  }
  return aHash;
}

//=================================================================================================

inline bool TopLoc_Location::IsEqual(const TopLoc_Location& theOther) const noexcept
{
  const void** p = (const void**)&myItems;
  const void** q = (const void**)&theOther.myItems;
  if (*p == *q)
  {
    return true;
  }
  // Iterate through both lists in parallel using pointers to avoid Handle copy overhead.
  const TopLoc_SListOfItemLocation* pIter1 = &myItems;
  const TopLoc_SListOfItemLocation* pIter2 = &theOther.myItems;
  while (pIter1->More() && pIter2->More())
  {
    if (pIter1->Value().myDatum != pIter2->Value().myDatum)
    {
      return false;
    }
    if (pIter1->Value().myPower != pIter2->Value().myPower)
    {
      return false;
    }
    pIter1 = &pIter1->Tail();
    pIter2 = &pIter2->Tail();
  }
  // Both lists should be exhausted at the same time for equality.
  return !pIter1->More() && !pIter2->More();
}

//=================================================================================================

inline bool TopLoc_Location::IsDifferent(const TopLoc_Location& theOther) const noexcept
{
  return !IsEqual(theOther);
}

//=================================================================================================

inline void ShallowDump(const TopLoc_Location& me, Standard_OStream& S)
{
  me.ShallowDump(S);
}

namespace std
{
template <>
struct hash<TopLoc_Location>
{
  size_t operator()(const TopLoc_Location& theLocation) const noexcept
  {
    return theLocation.HashCode();
  }
};
} // namespace std
