-
Notifications
You must be signed in to change notification settings - Fork 20
/
UIntArrayUtils.sol
142 lines (129 loc) · 3.45 KB
/
UIntArrayUtils.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
pragma solidity ^0.4.18;
// Functions use i++ because overflowing a uint256 by iteration isn't a concern
library UIntArrayUtils {
function map(uint256[] memory A, function(uint256) pure returns (uint256) fn)
internal
pure
returns (uint256[] memory)
{
uint256[] memory mapped = new uint256[](A.length);
for (uint256 i = 0; i < A.length; i++) {
mapped[i] = fn(A[i]);
}
return mapped;
}
function reduce(uint256[] memory A, function(uint256, uint256) pure returns (uint256) fn)
internal
pure
returns (uint256)
{
uint256 reduced = A[0];
for (uint256 i = 1; i < A.length; i++) {
reduced = fn(reduced, A[i]);
}
return reduced;
}
/**
* Looks through each value in the list, returning an array of all the values that pass a truth test (predicate).
*/
function filter(uint256[] memory A, function(uint256) pure returns (bool) predicate)
internal
pure
returns (uint256[] memory)
{
bool[] memory includeMap = new bool[](A.length);
uint256 count = 0;
for (uint256 i = 0; i < A.length; i++) {
if (predicate(A[i])) {
includeMap[i] = true;
count++;
}
}
uint256[] memory filtered = new uint256[](count);
uint256 j = 0;
for (i = 0; i < A.length; i++) {
if (includeMap[i]) {
filtered[j] = A[i];
j++;
}
}
return filtered;
}
function argFilter(uint256[] memory A, function(uint256) pure returns (bool) predicate)
internal
pure
returns (uint256[] memory)
{
bool[] memory includeMap = new bool[](A.length);
uint256 count = 0;
for (uint256 i = 0; i < A.length; i++) {
if (predicate(A[i])) {
includeMap[i] = true;
count++;
}
}
uint256[] memory indexArray = new uint256[](count);
uint256 j = 0;
for (i = 0; i < A.length; i++) {
if (includeMap[i]) {
indexArray[j] = i;
j++;
}
}
return indexArray;
}
// https://docs.scipy.org/doc/numpy-1.14.0/user/basics.indexing.html#index-arrays
function argGet(uint256[] memory A, uint256[] memory indexArray)
internal
pure
returns (uint256[] memory)
{
uint256[] memory array = new uint256[](indexArray.length);
for (uint256 i = 0; i < indexArray.length; i++) {
array[i] = A[indexArray[i]];
}
return array;
}
/**
* @return Returns index and isIn for the first occurrence starting from index 0
*/
function indexOf(uint256[] memory A, uint256 a) internal returns (uint256, bool) {
uint256 length = A.length;
for (uint256 i = 0; i < length; i++) {
if (A[i] == a) {
return (i, true);
}
}
return (0, false);
}
function sPopCheap(uint256[] storage A, uint256 index) internal returns (uint256) {
uint256 length = A.length;
if (index >= length) {
return length;
}
uint256 entry = A[index];
if (index != length - 1) {
A[index] = A[length - 1];
delete A[length - 1];
}
A.length--;
return entry;
}
function isEqual(uint256[] A, uint256[] B) internal pure returns (bool) {
if (A.length != B.length) {
return false;
}
for (uint256 i = 0; i < A.length; i++) {
if (A[i] != B[i]) {
return false;
}
}
return true;
}
function equal(uint256[] A, uint256[] B) internal pure returns (bool) {
return isEqual(A, B);
}
function eq(uint256[] A, uint256[] B) internal pure returns (bool) {
return isEqual(A, B);
}
}