Skip to content

Commit 339e08a

Browse files
authored
Added Bytes32Set to SetHelper (#94)
* Added Bytes32Set to SetHelper * typos * Added missing methods to ArrayHelper
1 parent f54793c commit 339e08a

File tree

12 files changed

+171
-10
lines changed

12 files changed

+171
-10
lines changed

contracts/libs/arrays/ArrayHelper.sol

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,19 @@ library ArrayHelper {
128128
}
129129
}
130130

131+
/**
132+
* @notice The function to reverse a bool array
133+
*/
134+
function reverse(bool[] memory arr_) internal pure returns (bool[] memory reversed_) {
135+
reversed_ = new bool[](arr_.length);
136+
uint256 i = arr_.length;
137+
138+
while (i > 0) {
139+
i--;
140+
reversed_[arr_.length - 1 - i] = arr_[i];
141+
}
142+
}
143+
131144
/**
132145
* @notice The function to reverse a string array
133146
*/
@@ -188,6 +201,21 @@ library ArrayHelper {
188201
return index_ + what_.length;
189202
}
190203

204+
/**
205+
* @notice The function to insert a bool array into the other array
206+
*/
207+
function insert(
208+
bool[] memory to_,
209+
uint256 index_,
210+
bool[] memory what_
211+
) internal pure returns (uint256) {
212+
for (uint256 i = 0; i < what_.length; i++) {
213+
to_[index_ + i] = what_[i];
214+
}
215+
216+
return index_ + what_.length;
217+
}
218+
191219
/**
192220
* @notice The function to insert a string array into the other array
193221
*/

contracts/libs/arrays/SetHelper.sol

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {StringSet} from "../data-structures/StringSet.sol";
1111
library SetHelper {
1212
using EnumerableSet for EnumerableSet.UintSet;
1313
using EnumerableSet for EnumerableSet.AddressSet;
14+
using EnumerableSet for EnumerableSet.Bytes32Set;
1415
using StringSet for StringSet.Set;
1516

1617
/**
@@ -33,6 +34,15 @@ library SetHelper {
3334
}
3435
}
3536

37+
/**
38+
* @notice The function to insert an array of elements into the bytes32 set
39+
*/
40+
function add(EnumerableSet.Bytes32Set storage set, bytes32[] memory array_) internal {
41+
for (uint256 i = 0; i < array_.length; i++) {
42+
set.add(array_[i]);
43+
}
44+
}
45+
3646
/**
3747
* @notice The function to insert an array of elements into the string set
3848
*/
@@ -62,6 +72,15 @@ library SetHelper {
6272
}
6373
}
6474

75+
/**
76+
* @notice The function for the strict insertion of an array of elements into the bytes32 set
77+
*/
78+
function strictAdd(EnumerableSet.Bytes32Set storage set, bytes32[] memory array_) internal {
79+
for (uint256 i = 0; i < array_.length; i++) {
80+
require(set.add(array_[i]), "SetHelper: element already exists");
81+
}
82+
}
83+
6584
/**
6685
* @notice The function for the strict insertion of an array of elements into the string set
6786
*/
@@ -91,6 +110,15 @@ library SetHelper {
91110
}
92111
}
93112

113+
/**
114+
* @notice The function to remove an array of elements from the bytes32 set
115+
*/
116+
function remove(EnumerableSet.Bytes32Set storage set, bytes32[] memory array_) internal {
117+
for (uint256 i = 0; i < array_.length; i++) {
118+
set.remove(array_[i]);
119+
}
120+
}
121+
94122
/**
95123
* @notice The function to remove an array of elements from the string set
96124
*/
@@ -120,6 +148,15 @@ library SetHelper {
120148
}
121149
}
122150

151+
/**
152+
* @notice The function for the strict removal of an array of elements from the bytes32 set
153+
*/
154+
function strictRemove(EnumerableSet.Bytes32Set storage set, bytes32[] memory array_) internal {
155+
for (uint256 i = 0; i < array_.length; i++) {
156+
require(set.remove(array_[i]), "SetHelper: no such element");
157+
}
158+
}
159+
123160
/**
124161
* @notice The function for the strict removal of an array of elements from the string set
125162
*/

contracts/libs/data-structures/memory/Vector.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {TypeCaster} from "../../utils/TypeCaster.sol";
1010
*
1111
* Currently Solidity allows resizing only storage arrays, which may be a roadblock if you need to
1212
* filter the elements by a specific property or add new ones without writing bulky code. The Vector library
13-
* is ment to help with that.
13+
* is meant to help with that.
1414
*
1515
* It is very important to create Vectors via constructors (newUint, newBytes32, newAddress) as they allocate and clean
1616
* the memory for the data structure.

contracts/libs/utils/DecimalsConverter.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {ERC20, IERC20, IERC20Metadata} from "@openzeppelin/contracts/token/ERC20
66
/**
77
* @notice This library is used to convert numbers that use token's N decimals to M decimals.
88
* Comes extremely handy with standardizing the business logic that is intended to work with many different ERC20 tokens
9-
* that have different precision (decimals). One can perform calculations with 18 decimals only and resort to convertion
9+
* that have different precision (decimals). One can perform calculations with 18 decimals only and resort to conversion
1010
* only when the payouts (or interactions) with the actual tokes have to be made.
1111
*
1212
* The best usage scenario involves accepting and calculating values with 18 decimals throughout the project, despite the tokens decimals.
@@ -168,7 +168,7 @@ library DecimalsConverter {
168168
}
169169

170170
/**
171-
* @notice The function to do the token precision convertion
171+
* @notice The function to do the token precision conversion
172172
* @param amount_ the amount to convert
173173
* @param baseToken_ current token
174174
* @param destToken_ desired token
@@ -183,7 +183,7 @@ library DecimalsConverter {
183183
}
184184

185185
/**
186-
* @notice The function to do the precision convertion
186+
* @notice The function to do the precision conversion
187187
* @param amount_ the amount to covert
188188
* @param baseDecimals_ current number precision
189189
* @param destDecimals_ desired number precision

contracts/libs/utils/TypeCaster.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pragma solidity ^0.8.4;
44
/**
55
* @notice This library simplifies non-obvious type castings.
66
*
7-
* Convertions from static to dynamic arrays, singleton arrays, and arrays of different types are supported.
7+
* Conversions from static to dynamic arrays, singleton arrays, and arrays of different types are supported.
88
*/
99
library TypeCaster {
1010
/**

contracts/mock/libs/arrays/ArrayHelperMock.sol

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ contract ArrayHelperMock {
3636
return arr_.reverse();
3737
}
3838

39+
function reverseBool(bool[] memory arr_) external pure returns (bool[] memory) {
40+
return arr_.reverse();
41+
}
42+
3943
function reverseString(string[] memory arr_) external pure returns (string[] memory) {
4044
return arr_.reverse();
4145
}
@@ -60,6 +64,14 @@ contract ArrayHelperMock {
6064
return (to_.insert(index_, what_), to_);
6165
}
6266

67+
function insertBool(
68+
bool[] memory to_,
69+
uint256 index_,
70+
bool[] memory what_
71+
) external pure returns (uint256, bool[] memory) {
72+
return (to_.insert(index_, what_), to_);
73+
}
74+
6375
function insertString(
6476
string[] memory to_,
6577
uint256 index_,

contracts/mock/libs/arrays/SetHelperMock.sol

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,16 @@ import {SetHelper} from "../../../libs/arrays/SetHelper.sol";
99
contract SetHelperMock {
1010
using EnumerableSet for EnumerableSet.UintSet;
1111
using EnumerableSet for EnumerableSet.AddressSet;
12+
using EnumerableSet for EnumerableSet.Bytes32Set;
1213
using StringSet for StringSet.Set;
1314
using SetHelper for StringSet.Set;
1415
using SetHelper for EnumerableSet.UintSet;
1516
using SetHelper for EnumerableSet.AddressSet;
17+
using SetHelper for EnumerableSet.Bytes32Set;
1618

1719
EnumerableSet.AddressSet internal addressSet;
1820
EnumerableSet.UintSet internal uintSet;
21+
EnumerableSet.Bytes32Set internal bytes32Set;
1922
StringSet.Set internal stringSet;
2023

2124
function addToAddressSet(address[] memory arr_) external {
@@ -26,6 +29,10 @@ contract SetHelperMock {
2629
uintSet.add(arr_);
2730
}
2831

32+
function addToBytes32Set(bytes32[] memory arr_) external {
33+
bytes32Set.add(arr_);
34+
}
35+
2936
function addToStringSet(string[] memory arr_) external {
3037
stringSet.add(arr_);
3138
}
@@ -38,6 +45,10 @@ contract SetHelperMock {
3845
uintSet.strictAdd(arr_);
3946
}
4047

48+
function strictAddToBytes32Set(bytes32[] memory arr_) external {
49+
bytes32Set.strictAdd(arr_);
50+
}
51+
4152
function strictAddToStringSet(string[] memory arr_) external {
4253
stringSet.strictAdd(arr_);
4354
}
@@ -50,6 +61,10 @@ contract SetHelperMock {
5061
uintSet.remove(arr_);
5162
}
5263

64+
function removeFromBytes32Set(bytes32[] memory arr_) external {
65+
bytes32Set.remove(arr_);
66+
}
67+
5368
function removeFromStringSet(string[] memory arr_) external {
5469
stringSet.remove(arr_);
5570
}
@@ -62,6 +77,10 @@ contract SetHelperMock {
6277
uintSet.strictRemove(arr_);
6378
}
6479

80+
function strictRemoveFromBytes32Set(bytes32[] memory arr_) external {
81+
bytes32Set.strictRemove(arr_);
82+
}
83+
6584
function strictRemoveFromStringSet(string[] memory arr_) external {
6685
stringSet.strictRemove(arr_);
6786
}
@@ -74,6 +93,10 @@ contract SetHelperMock {
7493
return uintSet.values();
7594
}
7695

96+
function getBytes32Set() external view returns (bytes32[] memory) {
97+
return bytes32Set.values();
98+
}
99+
77100
function getStringSet() external view returns (string[] memory) {
78101
return stringSet.values();
79102
}

contracts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solarity/solidity-lib",
3-
"version": "2.7.1",
3+
"version": "2.7.2",
44
"license": "MIT",
55
"author": "Distributed Lab",
66
"readme": "README.md",

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@solarity/solidity-lib",
3-
"version": "2.7.1",
3+
"version": "2.7.2",
44
"license": "MIT",
55
"author": "Distributed Lab",
66
"description": "Solidity Library by Distributed Lab",

0 commit comments

Comments
 (0)