示例工程 04-collections-cow

04-collections-cow/Sources/Ch04Demo/App.swift

swift · 2.0 KB

import Ch04Kit
 
@main
struct Ch04Demo {
    static func main() {
        let original = [1, 2, 3]
        let mutated = ValueSemantics.mutateCopy(of: original, at: 0, to: 9)
        print("[4.1] 原数组 = \(mutated.original)")
        print("[4.1] 副本改后:原 = \(mutated.original),副本 = \(mutated.copy)")
 
        let probe = COWProbe([1, 2, 3])
        var copy = probe
        copy.append(4)
        print("[4.2] 副本 copyCount = \(copy.copyCount),原 copyCount = \(probe.copyCount)")
        print("[4.2] 原 = \(probe.values),副本 = \(copy.values)")
 
        print("[4.3] slice = \(SliceLab.facts([10, 20, 30, 40]))")
        let parented = SliceLab.parentMutation([10, 20, 30, 40])
        print("[4.3] 父数组改后:父 = \(parented.parent),切片 = \(parented.slice)")
        print("[4.3] 末尾两个 = \(Array(SliceLab.last([10, 20, 30, 40], count: 2)))")
 
        print("[4.4] Set 排序 = \(CollectionLab.sortedSet([3, 1, 2, 3]))")
        print("[4.4] 字典键排序 = \(CollectionLab.sortedKeys([("b", 2), ("a", 1), ("c", 3)]))")
        print("[4.4] 字典条目排序 = \(CollectionLab.sortedEntries([("b", 2), ("a", 1), ("c", 3)]))")
 
        let scores = [
            Score(name: "bob", points: 90),
            Score(name: "ann", points: 90),
            Score(name: "cy", points: 80),
        ]
        print("[4.5] 排名 = \(SortingLab.describe(scores))")
 
        let reserved = CollectionLab.reserveCapacityProbe(10)
        print("[4.5] reserveCapacity(10):capacity = \(reserved.reserved),足够 = \(reserved.enough)")
 
        print("[4.6] MemoryLayout<Array<Int>> = \(MemoryLayout<Array<Int>>.size) 字节")
        print("[4.6] MemoryLayout<ArraySlice<Int>> = \(MemoryLayout<ArraySlice<Int>>.size) 字节")
        print("[4.6] MemoryLayout<Set<Int>> = \(MemoryLayout<Set<Int>>.size) 字节")
        print(
            "[4.6] MemoryLayout<Dictionary<String, Int>> = \(MemoryLayout<Dictionary<String, Int>>.size) 字节"
        )
    }
}