/
TreeGeneType.swift
55 lines (47 loc) · 1.59 KB
/
TreeGeneType.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
//
// TreeGeneType.swift
// SwiftGenetics
//
// Created by Santiago Gonzalez on 6/27/19.
// Copyright © 2019 Santiago Gonzalez. All rights reserved.
//
import Foundation
/// An abstract interface that all tree gene types conform to.
protocol TreeGeneType: Hashable {
var childCount: Int { get }
var isBinaryType: Bool { get }
var isUnaryType: Bool { get }
var isLeafType: Bool { get }
static var binaryTypes: [Self] { get }
static var unaryTypes: [Self] { get }
static var leafTypes: [Self] { get }
static var nonLeafTypes: [Self] { get }
static var allTypes: [Self] { get }
}
extension TreeGeneType {
var isBinaryType: Bool { return Self.binaryTypes.contains(self) }
var isUnaryType: Bool { return Self.unaryTypes.contains(self) }
var isLeafType: Bool { return Self.leafTypes.contains(self) }
static var nonLeafTypes: [Self] { return binaryTypes + unaryTypes }
static var allTypes: [Self] { return nonLeafTypes + leafTypes }
}
/// Templates can enforce certain constraints and define gene type sampling.
struct TreeGeneTemplate<T: TreeGeneType> {
/// Sampling array for binary gene types.
let binaryTypes: [T]
/// Sampling array for unary gene types.
let unaryTypes: [T]
/// Sampling array for leaf gene types.
let leafTypes: [T]
/// A sampling array for non-leaf types.
var nonLeafTypes: [T] { return binaryTypes + unaryTypes }
/// A sampling array for all types.
var allTypes: [T] { return nonLeafTypes + leafTypes }
}
extension TreeGeneTemplate: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(binaryTypes)
hasher.combine(unaryTypes)
hasher.combine(leafTypes)
}
}