Skip to content

Commit cc59fc4

Browse files
committed
Mark triangulation classes as final
Prior to their conversion to pybind11 classes in #24522, these types had `tp_flags = Py_TPFLAGS_DEFAULT`, meaning they did not have the `Py_TPFLAGS_BASETYPE` flag and could not be subtyped. As these are internal classes, I don't believe this was intentionally changed, so restore that flag. Additionally, since we require C++17 now, mark the C++ classes themselves as `final`. I believe this really only makes a difference if we have `virtual` methods (which we don't here), but that doesn't mean the compiler can't infer some other optimizations with this information.
1 parent 4ebc8ce commit cc59fc4

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

lib/matplotlib/_tri.pyi

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
# This is a private module implemented in C++
22
# As such these type stubs are overly generic, but here to allow these types
33
# as return types for public methods
4-
from typing import Any
4+
from typing import Any, final
55

6+
@final
67
class TrapezoidMapTriFinder:
78
def __init__(self, *args, **kwargs) -> None: ...
89
def find_many(self, *args, **kwargs) -> Any: ...
910
def get_tree_stats(self, *args, **kwargs) -> Any: ...
1011
def initialize(self, *args, **kwargs) -> Any: ...
1112
def print_tree(self, *args, **kwargs) -> Any: ...
1213

14+
@final
1315
class TriContourGenerator:
1416
def __init__(self, *args, **kwargs) -> None: ...
1517
def create_contour(self, *args, **kwargs) -> Any: ...
1618
def create_filled_contour(self, *args, **kwargs) -> Any: ...
1719

20+
@final
1821
class Triangulation:
1922
def __init__(self, *args, **kwargs) -> None: ...
2023
def calculate_plane_coefficients(self, *args, **kwargs) -> Any: ...

src/tri/_tri.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ namespace py = pybind11;
7878
/* An edge of a triangle consisting of an triangle index in the range 0 to
7979
* ntri-1 and an edge index in the range 0 to 2. Edge i goes from the
8080
* triangle's point i to point (i+1)%3. */
81-
struct TriEdge
81+
struct TriEdge final
8282
{
8383
TriEdge();
8484
TriEdge(int tri_, int edge_);
@@ -111,7 +111,7 @@ struct XY
111111
};
112112

113113
// 3D point with x,y,z coordinates.
114-
struct XYZ
114+
struct XYZ final
115115
{
116116
XYZ(const double& x_, const double& y_, const double& z_);
117117
XYZ cross(const XYZ& other) const;
@@ -123,7 +123,7 @@ struct XYZ
123123
};
124124

125125
// 2D bounding box, which may be empty.
126-
class BoundingBox
126+
class BoundingBox final
127127
{
128128
public:
129129
BoundingBox();
@@ -138,7 +138,7 @@ class BoundingBox
138138
/* A single line of a contour, which may be a closed line loop or an open line
139139
* strip. Identical adjacent points are avoided using push_back(), and a closed
140140
* line loop should also not have identical first and last points. */
141-
class ContourLine : public std::vector<XY>
141+
class ContourLine final : public std::vector<XY>
142142
{
143143
public:
144144
ContourLine();
@@ -157,7 +157,7 @@ void write_contour(const Contour& contour);
157157

158158
/* Triangulation with npoints points and ntri triangles. Derived fields are
159159
* calculated when they are first needed. */
160-
class Triangulation
160+
class Triangulation final
161161
{
162162
public:
163163
typedef py::array_t<double, py::array::c_style | py::array::forcecast> CoordinateArray;
@@ -254,7 +254,7 @@ class Triangulation
254254

255255
private:
256256
// An edge of a triangulation, composed of start and end point indices.
257-
struct Edge
257+
struct Edge final
258258
{
259259
Edge() : start(-1), end(-1) {}
260260
Edge(int start_, int end_) : start(start_), end(end_) {}
@@ -267,7 +267,7 @@ class Triangulation
267267
/* An edge of a boundary of a triangulation, composed of a boundary index
268268
* and an edge index within that boundary. Used to index into the
269269
* boundaries collection to obtain the corresponding TriEdge. */
270-
struct BoundaryEdge
270+
struct BoundaryEdge final
271271
{
272272
BoundaryEdge() : boundary(-1), edge(-1) {}
273273
BoundaryEdge(int boundary_, int edge_)
@@ -328,7 +328,7 @@ class Triangulation
328328

329329

330330
// Contour generator for a triangulation.
331-
class TriContourGenerator
331+
class TriContourGenerator final
332332
{
333333
public:
334334
typedef Triangulation::CoordinateArray CoordinateArray;
@@ -508,7 +508,7 @@ class TriContourGenerator
508508
* colinear points but only in the simplest of cases. No explicit testing of
509509
* the validity of the triangulation is performed as this is a computationally
510510
* more complex task than the trifinding itself. */
511-
class TrapezoidMapTriFinder
511+
class TrapezoidMapTriFinder final
512512
{
513513
public:
514514
typedef Triangulation::CoordinateArray CoordinateArray;
@@ -551,7 +551,7 @@ class TrapezoidMapTriFinder
551551
/* A Point consists of x,y coordinates as well as the index of a triangle
552552
* associated with the point, so that a search at this point's coordinates
553553
* can return a valid triangle index. */
554-
struct Point : XY
554+
struct Point final : XY
555555
{
556556
Point() : XY(), tri(-1) {}
557557
Point(const double& x, const double& y) : XY(x,y), tri(-1) {}
@@ -565,7 +565,7 @@ class TrapezoidMapTriFinder
565565
* the Edge which are used to map from trapezoid to triangle index. Also
566566
* stores pointers to the 3rd points of the below and above triangles,
567567
* which are only used to disambiguate triangles with colinear points. */
568-
struct Edge
568+
struct Edge final
569569
{
570570
Edge(const Point* left_,
571571
const Point* right_,
@@ -608,7 +608,7 @@ class TrapezoidMapTriFinder
608608
class Node; // Forward declaration.
609609

610610
// Helper structure used by TrapezoidMapTriFinder::get_tree_stats.
611-
struct NodeStats
611+
struct NodeStats final
612612
{
613613
NodeStats()
614614
: node_count(0), trapezoid_count(0), max_parent_count(0),
@@ -632,7 +632,7 @@ class TrapezoidMapTriFinder
632632
* The parent collection acts as a reference count to the number of times
633633
* a Node occurs in the search tree. When the parent count is reduced to
634634
* zero a Node can be safely deleted. */
635-
class Node
635+
class Node final
636636
{
637637
public:
638638
Node(const Point* point, Node* left, Node* right);// Type_XNode.
@@ -717,7 +717,7 @@ class TrapezoidMapTriFinder
717717
* To obtain the index of the triangle corresponding to a particular
718718
* Trapezoid, use the Edge member variables below.triangle_above or
719719
* above.triangle_below. */
720-
struct Trapezoid
720+
struct Trapezoid final
721721
{
722722
Trapezoid(const Point* left_,
723723
const Point* right_,

src/tri/_tri_wrapper.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
using namespace pybind11::literals;
44

55
PYBIND11_MODULE(_tri, m) {
6-
py::class_<Triangulation>(m, "Triangulation")
6+
py::class_<Triangulation>(m, "Triangulation", py::is_final())
77
.def(py::init<const Triangulation::CoordinateArray&,
88
const Triangulation::CoordinateArray&,
99
const Triangulation::TriangleArray&,
@@ -30,7 +30,7 @@ PYBIND11_MODULE(_tri, m) {
3030
.def("set_mask", &Triangulation::set_mask,
3131
"Set or clear the mask array.");
3232

33-
py::class_<TriContourGenerator>(m, "TriContourGenerator")
33+
py::class_<TriContourGenerator>(m, "TriContourGenerator", py::is_final())
3434
.def(py::init<Triangulation&,
3535
const TriContourGenerator::CoordinateArray&>(),
3636
"triangulation"_a,
@@ -43,7 +43,7 @@ PYBIND11_MODULE(_tri, m) {
4343
.def("create_filled_contour", &TriContourGenerator::create_filled_contour,
4444
"Create and return a filled contour.");
4545

46-
py::class_<TrapezoidMapTriFinder>(m, "TrapezoidMapTriFinder")
46+
py::class_<TrapezoidMapTriFinder>(m, "TrapezoidMapTriFinder", py::is_final())
4747
.def(py::init<Triangulation&>(),
4848
"triangulation"_a,
4949
"Create a new C++ TrapezoidMapTriFinder object.\n"

0 commit comments

Comments
 (0)