-
Notifications
You must be signed in to change notification settings - Fork 533
/
Copy pathMemoryArrayBuildingGasExample.sol
141 lines (109 loc) · 5.29 KB
/
MemoryArrayBuildingGasExample.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
pragma solidity ^0.4.20;
contract MemoryArrayBuildingCheap {
struct Item {
string name;
string category;
address owner;
uint32 zipcode;
uint32 price;
}
Item[] public items;
mapping(address => uint) public ownerItemCount;
function getItemsbyOwner(address _owner) public view returns (uint[]) {
uint[] memory result = new uint[](ownerItemCount[_owner]);
uint counter = 0;
for (uint i = 0; i < items.length; i++) {
if (items[i].owner == _owner) {
result[counter] = i;
counter++;
}
}
return result;
}
function initialize() public {
Item memory tempItem = Item("test1", "house", 0xca35b7d915458ef540ade6068dfe2f44e8fa733c, 80331, 212);
items.push(tempItem);
ownerItemCount[0xca35b7d915458ef540ade6068dfe2f44e8fa733c]++;
tempItem = Item("test2", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test3", "house", 0xca35b7d915458ef540ade6068dfe2f44e8fa733c, 80331, 212);
items.push(tempItem);
ownerItemCount[0xca35b7d915458ef540ade6068dfe2f44e8fa733c]++;
tempItem = Item("test4", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test5", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test6", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test7", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test8", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test9", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test10", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
}
}
contract MemoryArrayBuildingExpensive {
struct Item {
string name;
string category;
address owner;
uint32 zipcode;
uint32 price;
}
Item[] public items;
mapping(address => uint) public ownerItemCount;
function getItemsbyOwner(address _owner) public returns (uint[]) {
uint[] memory result = new uint[](ownerItemCount[_owner]);
uint counter = 0;
for (uint i = 0; i < items.length; i++) {
if (items[i].owner == _owner) {
result[counter] = i;
counter++;
}
}
return result;
}
function initialize() public {
Item memory tempItem = Item("test1", "house", 0xca35b7d915458ef540ade6068dfe2f44e8fa733c, 80331, 212);
items.push(tempItem);
ownerItemCount[0xca35b7d915458ef540ade6068dfe2f44e8fa733c]++;
tempItem = Item("test2", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test3", "house", 0xca35b7d915458ef540ade6068dfe2f44e8fa733c, 80331, 212);
items.push(tempItem);
ownerItemCount[0xca35b7d915458ef540ade6068dfe2f44e8fa733c]++;
tempItem = Item("test4", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test5", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test6", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test7", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test8", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test9", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
tempItem = Item("test10", "house", 0x0ad04da547702b9ca134f929e3c3009424b7da70, 80331, 212);
items.push(tempItem);
ownerItemCount[0x0ad04da547702b9ca134f929e3c3009424b7da70]++;
}
}