Skip to content

Commit ee30544

Browse files
author
Reed Es
committed
various changes to make api more consistent
1 parent b9ec622 commit ee30544

File tree

4 files changed

+131
-134
lines changed

4 files changed

+131
-134
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/Packages
44
/*.xcodeproj
55
xcuserdata/
6+
.swiftpm/

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ _SwiftSimpleTree_ is part of the [OpenAlloc](https://github.com/openalloc) famil
1010

1111
```swift
1212
let foo = SimpleTree<String>(value: "foo")
13-
let bar = foo.addChild(forValue: "bar")
14-
let baz = bar.addChild(forValue: "baz")
13+
let bar = foo.addChild(for: "bar")
14+
let baz = bar.addChild(for: "baz")
1515

16-
print(foo.getFirst(forValue: "baz")?.value)
16+
print(foo.getFirst(for: "baz")?.value)
1717

1818
=> "baz"
1919

@@ -28,6 +28,8 @@ print(foo.getAllChildValues())
2828

2929
## Types
3030

31+
Types scoped within `SimpleTree`:
32+
3133
- `typealias Node = SimpleTree<T>` - a tree node, where `T` is your hashable type.
3234

3335
- `typealias ValueSet = Set<T>` - a set of values, where `T` is your hashable type.
@@ -38,46 +40,44 @@ print(foo.getAllChildValues())
3840

3941
- `init(value: T)`: Initialize a new tree (containing the specified value at the root)
4042

41-
- `func addChild(forValue: T) -> Node`: Append a new node (containing the specified value) to our list of children
43+
- `func addChild(for: T) -> Node`: Append a new node (containing the specified value) to our list of children
4244

4345
#### Node Retrieval
4446

45-
- `func getParent(excludeValues: ValueSet) -> Node?`: Return the immediate parent node, if any. Optional list of parent values to be excluded. A match will cause this function to return nil.
46-
47-
- `func getParents(maxDepth: Int, excludeValues: ValueSet) -> [Node]`: Return the parent nodes, starting with immediate parent. Optional list of parent values to be excluded. A match will exclude further ancestors. Optional limit on depth.
47+
- `func getChildren(excludeValues: ValueSet) -> [Node]`: Fetch the child nodes of the node. Optional list of values for children to be excluded, along with their progeny. Traversal is breadth-first.
4848

49-
- `func getChildren(maxDepth: Int, excludeValues: ValueSet) -> [Node]`: Fetch child nodes. Optional list of values for children to be excluded, along with their progeny. Traversal is depth-first, with optional limit.
49+
- `func getChildren(maxDepth: Int, excludeValues: ValueSet) -> [Node]`: Fetch the child nodes of the node. Optional list of values for children to be excluded, along with their progeny. Traversal is depth-first, with optional limit.
5050

51-
#### Iterators
51+
- `func getParent(excludeValues: ValueSet) -> Node?`: Return the immediate parent node, if any. Optional list of parent values to be excluded. A match will cause this function to return nil.
5252

53-
- `func makeChildIterator(excludeValues: ValueSet) -> AnyIterator<Node>`: Create a iterator to traverse through the children (grandchildren, etc.) of the current node. Traversal is breadth-first. With short circuit of child branches on exclude filter.
53+
- `func getParents(maxDepth: Int, excludeValues: ValueSet) -> [Node]`: Return the parent nodes, starting with immediate parent. Optional list of parent values to be excluded. A match will exclude further ancestors. Optional limit on depth.
5454

55-
- `func makeParentIterator() -> AnyIterator<Node>`: Create a iterator to traverse up the tree through the parents of the current node, starting with the most recent parent, if any.
55+
- `func getAll(excludeValues: ValueSet) -> [Node]`: Fetch the node and its child nodes. Optional list of values for children to be excluded, along with their progeny. Traversal is breadth-first.
5656

57-
#### Iterative Node Search
57+
#### Node Search
5858

59-
- `func getFirst(forValue: T) -> Node?`: Traverse from the current node to find the first node with the specified value. Includes current node. Traversal is breadth-first.
59+
- `func getFirst(for: T) -> Node?`: Traverse from the current node to find the first node with the specified value. Traversal is breadth-first.
6060

61-
- `func getFirstChild(forValue: T) -> Node?`: Traverse the children from the current node to find the first child (grandchild, etc.) with the specified value. Traversal is breadth-first.
61+
- `func getFirstChild(for: T) -> Node?`: Traverse the children from the current node to find the first child (grandchild, etc.) with the specified value. Traversal is breadth-first.
6262

63-
#### Iterative Node Retrieval
63+
#### Iterators
6464

65-
- `func getAll(excludeValues: ValueSet) -> [Node]`: Flatten tree from current node. Includes current node. Optional list of values for children to be excluded, along with their progeny. Traversal is breadth-first.
65+
- `func makeChildIterator(excludeValues: ValueSet) -> AnyIterator<Node>`: Create a iterator to traverse through the children (grandchildren, etc.) of the current node. Traversal is breadth-first. With short circuit of child branches on exclude filter.
6666

67-
- `func getAllChildren(excludeValues: ValueSet) -> [Node]`: Get all the child nodes from the current node. Optional list of values for children to be excluded, along with their progeny. Traversal is breadth-first.
67+
- `func makeParentIterator() -> AnyIterator<Node>`: Create a iterator to traverse up the tree through the parents of the current node, starting with the most recent parent, if any.
6868

6969
#### Value retrieval
7070

71-
- `func getAllChildValues(excludeValues: ValueSet) -> [T]`: Get all the child values from the current node. Optional list of values for children to be excluded, along with their progeny. Traversal is breadth-first.
72-
73-
- `func getAllValues(excludeValues: ValueSet) -> [T]`: Flatten tree from current node. Includes value of current node. Optional list of values for children to be excluded, along with their progeny. Traversal is breadth-first.
71+
- `func getChildValues(excludeValues: ValueSet) -> [T]`: Fetch the values of the child nodes. Optional list of values for children to be excluded, along with their progeny. Traversal is breadth-first.
7472

75-
- `func getChildValues(maxDepth: Int, excludeValues: ValueSet) -> [T]`: Fetch child values. Optional list of values for children to be excluded, along with their progeny. Traversal is depth-first, with optional limit.
73+
- `func getChildValues(maxDepth: Int, excludeValues: ValueSet) -> [T]`: Fetch the values of the child nodes. Optional list of values for children to be excluded, along with their progeny. Traversal is depth-first, with optional limit.
7674

7775
- `func getParentValue(excludeValues: ValueSet) -> T?`: Return the value of the immediate parent node, if any. Optional list of parent values to be excluded. A match will cause this function to return nil.
7876

7977
- `func getParentValues(maxDepth: Int, excludeValues: ValueSet) -> [T]`: Return the values of the parent nodes, starting with immediate parent. Optional list of parent values to be excluded. A match will exclude further ancestors. Optional limit on depth.
8078

79+
- `func getAllValues(excludeValues: ValueSet) -> [T]`: Fetch values for the node and its child nodes. Includes value of current node. Optional list of values for children to be excluded, along with their progeny. Traversal is breadth-first.
80+
8181
## See Also
8282

8383
Swift open-source libraries (by the same author):

Sources/SimpleTree.swift

Lines changed: 39 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class SimpleTree<T> where T: Equatable & Hashable {
3737
extension SimpleTree {
3838

3939
/// Append a new node (containing the specified value) to our list of children
40-
public func addChild(forValue value: T) -> Node {
40+
public func addChild(for value: T) -> Node {
4141
let nuChild = Node(value: value)
4242
children.append(nuChild)
4343
nuChild.parent = self
@@ -74,10 +74,10 @@ extension SimpleTree {
7474
getParents(maxDepth: 1, excludeValues: excludeValues).first
7575
}
7676

77-
/// Fetch child nodes.
77+
/// Fetch the child nodes of the node.
7878
/// Optional list of values for children to be excluded, along with their progeny.
7979
/// Traversal is depth-first, with optional limit.
80-
public func getChildren(maxDepth: Int = Int.max, excludeValues: ValueSet = ValueSet()) -> [Node] {
80+
public func getChildren(maxDepth: Int, excludeValues: ValueSet = ValueSet()) -> [Node] {
8181
guard maxDepth > 0 else { return [] }
8282
var nodes = [Node]()
8383
for child in children {
@@ -89,6 +89,28 @@ extension SimpleTree {
8989
}
9090
return nodes
9191
}
92+
93+
/// Fetch the child nodes of the node.
94+
/// Optional list of values for children to be excluded, along with their progeny.
95+
/// Traversal is breadth-first.
96+
public func getChildren(excludeValues: ValueSet = ValueSet()) -> [Node] {
97+
var nodes = [Node]()
98+
let iter = self.makeChildIterator(excludeValues: excludeValues)
99+
while let node = iter.next() {
100+
nodes.append(node)
101+
}
102+
return nodes
103+
}
104+
105+
/// Fetch the node and its child nodes.
106+
/// Optional list of values for children to be excluded, along with their progeny.
107+
/// Traversal is breadth-first.
108+
public func getAll(excludeValues: ValueSet = ValueSet()) -> [Node] {
109+
guard !excludeValues.contains(self.value) else { return [] }
110+
var nodes: [Node] = [self]
111+
nodes.append(contentsOf: getChildren(excludeValues: excludeValues))
112+
return nodes
113+
}
92114
}
93115

94116
// MARK: - Iterators
@@ -100,9 +122,9 @@ extension SimpleTree {
100122
var node: Node? = self.parent
101123
return AnyIterator { () -> Node? in
102124
guard let _node = node else { return nil }
103-
let currentNode = _node
125+
let nextNode = _node
104126
node = _node.parent // okay if nil
105-
return currentNode
127+
return nextNode
106128
}
107129
}
108130

@@ -144,13 +166,13 @@ extension SimpleTree {
144166
}
145167
}
146168

147-
// MARK: - Iterative Node Search
169+
// MARK: - Node Search
148170

149171
extension SimpleTree {
150172

151173
/// Traverse the children from the current node to find the first child (grandchild, etc.) with the specified value.
152174
/// Traversal is breadth-first.
153-
public func getFirstChild(forValue value: T) -> Node? {
175+
public func getFirstChild(for value: T) -> Node? {
154176
let iter = self.makeChildIterator()
155177
while let node = iter.next() {
156178
if node.value == value {
@@ -160,38 +182,12 @@ extension SimpleTree {
160182
return nil
161183
}
162184

163-
/// Traverse from the current node to find the first node with the specified value. Includes current node.
185+
/// Traverse from the current node to find the first node with the specified value.
186+
/// Includes current node.
164187
/// Traversal is breadth-first.
165-
public func getFirst(forValue value: T) -> Node? {
188+
public func getFirst(for value: T) -> Node? {
166189
guard self.value != value else { return self }
167-
return getFirstChild(forValue: value)
168-
}
169-
}
170-
171-
// MARK: - Iterative Node Retrieval
172-
173-
extension SimpleTree {
174-
175-
/// Get all the child nodes from the current node.
176-
/// Optional list of values for children to be excluded, along with their progeny.
177-
/// Traversal is breadth-first.
178-
public func getAllChildren(excludeValues: ValueSet = ValueSet()) -> [Node] {
179-
var nodes = [Node]()
180-
let iter = self.makeChildIterator(excludeValues: excludeValues)
181-
while let node = iter.next() {
182-
nodes.append(node)
183-
}
184-
return nodes
185-
}
186-
187-
/// Flatten tree from current node. Includes current node.
188-
/// Optional list of values for children to be excluded, along with their progeny.
189-
/// Traversal is breadth-first.
190-
public func getAll(excludeValues: ValueSet = ValueSet()) -> [Node] {
191-
guard !excludeValues.contains(self.value) else { return [] }
192-
var nodes: [Node] = [self]
193-
nodes.append(contentsOf: getAllChildren(excludeValues: excludeValues))
194-
return nodes
190+
return getFirstChild(for: value)
195191
}
196192
}
197193

@@ -212,21 +208,21 @@ extension SimpleTree {
212208
getParent(excludeValues: excludeValues)?.value
213209
}
214210

215-
/// Fetch child values.
211+
/// Fetch the values of the child nodes.
216212
/// Optional list of values for children to be excluded, along with their progeny.
217213
/// Traversal is depth-first, with optional limit.
218-
public func getChildValues(maxDepth: Int = Int.max, excludeValues: ValueSet = ValueSet()) -> [T] {
214+
public func getChildValues(maxDepth: Int, excludeValues: ValueSet = ValueSet()) -> [T] {
219215
getChildren(maxDepth: maxDepth, excludeValues: excludeValues).map(\.value)
220216
}
221217

222-
/// Get all the child values from the current node.
218+
/// Fetch the values of the child nodes.
223219
/// Optional list of values for children to be excluded, along with their progeny.
224220
/// Traversal is breadth-first.
225-
public func getAllChildValues(excludeValues: ValueSet = ValueSet()) -> [T] {
226-
getAllChildren(excludeValues: excludeValues).map(\.value)
221+
public func getChildValues(excludeValues: ValueSet = ValueSet()) -> [T] {
222+
getChildren(excludeValues: excludeValues).map(\.value)
227223
}
228224

229-
/// Flatten tree from current node. Includes value of current node.
225+
/// Fetch values for the node and its child nodes.
230226
/// Optional list of values for children to be excluded, along with their progeny.
231227
/// Traversal is breadth-first.
232228
public func getAllValues(excludeValues: ValueSet = ValueSet()) -> [T] {

0 commit comments

Comments
 (0)