rust-postgres/types/range.rs

326 lines
10 KiB
Rust
Raw Normal View History

2013-10-31 05:35:49 +00:00
//! Types dealing with ranges of values
2013-10-29 05:35:52 +00:00
2013-10-31 02:55:25 +00:00
extern mod extra;
use extra::time::Timespec;
2013-10-31 05:35:49 +00:00
/// A trait that normalizes a range bound for a type
pub trait Normalizable {
2013-10-31 05:35:49 +00:00
/// Given a range bound, returns the normalized version of that bound. For
/// discrete types such as i32, the normalized lower bound is always
/// inclusive and the normalized upper bound is always exclusive. Other
/// types, such as Timespec, have no normalization process so their
/// implementation is a no-op.
///
/// The logic here should match the logic performed by the equivalent
/// Postgres type.
fn normalize<S: BoundSided>(bound: RangeBound<S, Self>)
-> RangeBound<S, Self>;
}
2013-10-30 03:18:27 +00:00
macro_rules! bounded_normalizable(
($t:ty) => (
impl Normalizable for $t {
fn normalize<S: BoundSided>(bound: RangeBound<S, $t>)
-> RangeBound<S, $t> {
match BoundSided::side(None::<S>) {
Upper if bound.type_ == Inclusive => {
assert!(bound.value != Bounded::max_value());
RangeBound::new(bound.value + 1, Exclusive)
}
Lower if bound.type_ == Exclusive => {
assert!(bound.value != Bounded::max_value());
RangeBound::new(bound.value + 1, Inclusive)
}
_ => bound
}
}
}
2013-10-30 03:18:27 +00:00
)
)
bounded_normalizable!(i32)
bounded_normalizable!(i64)
2013-10-31 02:55:25 +00:00
impl Normalizable for Timespec {
fn normalize<S: BoundSided>(bound: RangeBound<S, Timespec>)
-> RangeBound<S, Timespec> {
bound
}
}
2013-10-29 05:35:52 +00:00
enum BoundSide {
Upper,
Lower
}
trait BoundSided {
// param is a hack to get around lack of hints for self type
fn side(_: Option<Self>) -> BoundSide;
}
2013-10-31 05:35:49 +00:00
/// A tag type representing an upper bound
2013-10-31 02:55:25 +00:00
#[deriving(Eq,Clone)]
2013-10-29 05:35:52 +00:00
pub struct UpperBound;
2013-10-31 05:35:49 +00:00
/// A tag type representing a lower bound
2013-10-31 02:55:25 +00:00
#[deriving(Eq,Clone)]
2013-10-29 05:35:52 +00:00
pub struct LowerBound;
impl BoundSided for UpperBound {
fn side(_: Option<UpperBound>) -> BoundSide {
Upper
}
}
impl BoundSided for LowerBound {
fn side(_: Option<LowerBound>) -> BoundSide {
Lower
}
}
2013-10-31 05:35:49 +00:00
/// The type of a range bound
2013-10-31 02:55:25 +00:00
#[deriving(Eq,Clone)]
2013-10-29 05:35:52 +00:00
pub enum BoundType {
2013-10-31 05:35:49 +00:00
/// The bound includes its value
2013-10-29 05:35:52 +00:00
Inclusive,
2013-10-31 05:35:49 +00:00
/// The bound excludes its value
2013-10-29 05:35:52 +00:00
Exclusive
}
2013-10-31 05:35:49 +00:00
/// Represents a one-sided bound.
///
/// The side is determined by the `S` phantom parameter.
2013-10-31 02:55:25 +00:00
#[deriving(Eq,Clone)]
2013-10-29 05:35:52 +00:00
pub struct RangeBound<S, T> {
2013-10-31 05:35:49 +00:00
/// The value of the bound
2013-10-29 05:35:52 +00:00
value: T,
2013-10-31 05:35:49 +00:00
/// The type of the bound
2013-10-29 05:35:52 +00:00
type_: BoundType
}
impl<S: BoundSided, T: Ord> Ord for RangeBound<S, T> {
fn lt(&self, other: &RangeBound<S, T>) -> bool {
match (BoundSided::side(None::<S>), self.type_, other.type_) {
(Upper, Exclusive, Inclusive)
| (Lower, Inclusive, Exclusive) => self.value <= other.value,
_ => self.value < other.value
}
}
}
impl<S: BoundSided, T: Ord> RangeBound<S, T> {
2013-10-31 05:35:49 +00:00
/// Constructs a new range bound
2013-10-29 05:35:52 +00:00
pub fn new(value: T, type_: BoundType) -> RangeBound<S, T> {
RangeBound { value: value, type_: type_ }
}
2013-10-31 05:35:49 +00:00
/// Determines if a value lies within the range specified by this bound.
2013-10-29 05:35:52 +00:00
pub fn in_bounds(&self, value: &T) -> bool {
match (self.type_, BoundSided::side(None::<S>)) {
(Inclusive, Upper) if value <= &self.value => true,
(Exclusive, Upper) if value < &self.value => true,
(Inclusive, Lower) if value >= &self.value => true,
(Exclusive, Lower) if value > &self.value => true,
_ => false
}
}
}
2013-10-31 05:35:49 +00:00
/// The relation of a value to a range
2013-10-29 05:35:52 +00:00
#[deriving(Eq)]
pub enum RangeComparison {
2013-10-31 05:35:49 +00:00
/// The value lies above the range
2013-10-29 05:35:52 +00:00
Above,
2013-10-31 05:35:49 +00:00
/// The value lies within the range
2013-10-29 05:35:52 +00:00
Within,
2013-10-31 05:35:49 +00:00
/// The value lies below the range
2013-10-29 05:35:52 +00:00
Below
}
2013-10-31 05:35:49 +00:00
/// Represents a range of values.
2013-10-31 02:55:25 +00:00
#[deriving(Eq,Clone)]
2013-10-29 05:35:52 +00:00
pub struct Range<T> {
priv lower: Option<RangeBound<LowerBound, T>>,
priv upper: Option<RangeBound<UpperBound, T>>,
2013-10-29 05:35:52 +00:00
}
impl<T: Ord+Normalizable> Range<T> {
2013-10-31 05:35:49 +00:00
/// Creates a new range.
///
/// If a bound is `None`, the range is unbounded in that direction.
2013-10-29 05:35:52 +00:00
pub fn new(lower: Option<RangeBound<LowerBound, T>>,
upper: Option<RangeBound<UpperBound, T>>) -> Range<T> {
2013-10-30 03:18:27 +00:00
let lower = lower.map(|bound| { Normalizable::normalize(bound) });
let upper = upper.map(|bound| { Normalizable::normalize(bound) });
2013-10-29 05:35:52 +00:00
match (&lower, &upper) {
(&Some(ref lower), &Some(ref upper)) =>
assert!(lower.value <= upper.value),
_ => {}
}
2013-10-30 03:18:27 +00:00
Range { lower: lower, upper: upper }
}
2013-10-31 05:35:49 +00:00
/// Returns the lower bound if it exists.
pub fn lower<'a>(&'a self) -> &'a Option<RangeBound<LowerBound, T>> {
&self.lower
}
2013-10-31 05:35:49 +00:00
/// Returns the upper bound if it exists.
pub fn upper<'a>(&'a self) -> &'a Option<RangeBound<UpperBound, T>> {
&self.upper
2013-10-29 05:35:52 +00:00
}
2013-10-31 05:35:49 +00:00
/// Compares a value to this range.
2013-10-29 05:35:52 +00:00
pub fn cmp(&self, value: &T) -> RangeComparison {
let lower = do self.lower.as_ref().map_default(true) |b| {
b.in_bounds(value)
};
let upper = do self.upper.as_ref().map_default(true) |b| {
b.in_bounds(value)
};
match (lower, upper) {
(true, false) => Above,
(true, true) => Within,
(false, true) => Below,
_ => unreachable!()
}
}
2013-10-31 05:35:49 +00:00
/// Determines if a value lies within this range.
2013-10-29 05:35:52 +00:00
pub fn contains(&self, value: &T) -> bool {
self.cmp(value) == Within
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_range_bound_lower_lt() {
fn check(val1: int, inc1: BoundType, val2: int, inc2: BoundType, expected: bool) {
let a: RangeBound<LowerBound, int> = RangeBound::new(val1, inc1);
let b: RangeBound<LowerBound, int> = RangeBound::new(val2, inc2);
assert_eq!(expected, a < b);
}
check(1, Inclusive, 2, Exclusive, true);
check(1, Exclusive, 2, Inclusive, true);
check(1, Inclusive, 1, Exclusive, true);
check(2, Inclusive, 1, Inclusive, false);
check(2, Exclusive, 1, Exclusive, false);
check(1, Exclusive, 1, Inclusive, false);
check(1, Exclusive, 1, Exclusive, false);
check(1, Inclusive, 1, Inclusive, false);
}
#[test]
fn test_range_bound_upper_lt() {
fn check(val1: int, inc1: BoundType, val2: int, inc2: BoundType, expected: bool) {
let a: RangeBound<UpperBound, int> = RangeBound::new(val1, inc1);
let b: RangeBound<UpperBound, int> = RangeBound::new(val2, inc2);
assert_eq!(expected, a < b);
}
check(1, Inclusive, 2, Exclusive, true);
check(1, Exclusive, 2, Exclusive, true);
check(1, Exclusive, 1, Inclusive, true);
check(2, Inclusive, 1, Inclusive, false);
check(2, Exclusive, 1, Exclusive, false);
check(1, Inclusive, 1, Exclusive, false);
check(1, Inclusive, 1, Inclusive, false);
check(1, Exclusive, 1, Exclusive, false);
}
#[test]
fn test_range_bound_lower_in_bounds() {
fn check(bound: int, inc: BoundType, val: int, expected: bool) {
let b: RangeBound<LowerBound, int> = RangeBound::new(bound, inc);
assert_eq!(expected, b.in_bounds(&val));
}
check(1, Inclusive, 1, true);
check(1, Exclusive, 1, false);
check(1, Inclusive, 2, true);
check(1, Inclusive, 0, false);
}
#[test]
fn test_range_bound_upper_in_bounds() {
fn check(bound: int, inc: BoundType, val: int, expected: bool) {
let b: RangeBound<UpperBound, int> = RangeBound::new(bound, inc);
assert_eq!(expected, b.in_bounds(&val));
}
check(1, Inclusive, 1, true);
check(1, Exclusive, 1, false);
check(1, Inclusive, 2, false);
check(1, Inclusive, 0, true);
}
#[test]
fn test_range_cmp() {
let r = Range::new(Some(RangeBound::new(1i32, Inclusive)),
Some(RangeBound::new(3i32, Inclusive)));
2013-10-29 05:35:52 +00:00
assert_eq!(Above, r.cmp(&4));
assert_eq!(Within, r.cmp(&3));
assert_eq!(Within, r.cmp(&2));
assert_eq!(Within, r.cmp(&1));
assert_eq!(Below, r.cmp(&0));
let r = Range::new(Some(RangeBound::new(1i32, Exclusive)),
Some(RangeBound::new(3i32, Exclusive)));
2013-10-29 05:35:52 +00:00
assert_eq!(Above, r.cmp(&4));
assert_eq!(Above, r.cmp(&3));
assert_eq!(Within, r.cmp(&2));
assert_eq!(Below, r.cmp(&1));
assert_eq!(Below, r.cmp(&0));
let r = Range::new(None, Some(RangeBound::new(3i32, Inclusive)));
2013-10-29 05:35:52 +00:00
assert_eq!(Above, r.cmp(&4));
assert_eq!(Within, r.cmp(&2));
assert_eq!(Within, r.cmp(&Bounded::min_value()));
2013-10-29 05:35:52 +00:00
let r = Range::new(Some(RangeBound::new(1i32, Inclusive)), None);
assert_eq!(Within, r.cmp(&Bounded::max_value()));
2013-10-29 05:35:52 +00:00
assert_eq!(Within, r.cmp(&4));
assert_eq!(Below, r.cmp(&0));
let r = Range::new(None, None);
assert_eq!(Within, r.cmp(&Bounded::max_value()));
assert_eq!(Within, r.cmp(&0i32));
assert_eq!(Within, r.cmp(&Bounded::min_value()));
}
#[test]
fn test_normalize_lower() {
let r: RangeBound<LowerBound, i32> = RangeBound::new(10i32, Inclusive);
assert_eq!(RangeBound::new(10i32, Inclusive), Normalizable::normalize(r));
let r: RangeBound<LowerBound, i32> = RangeBound::new(10i32, Exclusive);
assert_eq!(RangeBound::new(11i32, Inclusive), Normalizable::normalize(r));
}
#[test]
fn test_normalize_upper() {
let r: RangeBound<UpperBound, i32> = RangeBound::new(10i32, Inclusive);
assert_eq!(RangeBound::new(11i32, Exclusive), Normalizable::normalize(r));
let r: RangeBound<UpperBound, i32> = RangeBound::new(10i32, Exclusive);
assert_eq!(RangeBound::new(10i32, Exclusive), Normalizable::normalize(r));
}
#[test]
fn test_range_normalizes() {
let r1 = Range::new(Some(RangeBound::new(10i32, Exclusive)),
Some(RangeBound::new(15i32, Inclusive)));
let r2 = Range::new(Some(RangeBound::new(11i32, Inclusive)),
Some(RangeBound::new(16i32, Exclusive)));
assert_eq!(r1, r2);
2013-10-29 05:35:52 +00:00
}
}