|
@@ -140,17 +140,19 @@ contract ERC721Token is SupportsInterfaceWithLookup, ERC721BasicToken, ERC721 {
|
|
function removeTokenFrom(address _from, uint256 _tokenId) internal {
|
|
function removeTokenFrom(address _from, uint256 _tokenId) internal {
|
|
super.removeTokenFrom(_from, _tokenId);
|
|
super.removeTokenFrom(_from, _tokenId);
|
|
|
|
|
|
|
|
+ // To prevent a gap in the array, we store the last token in the index of the token to delete, and
|
|
|
|
+ // then delete the last slot.
|
|
uint256 tokenIndex = ownedTokensIndex[_tokenId];
|
|
uint256 tokenIndex = ownedTokensIndex[_tokenId];
|
|
uint256 lastTokenIndex = ownedTokens[_from].length.sub(1);
|
|
uint256 lastTokenIndex = ownedTokens[_from].length.sub(1);
|
|
uint256 lastToken = ownedTokens[_from][lastTokenIndex];
|
|
uint256 lastToken = ownedTokens[_from][lastTokenIndex];
|
|
|
|
|
|
ownedTokens[_from][tokenIndex] = lastToken;
|
|
ownedTokens[_from][tokenIndex] = lastToken;
|
|
- ownedTokens[_from][lastTokenIndex] = 0;
|
|
|
|
|
|
+ ownedTokens[_from].length--; // This also deletes the contents at the last position of the array
|
|
|
|
+
|
|
// Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
|
|
// Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
|
|
// be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
|
|
// be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
|
|
// the lastToken to the first position, and then dropping the element placed in the last position of the list
|
|
// the lastToken to the first position, and then dropping the element placed in the last position of the list
|
|
|
|
|
|
- ownedTokens[_from].length--;
|
|
|
|
ownedTokensIndex[_tokenId] = 0;
|
|
ownedTokensIndex[_tokenId] = 0;
|
|
ownedTokensIndex[lastToken] = tokenIndex;
|
|
ownedTokensIndex[lastToken] = tokenIndex;
|
|
}
|
|
}
|