|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2021 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +public func emitCollection<C: Collection>( |
| 14 | + _ collection: C, |
| 15 | + name: String, |
| 16 | + type: String, |
| 17 | + into result: inout String, |
| 18 | + formatter: (C.Element) -> String |
| 19 | +) { |
| 20 | + result += """ |
| 21 | + static const \(type) \(name)[\(collection.count)] = { |
| 22 | + |
| 23 | + """ |
| 24 | + |
| 25 | + formatCollection(collection, into: &result, using: formatter) |
| 26 | + |
| 27 | + result += "\n};\n\n" |
| 28 | +} |
| 29 | + |
| 30 | +public func emitCollection<C: Collection>( |
| 31 | + _ collection: C, |
| 32 | + name: String, |
| 33 | + into result: inout String |
| 34 | +) where C.Element: FixedWidthInteger { |
| 35 | + result += """ |
| 36 | + static const __swift_uint\(C.Element.bitWidth)_t \(name)[\(collection.count)] = { |
| 37 | + |
| 38 | + """ |
| 39 | + |
| 40 | + formatCollection(collection, into: &result) { |
| 41 | + "0x\(String($0, radix: 16, uppercase: true))" |
| 42 | + } |
| 43 | + |
| 44 | + result += "\n};\n\n" |
| 45 | +} |
| 46 | + |
| 47 | +// Emits an abstract minimal perfect hash function into C arrays. |
| 48 | +public func emitMph(_ mph: Mph, name: String, into result: inout String) { |
| 49 | + emitMphSizes(mph, name, into: &result) |
| 50 | + emitMphBitarrays(mph, name, into: &result) |
| 51 | + emitMphRanks(mph, name, into: &result) |
| 52 | +} |
| 53 | + |
| 54 | +// BitArray sizes |
| 55 | +func emitMphSizes(_ mph: Mph, _ name: String, into result: inout String) { |
| 56 | + emitCollection( |
| 57 | + mph.bitArrays, |
| 58 | + name: "\(name)_sizes", |
| 59 | + type: "__swift_uint16_t", |
| 60 | + into: &result |
| 61 | + ) { |
| 62 | + "0x\(String($0.size, radix: 16, uppercase: true))" |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func emitMphBitarrays(_ mph: Mph, _ name: String, into result: inout String) { |
| 67 | + // Individual bitarrays |
| 68 | + |
| 69 | + for (i, ba) in mph.bitArrays.enumerated() { |
| 70 | + emitCollection(ba.words, name: "\(name)_keys\(i)", into: &result) |
| 71 | + } |
| 72 | + |
| 73 | + // Overall bitarrays |
| 74 | + |
| 75 | + emitCollection( |
| 76 | + mph.bitArrays.indices, |
| 77 | + name: "\(name)_keys", |
| 78 | + type: "__swift_uint64_t * const", |
| 79 | + into: &result |
| 80 | + ) { |
| 81 | + "\(name)_keys\($0)" |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func emitMphRanks(_ mph: Mph, _ name: String, into result: inout String) { |
| 86 | + // Individual ranks |
| 87 | + |
| 88 | + for (i, rank) in mph.ranks.enumerated() { |
| 89 | + emitCollection(rank, name: "\(name)_ranks\(i)", into: &result) |
| 90 | + } |
| 91 | + |
| 92 | + // Overall ranks |
| 93 | + |
| 94 | + emitCollection( |
| 95 | + mph.ranks.indices, |
| 96 | + name: "\(name)_ranks", |
| 97 | + type: "__swift_uint16_t * const", |
| 98 | + into: &result |
| 99 | + ) { |
| 100 | + "\(name)_ranks\($0)" |
| 101 | + } |
| 102 | +} |
0 commit comments