Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-28 09:19:37

0001 // Copyright (c) 2026 OPEN CASCADE SAS
0002 //
0003 // This file is part of Open CASCADE Technology software library.
0004 //
0005 // This library is free software; you can redistribute it and/or modify it under
0006 // the terms of the GNU Lesser General Public License version 2.1 as published
0007 // by the Free Software Foundation, with special exception defined in the file
0008 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
0009 // distribution for complete text of the license and disclaimer of any warranty.
0010 //
0011 // Alternatively, this file may be used under the terms of Open CASCADE
0012 // commercial license or contractual agreement.
0013 
0014 #ifndef _BRepGraph_UID_HeaderFile
0015 #define _BRepGraph_UID_HeaderFile
0016 
0017 #include <BRepGraph_NodeId.hxx>
0018 #include <Standard_Assert.hxx>
0019 #include <Standard_HashUtils.hxx>
0020 
0021 #include <cstddef>
0022 #include <cstdint>
0023 #include <functional>
0024 #include <limits>
0025 
0026 //! Unique definition-node identifier within a BRepGraph.
0027 //!
0028 //! Identity = (Kind, Counter). Two nodes of different kinds may share a
0029 //! counter value but their UIDs are distinct.  Within one kind, counter
0030 //! values never repeat (monotonic, never resets).
0031 //!
0032 //! Trivially copyable, cheap to pass by value.
0033 struct BRepGraph_UID
0034 {
0035   BRepGraph_NodeId::Kind Kind    = BRepGraph_NodeId::Kind::Solid;
0036   uint32_t               Counter = 0;
0037 
0038   //! Default: invalid UID (counter = 0 is the invalid sentinel).
0039   BRepGraph_UID() = default;
0040 
0041   //! Construct a valid UID.  Called internally by BRepGraphInc_Storage::AllocateNodeUID().
0042   //! @pre theCounter > 0 (counter = 0 is reserved as the invalid sentinel)
0043   BRepGraph_UID(const BRepGraph_NodeId::Kind theKind, const uint32_t theCounter)
0044       : Kind(theKind),
0045         Counter(theCounter)
0046   {
0047   }
0048 
0049   //! Factory: returns an explicitly invalid UID.
0050   static BRepGraph_UID Invalid() { return BRepGraph_UID(); }
0051 
0052   //! True if this UID has a valid kind and a non-zero counter.
0053   [[nodiscard]] bool IsValid() const { return Counter > 0 && BRepGraph_NodeId::IsValidKind(Kind); }
0054 
0055   [[nodiscard]] bool IsTopology() const
0056   {
0057     return IsValid() && BRepGraph_NodeId::IsTopologyKind(Kind);
0058   }
0059 
0060   [[nodiscard]] bool IsAssembly() const
0061   {
0062     return IsValid() && BRepGraph_NodeId::IsAssemblyKind(Kind);
0063   }
0064 
0065   //! Equality: Identity = (Kind, Counter). Two invalid UIDs are equal.
0066   friend bool operator==(const BRepGraph_UID& theLeft, const BRepGraph_UID& theRight) noexcept
0067   {
0068     if (theLeft.Counter == 0 || theRight.Counter == 0)
0069     {
0070       return (theLeft.Counter == 0) == (theRight.Counter == 0);
0071     }
0072     return theLeft.Kind == theRight.Kind && theLeft.Counter == theRight.Counter;
0073   }
0074 
0075   friend bool operator!=(const BRepGraph_UID& theLeft, const BRepGraph_UID& theRight) noexcept
0076   {
0077     return !(theLeft == theRight);
0078   }
0079 
0080   friend bool operator<(const BRepGraph_UID& theLeft, const BRepGraph_UID& theRight) noexcept
0081   {
0082     if (theLeft.Kind != theRight.Kind)
0083     {
0084       return static_cast<int>(theLeft.Kind) < static_cast<int>(theRight.Kind);
0085     }
0086     return theLeft.Counter < theRight.Counter;
0087   }
0088 
0089   //! Hash value compatible with operator==.
0090   [[nodiscard]] size_t HashValue() const noexcept
0091   {
0092     if (Counter == 0)
0093     {
0094       return opencascade::hash(0);
0095     }
0096     size_t aCombination[2];
0097     aCombination[0] = opencascade::hash(static_cast<int>(Kind));
0098     aCombination[1] = opencascade::hash(Counter);
0099     return opencascade::hashBytes(aCombination, sizeof(aCombination));
0100   }
0101 };
0102 
0103 static_assert(sizeof(BRepGraph_UID) <= 8, "BRepGraph_UID must stay compact");
0104 
0105 //! std::hash specialization for NCollection_DefaultHasher support.
0106 template <>
0107 struct std::hash<BRepGraph_UID>
0108 {
0109   size_t operator()(const BRepGraph_UID& theUID) const noexcept { return theUID.HashValue(); }
0110 };
0111 
0112 #endif // _BRepGraph_UID_HeaderFile