Skip to content

Commit 68cb080

Browse files
committed
Added tutorial 19
1 parent a2abc57 commit 68cb080

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ The companion to the Youtube tutorials
2121
- [Learning Solidity : Tutorial 16 Time Based Events](https://www.youtube.com/watch?v=HGw-yalqdgs)
2222
- [Learning Solidity : Tutorial 17 Polymorphism](https://www.youtube.com/watch?v=l_E5F5qnbtk)
2323
- [Learning Solidity : Tutorial 18 Randomness and Gambling](https://www.youtube.com/watch?v=3wY5PRliphE)
24+
- [Learning Solidity : Tutorial 19 Nested Arrays and Storage](https://www.youtube.com/watch?v=zkNHRJEuYQg)
2425

2526
### Support
2627

tutorial-19/NestedArrays.sol

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
pragma solidity ^0.4.0;
2+
3+
contract NestedArrays {
4+
5+
uint[][] private nested;
6+
7+
function getArray() public {
8+
nested.push([1,2,3]);
9+
}
10+
11+
// [1, 3, 2, 4]
12+
function toBytes(uint[] _array)
13+
public
14+
returns (bytes _ptr) {
15+
assembly {
16+
let len := mload(_array)
17+
_ptr := msize()
18+
19+
// Bytes
20+
mstore(_ptr, mul(add(len, 2), 0x20))
21+
22+
// Array
23+
mstore(add(_ptr, 0x20), 0x20)
24+
mstore(add(_ptr, 0x40), len)
25+
26+
let idx := 0
27+
loop:
28+
jumpi(end, eq(len, idx))
29+
mstore(add(_ptr, add(mul(idx,0x20),0x60)), mload(add(_array, add(0x20, mul(idx, 0x20)))))
30+
idx := add(idx, 1)
31+
jump(loop)
32+
33+
end:
34+
// Without return statements memory is overridden
35+
mstore(0x40, add(mul(len, 0x20), 0x20))
36+
}
37+
}
38+
39+
// ["0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x20","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x04","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x01","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x03","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x02","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x00","0x04"]
40+
function toArray(bytes _bytes)
41+
public
42+
returns (uint[] _ptr) {
43+
assembly {
44+
45+
// First 32 bytes are byte array properties
46+
// Next 32 bytes are the data size of the array elements
47+
let len := mload(add(_bytes, 0x40))
48+
_ptr := msize()
49+
50+
// Array
51+
mstore(_ptr, len)
52+
53+
let idx := 0
54+
loop:
55+
jumpi(end, eq(len, idx))
56+
mstore(add(_ptr, add(mul(idx,0x20),0x20)), mload(add(_bytes, add(0x60, mul(idx, 0x20)))))
57+
idx := add(idx, 1)
58+
jump(loop)
59+
60+
end:
61+
mstore(0x40, add(_ptr,add(mul(len, 0x20), 0x40)))
62+
}
63+
}
64+
65+
}

0 commit comments

Comments
 (0)