-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMDS.java
More file actions
152 lines (140 loc) · 4.36 KB
/
Copy pathMDS.java
File metadata and controls
152 lines (140 loc) · 4.36 KB
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
142
143
144
145
146
147
148
149
150
151
152
/**
* Class to implement Multi-Dimensional Search
*
* @author rbk
*/
public class MDS {
ItemIndex iIndex;
PriceIndex pIndex;
DescPriceIndex dpIndex;
DescriptionIndex dIndex;
public MDS() {
this.iIndex = new ItemIndex();
this.pIndex = new PriceIndex();
this.dIndex = new DescriptionIndex();
this.dpIndex = new DescPriceIndex();
}
/**
* Insert a new item. If an entry with the same id already exists, its
* description and price are replaced by the new values. If description is
* empty, then just the price is updated.
*
* @return Returns 1 if the item is new, and 0 otherwise.
*/
public int insert(long id, double price, long[] description, int size) {
Item item = new Item(id, dollarsToCents(price), description, size);
Item oldItem = iIndex.insert(item);
int isNew = 1;
if (oldItem != null) {
pIndex.decrement(oldItem.price);
dIndex.decrement(oldItem.descriptions);
dpIndex.decrement(oldItem.descriptions, oldItem.price);
isNew = 0;
}
pIndex.increment(item.price);
dIndex.increment(item.descriptions);
dpIndex.increment(item.descriptions, item.price);
return isNew;
}
/**
* Search for the item with the given id.
*
* @return Returns price of item with given id (or 0, if not found).
*/
public double find(long id) {
Item item = iIndex.find(id);
return item != null ? centsToDollars(item.price) : 0;
}
/**
* Delete item from storage.
*
* @return Returns the sum of the long ints that are in the description of
* the item deleted (or 0, if such an id did not exist).
*/
public long delete(long id) {
Item item = iIndex.delete(id);
long sum = 0;
if (item != null) {
pIndex.decrement(item.price);
dIndex.decrement(item.descriptions);
for (Long description : item.descriptions) {
dpIndex.decrement(description, item.price);
sum += description;
}
}
return sum;
}
/**
* Find items whose description contains n (exact match with one of the long
* ints in the item's description).
*
* @return Returns lowest price of those items.
*/
public double findMinPrice(long des) {
return centsToDollars(dpIndex.findMinPrice(des));
}
/**
* Find items whose description contains n.
*
* @return Returns highest price of those items.
*/
public double findMaxPrice(long des) {
return centsToDollars(dpIndex.findMaxPrice(des));
}
/**
* Find the items whose description contains n, and their prices fall within
* the given range, [low, high].
*
* @return Returns the number of those items.
*/
public int findPriceRange(long des, double lowPrice, double highPrice) {
return dpIndex.findPriceRange(des, dollarsToCents(lowPrice), dollarsToCents(highPrice));
}
/**
* Increases the price of every product, whose id is in the range [l,h], by
* r% and any fractional pennies in the new prices of items are discarded.
*
* @return Returns the sum of the net increases of the prices.
*/
public double priceHike(long minId, long maxId, double rate) {
long netHike = 0;
for (Item item : iIndex.range(minId, maxId)) {
long oldPrice = item.price;
long hike = (long) Math.floor(rate * oldPrice / 100);
item.price += hike;
netHike += hike;
pIndex.decrement(oldPrice);
pIndex.increment(item.price);
dpIndex.decrement(item.descriptions, oldPrice);
dpIndex.increment(item.descriptions, item.price);
}
return centsToDollars(netHike);
}
/**
* Find the items whose price is at least "low" and at most "high".
*
* @return Returns the number of those items.
*/
public int range(double lowPrice, double highPrice) {
return pIndex.range(dollarsToCents(lowPrice), dollarsToCents(highPrice));
}
/**
* Find the items that satisfy all of the following conditions:
*
* 1. The description of the item contains 8 or more numbers, and,
*
* 2. The description of the item contains exactly the same set of numbers
* as another item.
*
* @return Returns the number of those items.
*/
public int sameSame() {
return dIndex.sameSame();
}
private long dollarsToCents(double price) {
return (long) Math.round(price * 100);
}
private double centsToDollars(double price) {
return price / 100.0;
}
}