Skip to content

Commit 89b337d

Browse files
committed
Ordered Set - Apple Version
https://developer.apple.com/documentation/foundation/nsmutableorderedset In this repo, the ordered set is actually not the right apple library ordered set. So, I write the one exactly the same as Apple one.
1 parent c0e9916 commit 89b337d

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let s = AppleOrderedSet<Int>()
2+
3+
s.add(1)
4+
s.add(2)
5+
s.add(-1)
6+
s.add(0)
7+
s.insert(4, at: 3)
8+
9+
s.set(-1, at: 0)
10+
s.remove(-1)
11+
12+
print(s.object(at: 1))
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
public class AppleOrderedSet<T: Hashable> {
2+
private var objects: [T] = []
3+
private var indexOfKey: [T: Int] = [:]
4+
5+
public init() {}
6+
7+
// O(1)
8+
public func add(_ object: T) {
9+
guard indexOfKey[object] == nil else {
10+
return
11+
}
12+
13+
objects.append(object)
14+
indexOfKey[object] = objects.count - 1
15+
}
16+
17+
// O(n)
18+
public func insert(_ object: T, at index: Int) {
19+
assert(index < objects.count, "Index should be smaller than object count")
20+
assert(index >= 0, "Index should be bigger than 0")
21+
22+
guard indexOfKey[object] == nil else {
23+
return
24+
}
25+
26+
objects.insert(object, at: index)
27+
indexOfKey[object] = index
28+
for i in index+1..<objects.count {
29+
indexOfKey[objects[i]] = i
30+
}
31+
}
32+
33+
// O(1)
34+
public func object(at index: Int) -> T {
35+
assert(index < objects.count, "Index should be smaller than object count")
36+
assert(index >= 0, "Index should be bigger than 0")
37+
38+
return objects[index]
39+
}
40+
41+
// O(1)
42+
public func set(_ object: T, at index: Int) {
43+
assert(index < objects.count, "Index should be smaller than object count")
44+
assert(index >= 0, "Index should be bigger than 0")
45+
46+
guard indexOfKey[object] == nil else {
47+
return
48+
}
49+
50+
indexOfKey.removeValue(forKey: objects[index])
51+
indexOfKey[object] = index
52+
objects[index] = object
53+
}
54+
55+
// O(1)
56+
public func indexOf(_ object: T) -> Int {
57+
return indexOfKey[object] ?? -1
58+
}
59+
60+
// O(n)
61+
public func remove(_ object: T) {
62+
guard let index = indexOfKey[object] else {
63+
return
64+
}
65+
66+
indexOfKey.removeValue(forKey: object)
67+
objects.remove(at: index)
68+
for i in index..<objects.count {
69+
indexOfKey[objects[i]] = i
70+
}
71+
}
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Ordered Set/AppleOrderedSet.swift

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
public class AppleOrderedSet<T: Hashable> {
2+
private var objects: [T] = []
3+
private var indexOfKey: [T: Int] = [:]
4+
5+
public init() {}
6+
7+
// O(1)
8+
public func add(_ object: T) {
9+
guard indexOfKey[object] == nil else {
10+
return
11+
}
12+
13+
objects.append(object)
14+
indexOfKey[object] = objects.count - 1
15+
}
16+
17+
// O(n)
18+
public func insert(_ object: T, at index: Int) {
19+
assert(index < objects.count, "Index should be smaller than object count")
20+
assert(index >= 0, "Index should be bigger than 0")
21+
22+
guard indexOfKey[object] == nil else {
23+
return
24+
}
25+
26+
objects.insert(object, at: index)
27+
indexOfKey[object] = index
28+
for i in index+1..<objects.count {
29+
indexOfKey[objects[i]] = i
30+
}
31+
}
32+
33+
// O(1)
34+
public func object(at index: Int) -> T {
35+
assert(index < objects.count, "Index should be smaller than object count")
36+
assert(index >= 0, "Index should be bigger than 0")
37+
38+
return objects[index]
39+
}
40+
41+
// O(1)
42+
public func set(_ object: T, at index: Int) {
43+
assert(index < objects.count, "Index should be smaller than object count")
44+
assert(index >= 0, "Index should be bigger than 0")
45+
46+
guard indexOfKey[object] == nil else {
47+
return
48+
}
49+
50+
indexOfKey.removeValue(forKey: objects[index])
51+
indexOfKey[object] = index
52+
objects[index] = object
53+
}
54+
55+
// O(1)
56+
public func indexOf(_ object: T) -> Int {
57+
return indexOfKey[object] ?? -1
58+
}
59+
60+
// O(n)
61+
public func remove(_ object: T) {
62+
guard let index = indexOfKey[object] else {
63+
return
64+
}
65+
66+
indexOfKey.removeValue(forKey: object)
67+
objects.remove(at: index)
68+
for i in index..<objects.count {
69+
indexOfKey[objects[i]] = i
70+
}
71+
}
72+
}

swift-algorithm-club.xcworkspace/contents.xcworkspacedata

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)