You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Ordered Set/README.markdown
+20-20
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Ordered Set
2
2
3
-
An Ordered Set is a collection of unique items in sorted order. Items are usually sorted from least to greatest.
3
+
An Ordered Set is a collection of unique items in sorted order. Items are usually sorted from least to greatest.
4
4
5
5
The Ordered Set data type is a hybrid of:
6
6
@@ -11,7 +11,7 @@ It's important to keep in mind that two items can have the same *value* but stil
11
11
12
12
## Why use an ordered set?
13
13
14
-
Ordered Sets should be considered when you need to keep your collection sorted at all times, and you do lookups on the collection much more frequently than inserting or deleting items. Many of the lookup operations for an Ordered Set are **O(1)**.
14
+
Ordered Sets should be considered when you need to keep your collection sorted at all times, and you do lookups on the collection much more frequently than inserting or deleting items. Many of the lookup operations for an Ordered Set are **O(1)**.
15
15
16
16
A good example would be keeping track of the rankings of players in a scoreboard (see example 2 below).
17
17
@@ -55,15 +55,15 @@ public struct OrderedSet<T: Comparable> {
55
55
Lets take a look at the `insert()` function first. This first checks if the item already exists in the collection. If so, it returns and does not insert the item. Otherwise, it will insert the item through straightforward iteration.
56
56
57
57
```swift
58
-
publicmutatingfuncinsert(item: T){
58
+
publicmutatingfuncinsert(_item: T){
59
59
ifexists(item) {
60
60
return// don't add an item if it already exists
61
61
}
62
62
63
63
// Insert new the item just before the one that is larger.
64
64
for i in0..<count {
65
65
if internalSet[i] > item {
66
-
internalSet.insert(item, atIndex: i)
66
+
internalSet.insert(item, at: i)
67
67
return
68
68
}
69
69
}
@@ -82,19 +82,19 @@ The total performance of the `insert()` function is therefore **O(n)**.
82
82
Next up is the `remove()` function:
83
83
84
84
```swift
85
-
publicmutatingfuncremove(item: T) {
86
-
iflet index =indexOf(item) {
87
-
internalSet.removeAtIndex(index)
85
+
publicmutatingfuncremove(_item: T) {
86
+
iflet index =index(of: item) {
87
+
internalSet.remove(at: index)
88
88
}
89
89
}
90
90
```
91
91
92
92
First this checks if the item exists and then removes it from the array. Because of the `removeAtIndex()` function, the efficiency for remove is**O(n)**.
93
93
94
-
The next function is `indexOf()`, which takes in an object of type `T` and returns the index of the corresponding item if it isin the set, or `nil` if it is not. Since our setis sorted, we can use a binary search to quickly search for the item.
94
+
The next function is `indexOf()`, which takes in an object of type `T` and returns the index of the corresponding item if it isin the set, or `nil` if it is not. Since our setis sorted, we can use a binary search to quickly search for the item.
95
95
96
96
```swift
97
-
publicfuncindexOf(item: T) ->Int? {
97
+
publicfuncindex(of item: T) ->Int? {
98
98
var leftBound =0
99
99
var rightBound = count -1
100
100
@@ -115,9 +115,9 @@ The next function is `indexOf()`, which takes in an object of type `T` and retur
115
115
}
116
116
```
117
117
118
-
>**Note:** If you are not familiar with the concept of binary search, we have an [article that explains all about it](../Binary Search).
118
+
>**Note:** If you are not familiar with the concept of binary search, we have an [article that explains all about it](../Binary Search).
119
119
120
-
However, there is an important issue to deal with here. Recall that two objects can be unequal yet still have the same "value"for the purposes of comparing them. Since a set can contain multiple items with the same value, it is important to check that the binary search has landed on the correct item.
120
+
However, there is an important issue to deal with here. Recall that two objects can be unequal yet still have the same "value"for the purposes of comparing them. Since a set can contain multiple items with the same value, it is important to check that the binary search has landed on the correct item.
121
121
122
122
For example, consider this ordered set of `Player` objects. Each `Player` has a name and a number of points:
123
123
@@ -147,13 +147,13 @@ Therefore, we also need to check the items with the same value to the right and
147
147
break
148
148
}
149
149
}
150
-
150
+
151
151
returnnil
152
152
```
153
153
154
154
These loops start at the current `mid` value and then look at the neighboring values until we've found the correct object.
155
155
156
-
The combined runtime for `indexOf()` is**O(log(n) + k)**where**n**is the length of the set, and **k**is the number of items with the same *value*as the one that is being searched for.
156
+
The combined runtime for `indexOf()` is**O(log(n) + k)**where**n**is the length of the set, and **k**is the number of items with the same *value*as the one that is being searched for.
157
157
158
158
Since the setis sorted, the following operations are all **O(1)**:
159
159
@@ -168,15 +168,15 @@ Since the set is sorted, the following operations are all **O(1)**:
168
168
return count ==0?nil: internalSet[0]
169
169
}
170
170
171
-
// Returns the k-th largest element in the set, if k is in the range
171
+
// Returns the k-th largest element in the set, if k is in the range
172
172
// [1, count]. Returns nil otherwise.
173
-
publicfunckLargest(k: Int) -> T? {
173
+
publicfunckLargest(_k: Int) -> T? {
174
174
return k > count || k <=0?nil: internalSet[count - k]
175
175
}
176
176
177
177
// Returns the k-th smallest element in the set, if k is in the range
178
178
// [1, count]. Returns nil otherwise.
179
-
publicfunckSmallest(k: Int) -> T? {
179
+
publicfunckSmallest(_k: Int) -> T? {
180
180
return k > count || k <=0?nil: internalSet[k -1]
181
181
}
182
182
```
@@ -228,7 +228,7 @@ public struct Player: Comparable {
228
228
The `Player` also gets its own `==` and `<` operators. The `<` operatoris used to determine the sort order of the set, while `==` determines whether two objects are really equal.
229
229
230
230
Note that `==` compares both the name and the points:
231
-
231
+
232
232
```swifr
233
233
func==(x: Player, y: Player) ->Bool {
234
234
return x.name== y.name&& x.points== y.points
@@ -272,7 +272,7 @@ print("\(anotherPlayer.name) is ranked at level \(level) with \(anotherPlayer.po
272
272
273
273
### Example 3
274
274
275
-
The final example demonstrates the need to look for the right item even after the binary search has completed.
275
+
The final example demonstrates the need to look for the right item even after the binary search has completed.
276
276
277
277
We insert 9 players into the set:
278
278
@@ -299,7 +299,7 @@ The set looks something like this:
After the binary search finishes, the value of `mid` is at index 5:
@@ -312,7 +312,7 @@ However, this is not `Player 2`. Both `Player 4` and `Player 2` have the same po
312
312
But we do know that `Player 2` must be either to the immediate left or the right of `Player 4`, so we check both sides of `mid`. We only need to look at the objects with the same value as `Player 4`. The others are replaced by `X`:
313
313
314
314
[X, X, Player 1, Player 2, Player 3, Player 4, Player 5, X, X]
315
-
mid
315
+
mid
316
316
317
317
The code then first checks on the right of `mid` (where the `*` is):
0 commit comments