/
NumberList.sol
222 lines (174 loc) · 7.47 KB
/
NumberList.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
/* Autogenerated file. Do not edit manually. */
// Import schema type
import { SchemaType } from "@latticexyz/schema-type/src/solidity/SchemaType.sol";
// Import store internals
import { IStore } from "@latticexyz/store/src/IStore.sol";
import { StoreSwitch } from "@latticexyz/store/src/StoreSwitch.sol";
import { StoreCore } from "@latticexyz/store/src/StoreCore.sol";
import { Bytes } from "@latticexyz/store/src/Bytes.sol";
import { Memory } from "@latticexyz/store/src/Memory.sol";
import { SliceLib } from "@latticexyz/store/src/Slice.sol";
import { EncodeArray } from "@latticexyz/store/src/tightcoder/EncodeArray.sol";
import { Schema, SchemaLib } from "@latticexyz/store/src/Schema.sol";
import { PackedCounter, PackedCounterLib } from "@latticexyz/store/src/PackedCounter.sol";
bytes32 constant _tableId = bytes32(abi.encodePacked(bytes16(""), bytes16("NumberList")));
bytes32 constant NumberListTableId = _tableId;
library NumberList {
/** Get the table's schema */
function getSchema() internal pure returns (Schema) {
SchemaType[] memory _schema = new SchemaType[](1);
_schema[0] = SchemaType.UINT32_ARRAY;
return SchemaLib.encode(_schema);
}
function getKeySchema() internal pure returns (Schema) {
SchemaType[] memory _schema = new SchemaType[](0);
return SchemaLib.encode(_schema);
}
/** Get the table's metadata */
function getMetadata() internal pure returns (string memory, string[] memory) {
string[] memory _fieldNames = new string[](1);
_fieldNames[0] = "value";
return ("NumberList", _fieldNames);
}
/** Register the table's schema */
function registerSchema() internal {
StoreSwitch.registerSchema(_tableId, getSchema(), getKeySchema());
}
/** Register the table's schema (using the specified store) */
function registerSchema(IStore _store) internal {
_store.registerSchema(_tableId, getSchema(), getKeySchema());
}
/** Set the table's metadata */
function setMetadata() internal {
(string memory _tableName, string[] memory _fieldNames) = getMetadata();
StoreSwitch.setMetadata(_tableId, _tableName, _fieldNames);
}
/** Set the table's metadata (using the specified store) */
function setMetadata(IStore _store) internal {
(string memory _tableName, string[] memory _fieldNames) = getMetadata();
_store.setMetadata(_tableId, _tableName, _fieldNames);
}
/** Get value */
function get() internal view returns (uint32[] memory value) {
bytes32[] memory _keyTuple = new bytes32[](0);
bytes memory _blob = StoreSwitch.getField(_tableId, _keyTuple, 0);
return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32());
}
/** Get value (using the specified store) */
function get(IStore _store) internal view returns (uint32[] memory value) {
bytes32[] memory _keyTuple = new bytes32[](0);
bytes memory _blob = _store.getField(_tableId, _keyTuple, 0);
return (SliceLib.getSubslice(_blob, 0, _blob.length).decodeArray_uint32());
}
/** Set value */
function set(uint32[] memory value) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)));
}
/** Set value (using the specified store) */
function set(IStore _store, uint32[] memory value) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
_store.setField(_tableId, _keyTuple, 0, EncodeArray.encode((value)));
}
/** Get the length of value */
function length() internal view returns (uint256) {
bytes32[] memory _keyTuple = new bytes32[](0);
uint256 _byteLength = StoreSwitch.getFieldLength(_tableId, _keyTuple, 0, getSchema());
unchecked {
return _byteLength / 4;
}
}
/** Get the length of value (using the specified store) */
function length(IStore _store) internal view returns (uint256) {
bytes32[] memory _keyTuple = new bytes32[](0);
uint256 _byteLength = _store.getFieldLength(_tableId, _keyTuple, 0, getSchema());
unchecked {
return _byteLength / 4;
}
}
/**
* Get an item of value
* (unchecked, returns invalid data if index overflows)
*/
function getItem(uint256 _index) internal view returns (uint32) {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _blob = StoreSwitch.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 4, (_index + 1) * 4);
return (uint32(Bytes.slice4(_blob, 0)));
}
}
/**
* Get an item of value (using the specified store)
* (unchecked, returns invalid data if index overflows)
*/
function getItem(IStore _store, uint256 _index) internal view returns (uint32) {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
bytes memory _blob = _store.getFieldSlice(_tableId, _keyTuple, 0, getSchema(), _index * 4, (_index + 1) * 4);
return (uint32(Bytes.slice4(_blob, 0)));
}
}
/** Push an element to value */
function push(uint32 _element) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)));
}
/** Push an element to value (using the specified store) */
function push(IStore _store, uint32 _element) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
_store.pushToField(_tableId, _keyTuple, 0, abi.encodePacked((_element)));
}
/** Pop an element from value */
function pop() internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.popFromField(_tableId, _keyTuple, 0, 4);
}
/** Pop an element from value (using the specified store) */
function pop(IStore _store) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
_store.popFromField(_tableId, _keyTuple, 0, 4);
}
/**
* Update an element of value at `_index`
* (checked only to prevent modifying other tables; can corrupt own data if index overflows)
*/
function update(uint256 _index, uint32 _element) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
StoreSwitch.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)));
}
}
/**
* Update an element of value (using the specified store) at `_index`
* (checked only to prevent modifying other tables; can corrupt own data if index overflows)
*/
function update(IStore _store, uint256 _index, uint32 _element) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
unchecked {
_store.updateInField(_tableId, _keyTuple, 0, _index * 4, abi.encodePacked((_element)));
}
}
/** Tightly pack full data using this table's schema */
function encode(uint32[] memory value) internal pure returns (bytes memory) {
uint40[] memory _counters = new uint40[](1);
_counters[0] = uint40(value.length * 4);
PackedCounter _encodedLengths = PackedCounterLib.pack(_counters);
return abi.encodePacked(_encodedLengths.unwrap(), EncodeArray.encode((value)));
}
/** Encode keys as a bytes32 array using this table's schema */
function encodeKeyTuple() internal pure returns (bytes32[] memory _keyTuple) {
_keyTuple = new bytes32[](0);
}
/* Delete all data for given keys */
function deleteRecord() internal {
bytes32[] memory _keyTuple = new bytes32[](0);
StoreSwitch.deleteRecord(_tableId, _keyTuple);
}
/* Delete all data for given keys (using the specified store) */
function deleteRecord(IStore _store) internal {
bytes32[] memory _keyTuple = new bytes32[](0);
_store.deleteRecord(_tableId, _keyTuple);
}
}