Skip to content

Commit

Permalink
Update AddressArray.sol
Browse files Browse the repository at this point in the history
Improved existing Natspec within the AddressArray.Sol
  • Loading branch information
zodenode authored May 7, 2023
1 parent 7fb78be commit ff9e62b
Showing 1 changed file with 53 additions and 20 deletions.
73 changes: 53 additions & 20 deletions contracts/libraries/AddressArray.sol
Original file line number Diff line number Diff line change
@@ -1,36 +1,60 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

/// @title Library that implements address array on mapping, stores array length at 0 index.
/**
* @title Library that implements address array on mapping, stores array length at 0 index.
*/
library AddressArray {
/**
* @dev Error messages for different scenarios
* IndexOutOfBounds - raised when index is out of bounds
* PopFromEmptyArray - raised when pop is called on empty array
* OutputArrayTooSmall - raised when output array is too small
*/
error IndexOutOfBounds();
error PopFromEmptyArray();
error OutputArrayTooSmall();

/// @dev Data struct containing raw mapping.
/**
* @dev Data struct containing raw mapping.
*/
struct Data {
mapping(uint256 => uint256) _raw;
}

/// @dev Length of array.
function length(Data storage self) internal view returns (uint256) {
return self._raw[0] >> 160;
/**
* @dev Returns length of the array.
* @param self The `Data` struct containing raw mapping
* @return length The length of the array.
*/
function length(Data storage self) internal view returns (uint256 length) {
length = self._raw[0] >> 160;
}

/// @dev Returns data item from `self` storage at `i`.
/**
* @dev Returns data item from `self` storage at `i`.
* @param self The `Data` struct containing raw mapping
* @param i The index of the item to retrieve
* @return address The address at the specified index
*/
function at(Data storage self, uint256 i) internal view returns (address) {
return address(uint160(self._raw[i]));
}

/// @dev Returns list of addresses from storage `self`.
/**
* @dev Returns list of addresses from storage `self`.
* @param self The `Data` struct containing raw mapping
* @return arr The list of addresses from storage `self`
*/
function get(Data storage self) internal view returns (address[] memory arr) {
uint256 lengthAndFirst = self._raw[0];
arr = new address[](lengthAndFirst >> 160);
_get(self, arr, lengthAndFirst);
}

/// @dev Puts list of addresses from `self` storage into `output` array.
/**
* @dev Puts list of addresses from `self` storage into `output` array.
* @param self The `Data` struct containing raw mapping
* @param output The output array to store the list of addresses from `self`
* @return output The output array containing the list of addresses from `self`
*/
function get(Data storage self, address[] memory output) internal view returns (address[] memory) {
return _get(self, output, self._raw[0]);
}
Expand All @@ -53,7 +77,12 @@ library AddressArray {
return output;
}

/// @dev Array push back `account` operation on storage `self`.
/**
* @dev Adds `account` to the end of the array in storage `self`.
* @param self The `Data` struct containing raw mapping
* @param account The address to be added to the end of the array in storage `self`
* @return len The length of the array after adding the address
*/
function push(Data storage self, address account) internal returns (uint256) {
unchecked {
uint256 lengthAndFirst = self._raw[0];
Expand All @@ -68,7 +97,10 @@ library AddressArray {
}
}

/// @dev Array pop back operation for storage `self`.
/**
* @dev Array pop back operation for storage `self`.
* @param self The address array in storage to pop an element from.
*/
function pop(Data storage self) internal {
unchecked {
uint256 lengthAndFirst = self._raw[0];
Expand All @@ -81,12 +113,13 @@ library AddressArray {
}
}

/// @dev Set element for storage `self` at `index` to `account`.
function set(
Data storage self,
uint256 index,
address account
) internal {
/**
* @dev Set element for storage `self` at `index` to `account`.
* @param self The address array in storage to set an element in.
* @param index The index of the element to set.
* @param account The new address to set at the specified index.
*/
function set(Data storage self, uint256 index, address account) internal {
uint256 len = length(self);
if (index >= len) revert IndexOutOfBounds();

Expand Down

0 comments on commit ff9e62b

Please sign in to comment.