Skip to content

Commit 0c53121

Browse files
committed
✨ LinkedHashMap追加
1 parent 4e6ed1d commit 0c53121

File tree

10 files changed

+150
-18
lines changed

10 files changed

+150
-18
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,16 @@ Mapが予約語だったので変更
4343
#### HashMap
4444

4545
JavaのHashMapをJavaScriptにそれっぽく似せて作ったやつ
46+
47+
使い道なし
48+
49+
#### TreeMap
50+
51+
JavaのTreeMapをJavaScriptにそれっぽく似せて作ったやつ
52+
53+
使い道なし
54+
55+
#### LinkedHashMap
56+
57+
JavaのLinkedHashMapをJavaScriptにそれっぽく似せて作ったやつ
58+

dist/JavaLibraryScript.js

Lines changed: 66 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.js.map

Lines changed: 8 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/JavaLibraryScript.min.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/util/BaseMap.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class BaseMap extends Interface {
1818
clear: { returns: NoReturn },
1919
containsKey: { args: [NotEmpty], returns: Boolean },
2020
containsValue: { args: [NotEmpty], returns: Boolean },
21-
keySet: { returns: Array },
21+
keys: { returns: Array },
2222
values: { returns: Array },
2323
entrySet: { returns: Array },
2424
};

src/util/HashMap.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class HashMap extends BaseMap {
2020

2121
get(key) {
2222
this._checkKey(key);
23+
if (!this._map.has(key)) return undefined;
2324
return this._data.get(key);
2425
}
2526

@@ -64,7 +65,7 @@ class HashMap extends BaseMap {
6465
return false;
6566
}
6667

67-
keySet() {
68+
keys() {
6869
return Array.from(this._data.keys());
6970
}
7071

@@ -84,12 +85,22 @@ class HashMap extends BaseMap {
8485
return true;
8586
}
8687

88+
forEach(callback, thisArg) {
89+
for (const [key, value] of this._data.entries()) {
90+
callback.call(thisArg, value, key, this);
91+
}
92+
}
93+
8794
toString() {
8895
const data = Array.from(this.entries())
8996
.map(([k, v]) => `${k}=${v}`)
9097
.join(", ");
9198
return `{ ${data} }`;
9299
}
100+
101+
[Symbol.iterator]() {
102+
return this.entries()[Symbol.iterator]();
103+
}
93104
}
94105

95106
module.exports = HashMap;

src/util/LinkedHashMap.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const HashMap = require("./HashMap");
2+
3+
class LinkedHashMap extends HashMap {
4+
constructor(KeyType, ValueType, { accessOrder = false } = {}) {
5+
super(KeyType, ValueType);
6+
this._accessOrder = accessOrder;
7+
}
8+
9+
put(key, value) {
10+
this._checkKey(key);
11+
this._checkValue(value);
12+
13+
if (this._accessOrder && this._data.has(key)) {
14+
this._data.delete(key); // 移動のため一度削除
15+
}
16+
super.put(key, value);
17+
}
18+
19+
get(key) {
20+
const value = super.get(key);
21+
if (this._accessOrder && value !== undefined) {
22+
this._data.delete(key); // 移動のため一度削除
23+
this._data.set(key, value);
24+
}
25+
return value;
26+
}
27+
}
28+
29+
module.exports = LinkedHashMap;

src/util/TreeMap.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
const BaseMap = require("./BaseMap");
21
const HashMap = require("./HashMap");
32

43
class TreeMap extends HashMap {
@@ -48,6 +47,18 @@ class TreeMap extends HashMap {
4847
this._invalidateSortedKeys();
4948
}
5049

50+
keys() {
51+
return this._getSortedKeys().slice();
52+
}
53+
54+
values() {
55+
return this._getSortedKeys().map((k) => this._data.get(k));
56+
}
57+
58+
entrySet() {
59+
return this._getSortedKeys().map((k) => [k, this._data.get(k)]);
60+
}
61+
5162
firstKey() {
5263
const keys = this._getSortedKeys();
5364
return keys.length > 0 ? keys[0] : undefined;
@@ -95,6 +106,12 @@ class TreeMap extends HashMap {
95106
}
96107
return map;
97108
}
109+
110+
forEach(callback, thisArg) {
111+
for (const key of this._getSortedKeys()) {
112+
callback.call(thisArg, this._map.get(key), key, this);
113+
}
114+
}
98115
}
99116

100117
module.exports = TreeMap;

src/util/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
module.exports = {
22
BaseMap: require("./BaseMap.js"),
33
HashMap: require("./HashMap.js"),
4+
LinkedHashMap: require("./LinkedHashMap.js"),
45
TreeMap: require("./TreeMap.js")
56
};

0 commit comments

Comments
 (0)