/
Node.swift
83 lines (65 loc) · 1.72 KB
/
Node.swift
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
81
82
//
// Created by Viktor Belenyesi on 19/04/15.
// Copyright (c) 2015 Viktor Belenyesi. All rights reserved.
//
public class Node: Hashable {
private static var hashCounter = AtomicLong(0)
private let childrenHolder = SpinSet<WeakSet<Node>>(WeakSet())
public let hashValue = Int(Node.hashCounter.getAndIncrement()) // Hashable
public var children: Set<Node> {
return childrenHolder.value.filter {
$0.parents.contains(self)
}.set
}
public var descendants: Set<Node> {
return children ++ children.flatMap {
$0.descendants
}
}
public var parents: Set<Node> {
fatalError(ABSTRACT_METHOD)
}
public var ancestors: Set<Node> {
return parents ++ parents.flatMap {
$0.ancestors
}
}
func isSuccess() -> Bool {
return true
}
func error() -> NSError {
fatalError(ABSTRACT_METHOD)
}
func linkChild(child: Node) {
childrenHolder.spinSet { c in
guard (c.hasElementPassingTest {$0 == child}) else {
c.insert(child)
return c
}
return c
}
}
func unlinkChild(child: Node) {
childrenHolder.spinSet {
$0.filter {
$0 != child
}
}
}
func ping(incoming: Set<Node>) -> Set<Node> {
fatalError(ABSTRACT_METHOD)
}
private var alive = true
public func kill() {
alive = false
parents.forEach {
$0.unlinkChild(self)
}
}
var level: long {
fatalError(ABSTRACT_METHOD)
}
}
public func ==(lhs: Node, rhs: Node) -> Bool {
return lhs.hashValue == rhs.hashValue
}