Skip to content

Commit 22efd0d

Browse files
author
Erik Strottmann
committed
Standardize Trie indentation to 2 spaces
2 spaces is recommended by the Ray Wenderlich Swift Style Guide.
1 parent 0c93543 commit 22efd0d

File tree

7 files changed

+280
-282
lines changed

7 files changed

+280
-282
lines changed

Trie/ReadMe.md

+20-20
Original file line numberDiff line numberDiff line change
@@ -27,29 +27,29 @@ Tries are very useful for certain situations. Here are some of the advantages:
2727

2828
```swift
2929
func contains(word: String) -> Bool {
30-
guard !word.isEmpty else { return false }
30+
guard !word.isEmpty else { return false }
3131

32-
// 1
33-
var currentNode = root
32+
// 1
33+
var currentNode = root
3434

35-
// 2
36-
var characters = Array(word.lowercased())
37-
var currentIndex = 0
35+
// 2
36+
var characters = Array(word.lowercased())
37+
var currentIndex = 0
3838

39-
// 3
40-
while currentIndex < characters.count,
41-
let child = currentNode.children[characters[currentIndex]] {
42-
43-
currentNode = child
44-
currentIndex += 1
45-
}
46-
47-
// 4
48-
if currentIndex == characters.count && currentNode.isTerminating {
49-
return true
50-
} else {
51-
return false
52-
}
39+
// 3
40+
while currentIndex < characters.count,
41+
let child = currentNode.children[characters[currentIndex]] {
42+
43+
currentNode = child
44+
currentIndex += 1
45+
}
46+
47+
// 4
48+
if currentIndex == characters.count && currentNode.isTerminating {
49+
return true
50+
} else {
51+
return false
52+
}
5353
}
5454
```
5555

Trie/Trie/Trie.xcodeproj/project.pbxproj

+2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@
8787
EB798E191DFEF79900F0628D /* TrieUITests */,
8888
EB798DFB1DFEF79900F0628D /* Products */,
8989
);
90+
indentWidth = 2;
9091
sourceTree = "<group>";
92+
tabWidth = 2;
9193
};
9294
EB798DFB1DFEF79900F0628D /* Products */ = {
9395
isa = PBXGroup;

Trie/Trie/Trie/AppDelegate.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ import Cocoa
1111
@NSApplicationMain
1212
class AppDelegate: NSObject, NSApplicationDelegate {
1313

14-
func applicationDidFinishLaunching(_ aNotification: Notification) {
15-
// Insert code here to initialize your application
16-
}
14+
func applicationDidFinishLaunching(_ aNotification: Notification) {
15+
// Insert code here to initialize your application
16+
}
1717

18-
func applicationWillTerminate(_ aNotification: Notification) {
19-
// Insert code here to tear down your application
20-
}
18+
func applicationWillTerminate(_ aNotification: Notification) {
19+
// Insert code here to tear down your application
20+
}
2121

2222
}

Trie/Trie/Trie/Trie.swift

+58-60
Original file line numberDiff line numberDiff line change
@@ -43,54 +43,53 @@ class TrieNode<T: Hashable> {
4343
/// A trie data structure containing words. Each node is a single
4444
/// character of a word.
4545
class Trie: NSObject, NSCoding {
46-
typealias Node = TrieNode<Character>
47-
/// The number of words in the trie
48-
public var count: Int {
49-
return wordCount
50-
}
51-
/// Is the trie empty?
52-
public var isEmpty: Bool {
53-
return wordCount == 0
54-
}
55-
/// All words currently in the trie
56-
public var words: [String] {
57-
return wordsInSubtrie(rootNode: root, partialWord: "")
58-
}
59-
fileprivate let root: Node
60-
fileprivate var wordCount: Int
61-
62-
/// Creates an empty trie.
63-
override init() {
64-
root = Node()
65-
wordCount = 0
66-
super.init()
67-
}
46+
typealias Node = TrieNode<Character>
47+
/// The number of words in the trie
48+
public var count: Int {
49+
return wordCount
50+
}
51+
/// Is the trie empty?
52+
public var isEmpty: Bool {
53+
return wordCount == 0
54+
}
55+
/// All words currently in the trie
56+
public var words: [String] {
57+
return wordsInSubtrie(rootNode: root, partialWord: "")
58+
}
59+
fileprivate let root: Node
60+
fileprivate var wordCount: Int
61+
62+
/// Creates an empty trie.
63+
override init() {
64+
root = Node()
65+
wordCount = 0
66+
super.init()
67+
}
6868

69-
// MARK: NSCoding
69+
// MARK: NSCoding
7070

71-
/// Initializes the trie with words from an archive
72-
///
73-
/// - Parameter decoder: Decodes the archive
74-
required convenience init?(coder decoder: NSCoder) {
75-
self.init()
76-
let words = decoder.decodeObject(forKey: "words") as? [String]
77-
for word in words! {
78-
self.insert(word: word)
79-
}
71+
/// Initializes the trie with words from an archive
72+
///
73+
/// - Parameter decoder: Decodes the archive
74+
required convenience init?(coder decoder: NSCoder) {
75+
self.init()
76+
let words = decoder.decodeObject(forKey: "words") as? [String]
77+
for word in words! {
78+
self.insert(word: word)
8079
}
80+
}
8181

82-
/// Encodes the words in the trie by putting them in an array then encoding
83-
/// the array.
84-
///
85-
/// - Parameter coder: The object that will encode the array
86-
func encode(with coder: NSCoder) {
87-
coder.encode(self.words, forKey: "words")
88-
}
82+
/// Encodes the words in the trie by putting them in an array then encoding
83+
/// the array.
84+
///
85+
/// - Parameter coder: The object that will encode the array
86+
func encode(with coder: NSCoder) {
87+
coder.encode(self.words, forKey: "words")
88+
}
8989
}
9090

9191
// MARK: - Adds methods: insert, remove, contains
9292
extension Trie {
93-
9493
/// Inserts a word into the trie. If the word is already present,
9594
/// there is no change.
9695
///
@@ -142,14 +141,14 @@ extension Trie {
142141
/// - Returns: the node where the search ended, nil if the
143142
/// search failed.
144143
private func findLastNodeOf(word: String) -> Node? {
145-
var currentNode = root
146-
for character in word.lowercased() {
147-
guard let childNode = currentNode.children[character] else {
148-
return nil
149-
}
150-
currentNode = childNode
144+
var currentNode = root
145+
for character in word.lowercased() {
146+
guard let childNode = currentNode.children[character] else {
147+
return nil
151148
}
152-
return currentNode
149+
currentNode = childNode
150+
}
151+
return currentNode
153152
}
154153

155154
/// Attempts to walk to the terminating node of a word. The
@@ -160,10 +159,9 @@ extension Trie {
160159
/// search failed.
161160
private func findTerminalNodeOf(word: String) -> Node? {
162161
if let lastNode = findLastNodeOf(word: word) {
163-
return lastNode.isTerminating ? lastNode : nil
162+
return lastNode.isTerminating ? lastNode : nil
164163
}
165164
return nil
166-
167165
}
168166

169167
/// Deletes a word from the trie by starting with the last letter
@@ -237,17 +235,17 @@ extension Trie {
237235
/// - prefix: the letters for word prefix
238236
/// - Returns: the words in the subtrie that start with prefix
239237
func findWordsWithPrefix(prefix: String) -> [String] {
240-
var words = [String]()
241-
let prefixLowerCased = prefix.lowercased()
242-
if let lastNode = findLastNodeOf(word: prefixLowerCased) {
243-
if lastNode.isTerminating {
244-
words.append(prefixLowerCased)
245-
}
246-
for childNode in lastNode.children.values {
247-
let childWords = wordsInSubtrie(rootNode: childNode, partialWord: prefixLowerCased)
248-
words += childWords
249-
}
238+
var words = [String]()
239+
let prefixLowerCased = prefix.lowercased()
240+
if let lastNode = findLastNodeOf(word: prefixLowerCased) {
241+
if lastNode.isTerminating {
242+
words.append(prefixLowerCased)
243+
}
244+
for childNode in lastNode.children.values {
245+
let childWords = wordsInSubtrie(rootNode: childNode, partialWord: prefixLowerCased)
246+
words += childWords
250247
}
251-
return words
248+
}
249+
return words
252250
}
253251
}

Trie/Trie/Trie/ViewController.swift

+8-8
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,16 @@ import Cocoa
1010

1111
class ViewController: NSViewController {
1212

13-
override func viewDidLoad() {
14-
super.viewDidLoad()
13+
override func viewDidLoad() {
14+
super.viewDidLoad()
1515

16-
// Do any additional setup after loading the view.
17-
}
16+
// Do any additional setup after loading the view.
17+
}
1818

19-
override var representedObject: Any? {
20-
didSet {
21-
// Update the view, if already loaded.
22-
}
19+
override var representedObject: Any? {
20+
didSet {
21+
// Update the view, if already loaded.
2322
}
23+
}
2424

2525
}

0 commit comments

Comments
 (0)