1
1
mirror of https://github.com/github/semantic.git synced 2024-11-24 08:54:07 +03:00

Add a Cofree.size function.

This should avoid the protocol indirection around `TermType`.
This commit is contained in:
Rob Rix 2015-11-12 17:44:35 -05:00
parent 8cf0ef097c
commit 822ce7ccc5

View File

@ -201,4 +201,25 @@ extension Cofree {
}
// MARK: - Size
extension Cofree {
/// The count of nodes in the receiver.
///
/// This is used to compute the cost of patches, such that a patch inserting a very large tree will be charged approximately the same as a very large tree consisting of many small patches.
public static func size(term: Cofree) -> Int {
switch term.unwrap {
case .Leaf:
return 1
case let .Indexed(a):
return a.reduce(0) { $0 + size($1) }
case let .Fixed(a):
return a.reduce(0) { $0 + size($1) }
case let .Keyed(a):
return a.reduce(0) { $0 + size($1.1) }
}
}
}
import Prelude