Skip to content

Commit

Permalink
Merge pull request #14 from rokon-uddin/heap
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohammed Rokon Uddin authored Sep 8, 2023
2 parents 43239a5 + e64fae2 commit f3651c9
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions {{cookiecutter.app_name}}/Common/Sources/Common/Heap.swift
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)
}
}

0 comments on commit f3651c9

Please sign in to comment.