|
2 | 2 | [](https://swift.org/) [](https://swift.org/package-manager/) [](https://developer.apple.com/discover/) [](https://lbesson.mit-license.org/) |
3 | 3 |
|
4 | 4 |
|
5 | | -A Queue implementation in Swift with constant (O(1)) time `enqueue` and `dequeue` operations that favors Value Semantics. |
| 5 | +A Queue implementation in Swift with constant (O(1)) time `enqueue` and `dequeue` operations that adopts Value Semantics. |
6 | 6 |
|
7 | | -`Queue` and `LinkedList` are both value types (structs). `LinkedList` uses the reference type `Node`. |
8 | | -The copying only takes place on write operations `append:value`, `remove:value` and `removeFirst` when the list's tail is referenced more than once. Copying operates in linear **(O(n))** time as it copies the whole list recursively. This is an innate tradeoff we have to make when we want to favor Value Semantics. However, it only applies when the struct is copied AND mutated. If it is only modified via those functions or only shared and read, there is no copying that takes place. Thus, leaving `queue:value` and `enqueue:value` of `Queue` and `append:value` and `removeFirst` functions of `LinkedList` to operate in constant time **(O(1))**. |
| 7 | +## Overview |
| 8 | +`Queue` and `LinkedList` are both value types (structs). `LinkedList` is composed of `Node`s to refer to the next node. |
| 9 | + |
| 10 | +Queue adopts Value Semantics while also maintaining the constant time operations. |
| 11 | +The copying only takes place on write (mutating) operations when the list's tail is referenced more than once. Copying operates in linear **(O(n))** time as it copies the whole list recursively. This is an innate tradeoff we have to make when we want to adopt Value Semantics. However, it only applies when the struct is copied AND mutated. |
| 12 | + |
| 13 | +If it is modified via those functions or only shared and read, there is no copying that takes place. Thus, leaving `queue:value` and `enqueue:value` of `Queue` and `append:value`, `prepend:value`, `removeFirst` functions of `LinkedList` to operate in constant time **(O(1))**. |
| 14 | + |
| 15 | +## Installation |
| 16 | +Add it to your Package list: |
| 17 | +Xcode -> File -> Add Packages -> Paste the Repo URL to Search { https://github.com/alpavanoglu/Queue } |
| 18 | + |
| 19 | +Or you can simply copy over the `Queue.swift` along with `LinkedList.swift`. |
| 20 | + |
| 21 | +## Usage |
| 22 | +Construct with an empty initializer: |
| 23 | +```Swift |
| 24 | +let queue: Queue<Int> = [] |
| 25 | +``` |
| 26 | +Or an Array: |
| 27 | +```Swift |
| 28 | +let queue: Queue = ["one", "two", "three"] |
| 29 | +``` |
| 30 | +Access the front and rear of the Queue as: |
| 31 | +```Swift |
| 32 | +let queue: Queue = ["one", "two", "three"] |
| 33 | +queue.front // "three" |
| 34 | +queue.rear // "one" |
| 35 | +``` |
| 36 | +Enqueue: |
| 37 | +```Swift |
| 38 | +queue.enqueue("zero") // ["zero", "one", "two", "three"] |
| 39 | +``` |
| 40 | +And dequeue: |
| 41 | +```Swift |
| 42 | +queue.enqueue("dequeue") // ["zero", "one", "two",] |
| 43 | +``` |
0 commit comments