Skip to content

Instantly share code, notes, and snippets.

@eonist
Created January 13, 2017 21:13
Show Gist options
  • Save eonist/d5982e90a53ad3f3ab5efcfa56d328a1 to your computer and use it in GitHub Desktop.
Save eonist/d5982e90a53ad3f3ab5efcfa56d328a1 to your computer and use it in GitHub Desktop.
Temp
import Foundation
/**
* EXAMPLE: Range(start:2,end:6).count//4
*/
extension Range {
/**
* EXAMPLE: Range<Int>(0,3).numOfIndecies()//4 -> because [0,1,2,3].count// 4
* NOTE: only works with Range<Int> for now
*/
init(_ start:Bound,_ end:Bound){/*Conveninence initializer*/
//TODO: try ->return self.indices.count or self.underestimateCount()
self.init(start..<end/*<-CountableRange<Int>*/)//which->converts to Range<Int>
}
var start:Bound {return self.lowerBound}
var end:Bound {return self.upperBound}
var length:Int {return (self.upperBound as! Int) - (self.lowerBound as! Int)}/*convenince*/
}
extension CountableRange/* where Bound:Integer */{
var start:Bound {return self.startIndex}/*convenince*/
var end:Bound {return self.endIndex}/*convenince*/
//you can also use the native var: .count instead of the bellow .length
var length:Int {return (self.endIndex as! Int) - (self.startIndex as! Int)}/*convenince*/
var numOfIndecies:Int{ return (self.end as! Int) - (self.start as! Int) + 1 }
}
extension Range where Bound:Comparable {
func equals(_ range:Range<Bound>)->Bool {/*Convenience*/
return RangeAsserter.equals(self ,range)
}
func within(_ number:Bound)->Bool{/*Convenience*/
return RangeAsserter.within(self, number)
}
func contained(_ number:Bound)->Bool{/*Convenience*/
return RangeAsserter.contained(self, number)
}
func overlaps(_ range:Range<Bound>)->Bool{/*Convenience*/
return RangeAsserter.overlaps(self, range)
}
func contains(_ range:Range<Bound>)->Bool{/*Convenience*/
return RangeAsserter.contains(self, range)
}
func edge(_ index:Bound)->Bool{/*Convenience*/
return RangeAsserter.edge(index, self)
}
}
/*
//seems to be native already
public func ==<T: Comparable> (a: Range<T>, b:Range<T>) -> Bool{/*convenience*/
return a.equals(b)
}*/
/**
* NOTE: Swift has some of these methods built in but its nice to have them in one place, and also so that you can create other methods with similar DNA
*/
class RangeAsserter{
/**
* Asserts if PARAM: a equals PARAM: b
* NOTE: you can also use the native: a == b (I think)
*/
static func equals<T:Comparable>(_ a: Range<T>, _ b:Range<T>) -> Bool {//where T: Comparable
return a == b
}
/**
* Asserts if PARAM: value is within the PARAM: range
* TODO: Add some examples
* TODO: a potential bug is that <= max isn't correct, if it is max then its not within, if you think about how integers work, but this may also be correct since if a number range is asserted it may need to also include the max, for now use contained if you deal with integers
*/
static func within<T:Comparable>(_ range:Range<T>,_ number:T)->Bool {
return number <= RangeParser.max(range) && number >= RangeParser.min(range)
}
/**
* NOTE: this method is supplimentary to the within method, concerning the "max" problem
* NOTE: another name for this could be absolutlyWithin
*/
static func contained<T:Comparable>(_ range:Range<T>,_ number:T)->Bool {
return number >= RangeParser.min(range) && number < RangeParser.max(range)
}
/**
* Asserts if PARAM: a overlaps PARAM: b
*/
static func overlaps<T:Comparable>(_ a:Range<T>,_ b:Range<T>)->Bool {
return RangeAsserter.equals(a,b) || contains(a,b) || contains(b,a) || within(a,b.start) || within(a,b.end)
}
/**
* Asserts if PARAM: a contains PARAM: a or PARAM: b contains PARAM: a
*/
static func contains<T:Comparable>(_ a:Range<T>,_ b:Range<T>)->Bool {
return a.start <= b.start && a.end >= b.end
}
/**
* Asserts if PARAM: index is on either edge of PARAM: range
*/
static func edge<T:Comparable>(_ index:T, _ range:Range<T>)->Bool {
return index == range.start || index == range.end
}
}
/**
* NOTE: Swift has some of these methods built in but its nice to have them in one place, and also so that you can create other methods with similar DNA
* TODO: Look up Strideable when you need to impliment distatnceTo and advanceBy or even difference
*/
class RangeParser {
/**
* Returns the minimum or smallest value in the range.
*/
static func min<T:Comparable>(_ range:Range<T>)->T {
return Swift.min(range.start, range.end);
}
/**
*
*/
static func describe<T>(_ range:Range<T>) {
Swift.print("range.start: " + "\(range.start)")
Swift.print("range.end: " + "\(range.end)")
}
/**
* Returns The maximum or largest value in the range.
*/
static func max<T:Comparable>(_ range:Range<T>)->T {
return Swift.max(range.start, range.end);
}
/**
* Returns a clone of the PARAM: range instance
*/
static func clone<T>(_ range:Range<T>) -> Range<T> {
return Range(range.start, range.end)
}
/**
* NOTE: only works with Range<Int>
*/
static func difference(_ range:Range<Int>) -> Int{
return range.end - range.start
}
/**
* EXAMPLE: Range<Int>(0,3).numOfIndecies()//4 -> because [0,1,2,3].count// 4
* NOTE: only works with Range<Int> for now
*/
static func numOfIndecies(_ range:Range<Int>) -> Int{
return range.end - range.start + 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment