@yorozu/utils 0.1.1 → 0.1.4

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/lib/index.cjs CHANGED
@@ -1382,94 +1382,59 @@ var Deque = class {
1382
1382
  // src/structures/lru-map.ts
1383
1383
  var LruMap = class {
1384
1384
  #capacity;
1385
- #head;
1386
- #tail;
1387
1385
  #map;
1388
- #size = 0;
1389
1386
  constructor(capacity, MapImpl = Map) {
1390
1387
  this.#capacity = capacity;
1391
1388
  this.#map = new MapImpl();
1392
1389
  }
1393
1390
  get size() {
1394
- return this.#size;
1391
+ return this.#map.size;
1395
1392
  }
1396
1393
  get(key) {
1397
- let item = this.#map.get(key);
1398
- if (!item) return void 0;
1399
- this.#markUsed(item);
1400
- return item.value;
1394
+ if (!this.#map.has(key)) return void 0;
1395
+ let value = this.#map.get(key);
1396
+ this.#map.delete(key);
1397
+ this.#map.set(key, value);
1398
+ return value;
1401
1399
  }
1402
1400
  has(key) {
1403
1401
  return this.#map.has(key);
1404
1402
  }
1405
1403
  set(key, value) {
1406
- let item = this.#map.get(key);
1407
- if (item) {
1408
- item.value = value;
1409
- this.#markUsed(item);
1410
- return;
1411
- }
1412
- item = {
1413
- key,
1414
- value,
1415
- next: void 0,
1416
- prev: void 0
1417
- };
1418
- this.#map.set(key, item);
1419
- if (this.#head) {
1420
- this.#head.prev = item;
1421
- item.next = this.#head;
1422
- } else {
1423
- this.#tail = item;
1404
+ if (this.#map.has(key)) {
1405
+ this.#map.delete(key);
1424
1406
  }
1425
- this.#head = item;
1426
- this.#size += 1;
1427
- if (this.#size > this.#capacity) {
1428
- let oldest = this.#tail;
1429
- if (oldest) this.#remove(oldest);
1407
+ this.#map.set(key, value);
1408
+ if (this.#map.size > this.#capacity) {
1409
+ let oldest = this.#map.keys().next().value;
1410
+ if (oldest !== void 0) {
1411
+ this.#map.delete(oldest);
1412
+ }
1430
1413
  }
1431
1414
  }
1432
1415
  delete(key) {
1433
- let item = this.#map.get(key);
1434
- if (item) this.#remove(item);
1416
+ this.#map.delete(key);
1435
1417
  }
1436
1418
  clear() {
1437
1419
  this.#map.clear();
1438
- this.#head = void 0;
1439
- this.#tail = void 0;
1440
- this.#size = 0;
1441
- }
1442
- #markUsed(item) {
1443
- if (item === this.#head) return;
1444
- if (item.prev) {
1445
- if (item === this.#tail) this.#tail = item.prev;
1446
- item.prev.next = item.next;
1447
- }
1448
- if (item.next) item.next.prev = item.prev;
1449
- item.prev = void 0;
1450
- item.next = this.#head;
1451
- if (this.#head) this.#head.prev = item;
1452
- this.#head = item;
1453
- }
1454
- #remove(item) {
1455
- if (item.prev) {
1456
- this.#tail = item.prev;
1457
- this.#tail.next = void 0;
1458
- } else {
1459
- this.#tail = void 0;
1460
- this.#head = void 0;
1461
- }
1462
- item.prev = item.next = void 0;
1463
- this.#map.delete(item.key);
1464
- this.#size -= 1;
1420
+ }
1421
+ *[Symbol.iterator]() {
1422
+ yield* this.#map;
1423
+ }
1424
+ entries() {
1425
+ return this.#map.entries();
1426
+ }
1427
+ keys() {
1428
+ return this.#map.keys();
1429
+ }
1430
+ values() {
1431
+ return this.#map.values();
1465
1432
  }
1466
1433
  };
1467
1434
 
1468
1435
  // src/structures/lru-set.ts
1469
1436
  var LruSet = class {
1470
1437
  #capacity;
1471
- #head;
1472
- #tail;
1473
1438
  #set;
1474
1439
  constructor(capacity, SetImpl = Set) {
1475
1440
  this.#capacity = capacity;
@@ -1479,26 +1444,29 @@ var LruSet = class {
1479
1444
  return this.#set.size;
1480
1445
  }
1481
1446
  add(value) {
1482
- if (this.#set.has(value)) return;
1483
- if (!this.#head) this.#head = { value };
1484
- if (!this.#tail) this.#tail = this.#head;
1485
- else {
1486
- this.#tail.next = { value };
1487
- this.#tail = this.#tail.next;
1447
+ if (this.#set.has(value)) {
1448
+ this.#set.delete(value);
1488
1449
  }
1489
1450
  this.#set.add(value);
1490
- if (this.#set.size > this.#capacity && this.#head !== void 0) {
1491
- this.#set.delete(this.#head.value);
1492
- this.#head = this.#head.next;
1451
+ if (this.#set.size > this.#capacity) {
1452
+ let oldest = this.#set.keys().next().value;
1453
+ if (oldest !== void 0) {
1454
+ this.#set.delete(oldest);
1455
+ }
1493
1456
  }
1494
1457
  }
1495
1458
  has(value) {
1496
1459
  return this.#set.has(value);
1497
1460
  }
1498
1461
  clear() {
1499
- this.#head = this.#tail = void 0;
1500
1462
  this.#set.clear();
1501
1463
  }
1464
+ *[Symbol.iterator]() {
1465
+ yield* this.#set;
1466
+ }
1467
+ toArray() {
1468
+ return Array.from(this.#set);
1469
+ }
1502
1470
  };
1503
1471
 
1504
1472
  // src/not-sync/async-lock.ts
package/lib/index.d.cts CHANGED
@@ -327,21 +327,19 @@ declare class Deque<T> {
327
327
  [Symbol.iterator](): Iterator<T>;
328
328
  }
329
329
 
330
- interface TwoWayLinkedList<K, V> {
331
- key: K;
332
- value: V;
333
- prev?: TwoWayLinkedList<K, V>;
334
- next?: TwoWayLinkedList<K, V>;
335
- }
336
330
  declare class LruMap<K, V> {
337
331
  #private;
338
- constructor(capacity: number, MapImpl?: new () => Map<K, TwoWayLinkedList<K, V>>);
332
+ constructor(capacity: number, MapImpl?: new () => Map<K, V>);
339
333
  get size(): number;
340
334
  get(key: K): V | undefined;
341
335
  has(key: K): boolean;
342
336
  set(key: K, value: V): void;
343
337
  delete(key: K): void;
344
338
  clear(): void;
339
+ [Symbol.iterator](): IterableIterator<[K, V]>;
340
+ entries(): IterableIterator<[K, V]>;
341
+ keys(): IterableIterator<K>;
342
+ values(): IterableIterator<V>;
345
343
  }
346
344
 
347
345
  declare class LruSet<T> {
@@ -351,6 +349,8 @@ declare class LruSet<T> {
351
349
  add(value: T): void;
352
350
  has(value: T): boolean;
353
351
  clear(): void;
352
+ [Symbol.iterator](): IterableIterator<T>;
353
+ toArray(): Array<T>;
354
354
  }
355
355
 
356
356
  declare class AsyncQueue<T> {
package/lib/index.d.ts CHANGED
@@ -327,21 +327,19 @@ declare class Deque<T> {
327
327
  [Symbol.iterator](): Iterator<T>;
328
328
  }
329
329
 
330
- interface TwoWayLinkedList<K, V> {
331
- key: K;
332
- value: V;
333
- prev?: TwoWayLinkedList<K, V>;
334
- next?: TwoWayLinkedList<K, V>;
335
- }
336
330
  declare class LruMap<K, V> {
337
331
  #private;
338
- constructor(capacity: number, MapImpl?: new () => Map<K, TwoWayLinkedList<K, V>>);
332
+ constructor(capacity: number, MapImpl?: new () => Map<K, V>);
339
333
  get size(): number;
340
334
  get(key: K): V | undefined;
341
335
  has(key: K): boolean;
342
336
  set(key: K, value: V): void;
343
337
  delete(key: K): void;
344
338
  clear(): void;
339
+ [Symbol.iterator](): IterableIterator<[K, V]>;
340
+ entries(): IterableIterator<[K, V]>;
341
+ keys(): IterableIterator<K>;
342
+ values(): IterableIterator<V>;
345
343
  }
346
344
 
347
345
  declare class LruSet<T> {
@@ -351,6 +349,8 @@ declare class LruSet<T> {
351
349
  add(value: T): void;
352
350
  has(value: T): boolean;
353
351
  clear(): void;
352
+ [Symbol.iterator](): IterableIterator<T>;
353
+ toArray(): Array<T>;
354
354
  }
355
355
 
356
356
  declare class AsyncQueue<T> {
package/lib/index.js CHANGED
@@ -1297,94 +1297,59 @@ var Deque = class {
1297
1297
  // src/structures/lru-map.ts
1298
1298
  var LruMap = class {
1299
1299
  #capacity;
1300
- #head;
1301
- #tail;
1302
1300
  #map;
1303
- #size = 0;
1304
1301
  constructor(capacity, MapImpl = Map) {
1305
1302
  this.#capacity = capacity;
1306
1303
  this.#map = new MapImpl();
1307
1304
  }
1308
1305
  get size() {
1309
- return this.#size;
1306
+ return this.#map.size;
1310
1307
  }
1311
1308
  get(key) {
1312
- let item = this.#map.get(key);
1313
- if (!item) return void 0;
1314
- this.#markUsed(item);
1315
- return item.value;
1309
+ if (!this.#map.has(key)) return void 0;
1310
+ let value = this.#map.get(key);
1311
+ this.#map.delete(key);
1312
+ this.#map.set(key, value);
1313
+ return value;
1316
1314
  }
1317
1315
  has(key) {
1318
1316
  return this.#map.has(key);
1319
1317
  }
1320
1318
  set(key, value) {
1321
- let item = this.#map.get(key);
1322
- if (item) {
1323
- item.value = value;
1324
- this.#markUsed(item);
1325
- return;
1326
- }
1327
- item = {
1328
- key,
1329
- value,
1330
- next: void 0,
1331
- prev: void 0
1332
- };
1333
- this.#map.set(key, item);
1334
- if (this.#head) {
1335
- this.#head.prev = item;
1336
- item.next = this.#head;
1337
- } else {
1338
- this.#tail = item;
1319
+ if (this.#map.has(key)) {
1320
+ this.#map.delete(key);
1339
1321
  }
1340
- this.#head = item;
1341
- this.#size += 1;
1342
- if (this.#size > this.#capacity) {
1343
- let oldest = this.#tail;
1344
- if (oldest) this.#remove(oldest);
1322
+ this.#map.set(key, value);
1323
+ if (this.#map.size > this.#capacity) {
1324
+ let oldest = this.#map.keys().next().value;
1325
+ if (oldest !== void 0) {
1326
+ this.#map.delete(oldest);
1327
+ }
1345
1328
  }
1346
1329
  }
1347
1330
  delete(key) {
1348
- let item = this.#map.get(key);
1349
- if (item) this.#remove(item);
1331
+ this.#map.delete(key);
1350
1332
  }
1351
1333
  clear() {
1352
1334
  this.#map.clear();
1353
- this.#head = void 0;
1354
- this.#tail = void 0;
1355
- this.#size = 0;
1356
- }
1357
- #markUsed(item) {
1358
- if (item === this.#head) return;
1359
- if (item.prev) {
1360
- if (item === this.#tail) this.#tail = item.prev;
1361
- item.prev.next = item.next;
1362
- }
1363
- if (item.next) item.next.prev = item.prev;
1364
- item.prev = void 0;
1365
- item.next = this.#head;
1366
- if (this.#head) this.#head.prev = item;
1367
- this.#head = item;
1368
- }
1369
- #remove(item) {
1370
- if (item.prev) {
1371
- this.#tail = item.prev;
1372
- this.#tail.next = void 0;
1373
- } else {
1374
- this.#tail = void 0;
1375
- this.#head = void 0;
1376
- }
1377
- item.prev = item.next = void 0;
1378
- this.#map.delete(item.key);
1379
- this.#size -= 1;
1335
+ }
1336
+ *[Symbol.iterator]() {
1337
+ yield* this.#map;
1338
+ }
1339
+ entries() {
1340
+ return this.#map.entries();
1341
+ }
1342
+ keys() {
1343
+ return this.#map.keys();
1344
+ }
1345
+ values() {
1346
+ return this.#map.values();
1380
1347
  }
1381
1348
  };
1382
1349
 
1383
1350
  // src/structures/lru-set.ts
1384
1351
  var LruSet = class {
1385
1352
  #capacity;
1386
- #head;
1387
- #tail;
1388
1353
  #set;
1389
1354
  constructor(capacity, SetImpl = Set) {
1390
1355
  this.#capacity = capacity;
@@ -1394,26 +1359,29 @@ var LruSet = class {
1394
1359
  return this.#set.size;
1395
1360
  }
1396
1361
  add(value) {
1397
- if (this.#set.has(value)) return;
1398
- if (!this.#head) this.#head = { value };
1399
- if (!this.#tail) this.#tail = this.#head;
1400
- else {
1401
- this.#tail.next = { value };
1402
- this.#tail = this.#tail.next;
1362
+ if (this.#set.has(value)) {
1363
+ this.#set.delete(value);
1403
1364
  }
1404
1365
  this.#set.add(value);
1405
- if (this.#set.size > this.#capacity && this.#head !== void 0) {
1406
- this.#set.delete(this.#head.value);
1407
- this.#head = this.#head.next;
1366
+ if (this.#set.size > this.#capacity) {
1367
+ let oldest = this.#set.keys().next().value;
1368
+ if (oldest !== void 0) {
1369
+ this.#set.delete(oldest);
1370
+ }
1408
1371
  }
1409
1372
  }
1410
1373
  has(value) {
1411
1374
  return this.#set.has(value);
1412
1375
  }
1413
1376
  clear() {
1414
- this.#head = this.#tail = void 0;
1415
1377
  this.#set.clear();
1416
1378
  }
1379
+ *[Symbol.iterator]() {
1380
+ yield* this.#set;
1381
+ }
1382
+ toArray() {
1383
+ return Array.from(this.#set);
1384
+ }
1417
1385
  };
1418
1386
 
1419
1387
  // src/not-sync/async-lock.ts
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yorozu/utils",
3
- "version": "0.1.1",
3
+ "version": "0.1.4",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "author": "inshinrei",