Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions bench/algorithm/binarytrees/2.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import Foundation

let minDepth = 4
let maxDepth: Int = {
if CommandLine.argc <= 1 { return 10 }
return max(minDepth + 2, Int(CommandLine.arguments[1]) ?? 10)
}()

let stretchDepth = maxDepth + 1
print("stretch tree of depth \(stretchDepth)\t check: \(Tree(depth: stretchDepth).check())")

let longLivedTree = Tree(depth: maxDepth)
let nResults = (maxDepth - minDepth) / 2 + 1

for i in 0..<nResults {
let depth = i * 2 + minDepth
let n = 1 << (maxDepth - depth + minDepth)

var chk = 0
for _ in 0..<n {
chk += Tree(depth: depth).check()
}
print("\(n)\t trees of depth \(depth)\t check: \(chk)")
}

print("long lived tree of depth \(maxDepth)\t check: \(longLivedTree.check())")

struct Tree {
struct Node {
var left: Int
var right: Int
}

private static let none: Int = -1
private var nodes: [Node] = []
private var root: Int = none

init(depth: Int) {
let total = (1 << (depth + 1)) - 1
guard total > 0 else { return }

nodes = Array(repeating: Node(left: Self.none, right: Self.none), count: total)
root = 0

for i in 0..<total {
let l = 2 * i + 1
let r = l + 1
if l < total { nodes[i].left = l }
if r < total { nodes[i].right = r }
}
}

func check() -> Int {
let span = nodes.span
return _check(index: root, span: span)
}

private func _check(index: Int, span: Span<Node>) -> Int {
if index == Self.none { return 0 }
let node = span[Int(index)]
return 1 + _check(index: node.left, span: span)
+ _check(index: node.right, span: span)
}
}
3 changes: 2 additions & 1 deletion bench/bench_swift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ problems:
- name: binarytrees
source:
- 1.swift
- 2.swift
- name: nbody
source:
- 7.swift
Expand All @@ -30,7 +31,7 @@ environments:
- os: linux
compiler: swift
version: latest
docker: swift:jammy
docker: swift:6.2.0-slim
include: swift
include_sub_dir: Sources/app
build: swift build --static-swift-stdlib -c release -v -Xswiftc -Ounchecked
Expand Down