|
1 | 1 | # Ordered Set
|
| 2 | +An Ordered Set is a collection of unique items in sorted order. Items are usually sorted from least to greatest. The Ordered Set data type is a representation of a [Set in Mathematics](https://en.wikipedia.org/wiki/Set_(mathematics)). It's important to keep in mind that two items can have the same *value* but still may not be equal. |
| 3 | +For example, we could define "a" and "z" to have the same value (their lengths), but clearly "a" != "z". |
| 4 | + |
| 5 | +### Examples of Ordered Sets |
| 6 | +``` |
| 7 | +{1, 2, 3, 6, 8, 10, 1000} |
| 8 | +Where each item (Integers) has it's normal definition of value and equality |
| 9 | +``` |
| 10 | +``` |
| 11 | +{"a", "is", "set", "this"} |
| 12 | +Where each item (String) has it's value equal to it's length |
| 13 | +``` |
| 14 | + |
| 15 | +### These are not Ordered Sets |
| 16 | +``` |
| 17 | +{1, 1, 2, 3, 5, 8} |
| 18 | +This Set violates the property of uniqueness |
| 19 | +``` |
| 20 | +``` |
| 21 | +{1, 11, 2, 3} |
| 22 | +This Set violates the sorted property |
| 23 | +``` |
| 24 | + |
| 25 | +## The Code |
| 26 | +We'll start by creating our internal representation for the Ordered Set. Since the idea of a set is similar to that of an array, we will use an array to represent our set. Furthermore, since we'll need to keep our set sorted, we need to compare the individual elemants. Thus, any type must conform to the [Comparable Protocol](https://developer.apple.com/library/watchos/documentation/Swift/Reference/Swift_Comparable_Protocol/index.html). |
| 27 | + |
| 28 | +``` swift |
| 29 | +public struct OrderedSet<T: Comparable> { |
| 30 | + private var internalSet: [T]! = nil |
| 31 | + |
| 32 | + // returns size of Set |
| 33 | + public var count: Int { |
| 34 | + return internalSet!.count |
| 35 | + } |
| 36 | + |
| 37 | + public init(){ |
| 38 | + internalSet = [T]() // create the internal array on init |
| 39 | + } |
| 40 | + ... |
| 41 | +``` |
| 42 | + |
| 43 | +Lets take a look at the insert function first. The insert function first checks if the item already exists, and if so returns and does not insert the item. Otherwise, it will insert the item through straight forward iteration. It starts from the first item, and checks to see if this item is larger than the item we want to insert. Once we find such an item, we insert the given item into it's place, and shift the array over to the right by 1. |
| 44 | + |
| 45 | +``` swift |
| 46 | + // inserts an item |
| 47 | + public mutating func insert(item: T){ |
| 48 | + if exists(item) { |
| 49 | + return // don't add an item if it already exists |
| 50 | + } |
| 51 | + // if the set is initially empty, we need to simply append the item to internalSet |
| 52 | + if count == 0 { |
| 53 | + internalSet.append(item) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + for i in 0..<count { |
| 58 | + if internalSet[i] > item { |
| 59 | + internalSet.insert(item, atIndex: i) |
| 60 | + return |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + // if an item is larger than any item in the current set, append it to the back. |
| 65 | + internalSet.append(item) |
| 66 | + } |
| 67 | +``` |
| 68 | +The first part of the function checks if the item is already in the set.As we'll see later on, this has an efficiency of **O(nlog n + k)**. The second part iterates through the interal array so that it can find a spot for our given item. This is at worse **O(n)**. The insert function for arrays has an efficiency of **O(nlog(n))**, thus making the insert function for our Ordered Set **O(nlog(n) + k)** where k is the number of items with the same value as the item we are inserting. |
| 69 | + |
| 70 | + |
| 71 | +Next we have the remove function. First check if the item exists. If not, then return and no nothing. If it does exist, remove it. |
| 72 | + |
| 73 | +``` swift |
| 74 | + // removes an item if it exists |
| 75 | + public mutating func remove(item: T) { |
| 76 | + if !exists(item) { |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + internalSet.removeAtIndex(findIndex(item)) |
| 81 | + } |
| 82 | +``` |
| 83 | +Again, because of the `exists` function, the efficiency for remove is **O(nlog(n) + k)** |
2 | 84 |
|
3 |
| -Under Construction |
4 | 85 |
|
5 | 86 | *Written By Zain Humayun*
|
0 commit comments