@dcl/ecs 7.24.2 → 7.24.3-28183854529.commit-ed08c4e

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,3 +1,7 @@
1
+ /** Returns a string representation of the given coordinates in the format "x,y" */
2
+ function formatCoord(coord) {
3
+ return `${coord.x},${coord.y}`;
4
+ }
1
5
  /**
2
6
  * Returns true if the given parcels array are connected
3
7
  */
@@ -5,8 +9,8 @@ export function areConnected(parcels) {
5
9
  if (parcels.length === 0) {
6
10
  return false;
7
11
  }
8
- const visited = visitParcel(parcels[0], parcels);
9
- return visited.length === parcels.length;
12
+ const visited = visitConnectedParcels(parcels[0], parcels);
13
+ return visited.size === parcels.length;
10
14
  }
11
15
  /**
12
16
  * Returns true if the given coords are equal
@@ -14,19 +18,38 @@ export function areConnected(parcels) {
14
18
  export function isEqual(p1, p2) {
15
19
  return p1.x === p2.x && p1.y === p2.y;
16
20
  }
17
- function visitParcel(parcel, allParcels, visited = []) {
18
- const isVisited = visited.some((visitedParcel) => isEqual(visitedParcel, parcel));
19
- if (!isVisited) {
20
- visited.push(parcel);
21
- const neighbours = getNeighbours(parcel.x, parcel.y, allParcels);
22
- neighbours.forEach((neighbours) => visitParcel(neighbours, allParcels, visited));
21
+ /**
22
+ * Returns the list of connected parcels starting from the given parcel.
23
+ * @param parcel - The starting parcel to visit
24
+ * @param allParcels - The list of all parcels to consider for connectivity
25
+ * @returns The list of connected parcels starting from the given parcel
26
+ * @remarks This function uses an iterative depth-first search (DFS) approach to avoid blowing the call stack on large connected parcel sets.
27
+ */
28
+ function visitConnectedParcels(parcel, allParcels) {
29
+ const allParcelsSet = new Set(allParcels.map(formatCoord));
30
+ const visitedSet = new Set();
31
+ const stackToVisit = [parcel];
32
+ while (stackToVisit.length > 0) {
33
+ const currentParcel = stackToVisit.pop();
34
+ const key = formatCoord(currentParcel);
35
+ const isVisited = visitedSet.has(key);
36
+ if (!isVisited) {
37
+ visitedSet.add(key);
38
+ const neighbours = getNeighbours(currentParcel.x, currentParcel.y, allParcelsSet);
39
+ for (const n of neighbours) {
40
+ if (!visitedSet.has(formatCoord(n)))
41
+ stackToVisit.push(n);
42
+ }
43
+ }
23
44
  }
24
- return visited;
25
- }
26
- function getIsNeighbourMatcher(x, y) {
27
- return (coords) => (coords.x === x && (coords.y + 1 === y || coords.y - 1 === y)) ||
28
- (coords.y === y && (coords.x + 1 === x || coords.x - 1 === x));
45
+ return visitedSet;
29
46
  }
30
47
  function getNeighbours(x, y, parcels) {
31
- return parcels.filter(getIsNeighbourMatcher(x, y));
48
+ const neighbourCandidates = [
49
+ { x: x + 1, y },
50
+ { x: x - 1, y },
51
+ { x, y: y + 1 },
52
+ { x, y: y - 1 }
53
+ ];
54
+ return neighbourCandidates.filter((c) => parcels.has(formatCoord(c)));
32
55
  }
@@ -1,6 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isEqual = exports.areConnected = void 0;
4
+ /** Returns a string representation of the given coordinates in the format "x,y" */
5
+ function formatCoord(coord) {
6
+ return `${coord.x},${coord.y}`;
7
+ }
4
8
  /**
5
9
  * Returns true if the given parcels array are connected
6
10
  */
@@ -8,8 +12,8 @@ function areConnected(parcels) {
8
12
  if (parcels.length === 0) {
9
13
  return false;
10
14
  }
11
- const visited = visitParcel(parcels[0], parcels);
12
- return visited.length === parcels.length;
15
+ const visited = visitConnectedParcels(parcels[0], parcels);
16
+ return visited.size === parcels.length;
13
17
  }
14
18
  exports.areConnected = areConnected;
15
19
  /**
@@ -19,19 +23,38 @@ function isEqual(p1, p2) {
19
23
  return p1.x === p2.x && p1.y === p2.y;
20
24
  }
21
25
  exports.isEqual = isEqual;
22
- function visitParcel(parcel, allParcels, visited = []) {
23
- const isVisited = visited.some((visitedParcel) => isEqual(visitedParcel, parcel));
24
- if (!isVisited) {
25
- visited.push(parcel);
26
- const neighbours = getNeighbours(parcel.x, parcel.y, allParcels);
27
- neighbours.forEach((neighbours) => visitParcel(neighbours, allParcels, visited));
26
+ /**
27
+ * Returns the list of connected parcels starting from the given parcel.
28
+ * @param parcel - The starting parcel to visit
29
+ * @param allParcels - The list of all parcels to consider for connectivity
30
+ * @returns The list of connected parcels starting from the given parcel
31
+ * @remarks This function uses an iterative depth-first search (DFS) approach to avoid blowing the call stack on large connected parcel sets.
32
+ */
33
+ function visitConnectedParcels(parcel, allParcels) {
34
+ const allParcelsSet = new Set(allParcels.map(formatCoord));
35
+ const visitedSet = new Set();
36
+ const stackToVisit = [parcel];
37
+ while (stackToVisit.length > 0) {
38
+ const currentParcel = stackToVisit.pop();
39
+ const key = formatCoord(currentParcel);
40
+ const isVisited = visitedSet.has(key);
41
+ if (!isVisited) {
42
+ visitedSet.add(key);
43
+ const neighbours = getNeighbours(currentParcel.x, currentParcel.y, allParcelsSet);
44
+ for (const n of neighbours) {
45
+ if (!visitedSet.has(formatCoord(n)))
46
+ stackToVisit.push(n);
47
+ }
48
+ }
28
49
  }
29
- return visited;
30
- }
31
- function getIsNeighbourMatcher(x, y) {
32
- return (coords) => (coords.x === x && (coords.y + 1 === y || coords.y - 1 === y)) ||
33
- (coords.y === y && (coords.x + 1 === x || coords.x - 1 === x));
50
+ return visitedSet;
34
51
  }
35
52
  function getNeighbours(x, y, parcels) {
36
- return parcels.filter(getIsNeighbourMatcher(x, y));
53
+ const neighbourCandidates = [
54
+ { x: x + 1, y },
55
+ { x: x - 1, y },
56
+ { x, y: y + 1 },
57
+ { x, y: y - 1 }
58
+ ];
59
+ return neighbourCandidates.filter((c) => parcels.has(formatCoord(c)));
37
60
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dcl/ecs",
3
3
  "description": "Decentraland ECS",
4
- "version": "7.24.2",
4
+ "version": "7.24.3-28183854529.commit-ed08c4e",
5
5
  "author": "DCL",
6
6
  "bugs": "https://github.com/decentraland/ecs/issues",
7
7
  "files": [
@@ -34,5 +34,5 @@
34
34
  "dependencies": {},
35
35
  "types": "./dist/index.d.ts",
36
36
  "typings": "./dist/index.d.ts",
37
- "commit": "0c395302e3bc05ab4354776769a97ec9be81a741"
37
+ "commit": "ed08c4e2ba560914e13634e28ab7879b70c01070"
38
38
  }