-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from rokon-uddin/heap
- Loading branch information
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
{{cookiecutter.app_name}}/Common/Sources/Common/Heap.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// | ||
// Heap.swift | ||
// Common | ||
// | ||
// Created by {{ cookiecutter.creator }} on {% now 'utc', '%d/%m/%Y' %}. | ||
// Copyright © {% now 'utc', '%Y' %} {{cookiecutter.company_name}}. All rights reserved. | ||
// | ||
|
||
private final class Reference<T: Equatable>: Equatable { | ||
var value: T | ||
init(_ value: T) { | ||
self.value = value | ||
} | ||
static func == (lhs: Reference<T>, rhs: Reference<T>) -> Bool { | ||
lhs.value == rhs.value | ||
} | ||
} | ||
|
||
@propertyWrapper public struct Heap<T: Equatable>: Equatable { | ||
private var reference: Reference<T> | ||
|
||
public init(_ value: T) { | ||
reference = .init(value) | ||
} | ||
|
||
public var wrappedValue: T { | ||
get { reference.value } | ||
set { | ||
if !isKnownUniquelyReferenced(&reference) { | ||
reference = .init(newValue) | ||
return | ||
} | ||
reference.value = newValue | ||
} | ||
} | ||
public var projectedValue: Heap<T> { | ||
self | ||
} | ||
} | ||
|
||
extension Heap: Hashable where T: Hashable { | ||
public func hash(into hasher: inout Hasher) { | ||
hasher.combine(wrappedValue) | ||
} | ||
} | ||
|
||
extension Heap: Decodable where T: Decodable { | ||
public init(from decoder: Decoder) throws { | ||
let container = try decoder.singleValueContainer() | ||
let value = try container.decode(T.self) | ||
self = Heap(value) | ||
} | ||
} | ||
|
||
extension Heap: Encodable where T: Encodable { | ||
public func encode(to encoder: Encoder) throws { | ||
var container = encoder.singleValueContainer() | ||
try container.encode(wrappedValue) | ||
} | ||
} |