Skip to content

Instantly share code, notes, and snippets.

@makadev
Created February 17, 2017 14:16
Show Gist options
  • Save makadev/2243d9428f89637cde5b1405e67edf12 to your computer and use it in GitHub Desktop.
Save makadev/2243d9428f89637cde5b1405e67edf12 to your computer and use it in GitHub Desktop.
extension Dictionary {
/**
Check that given Order List is a consistent Order List for given Table:
1. Order and Table have the same amount of Entries
2. All Keys in Order List are Key in Table and there is no Duplicate
*/
public static func OrderTableSanityCheck(forTable table: [Key:Value], withOrder order: [Key]) -> Bool {
// sanitize check 1: both need to have the same amount of element
if table.count != order.count {
return false
}
// sanitize check 2: that event_order contains only valid events and has no duplicate entries
var dup_table: [Key:Value] = [:]
for item in order {
if let res = table[item] {
// entry exists
if let _ = dup_table[item] {
// pass
} else {
// no duplicate entry
dup_table[item] = res
continue
}
}
// somethings not right -> kill tables and return
return false
}
return true
}
/**
Check that given Order List is a consistent Order List for given Table:
1. Order and Table have the same amount of Entries
2. All Keys in Order List are Key in Table and there is no Duplicate
*/
public nonmutating func checkOrder(givenBy order: [Key]) -> Bool {
return Dictionary.OrderTableSanityCheck(forTable: self, withOrder: order)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment