Skip to content

Commit a67d2e1

Browse files
authored
Add Installation and Usage to README
1 parent 8a87c2f commit a67d2e1

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

README.md

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,42 @@
22
[![Swift](https://img.shields.io/badge/Swift-100%25-orange?logo=swift&style=flat)](https://swift.org/) [![SwiftPM](https://img.shields.io/badge/SwiftPM-Compatible-red)](https://swift.org/package-manager/) [![Platforms](https://img.shields.io/badge/Platforms-macOS%20iOS%20watchOS%20tvOS-ec83b8)](https://developer.apple.com/discover/) [![MIT license](https://img.shields.io/badge/License-MIT-blue.svg)](https://lbesson.mit-license.org/)
33

44

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.
66

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

Comments
 (0)