-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAsArrays.sol
More file actions
285 lines (249 loc) · 8.15 KB
/
AsArrays.sol
File metadata and controls
285 lines (249 loc) · 8.15 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
import "./AsCast.sol";
/**
* _ _ _
* __ _ ___| |_ _ __ ___ | | __ _| |__
* / ` / __| _| '__/ \| |/ ` | ' \
* | O \__ \ |_| | | O | | O | O |
* \__,_|___/.__|_| \___/|_|\__,_|_.__/ ©️ 2024
*
* @title AsArrays Library - Astrolab's Array manipulation library
* @author Astrolab DAO
*/
library AsArrays {
using AsCast for address;
/*═══════════════════════════════════════════════════════════════╗
║ VIEWS ║
╚═══════════════════════════════════════════════════════════════*/
/**
* @notice Returns the sum of all elements in the array
* @param self Storage array containing uint256 type variables
* @return value Sum of all elements, does not check for overflow
*/
function sum(uint256[] storage self) public view returns (uint256 value) {
assembly {
let ptr := mload(0x40) // free memory pointer
mstore(ptr, self.slot) // store the array's slot
mstore(0x40, add(ptr, 0x20)) // update the free memory pointer
let len := sload(self.slot) // array length
for { let i := 0 } lt(i, len) { i := add(i, 1) } {
let el := sload(add(keccak256(ptr, 0x20), i)) // load each element
value := add(value, el) // accumulate the sum
}
mstore(0x40, add(ptr, 0x20)) // update the free memory pointer
}
}
/**
* @dev Returns the maximum value in the given array
* @param self Array to find the maximum value from
* @return value Maximum value in the array
*/
function max(uint256[] storage self) public view returns (uint256 value) {
assembly {
let ptr := mload(0x40) // free memory pointer
mstore(ptr, self.slot) // store the array's slot
mstore(0x40, add(ptr, 0x20)) // update the free memory pointer
value := sload(keccak256(ptr, 0x20)) // init max value with the first element
let len := sload(self.slot) // array length
// iterate over the array
for { let i := 1 } lt(i, len) { i := add(i, 1) } {
let el := sload(add(keccak256(ptr, 0x20), i)) // load element
if gt(el, value) { value := el } // update max value
}
}
}
/**
* @dev Returns the minimum value in the given array
* @param self Array to find the minimum value from
* @return value Minimum value in the array
*/
function min(uint256[] storage self) public view returns (uint256 value) {
assembly {
let ptr := mload(0x40) // free memory pointer
mstore(ptr, self.slot) // store the array's slot
mstore(0x40, add(ptr, 0x20)) // update the free memory pointer
value := sload(keccak256(ptr, 0x20)) // init min value with the first element
let len := sload(self.slot) // array length
// iterate over the array
for { let i := 1 } lt(i, len) { i := add(i, 1) } {
let el := sload(add(keccak256(ptr, 0x20), i)) // load element
if lt(el, value) { value := el } // update min value
}
}
}
/**
* @notice Returns a reference to the array
* @param data array to be referenced
* @return ptr reference of the array
*/
function ref(uint256[] memory data) internal pure returns (uint256 ptr) {
assembly {
ptr := data
}
}
/**
* @dev Fills a dynamic array with a specific value
* @param a Value to fill the array with
* @param n Size of the array
* @return arr Filled array
*/
function fill(uint8 a, uint64 n) internal pure returns (uint8[] memory arr) {
arr = new uint8[](n);
for (uint256 i = 0; i < n;) {
arr[i] = a;
unchecked {
i++;
}
}
}
function fill(bytes32 a, uint64 n) internal pure returns (bytes32[] memory arr) {
arr = new bytes32[](n);
for (uint256 i = 0; i < n;) {
arr[i] = a;
unchecked {
i++;
}
}
}
function fill(uint256 a, uint64 n) internal pure returns (uint256[] memory arr) {
arr = new uint256[](n);
for (uint256 i = 0; i < n;) {
arr[i] = a;
unchecked {
i++;
}
}
}
/**
* @dev Converts a value to a one-element array
* @param a Value to convert to an array
* @return arr Resulting array
*/
function toArray(uint8 a) internal pure returns (uint8[] memory arr) {
arr = new uint8[](1);
arr[0] = a;
}
function toArray(uint8 a, uint8 b) internal pure returns (uint8[] memory arr) {
arr = new uint8[](2);
(arr[0], arr[1]) = (a, b);
}
function toArray16(uint16 a) internal pure returns (uint16[] memory arr) {
arr = new uint16[](1);
arr[0] = a;
}
function toArray16(uint16 a, uint16 b) internal pure returns (uint16[] memory arr) {
arr = new uint16[](2);
(arr[0], arr[1]) = (a, b);
}
function toArray16(uint16 a, uint16 b, uint16 c, uint16 d) internal pure returns (uint16[] memory arr) {
arr = new uint16[](4);
(arr[0], arr[1], arr[2], arr[3]) = (a, b, c, d);
}
function toArray(uint256 a) internal pure returns (uint256[] memory arr) {
arr = new uint256[](1);
arr[0] = a;
}
function toArray(uint256 a, uint256 b) internal pure returns (uint256[] memory arr) {
arr = new uint256[](2);
(arr[0], arr[1]) = (a, b);
}
function toArray(
uint256 a,
uint256 b,
uint256 c
) internal pure returns (uint256[] memory arr) {
arr = new uint256[](3);
(arr[0], arr[1], arr[2]) = (a, b, c);
}
function toArray(address a) internal pure returns (address[] memory arr) {
arr = new address[](1);
arr[0] = a;
}
function toArray(address a, address b) internal pure returns (address[] memory arr) {
arr = new address[](2);
(arr[0], arr[1]) = (a, b);
}
function toArray(
address a,
address b,
address c
) internal pure returns (address[] memory arr) {
arr = new address[](3);
(arr[0], arr[1], arr[2]) = (a, b, c);
}
function toBytes32Array(address a) internal pure returns (bytes32[] memory arr) {
arr = new bytes32[](1);
arr[0] = a.toBytes32();
}
function toBytes32Array(
address a,
address b
) internal pure returns (bytes32[] memory arr) {
arr = new bytes32[](2);
(arr[0], arr[1]) = (a.toBytes32(), b.toBytes32());
}
function toBytes32Array(
address a,
address b,
address c
) internal pure returns (bytes32[] memory arr) {
arr = new bytes32[](3);
(arr[0], arr[1], arr[2]) = (a.toBytes32(), b.toBytes32(), c.toBytes32());
}
function toArray(bytes32 a) internal pure returns (bytes32[] memory arr) {
arr = new bytes32[](1);
arr[0] = a;
}
function toArray(bytes32 a, bytes32 b) internal pure returns (bytes32[] memory arr) {
arr = new bytes32[](2);
(arr[0], arr[1]) = (a, b);
}
function toArray(bytes32 a, bytes32 b, bytes32 c) internal pure returns (bytes32[] memory arr) {
arr = new bytes32[](3);
(arr[0], arr[1], arr[2]) = (a, b, c);
}
function toArray(bytes memory a) internal pure returns (bytes[] memory arr) {
arr = new bytes[](1);
arr[0] = a;
}
function toArray(bytes memory a, bytes memory b) internal pure returns (bytes[] memory arr) {
arr = new bytes[](2);
(arr[0], arr[1]) = (a, b);
}
function dynamic(uint256[8] memory fixedArray)
internal
pure
returns (uint256[] memory arr)
{
arr = new uint256[](fixedArray.length);
for (uint256 i = 0; i < fixedArray.length;) {
arr[i] = fixedArray[i];
unchecked {
i++;
}
}
}
function dynamic(uint16[8] memory fixedArray)
internal
pure
returns (uint16[] memory arr)
{
arr = new uint16[](fixedArray.length);
for (uint256 i = 0; i < fixedArray.length;) {
arr[i] = fixedArray[i];
unchecked {
i++;
}
}
}
function dynamic(uint8[8] memory fixedArray) internal pure returns (uint8[] memory arr) {
arr = new uint8[](fixedArray.length);
for (uint256 i = 0; i < fixedArray.length;) {
arr[i] = fixedArray[i];
unchecked {
i++;
}
}
}
}