Skip to content

Commit 004e513

Browse files
committed
Merge pull request kodecocodes#3 from hollance/master
updates
2 parents 67a3dc9 + cfb94d0 commit 004e513

File tree

24 files changed

+850
-18
lines changed

24 files changed

+850
-18
lines changed

Deque/Deque.playground/Contents.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//: Playground - noun: a place where people can play
2+
3+
public struct Deque<T> {
4+
private var array = [T]()
5+
6+
public var isEmpty: Bool {
7+
return array.isEmpty
8+
}
9+
10+
public var count: Int {
11+
return array.count
12+
}
13+
14+
public mutating func enqueue(element: T) {
15+
array.append(element)
16+
}
17+
18+
public mutating func enqueueFront(element: T) {
19+
array.insert(element, atIndex: 0)
20+
}
21+
22+
public mutating func dequeue() -> T? {
23+
if isEmpty {
24+
return nil
25+
} else {
26+
return array.removeFirst()
27+
}
28+
}
29+
30+
public mutating func dequeueBack() -> T? {
31+
if isEmpty {
32+
return nil
33+
} else {
34+
return array.removeLast()
35+
}
36+
}
37+
38+
public func peekFront() -> T? {
39+
return array.first
40+
}
41+
42+
public func peekBack() -> T? {
43+
return array.last
44+
}
45+
}
46+
47+
var deque = Deque<Int>()
48+
deque.enqueue(1)
49+
deque.enqueue(2)
50+
deque.enqueue(3)
51+
deque.enqueue(4)
52+
53+
deque.dequeue() // 1
54+
deque.dequeueBack() // 4
55+
56+
deque.enqueueFront(5)
57+
deque.dequeue() // 5
Lines changed: 4 additions & 0 deletions
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='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Deque/Deque.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Deque (pronounced "deck"), a double-ended queue
3+
4+
This particular implementation is simple but not very efficient. Several
5+
operations are O(n). A more efficient implementation would use a doubly
6+
linked list or a circular buffer.
7+
*/
8+
public struct Deque<T> {
9+
private var array = [T]()
10+
11+
public var isEmpty: Bool {
12+
return array.isEmpty
13+
}
14+
15+
public var count: Int {
16+
return array.count
17+
}
18+
19+
public mutating func enqueue(element: T) {
20+
array.append(element)
21+
}
22+
23+
public mutating func enqueueFront(element: T) {
24+
array.insert(element, atIndex: 0)
25+
}
26+
27+
public mutating func dequeue() -> T? {
28+
if isEmpty {
29+
return nil
30+
} else {
31+
return array.removeFirst()
32+
}
33+
}
34+
35+
public mutating func dequeueBack() -> T? {
36+
if isEmpty {
37+
return nil
38+
} else {
39+
return array.removeLast()
40+
}
41+
}
42+
43+
public func peekFront() -> T? {
44+
return array.first
45+
}
46+
47+
public func peekBack() -> T? {
48+
return array.last
49+
}
50+
}

Deque/README.markdown

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Deque
2+
3+
A double-ended queue. For some reason this is pronounced as "deck".
4+
5+
A regular [queue](../Queue/) adds new elements to the back and dequeues from the front. The deque also allows enqueuing at the front and dequeuing from the back, and peeking at both ends.
6+
7+
Here is a very basic implementation of a deque in Swift:
8+
9+
```swift
10+
public struct Deque<T> {
11+
private var array = [T]()
12+
13+
public var isEmpty: Bool {
14+
return array.isEmpty
15+
}
16+
17+
public var count: Int {
18+
return array.count
19+
}
20+
21+
public mutating func enqueue(element: T) {
22+
array.append(element)
23+
}
24+
25+
public mutating func enqueueFront(element: T) {
26+
array.insert(element, atIndex: 0)
27+
}
28+
29+
public mutating func dequeue() -> T? {
30+
if isEmpty {
31+
return nil
32+
} else {
33+
return array.removeFirst()
34+
}
35+
}
36+
37+
public mutating func dequeueBack() -> T? {
38+
if isEmpty {
39+
return nil
40+
} else {
41+
return array.removeLast()
42+
}
43+
}
44+
45+
public func peekFront() -> T? {
46+
return array.first
47+
}
48+
49+
public func peekBack() -> T? {
50+
return array.last
51+
}
52+
}
53+
```
54+
55+
This uses an array internally. Enqueuing and dequeuing is simply a matter of adding and removing items from the front or back of the array.
56+
57+
An example of how to use it in a playground:
58+
59+
```swift
60+
var deque = Deque<Int>()
61+
deque.enqueue(1)
62+
deque.enqueue(2)
63+
deque.enqueue(3)
64+
deque.enqueue(4)
65+
66+
deque.dequeue() // 1
67+
deque.dequeueBack() // 4
68+
69+
deque.enqueueFront(5)
70+
deque.dequeue() // 5
71+
```
72+
73+
This particular implementation of `Deque` is simple but not very efficient. Several operations are **O(n)**, notably `enqueueFront()` and `dequeue()`. I've included it only to show the principle of what a deque does.
74+
75+
A more efficient implementation would use a [doubly linked list](../Linked List/), a [circular buffer](../Ring Buffer/), or two [stacks](../Stack/) facing opposite directions.
76+
77+
*Written for Swift Algorithm Club by Matthijs Hollemans*
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
//: Playground - noun: a place where people can play
2+
3+
public struct OrderedArray<T: Comparable> {
4+
private var array = [T]()
5+
6+
public init(array: [T]) {
7+
self.array = array.sort()
8+
}
9+
10+
public var isEmpty: Bool {
11+
return array.isEmpty
12+
}
13+
14+
public var count: Int {
15+
return array.count
16+
}
17+
18+
public subscript(index: Int) -> T {
19+
return array[index]
20+
}
21+
22+
public mutating func removeAtIndex(index: Int) -> T {
23+
return array.removeAtIndex(index)
24+
}
25+
26+
public mutating func removeAll() {
27+
array.removeAll()
28+
}
29+
30+
public mutating func insert(newElement: T) -> Int {
31+
let i = findInsertionPoint(newElement)
32+
array.insert(newElement, atIndex: i)
33+
return i
34+
}
35+
36+
/*
37+
// Slow version that looks at every element in the array.
38+
private func findInsertionPoint(newElement: T) -> Int {
39+
for i in 0..<array.count {
40+
if newElement <= array[i] {
41+
return i
42+
}
43+
}
44+
return array.count
45+
}
46+
*/
47+
48+
// Fast version that uses a binary search.
49+
private func findInsertionPoint(newElement: T) -> Int {
50+
var range = 0..<array.count
51+
while range.startIndex < range.endIndex {
52+
let midIndex = range.startIndex + (range.endIndex - range.startIndex) / 2
53+
if array[midIndex] == newElement {
54+
return midIndex
55+
} else if array[midIndex] < newElement {
56+
range.startIndex = midIndex + 1
57+
} else {
58+
range.endIndex = midIndex
59+
}
60+
}
61+
return range.startIndex
62+
}
63+
}
64+
65+
extension OrderedArray: CustomStringConvertible {
66+
public var description: String {
67+
return array.description
68+
}
69+
}
70+
71+
72+
73+
var a = OrderedArray<Int>(array: [5, 1, 3, 9, 7, -1])
74+
a // [-1, 1, 3, 5, 7, 9]
75+
76+
a.insert(4) // inserted at index 3
77+
a // [-1, 1, 3, 4, 5, 7, 9]
78+
79+
a.insert(-2) // inserted at index 0
80+
a.insert(10) // inserted at index 8
81+
a // [-2, -1, 1, 3, 4, 5, 7, 9, 10]
82+
Lines changed: 4 additions & 0 deletions
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='osx'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
</TimelineItems>
6+
</Timeline>

Ordered Array/OrderedArray.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
An ordered array. When you add a new item to this array, it is inserted in
3+
sorted position.
4+
*/
5+
public struct OrderedArray<T: Comparable> {
6+
private var array = [T]()
7+
8+
public init(array: [T]) {
9+
self.array = array.sort()
10+
}
11+
12+
public var isEmpty: Bool {
13+
return array.isEmpty
14+
}
15+
16+
public var count: Int {
17+
return array.count
18+
}
19+
20+
public subscript(index: Int) -> T {
21+
return array[index]
22+
}
23+
24+
public mutating func removeAtIndex(index: Int) -> T {
25+
return array.removeAtIndex(index)
26+
}
27+
28+
public mutating func removeAll() {
29+
array.removeAll()
30+
}
31+
32+
public mutating func insert(newElement: T) -> Int {
33+
let i = findInsertionPoint(newElement)
34+
array.insert(newElement, atIndex: i)
35+
return i
36+
}
37+
38+
private func findInsertionPoint(newElement: T) -> Int {
39+
var range = 0..<array.count
40+
while range.startIndex < range.endIndex {
41+
let midIndex = range.startIndex + (range.endIndex - range.startIndex) / 2
42+
if array[midIndex] == newElement {
43+
return midIndex
44+
} else if array[midIndex] < newElement {
45+
range.startIndex = midIndex + 1
46+
} else {
47+
range.endIndex = midIndex
48+
}
49+
}
50+
return range.startIndex
51+
}
52+
}
53+
54+
extension OrderedArray: CustomStringConvertible {
55+
public var description: String {
56+
return array.description
57+
}
58+
}

0 commit comments

Comments
 (0)