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 _BRepGraphInc_RepId_HeaderFile
0015 #define _BRepGraphInc_RepId_HeaderFile
0016 
0017 #include <Standard_Assert.hxx>
0018 #include <Standard_HashUtils.hxx>
0019 
0020 #include <cstdint>
0021 #include <functional>
0022 #include <limits>
0023 
0024 class BRepGraph;
0025 
0026 //! Lightweight typed index into a per-kind use-record vector inside BRepGraph.
0027 //!
0028 //! The pair (Kind, Index) forms a unique use-record identifier within one graph
0029 //! instance. Default-constructed RepId has Index = UINT32_MAX (invalid).
0030 //!
0031 //! Use records are session-local representation slots with no public stable UID,
0032 //! graph-level lock state, independent mutation generation, or layer callbacks.
0033 //! They do have a soft-removed state so an owner can clear and later reuse its slot.
0034 struct BRepGraph_RepId
0035 {
0036   //! Enumeration of use-record kinds.
0037   enum class Kind : int
0038   {
0039     EdgeCurve3D        = 0, //!< Geom_Curve use for edges
0040     EdgePolygon3D      = 1, //!< Poly_Polygon3D use for edges
0041     CoEdgeCurve2D      = 2, //!< Geom2d_Curve use for coedges
0042     CoEdgePolygon2D    = 3, //!< Poly_Polygon2D use for coedges
0043     CoEdgePolygonOnTri = 4, //!< Poly_PolygonOnTriangulation use for coedges
0044     FaceSurface        = 5, //!< Geom_Surface use for faces
0045     FaceTriangulation  = 6  //!< Poly_Triangulation use for faces
0046   };
0047 
0048   //! True if the kind value is one of the supported use-record kinds.
0049   static bool IsValidKind(const Kind theKind)
0050   {
0051     switch (theKind)
0052     {
0053       case Kind::EdgeCurve3D:
0054       case Kind::EdgePolygon3D:
0055       case Kind::CoEdgeCurve2D:
0056       case Kind::CoEdgePolygon2D:
0057       case Kind::CoEdgePolygonOnTri:
0058       case Kind::FaceSurface:
0059       case Kind::FaceTriangulation:
0060         return true;
0061     }
0062     return false;
0063   }
0064 
0065   //! Compile-time typed wrapper around BRepGraph_RepId.
0066   template <Kind TheKind>
0067   struct Typed
0068   {
0069     static constexpr uint32_t THE_START_INDEX   = 0u;
0070     static constexpr uint32_t THE_INVALID_INDEX = std::numeric_limits<uint32_t>::max();
0071 
0072     uint32_t Index;
0073 
0074     //! Default: invalid.
0075     Typed()
0076         : Index(THE_INVALID_INDEX)
0077     {
0078     }
0079 
0080     //! Construct from index.
0081     explicit Typed(const uint32_t theIdx)
0082         : Index(theIdx)
0083     {
0084     }
0085 
0086     //! First valid id in a dense sequence.
0087     [[nodiscard]] static Typed Start() { return Typed(THE_START_INDEX); }
0088 
0089     //! Invalid sentinel id.
0090     [[nodiscard]] static Typed Invalid() { return Typed(); }
0091 
0092     //! True if this id points to an allocated slot.
0093     [[nodiscard]] bool IsValid() const { return Index != THE_INVALID_INDEX; }
0094 
0095     //! True if this id is within [0, theMaxCount).
0096     [[nodiscard]] bool IsValid(const uint32_t theMaxCount) const
0097     {
0098       return IsValid() && Index < theMaxCount;
0099     }
0100 
0101     //! Implicit conversion to untyped RepId.
0102     operator BRepGraph_RepId() const { return BRepGraph_RepId(TheKind, Index); }
0103 
0104     //! Return true if this use entry has been soft-removed in the given graph.
0105     [[nodiscard]] bool IsRemoved(const BRepGraph& theGraph) const
0106     {
0107       return BRepGraph_RepId(*this).IsRemoved(theGraph);
0108     }
0109 
0110     //! Pre-increment.
0111     Typed& operator++()
0112     {
0113       Standard_ASSERT_VOID(Index != THE_INVALID_INDEX, "pre-increment on invalid use id");
0114       ++Index;
0115       return *this;
0116     }
0117 
0118     //! Post-increment.
0119     Typed operator++(int)
0120     {
0121       Standard_ASSERT_VOID(Index != THE_INVALID_INDEX, "post-increment on invalid use id");
0122       Typed aPrev = *this;
0123       ++Index;
0124       return aPrev;
0125     }
0126 
0127     bool operator==(const Typed& theOther) const { return Index == theOther.Index; }
0128 
0129     bool operator!=(const Typed& theOther) const { return Index != theOther.Index; }
0130 
0131     bool operator<(const Typed& theOther) const { return Index < theOther.Index; }
0132 
0133     bool operator<=(const Typed& theOther) const { return Index <= theOther.Index; }
0134 
0135     bool operator>(const Typed& theOther) const { return Index > theOther.Index; }
0136 
0137     bool operator>=(const Typed& theOther) const { return Index >= theOther.Index; }
0138   };
0139 
0140   static constexpr uint32_t THE_START_INDEX   = 0u;
0141   static constexpr uint32_t THE_INVALID_INDEX = std::numeric_limits<uint32_t>::max();
0142 
0143   Kind     RepKind;
0144   uint32_t Index;
0145 
0146   //! Default: invalid RepId.
0147   BRepGraph_RepId()
0148       : RepKind(Kind::EdgeCurve3D),
0149         Index(THE_INVALID_INDEX)
0150   {
0151   }
0152 
0153   BRepGraph_RepId(const Kind theKind, const uint32_t theIdx)
0154       : RepKind(theKind),
0155         Index(theIdx)
0156   {
0157   }
0158 
0159   //! True if this id points to an allocated slot.
0160   [[nodiscard]] bool IsValid() const { return IsValidKind(RepKind) && Index != THE_INVALID_INDEX; }
0161 
0162   //! True if this id is within [0, theMaxCount).
0163   [[nodiscard]] bool IsValid(const uint32_t theMaxCount) const
0164   {
0165     return IsValid() && Index < theMaxCount;
0166   }
0167 
0168   bool operator==(const BRepGraph_RepId& theOther) const
0169   {
0170     return RepKind == theOther.RepKind && Index == theOther.Index;
0171   }
0172 
0173   bool operator!=(const BRepGraph_RepId& theOther) const { return !(*this == theOther); }
0174 
0175   bool operator<(const BRepGraph_RepId& theOther) const
0176   {
0177     if (RepKind != theOther.RepKind)
0178     {
0179       return static_cast<int>(RepKind) < static_cast<int>(theOther.RepKind);
0180     }
0181     return Index < theOther.Index;
0182   }
0183 
0184   //! Return true if this use entry has been soft-removed in the given graph.
0185   [[nodiscard]] Standard_EXPORT bool IsRemoved(const BRepGraph& theGraph) const;
0186 };
0187 
0188 // Convenience type aliases for typed RepIds.
0189 using BRepGraph_EdgeCurve3DRepId   = BRepGraph_RepId::Typed<BRepGraph_RepId::Kind::EdgeCurve3D>;
0190 using BRepGraph_EdgePolygon3DRepId = BRepGraph_RepId::Typed<BRepGraph_RepId::Kind::EdgePolygon3D>;
0191 using BRepGraph_CoEdgeCurve2DRepId = BRepGraph_RepId::Typed<BRepGraph_RepId::Kind::CoEdgeCurve2D>;
0192 using BRepGraph_CoEdgePolygon2DRepId =
0193   BRepGraph_RepId::Typed<BRepGraph_RepId::Kind::CoEdgePolygon2D>;
0194 using BRepGraph_CoEdgePolygonOnTriRepId =
0195   BRepGraph_RepId::Typed<BRepGraph_RepId::Kind::CoEdgePolygonOnTri>;
0196 using BRepGraph_FaceSurfaceRepId = BRepGraph_RepId::Typed<BRepGraph_RepId::Kind::FaceSurface>;
0197 using BRepGraph_FaceTriangulationRepId =
0198   BRepGraph_RepId::Typed<BRepGraph_RepId::Kind::FaceTriangulation>;
0199 
0200 template <>
0201 struct std::hash<BRepGraph_RepId>
0202 {
0203   size_t operator()(const BRepGraph_RepId& theId) const noexcept
0204   {
0205     size_t aCombination[2];
0206     aCombination[0] = opencascade::hash(static_cast<int>(theId.RepKind));
0207     aCombination[1] = opencascade::hash(theId.Index);
0208     return opencascade::hashBytes(aCombination, sizeof(aCombination));
0209   }
0210 };
0211 
0212 template <BRepGraph_RepId::Kind TheKind>
0213 struct std::hash<BRepGraph_RepId::Typed<TheKind>>
0214 {
0215   size_t operator()(const BRepGraph_RepId::Typed<TheKind>& theId) const noexcept
0216   {
0217     return std::hash<uint32_t>{}(theId.Index);
0218   }
0219 };
0220 
0221 #endif // _BRepGraphInc_RepId_HeaderFile