utilium 1.2.7 → 1.2.9

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.
package/dist/cache.d.ts CHANGED
@@ -38,7 +38,7 @@ export declare class Resource<ID> {
38
38
  /** The resource ID */
39
39
  readonly id: ID;
40
40
  /** The full size of the resource */
41
- readonly size: number;
41
+ size: number;
42
42
  protected readonly options: Options;
43
43
  /** Regions used to reduce unneeded allocations. Think of sparse arrays. */
44
44
  readonly regions: Region[];
@@ -47,10 +47,19 @@ export declare class Resource<ID> {
47
47
  id: ID,
48
48
  /** The full size of the resource */
49
49
  size: number, options: Options, resources?: Map<ID, Resource<ID> | undefined>);
50
+ /**
51
+ * Ensure the full size of the resource is *at least* `newSize`
52
+ */
53
+ grow(newSize: number): void;
50
54
  /** Combines adjacent regions and combines adjacent ranges within a region */
51
55
  collect(): void;
52
56
  /** Takes an initial range and finds the sub-ranges that are not in the cache */
53
57
  missing(start: number, end: number): Range[];
58
+ /**
59
+ * Get the cached sub-ranges of an initial range.
60
+ * This is conceptually the inverse of `missing`.
61
+ */
62
+ cached(start: number, end: number): Range[];
54
63
  /** Get the region who's ranges include an offset */
55
64
  regionAt(offset: number): Region | undefined;
56
65
  /** Add new data to the cache at given specified offset */
package/dist/cache.js CHANGED
@@ -23,6 +23,12 @@ export class Resource {
23
23
  this.regions.push({ offset: 0, data: new Uint8Array(size), ranges: [] });
24
24
  resources?.set(id, this);
25
25
  }
26
+ /**
27
+ * Ensure the full size of the resource is *at least* `newSize`
28
+ */
29
+ grow(newSize) {
30
+ this.size = Math.max(this.size, newSize);
31
+ }
26
32
  /** Combines adjacent regions and combines adjacent ranges within a region */
27
33
  collect() {
28
34
  if (!this.options.sparse)
@@ -83,6 +89,39 @@ export class Resource {
83
89
  missingRanges.push({ start, end });
84
90
  return missingRanges;
85
91
  }
92
+ /**
93
+ * Get the cached sub-ranges of an initial range.
94
+ * This is conceptually the inverse of `missing`.
95
+ */
96
+ cached(start, end) {
97
+ const cachedRanges = [];
98
+ for (const region of this.regions) {
99
+ if (region.offset >= end)
100
+ break;
101
+ for (const range of region.ranges) {
102
+ if (range.end <= start)
103
+ continue;
104
+ if (range.start >= end)
105
+ break;
106
+ cachedRanges.push({
107
+ start: Math.max(start, range.start),
108
+ end: Math.min(end, range.end),
109
+ });
110
+ }
111
+ }
112
+ cachedRanges.sort((a, b) => a.start - b.start);
113
+ const merged = [];
114
+ for (const curr of cachedRanges) {
115
+ const last = merged.at(-1);
116
+ if (last && curr.start <= last.end) {
117
+ last.end = Math.max(last.end, curr.end);
118
+ }
119
+ else {
120
+ merged.push(curr);
121
+ }
122
+ }
123
+ return merged;
124
+ }
86
125
  /** Get the region who's ranges include an offset */
87
126
  regionAt(offset) {
88
127
  if (!this.regions.length)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "utilium",
3
- "version": "1.2.7",
3
+ "version": "1.2.9",
4
4
  "description": "Typescript utilities",
5
5
  "funding": {
6
6
  "type": "individual",
package/src/cache.ts CHANGED
@@ -49,7 +49,7 @@ export class Resource<ID> {
49
49
  /** The resource ID */
50
50
  public readonly id: ID,
51
51
  /** The full size of the resource */
52
- public readonly size: number,
52
+ public size: number,
53
53
  protected readonly options: Options,
54
54
  resources?: Map<ID, Resource<ID> | undefined>
55
55
  ) {
@@ -59,6 +59,13 @@ export class Resource<ID> {
59
59
  resources?.set(id, this);
60
60
  }
61
61
 
62
+ /**
63
+ * Ensure the full size of the resource is *at least* `newSize`
64
+ */
65
+ public grow(newSize: number) {
66
+ this.size = Math.max(this.size, newSize);
67
+ }
68
+
62
69
  /** Combines adjacent regions and combines adjacent ranges within a region */
63
70
  public collect(): void {
64
71
  if (!this.options.sparse) return;
@@ -105,7 +112,6 @@ export class Resource<ID> {
105
112
 
106
113
  for (const range of region.ranges) {
107
114
  if (range.end <= start) continue;
108
-
109
115
  if (range.start >= end) break;
110
116
 
111
117
  if (range.start > start) {
@@ -127,6 +133,41 @@ export class Resource<ID> {
127
133
  return missingRanges;
128
134
  }
129
135
 
136
+ /**
137
+ * Get the cached sub-ranges of an initial range.
138
+ * This is conceptually the inverse of `missing`.
139
+ */
140
+ public cached(start: number, end: number): Range[] {
141
+ const cachedRanges: Range[] = [];
142
+
143
+ for (const region of this.regions) {
144
+ if (region.offset >= end) break;
145
+
146
+ for (const range of region.ranges) {
147
+ if (range.end <= start) continue;
148
+ if (range.start >= end) break;
149
+
150
+ cachedRanges.push({
151
+ start: Math.max(start, range.start),
152
+ end: Math.min(end, range.end),
153
+ });
154
+ }
155
+ }
156
+
157
+ cachedRanges.sort((a, b) => a.start - b.start);
158
+ const merged: Range[] = [];
159
+ for (const curr of cachedRanges) {
160
+ const last = merged.at(-1);
161
+ if (last && curr.start <= last.end) {
162
+ last.end = Math.max(last.end, curr.end);
163
+ } else {
164
+ merged.push(curr);
165
+ }
166
+ }
167
+
168
+ return merged;
169
+ }
170
+
130
171
  /** Get the region who's ranges include an offset */
131
172
  public regionAt(offset: number): Region | undefined {
132
173
  if (!this.regions.length) return;