-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMMFileTree.swift
More file actions
81 lines (62 loc) · 1.68 KB
/
MMFileTree.swift
File metadata and controls
81 lines (62 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
//
// MMFileTree.swift
// MMTreeTablViewExample
//
// Created by JefferyYu on 2022/2/26.
//
import UIKit
class MMFileTree<E> {
/**
核心是 子node 和父node之间的关系
*/
var root: MMNode<E>
required init(root: MMNode<E>) {
self.root = root
}
public func add(_ node: MMNode<E>, to parent: MMNode<E>) {
}
public func delete(_ node: MMNode<E>, from parent: MMNode<E>? = nil) {
}
public func update(_ node: MMNode<E>, form parentOld: MMNode<E>, to parentNew: MMNode<E>) {
}
var childrenCount: Int {
return root.children.count
}
}
public func += (lhs: MMNode<Any>, rhs: MMNode<Any>) { lhs.children.append(rhs) }
public func += (lhs: MMNode<Any>, rhs: [MMNode<Any>]) { lhs.children += rhs }
public class MMNode<E> {
var element: E
var parent: MMNode<E>?
var children = [MMNode<E>]()
var isOpen: Bool
var numberOfChildren: Int { get { children.count } }
init(element: E, parent: MMNode<E>? = nil, isOpen: Bool = false) {
self.element = element
self.parent = parent
self.isOpen = isOpen
}
func add(_ node: MMNode<E>) {
children += node
node.parent = self
}
func add(_ nodes: [MMNode<E>]) {
children += nodes
nodes.forEach { $0.parent = self }
}
func child(at: Int) -> MMNode<E>? {
if children.count <= at { return nil }
return children[at]
}
var depth: Int {
get {
var result = 0
var myParent = parent
while myParent != nil {
result += 1
myParent = myParent?.parent
}
return result
}
}
}