Last active
February 28, 2021 04:15
-
-
Save richardwei6/bd44f9630e4ebff157b7d69f59aacdf2 to your computer and use it in GitHub Desktop.
A simple class to find the delta between subsequent data variables
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
// | |
// imageDeltaFinder.swift | |
// Omegacam-ios | |
// | |
// Created by Richard Wei on 2/24/21. | |
// | |
import Foundation | |
import AVFoundation | |
class dataDeltaFinder{ | |
static private var previousData : Data = Data(); | |
static public func input(_ c: Data) -> UInt64{ | |
let output = delta(previousData, c); | |
previousData = c; | |
return output; | |
} | |
static private func delta(_ a: Data, _ b: Data) -> UInt64{ | |
let a_a = convertDataToAUInt8(a); | |
let b_a = convertDataToAUInt8(b); | |
var deltaCount : UInt64 = 0; | |
var i = 0; | |
while ( i < a_a.count && i < b_a.count ){ | |
if (a[i] != b[i]){ | |
deltaCount += 1; | |
} | |
i+=1; | |
} | |
deltaCount += UInt64(max(a_a.count , b_a.count) - i); | |
return deltaCount; | |
} | |
// | |
static internal func convertDataToAUInt8(_ d: Data) -> [UInt8]{ | |
var t : [UInt8] = []; | |
t.append(contentsOf: d); | |
return t; | |
} | |
// | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment