Skip to content

Commit 46ac854

Browse files
author
318236213
committed
fixed find function
1 parent 172532c commit 46ac854

File tree

2 files changed

+89
-13
lines changed

2 files changed

+89
-13
lines changed

Ordered Set/OrderedSet.playground/Contents.swift

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ extension String {
2323
struct Player : Comparable {
2424
var name: String! = String.random()
2525
var points = randomNum(0, max: 5000)
26+
27+
init(name: String, points: Int){
28+
self.name = name
29+
self.points = points
30+
}
31+
32+
init(){}
2633
}
2734

2835
// == operator for struct Player
@@ -42,6 +49,12 @@ func print(player: Player){
4249
print("Player: \(player.name) | Points: \(player.points)")
4350
}
4451

52+
func print(set: OrderedSet<Player>){
53+
for i in 0..<set.count {
54+
print(set[set.count - i - 1])
55+
}
56+
}
57+
4558
import Foundation
4659

4760
// An Ordered Set is a collection where all items in the set follow an ordering, usually ordered from
@@ -94,7 +107,7 @@ public struct OrderedSet<T: Comparable> {
94107
// returns true if and only if the item exists somewhere in the set
95108
public func exists(item: T) -> Bool {
96109
let index = findIndex(item)
97-
return index != -1 && internalSet[index] == item
110+
return index != -1
98111
}
99112

100113
// returns the index of an item if it exists, otherwise returns -1.
@@ -110,13 +123,38 @@ public struct OrderedSet<T: Comparable> {
110123
} else if internalSet[mid] < item {
111124
leftBound = mid + 1
112125
} else {
113-
return mid
126+
// check the mid value to see if it is the item we are looking for
127+
if internalSet[mid] == item {
128+
return mid
129+
}
130+
131+
var j = mid
132+
133+
// check right side of mid
134+
while j < internalSet.count - 1 && !(internalSet[j] < internalSet[j + 1]) {
135+
if internalSet[j + 1] == item {
136+
return j + 1
137+
}
138+
139+
j++
140+
}
141+
142+
143+
// check right side of mid
144+
while j > 0 && !(internalSet[j] < internalSet[j - 1]) {
145+
if internalSet[j - 1] == item {
146+
return j - 1
147+
}
148+
149+
j--
150+
}
151+
return -1
114152
}
115153
}
116154

117155
return -1
118156
}
119-
157+
120158
// returns the item at the given index. assertion fails if the index is out of the range
121159
// of [0, count)
122160
public subscript(index: Int) -> T {
@@ -185,9 +223,7 @@ for _ in 0..<20 {
185223
playerSet.insert(anotherPlayer)
186224

187225
// print all players in order
188-
for i in 0..<playerSet.count {
189-
print(playerSet[playerSet.count - i - 1])
190-
}
226+
print(playerSet)
191227

192228

193229
// highest and lowest players:
@@ -199,13 +235,27 @@ print("'Another Player (\(anotherPlayer.name))' is ranked at level: \(playerSet.
199235

200236

201237

238+
// Example with multiple entries with the same value
202239

240+
var repeatedSet = OrderedSet<Player>()
203241

242+
repeatedSet.insert(Player(name:"Player 1", points: 100))
243+
repeatedSet.insert(Player(name: "Player 1", points: 100))
244+
repeatedSet.insert(Player(name: "Player 2", points: 100))
245+
repeatedSet.insert(Player(name: "Player 3", points: 100))
246+
repeatedSet.insert(Player(name: "Player 4", points: 100))
247+
repeatedSet.insert(Player(name: "Player 5", points: 100))
248+
repeatedSet.insert(Player(name: "Player 6", points: 50))
249+
repeatedSet.insert(Player(name: "Player 7", points: 200))
250+
repeatedSet.insert(Player(name: "Player 8", points: 250))
251+
repeatedSet.insert(Player(name: "Player 9", points: 25))
204252

253+
print(repeatedSet)
205254

206-
207-
208-
255+
// find player 5
256+
print(repeatedSet.findIndex(Player(name: "Player 5", points: 100)))
257+
print(repeatedSet.findIndex(Player(name: "Random Player", points: 100)))
258+
print(repeatedSet.findIndex(Player(name: "Player 5", points: 1000)))
209259

210260

211261

Ordered Set/OrderedSet.swift

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ public struct OrderedSet<T: Comparable> {
1212
internalSet = [T]() // create the internal array on init
1313
}
1414

15-
// inserts an item into the set if it doesn't exist
15+
// inserts an item
16+
// O(n log n)
1617
public mutating func insert(item: T){
1718
if exists(item) {
1819
return // don't add an item if it already exists
@@ -47,7 +48,7 @@ public struct OrderedSet<T: Comparable> {
4748
// returns true if and only if the item exists somewhere in the set
4849
public func exists(item: T) -> Bool {
4950
let index = findIndex(item)
50-
return index != -1 && internalSet[index] == item
51+
return index != -1
5152
}
5253

5354
// returns the index of an item if it exists, otherwise returns -1.
@@ -63,7 +64,32 @@ public struct OrderedSet<T: Comparable> {
6364
} else if internalSet[mid] < item {
6465
leftBound = mid + 1
6566
} else {
66-
return mid
67+
// check the mid value to see if it is the item we are looking for
68+
if internalSet[mid] == item {
69+
return mid
70+
}
71+
72+
var j = mid
73+
74+
// check right side of mid
75+
while j < internalSet.count - 1 && !(internalSet[j] < internalSet[j + 1]) {
76+
if internalSet[j + 1] == item {
77+
return j + 1
78+
}
79+
80+
j++
81+
}
82+
83+
84+
// check right side of mid
85+
while j > 0 && !(internalSet[j] < internalSet[j - 1]) {
86+
if internalSet[j - 1] == item {
87+
return j - 1
88+
}
89+
90+
j--
91+
}
92+
return -1
6793
}
6894
}
6995

@@ -98,4 +124,4 @@ public struct OrderedSet<T: Comparable> {
98124
public func kSmallest(k: Int) -> T! {
99125
return k > count || k <= 0 ? nil : internalSet[k - 1]
100126
}
101-
}
127+
}

0 commit comments

Comments
 (0)