From ef3220b5e2da045dd1a32f48d3239afa5af26a19 Mon Sep 17 00:00:00 2001 From: Jonathan Olson Date: Wed, 1 Mar 2023 14:03:30 -0700 Subject: [PATCH] Poolable => Pool in kite, see https://github.com/phetsims/phet-core/issues/103 --- js/ops/Boundary.js | 14 +++++++---- js/ops/Edge.js | 18 +++++++++----- js/ops/Face.js | 14 +++++++---- js/ops/Graph.js | 56 +++++++++++++++++++++---------------------- js/ops/HalfEdge.js | 14 +++++++---- js/ops/Loop.js | 14 +++++++---- js/ops/SegmentTree.ts | 20 +++++++++------- js/ops/Vertex.js | 14 +++++++---- 8 files changed, 101 insertions(+), 63 deletions(-) diff --git a/js/ops/Boundary.js b/js/ops/Boundary.js index 44fcd85..4761b6d 100644 --- a/js/ops/Boundary.js +++ b/js/ops/Boundary.js @@ -11,7 +11,7 @@ import Bounds2 from '../../../dot/js/Bounds2.js'; import Ray2 from '../../../dot/js/Ray2.js'; import Vector2 from '../../../dot/js/Vector2.js'; import cleanArray from '../../../phet-core/js/cleanArray.js'; -import Poolable from '../../../phet-core/js/Poolable.js'; +import Pool from '../../../phet-core/js/Pool.js'; import { kite, Subpath } from '../imports.js'; let globaId = 0; @@ -20,7 +20,7 @@ class Boundary { /** * @public (kite-internal) * - * NOTE: Use Boundary.createFromPool for most usage instead of using the constructor directly. + * NOTE: Use Boundary.pool.create for most usage instead of using the constructor directly. * * @param {Array.} halfEdges */ @@ -236,10 +236,16 @@ class Boundary { } return new Subpath( segments, null, true ); } + + // @public + freeToPool() { + Boundary.pool.freeToPool( this ); + } + + // @public + static pool = new Pool( Boundary ); } kite.register( 'Boundary', Boundary ); -Poolable.mixInto( Boundary ); - export default Boundary; \ No newline at end of file diff --git a/js/ops/Edge.js b/js/ops/Edge.js index 1654e41..dad5499 100644 --- a/js/ops/Edge.js +++ b/js/ops/Edge.js @@ -6,7 +6,7 @@ * @author Jonathan Olson */ -import Poolable from '../../../phet-core/js/Poolable.js'; +import Pool from '../../../phet-core/js/Pool.js'; import { HalfEdge, kite, Line, Segment, Vertex } from '../imports.js'; let globaId = 0; @@ -15,7 +15,7 @@ class Edge { /** * @public (kite-internal) * - * NOTE: Use Edge.createFromPool for most usage instead of using the constructor directly. + * NOTE: Use Edge.pool.create for most usage instead of using the constructor directly. * * @param {Segment} segment * @param {Vertex} startVertex @@ -58,8 +58,8 @@ class Edge { this.signedAreaFragment = segment.getSignedAreaFragment(); // @public {HalfEdge|null} - Null when disposed (in pool) - this.forwardHalf = HalfEdge.createFromPool( this, false ); - this.reversedHalf = HalfEdge.createFromPool( this, true ); + this.forwardHalf = HalfEdge.pool.create( this, false ); + this.reversedHalf = HalfEdge.pool.create( this, true ); // @public {boolean} - Used for depth-first search this.visited = false; @@ -141,10 +141,16 @@ class Edge { assert && assert( !( this.segment instanceof Line ) || this.startVertex !== this.endVertex, 'No line segments for same vertices' ); } + + // @public + freeToPool() { + Edge.pool.freeToPool( this ); + } + + // @public + static pool = new Pool( Edge ); } kite.register( 'Edge', Edge ); -Poolable.mixInto( Edge ); - export default Edge; \ No newline at end of file diff --git a/js/ops/Face.js b/js/ops/Face.js index 4a09b27..b5734ee 100644 --- a/js/ops/Face.js +++ b/js/ops/Face.js @@ -12,7 +12,7 @@ */ import cleanArray from '../../../phet-core/js/cleanArray.js'; -import Poolable from '../../../phet-core/js/Poolable.js'; +import Pool from '../../../phet-core/js/Pool.js'; import { kite } from '../imports.js'; let globaId = 0; @@ -21,7 +21,7 @@ class Face { /** * @public (kite-internal) * - * NOTE: Use Face.createFromPool for most usage instead of using the constructor directly. + * NOTE: Use Face.pool.create for most usage instead of using the constructor directly. * * @param {Boundary|null} boundary - Null if it's the unbounded face */ @@ -127,10 +127,16 @@ class Face { this.recursivelyAddHoles( outerBoundary.childBoundaries[ i ] ); } } + + // @public + freeToPool() { + Face.pool.freeToPool( this ); + } + + // @public + static pool = new Pool( Face ); } kite.register( 'Face', Face ); -Poolable.mixInto( Face ); - export default Face; \ No newline at end of file diff --git a/js/ops/Graph.js b/js/ops/Graph.js index 4a209ec..e35d659 100644 --- a/js/ops/Graph.js +++ b/js/ops/Graph.js @@ -59,7 +59,7 @@ class Graph { this.loops = []; // @public {Face} - this.unboundedFace = Face.createFromPool( null ); + this.unboundedFace = Face.pool.create( null ); // @public {Array.} this.faces = [ this.unboundedFace ]; @@ -142,7 +142,7 @@ class Graph { } ); graph.boundaries = obj.boundaries.map( data => { - const boundary = new Boundary( data.halfEdges.map( id => halfEdgeMap[ id ] ) ); + const boundary = Boundary.pool.create( data.halfEdges.map( id => halfEdgeMap[ id ] ) ); boundaryMap[ data.id ] = boundary; boundary.signedArea = data.signedArea; boundary.bounds = Bounds2.Bounds2IO.fromStateObject( data.bounds ); @@ -249,27 +249,27 @@ class Graph { // If they are exactly equal, don't take a chance on floating-point arithmetic if ( start.equals( end ) ) { - vertices.push( Vertex.createFromPool( start ) ); + vertices.push( Vertex.pool.create( start ) ); } else { assert && assert( start.distance( end ) < 1e-5, 'Inaccurate start/end points' ); - vertices.push( Vertex.createFromPool( start.average( end ) ) ); + vertices.push( Vertex.pool.create( start.average( end ) ) ); } } if ( !closed ) { // If we aren't closed, create an "end" vertex since it may be different from the "start" - vertices.push( Vertex.createFromPool( segments[ segments.length - 1 ].end ) ); + vertices.push( Vertex.pool.create( segments[ segments.length - 1 ].end ) ); } // Create the loop object from the vertices, filling in edges - const loop = Loop.createFromPool( shapeId, closed ); + const loop = Loop.pool.create( shapeId, closed ); for ( index = 0; index < segments.length; index++ ) { let nextIndex = index + 1; if ( closed && nextIndex === segments.length ) { nextIndex = 0; } - const edge = Edge.createFromPool( segments[ index ], vertices[ index ], vertices[ nextIndex ] ); + const edge = Edge.pool.create( segments[ index ], vertices[ index ], vertices[ nextIndex ] ); loop.halfEdges.push( edge.forwardHalf ); this.addEdge( edge ); } @@ -381,19 +381,19 @@ class Graph { const edge = this.edges[ i ]; if ( edge.forwardHalf.face.filled !== edge.reversedHalf.face.filled ) { if ( !vertexMap[ edge.startVertex.id ] ) { - const newStartVertex = Vertex.createFromPool( edge.startVertex.point ); + const newStartVertex = Vertex.pool.create( edge.startVertex.point ); graph.vertices.push( newStartVertex ); vertexMap[ edge.startVertex.id ] = newStartVertex; } if ( !vertexMap[ edge.endVertex.id ] ) { - const newEndVertex = Vertex.createFromPool( edge.endVertex.point ); + const newEndVertex = Vertex.pool.create( edge.endVertex.point ); graph.vertices.push( newEndVertex ); vertexMap[ edge.endVertex.id ] = newEndVertex; } const startVertex = vertexMap[ edge.startVertex.id ]; const endVertex = vertexMap[ edge.endVertex.id ]; - graph.addEdge( Edge.createFromPool( edge.segment, startVertex, endVertex ) ); + graph.addEdge( Edge.pool.create( edge.segment, startVertex, endVertex ) ); } } @@ -735,7 +735,7 @@ class Graph { let beforeVertex; if ( aBefore && bBefore ) { - beforeVertex = Vertex.createFromPool( middle.start ); + beforeVertex = Vertex.pool.create( middle.start ); this.vertices.push( beforeVertex ); } else if ( aBefore ) { @@ -747,7 +747,7 @@ class Graph { let afterVertex; if ( aAfter && bAfter ) { - afterVertex = Vertex.createFromPool( middle.end ); + afterVertex = Vertex.pool.create( middle.end ); this.vertices.push( afterVertex ); } else if ( aAfter ) { @@ -757,7 +757,7 @@ class Graph { afterVertex = aEdge.endVertex; } - const middleEdge = Edge.createFromPool( middle, beforeVertex, afterVertex ); + const middleEdge = Edge.pool.create( middle, beforeVertex, afterVertex ); newEdges.push( middleEdge ); let aBeforeEdge; @@ -767,19 +767,19 @@ class Graph { // Add "leftover" edges if ( aBefore ) { - aBeforeEdge = Edge.createFromPool( aBefore, aEdge.startVertex, beforeVertex ); + aBeforeEdge = Edge.pool.create( aBefore, aEdge.startVertex, beforeVertex ); newEdges.push( aBeforeEdge ); } if ( aAfter ) { - aAfterEdge = Edge.createFromPool( aAfter, afterVertex, aEdge.endVertex ); + aAfterEdge = Edge.pool.create( aAfter, afterVertex, aEdge.endVertex ); newEdges.push( aAfterEdge ); } if ( bBefore ) { - bBeforeEdge = Edge.createFromPool( bBefore, bEdge.startVertex, overlap.a > 0 ? beforeVertex : afterVertex ); + bBeforeEdge = Edge.pool.create( bBefore, bEdge.startVertex, overlap.a > 0 ? beforeVertex : afterVertex ); newEdges.push( bBeforeEdge ); } if ( bAfter ) { - bAfterEdge = Edge.createFromPool( bAfter, overlap.a > 0 ? afterVertex : beforeVertex, bEdge.endVertex ); + bAfterEdge = Edge.pool.create( bAfter, overlap.a > 0 ? afterVertex : beforeVertex, bEdge.endVertex ); newEdges.push( bAfterEdge ); } @@ -830,12 +830,12 @@ class Graph { const segments = segment.subdivisions( [ selfIntersection.aT, selfIntersection.bT ] ); - const vertex = Vertex.createFromPool( selfIntersection.point ); + const vertex = Vertex.pool.create( selfIntersection.point ); this.vertices.push( vertex ); - const startEdge = Edge.createFromPool( segments[ 0 ], edge.startVertex, vertex ); - const middleEdge = Edge.createFromPool( segments[ 1 ], vertex, vertex ); - const endEdge = Edge.createFromPool( segments[ 2 ], vertex, edge.endVertex ); + const startEdge = Edge.pool.create( segments[ 0 ], edge.startVertex, vertex ); + const middleEdge = Edge.pool.create( segments[ 1 ], vertex, vertex ); + const endEdge = Edge.pool.create( segments[ 2 ], vertex, edge.endVertex ); this.removeEdge( edge ); @@ -1007,7 +1007,7 @@ class Graph { vertex = bT < 0.5 ? bEdge.startVertex : bEdge.endVertex; } else { - vertex = Vertex.createFromPool( point ); + vertex = Vertex.pool.create( point ); this.vertices.push( vertex ); } @@ -1048,8 +1048,8 @@ class Graph { const segments = edge.segment.subdivided( t ); assert && assert( segments.length === 2 ); - const firstEdge = Edge.createFromPool( segments[ 0 ], edge.startVertex, vertex ); - const secondEdge = Edge.createFromPool( segments[ 1 ], vertex, edge.endVertex ); + const firstEdge = Edge.pool.create( segments[ 0 ], edge.startVertex, vertex ); + const secondEdge = Edge.pool.create( segments[ 1 ], vertex, edge.endVertex ); // Remove old connections this.removeEdge( edge ); @@ -1131,7 +1131,7 @@ class Graph { const distance = vertex.point.distance( otherVertex.point ); if ( distance < 1e-5 ) { - const newVertex = Vertex.createFromPool( distance === 0 ? vertex.point : vertex.point.average( otherVertex.point ) ); + const newVertex = Vertex.pool.create( distance === 0 ? vertex.point : vertex.point.average( otherVertex.point ) ); this.vertices.push( newVertex ); arrayRemove( this.vertices, vertex ); @@ -1146,7 +1146,7 @@ class Graph { if ( ( edge.segment.bounds.width > 1e-5 || edge.segment.bounds.height > 1e-5 ) && ( edge.segment instanceof Cubic || edge.segment instanceof Arc || edge.segment instanceof EllipticalArc ) ) { // Replace it with a new edge that is from the vertex to itself - const replacementEdge = Edge.createFromPool( edge.segment, newVertex, newVertex ); + const replacementEdge = Edge.pool.create( edge.segment, newVertex, newVertex ); this.addEdge( replacementEdge ); this.replaceEdgeInLoops( edge, [ replacementEdge.forwardHalf ] ); } @@ -1347,13 +1347,13 @@ class Graph { break; } } - const boundary = Boundary.createFromPool( boundaryHalfEdges ); + const boundary = Boundary.pool.create( boundaryHalfEdges ); ( boundary.signedArea > 0 ? this.innerBoundaries : this.outerBoundaries ).push( boundary ); this.boundaries.push( boundary ); } for ( let i = 0; i < this.innerBoundaries.length; i++ ) { - this.faces.push( Face.createFromPool( this.innerBoundaries[ i ] ) ); + this.faces.push( Face.pool.create( this.innerBoundaries[ i ] ) ); } } diff --git a/js/ops/HalfEdge.js b/js/ops/HalfEdge.js index 3d1d5df..7017f31 100644 --- a/js/ops/HalfEdge.js +++ b/js/ops/HalfEdge.js @@ -8,7 +8,7 @@ */ import Vector2 from '../../../dot/js/Vector2.js'; -import Poolable from '../../../phet-core/js/Poolable.js'; +import Pool from '../../../phet-core/js/Pool.js'; import { kite } from '../imports.js'; let globaId = 0; @@ -17,7 +17,7 @@ class HalfEdge { /** * @public (kite-internal) * - * NOTE: Use HalfEdge.createFromPool for most usage instead of using the constructor directly. + * NOTE: Use HalfEdge.pool.create for most usage instead of using the constructor directly. * * @param {Edge} edge * @param {boolean} isReversed @@ -195,10 +195,16 @@ class HalfEdge { return this.edge.segment; } } + + // @public + freeToPool() { + HalfEdge.pool.freeToPool( this ); + } + + // @public + static pool = new Pool( HalfEdge ); } kite.register( 'HalfEdge', HalfEdge ); -Poolable.mixInto( HalfEdge ); - export default HalfEdge; \ No newline at end of file diff --git a/js/ops/Loop.js b/js/ops/Loop.js index ad72f6e..16adf63 100644 --- a/js/ops/Loop.js +++ b/js/ops/Loop.js @@ -15,7 +15,7 @@ */ import cleanArray from '../../../phet-core/js/cleanArray.js'; -import Poolable from '../../../phet-core/js/Poolable.js'; +import Pool from '../../../phet-core/js/Pool.js'; import { kite, Subpath } from '../imports.js'; let globaId = 0; @@ -24,7 +24,7 @@ class Loop { /** * @public (kite-internal) * - * NOTE: Use Loop.createFromPool for most usage instead of using the constructor directly. + * NOTE: Use Loop.pool.create for most usage instead of using the constructor directly. * * @param {number} shapeId * @param {boolean} closed @@ -102,10 +102,16 @@ class Loop { cleanArray( this.halfEdges ); this.freeToPool(); } + + // @public + freeToPool() { + Loop.pool.freeToPool( this ); + } + + // @public + static pool = new Pool( Loop ); } kite.register( 'Loop', Loop ); -Poolable.mixInto( Loop ); - export default Loop; \ No newline at end of file diff --git a/js/ops/SegmentTree.ts b/js/ops/SegmentTree.ts index 45216f4..f2fc760 100644 --- a/js/ops/SegmentTree.ts +++ b/js/ops/SegmentTree.ts @@ -13,7 +13,7 @@ import arrayRemove from '../../../phet-core/js/arrayRemove.js'; import cleanArray from '../../../phet-core/js/cleanArray.js'; -import Poolable from '../../../phet-core/js/Poolable.js'; +import Pool from '../../../phet-core/js/Pool.js'; import { Edge, kite } from '../imports.js'; let globalId = 1; @@ -39,8 +39,7 @@ export default abstract class SegmentTree implements SegmentInfo { * segments */ public constructor( epsilon = 1e-6 ) { - // @ts-expect-error -- TODO: Poolable support - this.rootNode = SegmentNode.createFromPool( this, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY ); + this.rootNode = SegmentNode.pool.create( this, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY ) as SegmentNode; this.rootNode.isBlack = true; this.epsilon = epsilon; @@ -431,13 +430,11 @@ class SegmentNode { else { this.splitValue = n; - // @ts-expect-error -- TODO: Poolable support - const newLeft = SegmentNode.createFromPool( this.tree, this.min, n ); + const newLeft = SegmentNode.pool.create( this.tree, this.min, n ) as SegmentNode; newLeft.parent = this; this.left = newLeft; - // @ts-expect-error -- TODO: Poolable support - const newRight = SegmentNode.createFromPool( this.tree, n, this.max ); + const newRight = SegmentNode.pool.create( this.tree, n, this.max ) as SegmentNode; newRight.parent = this; this.right = newRight; @@ -561,7 +558,12 @@ class SegmentNode { public toString(): string { return `[${this.min} ${this.max}] split:${this.splitValue} ${this.isBlack ? 'black' : 'red'} ${this.items}`; } -} -Poolable.mixInto( SegmentNode ); + public freeToPool(): void { + SegmentNode.pool.freeToPool( this ); + } + + public static pool = new Pool( SegmentNode ); + +} kite.register( 'SegmentTree', SegmentTree ); diff --git a/js/ops/Vertex.js b/js/ops/Vertex.js index a945144..9b05e1c 100644 --- a/js/ops/Vertex.js +++ b/js/ops/Vertex.js @@ -10,7 +10,7 @@ import Vector2 from '../../../dot/js/Vector2.js'; import cleanArray from '../../../phet-core/js/cleanArray.js'; -import Poolable from '../../../phet-core/js/Poolable.js'; +import Pool from '../../../phet-core/js/Pool.js'; import { kite, Line } from '../imports.js'; let globaId = 0; @@ -19,7 +19,7 @@ class Vertex { /** * @public (kite-internal) * - * NOTE: Use Vertex.createFromPool for most usage instead of using the constructor directly. + * NOTE: Use Vertex.pool.create for most usage instead of using the constructor directly. * * @param {Vector2} point - The point where the vertex should be located. */ @@ -166,10 +166,16 @@ class Vertex { } } } + + // @public + freeToPool() { + Vertex.pool.freeToPool( this ); + } + + // @public + static pool = new Pool( Vertex ); } kite.register( 'Vertex', Vertex ); -Poolable.mixInto( Vertex ); - export default Vertex; \ No newline at end of file