Skip to content

Commit 15458ff

Browse files
committed
Merge branch 'master' of https://github.com/dn-m/Structure
2 parents 60d646e + b179331 commit 15458ff

13 files changed

+1006
-70
lines changed

Sources/DataStructures/ContiguousSegmentCollection/ContiguousSegmentCollection.swift

Lines changed: 512 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Fragmentable.swift
3+
// DataStructures
4+
//
5+
// Created by James Bean on 8/22/18.
6+
//
7+
8+
/// Interface for types which can be fragmented into smaller pieces.
9+
public protocol Fragmentable {
10+
11+
// MARK: - Associated Types
12+
13+
/// Type of fragment that is created from this type.
14+
associatedtype Fragment
15+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Intervallic.swift
3+
// DataStructures
4+
//
5+
// Created by James Bean on 8/22/18.
6+
//
7+
8+
import Algebra
9+
10+
public protocol Intervallic: Measured {
11+
12+
// MARK - Associated Types
13+
14+
/// The type which is used to measure this type.
15+
associatedtype Metric
16+
17+
// MARK: - Instance Properties
18+
19+
/// Length of the `Spanning` type in the given `Metric`.
20+
var length: Metric { get }
21+
}
22+
23+
extension Numeric where Self: Comparable {
24+
25+
/// - Returns: Self as its length.
26+
public var length: Self {
27+
return self
28+
}
29+
}
30+
31+
extension Int: Intervallic { }
32+
extension Float: Intervallic { }
33+
extension Double: Intervallic { }
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// IntervallicFragmentable.swift
3+
// DataStructures
4+
//
5+
// Created by James Bean on 8/24/18.
6+
//
7+
8+
import Algebra
9+
10+
/// Interface for types which are `Intervallic`, and can be fragmented into a type which shares its
11+
/// `Metric` type.
12+
public protocol IntervallicFragmentable: Intervallic, Fragmentable
13+
where Fragment: Intervallic, Fragment.Metric == Metric
14+
{
15+
16+
// MARK: - Associated Types
17+
18+
/// The fragment of an `IntervallicFragmentable` type.
19+
associatedtype Fragment
20+
21+
// MARK: - Instance Methods
22+
23+
/// - Returns: The `Fragment` in the given `range`.
24+
func fragment(in range: Range<Metric>) -> Fragment
25+
}
26+
27+
extension IntervallicFragmentable where Metric: Zero {
28+
29+
/// - Returns: The `Fragment` of this `IntervallicFragmentable`-conforming type value in the
30+
/// given `range`.
31+
public func fragment(in range: PartialRangeUpTo<Metric>) -> Fragment {
32+
return fragment(in: .zero ..< range.upperBound)
33+
}
34+
35+
/// - Returns: The `Fragment` of this `IntervallicFragmentable`-conforming type value in the
36+
/// given `range`.
37+
public func fragment(in range: PartialRangeFrom<Metric>) -> Fragment {
38+
return fragment(in: range.lowerBound ..< length)
39+
}
40+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//
2+
// Measured.swift
3+
// DataStructures
4+
//
5+
// Created by James Bean on 8/22/18.
6+
//
7+
8+
/// Interface for types which are measured by some `Metric` type.
9+
public protocol Measured {
10+
11+
// MARK: - Associated Types
12+
13+
/// The type which is used to measure this type.
14+
associatedtype Metric: SignedNumeric, Comparable
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// Totalizable.swift
3+
// DataStructures
4+
//
5+
// Created by James Bean on 8/22/18.
6+
//
7+
8+
import Algebra
9+
10+
/// Interface for fragment types which can create a fragment version of a whole instance of the
11+
/// type.
12+
public protocol Totalizable {
13+
14+
// MARK: - Associated Types
15+
16+
/// The type of the given `Whole`.
17+
associatedtype Whole
18+
19+
// MARK: - Initializers
20+
21+
/// Creates a `Totalizable` fragment with an instance of the `Whole` type.
22+
init(whole: Whole)
23+
}

Sources/DataStructures/DictionaryProtocol.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public protocol DictionaryProtocol: Collection {
3030

3131
/// - Returns: `Value` for the given `key`, if present. Otherwise, `nil`.
3232
subscript (key: Key) -> Value? { get set }
33+
34+
/// Reserves the required amount of memory to store the given `minimumCapacity` of key-value
35+
/// pairs.
36+
mutating func reserveCapacity(_ minimumCapacity: Int)
3337
}
3438

3539
extension DictionaryProtocol {

Sources/DataStructures/OrderedDictionary.swift

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public struct OrderedDictionary <Key: Hashable, Value>: DictionaryProtocol {
1515
public var keys: [Key] = []
1616

1717
/// Values.
18-
public var values: [Key: Value] = [:]
18+
public var unordered: [Key: Value] = [:]
1919

2020
// MARK: - Initializers
2121

@@ -27,7 +27,7 @@ public struct OrderedDictionary <Key: Hashable, Value>: DictionaryProtocol {
2727
public init(minimumCapacity: Int) {
2828
self.keys = []
2929
self.keys.reserveCapacity(minimumCapacity)
30-
self.values = .init(minimumCapacity: minimumCapacity)
30+
self.unordered = .init(minimumCapacity: minimumCapacity)
3131
}
3232

3333
// MARK: - Subscripts
@@ -36,18 +36,18 @@ public struct OrderedDictionary <Key: Hashable, Value>: DictionaryProtocol {
3636
public subscript(key: Key) -> Value? {
3737

3838
get {
39-
return values[key]
39+
return unordered[key]
4040
}
4141

4242
set {
4343

4444
guard let newValue = newValue else {
45-
values.removeValue(forKey: key)
45+
unordered.removeValue(forKey: key)
4646
keys = keys.filter { $0 != key }
4747
return
4848
}
4949

50-
let oldValue = values.updateValue(newValue, forKey: key)
50+
let oldValue = unordered.updateValue(newValue, forKey: key)
5151
if oldValue == nil {
5252
keys.append(key)
5353
}
@@ -59,31 +59,33 @@ public struct OrderedDictionary <Key: Hashable, Value>: DictionaryProtocol {
5959
/// Append `value` for given `key`.
6060
public mutating func append(_ value: Value, key: Key) {
6161
keys.append(key)
62-
values[key] = value
62+
unordered[key] = value
6363
}
6464

6565
/// Insert `value` for given `key` at `index`.
6666
public mutating func insert(_ value: Value, key: Key, index: Int) {
6767
keys.insert(key, at: index)
68-
values[key] = value
68+
unordered[key] = value
6969
}
7070

7171
/// Append the contents of another `OrderedDictionary` structure.
7272
public mutating func appendContents(of orderedDictionary: OrderedDictionary<Key,Value>) {
7373
keys.append(contentsOf: orderedDictionary.keys)
7474
for key in orderedDictionary.keys {
75-
values.updateValue(orderedDictionary[key]!, forKey: key)
75+
unordered.updateValue(orderedDictionary[key]!, forKey: key)
7676
}
7777
}
7878

79-
/// - returns: The value at the given `index`.
80-
public func value(index: Int) -> Value? {
81-
82-
guard index >= 0 && index < keys.count else {
83-
return nil
84-
}
79+
/// Reserves the amount of memory required to store the given `minimumCapacity` of elements.
80+
public mutating func reserveCapacity(_ minimumCapacity: Int) {
81+
keys.reserveCapacity(minimumCapacity)
82+
unordered.reserveCapacity(minimumCapacity)
83+
}
8584

86-
return values[keys[index]]
85+
/// - Returns: The value at the given `index`.
86+
public func value(index: Int) -> Value? {
87+
guard index >= 0 && index < keys.count else { return nil }
88+
return unordered[keys[index]]
8789
}
8890
}
8991

@@ -114,24 +116,21 @@ extension OrderedDictionary: Collection {
114116
/// - returns: Element at the given `index`.
115117
public subscript (index: Int) -> (Key, Value) {
116118
let key = keys[index]
117-
let value = values[key]!
119+
let value = unordered[key]!
118120
return (key, value)
119121
}
120122
}
121123

122124
extension OrderedDictionary: Equatable where Value: Equatable { }
125+
extension OrderedDictionary: Hashable where Value: Hashable { }
123126

124127
extension OrderedDictionary: ExpressibleByDictionaryLiteral {
125128

126129
// MARK: - ExpressibleByDictionaryLiteral
127130

128131
/// Create an `OrderedDictionary` with a `DictionaryLiteral`.
129132
public init(dictionaryLiteral elements: (Key, Value)...) {
130-
131-
self.init()
132-
133-
elements.forEach { (k, v) in
134-
append(v, key: k)
135-
}
133+
self.init(minimumCapacity: elements.count)
134+
elements.forEach { (k, v) in append(v, key: k) }
136135
}
137136
}

Sources/DataStructures/SortedArray.swift

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,27 @@ public struct SortedArray <Element: Comparable>:
3535
self.init(presorted: Array(elements).sorted())
3636
}
3737

38-
/// Create a `SortedArray` with the given array of presorted elements.
38+
/// Create a `SortedArray` with the given collection of presorted elements.
3939
///
4040
/// - Warning: You must be certain that `presorted` is sorted, otherwise undefined behavior is
4141
/// certain.
4242
public init <C> (presorted: C) where C: Collection, C.Element == Element {
4343
self.base = Array(presorted)
4444
}
4545

46+
/// Create a `SortedArray` with the given array of presorted elements.
47+
///
48+
/// - Warning: You must be certain that `presorted` is sorted, otherwise undefined behavior is
49+
/// certain.
50+
public init(presorted: [Element]) {
51+
self.base = presorted
52+
}
53+
54+
/// Creates a `SortedArray` with the contents of another one.
55+
public init(_ sorted: SortedArray) {
56+
self.init(presorted: sorted.base)
57+
}
58+
4659
// MARK: - Instance Methods
4760

4861
/// Remove the given `element`, if it is contained herein.
@@ -62,12 +75,23 @@ public struct SortedArray <Element: Comparable>:
6275
}
6376

6477
/// Insert the contents of another sequence of `T`.
65-
public mutating func insert <S> (contentsOf elements: S)
66-
where S: Sequence, S.Element == Element
67-
{
78+
public mutating func insert <S: Sequence> (contentsOf elements: S) where S.Element == Element {
6879
elements.forEach { insert($0) }
6980
}
7081

82+
/// Reserves the amount of memory to store the given `minimumCapacity` of elements.
83+
public mutating func reserveCapacity(_ minimumCapacity: Int) {
84+
base.reserveCapacity(minimumCapacity)
85+
}
86+
87+
/// Appends the given `element`.
88+
///
89+
/// - Warning: This element _must_ be greater or equal to the current maximum, otherwise there
90+
/// will be undefined behavior ahead.
91+
public mutating func append(guaranteedMax element: Element) {
92+
base.append(element)
93+
}
94+
7195
/// - Returns: Index for the given `element`, if it exists. Otherwise, `nil`.
7296
public func index(of element: Element) -> Int? {
7397
let index = self.index(for: element)
@@ -95,6 +119,7 @@ public struct SortedArray <Element: Comparable>:
95119
}
96120

97121
extension SortedArray: Equatable { }
122+
extension SortedArray: Hashable where Element: Hashable { }
98123

99124
extension SortedArray {
100125

@@ -109,7 +134,7 @@ extension SortedArray: Additive {
109134
// MARK: - Additive
110135

111136
/// - Returns: Empty `SortedArray`.
112-
public static var zero: SortedArray<Element> {
137+
public static var zero: SortedArray {
113138
return SortedArray()
114139
}
115140

0 commit comments

Comments
 (0)