rust-postgres/types/range.rs

500 lines
15 KiB
Rust
Raw Normal View History

2013-10-31 05:35:49 +00:00
//! Types dealing with ranges of values
2014-01-23 04:32:27 +00:00
//!
//! # Macros
//!
//! The `quote!` macro can make it easier to create ranges. It roughly mirrors
//! traditional mathematic range syntax.
//!
//! # Note
//!
//! The `Range`, `RangeBound`, `Inclusive`, and `Exclusive` types must be
//! directly usable at the location the macro is used.
//!
//! ```rust
//! #[feature(phase)];
//!
//! #[phase(syntax, link)]
//! extern mod postgres;
//!
//! use postgres::types::range::{Range, RangeBound, Inclusive, Exclusive};
//!
//! fn main() {
//! # let mut r: Range<i32>;
//! // a closed interval
//! r = range!('[' 5i32, 10i32 ']');
//! // an open interval
//! r = range!('(' 5i32, 10i32 ')');
//! // half-open intervals
//! r = range!('(' 5i32, 10i32 ']');
//! r = range!('[' 5i32, 10i32 ')');
//! // a closed lower-bounded interval
//! r = range!('[' 5i32, ')');
//! // an open lower-bounded interval
//! r = range!('(' 5i32, ')');
//! // a closed upper-bounded interval
//! r = range!('(', 10i32 ']');
//! // an open upper-bounded interval
//! r = range!('(', 10i32 ')');
//! // an unbounded interval
//! r = range!('(', ')');
//! // an empty interval
//! r = range!(empty);
//! }
2014-01-02 06:22:10 +00:00
#[macro_escape];
2013-10-29 05:35:52 +00:00
2013-10-31 02:55:25 +00:00
extern mod extra;
2013-11-01 04:28:45 +00:00
use std::cmp;
2014-01-17 06:29:58 +00:00
use std::i32;
use std::i64;
2013-10-31 02:55:25 +00:00
use extra::time::Timespec;
2014-01-23 04:32:27 +00:00
#[macro_export]
macro_rules! range(
(empty) => (Range::empty());
('(', ')') => (Range::new(None, None));
('(', $h:expr ')') => (
Range::new(None, Some(RangeBound::new($h, Exclusive)))
);
('(', $h:expr ']') => (
Range::new(None, Some(RangeBound::new($h, Inclusive)))
);
('(' $l:expr, ')') => (
Range::new(Some(RangeBound::new($l, Exclusive)), None)
);
('[' $l:expr, ')') => (
Range::new(Some(RangeBound::new($l, Inclusive)), None)
);
('(' $l:expr, $h:expr ')') => (
Range::new(Some(RangeBound::new($l, Exclusive)),
Some(RangeBound::new($h, Exclusive)))
);
('(' $l:expr, $h:expr ']') => (
Range::new(Some(RangeBound::new($l, Exclusive)),
Some(RangeBound::new($h, Inclusive)))
);
('[' $l:expr, $h:expr ')') => (
Range::new(Some(RangeBound::new($l, Inclusive)),
Some(RangeBound::new($h, Exclusive)))
);
('[' $l:expr, $h:expr ']') => (
Range::new(Some(RangeBound::new($l, Inclusive)),
Some(RangeBound::new($h, Inclusive)))
)
)
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(
2014-01-17 06:29:58 +00:00
($t:ident) => (
2013-10-30 03:18:27 +00:00
impl Normalizable for $t {
fn normalize<S: BoundSided>(bound: RangeBound<S, $t>)
-> RangeBound<S, $t> {
2014-01-02 05:14:40 +00:00
match (BoundSided::side(None::<S>), bound.type_) {
(Upper, Inclusive) => {
2014-01-26 05:27:06 +00:00
assert!(bound.value != $t::MAX);
2013-10-30 03:18:27 +00:00
RangeBound::new(bound.value + 1, Exclusive)
}
2014-01-02 05:14:40 +00:00
(Lower, Exclusive) => {
2014-01-26 05:27:06 +00:00
assert!(bound.value != $t::MAX);
2013-10-30 03:18:27 +00:00
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-11-01 06:10:55 +00:00
#[deriving(Eq)]
2013-10-29 05:35:52 +00:00
enum BoundSide {
Upper,
Lower
}
#[doc(hidden)]
2013-10-29 05:35:52 +00:00
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-12-17 04:30:48 +00:00
pub enum 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-12-17 04:30:48 +00:00
pub enum LowerBound {}
2013-10-29 05:35:52 +00:00
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>)) {
2014-01-02 05:12:01 +00:00
(Inclusive, Upper) => value <= &self.value,
(Exclusive, Upper) => value < &self.value,
(Inclusive, Lower) => value >= &self.value,
(Exclusive, Lower) => value > &self.value,
2013-10-29 05:35:52 +00:00
}
}
}
struct OptBound<'a, S, T>(Option<&'a RangeBound<S, T>>);
2013-11-01 06:10:55 +00:00
2013-12-12 03:22:18 +00:00
impl<'a, S: BoundSided, T: Ord> Ord for OptBound<'a, S, T> {
fn lt(&self, other: &OptBound<'a, S, T>) -> bool {
match (*self, *other) {
(OptBound(None), OptBound(None)) => false,
(OptBound(None), _) => BoundSided::side(None::<S>) == Lower,
(_, OptBound(None)) => BoundSided::side(None::<S>) == Upper,
(OptBound(Some(a)), OptBound(Some(b))) => a < b
2013-11-01 06:10:55 +00:00
}
}
}
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-31 07:04:45 +00:00
pub enum Range<T> {
priv Empty,
priv Normal(Option<RangeBound<LowerBound, T>>,
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-12-06 07:09:03 +00:00
let lower = lower.map(|bound| Normalizable::normalize(bound));
let upper = upper.map(|bound| Normalizable::normalize(bound));
2013-10-30 03:18:27 +00:00
2013-10-29 05:35:52 +00:00
match (&lower, &upper) {
2013-11-01 04:28:45 +00:00
(&Some(ref lower), &Some(ref upper)) => {
let empty = match (lower.type_, upper.type_) {
(Inclusive, Inclusive) => lower.value > upper.value,
_ => lower.value >= upper.value
};
if empty {
return Empty;
}
}
2013-10-29 05:35:52 +00:00
_ => {}
}
2013-10-31 07:04:45 +00:00
Normal(lower, upper)
}
/// Creates a new empty range.
pub fn empty() -> Range<T> {
Empty
}
/// Determines if this range is the empty range.
pub fn is_empty(&self) -> bool {
match *self {
Empty => true,
Normal(..) => false
2013-10-31 07:04:45 +00:00
}
}
2013-10-31 05:35:49 +00:00
/// Returns the lower bound if it exists.
pub fn lower<'a>(&'a self) -> Option<&'a RangeBound<LowerBound, T>> {
2013-10-31 07:04:45 +00:00
match *self {
Normal(Some(ref lower), _) => Some(lower),
_ => None
2013-10-31 07:04:45 +00:00
}
}
2013-10-31 05:35:49 +00:00
/// Returns the upper bound if it exists.
pub fn upper<'a>(&'a self) -> Option<&'a RangeBound<UpperBound, T>> {
2013-10-31 07:04:45 +00:00
match *self {
Normal(_, Some(ref upper)) => Some(upper),
_ => None
2013-10-29 05:35:52 +00:00
}
}
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 {
2013-10-31 07:04:45 +00:00
match *self {
Empty => false,
Normal(ref lower, ref upper) => {
2014-01-09 05:48:55 +00:00
lower.as_ref().map_or(true, |b| b.in_bounds(value)) &&
upper.as_ref().map_or(true, |b| b.in_bounds(value))
2013-10-31 07:04:45 +00:00
}
}
2013-10-29 05:35:52 +00:00
}
2013-11-01 06:10:55 +00:00
/// Determines if a range lies completely within this range.
pub fn contains_range(&self, other: &Range<T>) -> bool {
if other.is_empty() {
return true;
}
if self.is_empty() {
return false;
}
OptBound(self.lower()) <= OptBound(other.lower()) &&
OptBound(self.upper()) >= OptBound(other.upper())
}
2013-10-29 05:35:52 +00:00
}
2013-11-01 04:28:45 +00:00
impl<T: Ord+Normalizable+Clone> Range<T> {
/// Returns the intersection of this range with another
pub fn intersect(&self, other: &Range<T>) -> Range<T> {
if self.is_empty() || other.is_empty() {
return Range::empty();
}
2014-01-05 06:44:33 +00:00
let OptBound(lower) = cmp::max(OptBound(self.lower()),
OptBound(other.lower()));
let OptBound(upper) = cmp::min(OptBound(self.upper()),
OptBound(other.upper()));
2013-11-01 04:28:45 +00:00
Range::new(lower.map(|v| v.clone()), upper.map(|v| v.clone()))
2013-11-01 04:28:45 +00:00
}
}
2013-10-29 05:35:52 +00:00
#[cfg(test)]
mod test {
2014-01-17 06:29:58 +00:00
use std::i32;
2013-10-29 05:35:52 +00:00
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]
2013-10-31 07:04:45 +00:00
fn test_range_contains() {
2014-01-02 04:32:48 +00:00
let r = range!('[' 1i32, 3i32 ']');
2013-10-31 07:04:45 +00:00
assert!(!r.contains(&4));
assert!(r.contains(&3));
assert!(r.contains(&2));
assert!(r.contains(&1));
assert!(!r.contains(&0));
2013-10-29 05:35:52 +00:00
2014-01-02 04:32:48 +00:00
let r = range!('(' 1i32, 3i32 ')');
2013-10-31 07:04:45 +00:00
assert!(!r.contains(&4));
assert!(!r.contains(&3));
assert!(r.contains(&2));
assert!(!r.contains(&1));
assert!(!r.contains(&0));
2013-10-29 05:35:52 +00:00
2014-01-02 04:32:48 +00:00
let r = range!('(', 3i32 ']');
2013-10-31 07:04:45 +00:00
assert!(!r.contains(&4));
assert!(r.contains(&2));
2014-01-26 05:27:06 +00:00
assert!(r.contains(&i32::MIN));
2013-10-29 05:35:52 +00:00
2014-01-02 04:32:48 +00:00
let r = range!('[' 1i32, ')');
2014-01-26 05:27:06 +00:00
assert!(r.contains(&i32::MAX));
2013-10-31 07:04:45 +00:00
assert!(r.contains(&4));
assert!(!r.contains(&0));
2013-10-29 05:35:52 +00:00
2014-01-02 04:32:48 +00:00
let r = range!('(', ')');
2014-01-26 05:27:06 +00:00
assert!(r.contains(&i32::MAX));
2013-10-31 07:04:45 +00:00
assert!(r.contains(&0i32));
2014-01-26 05:27:06 +00:00
assert!(r.contains(&i32::MIN));
}
#[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() {
2014-01-02 04:36:46 +00:00
let r1 = range!('(' 10i32, 15i32 ']');
let r2 = range!('[' 11i32, 16i32 ')');
assert_eq!(r1, r2);
2013-10-29 05:35:52 +00:00
}
2013-11-01 04:28:45 +00:00
#[test]
fn test_range_empty() {
assert!((range!('(' 9i32, 10i32 ')')).is_empty());
assert!((range!('[' 10i32, 10i32 ')')).is_empty());
assert!((range!('(' 10i32, 10i32 ']')).is_empty());
assert!((range!('[' 10i32, 9i32 ']')).is_empty());
2013-11-01 04:28:45 +00:00
}
#[test]
fn test_intersection() {
let r1 = range!('[' 10i32, 15i32 ')');
let r2 = range!('(' 20i32, 25i32 ']');
2013-11-01 04:28:45 +00:00
assert!(r1.intersect(&r2).is_empty());
assert!(r2.intersect(&r1).is_empty());
assert_eq!(r1, r1.intersect(&range!('(', ')')));
assert_eq!(r1, (range!('(', ')')).intersect(&r1));
2013-11-01 04:28:45 +00:00
let r2 = range!('(' 10i32, ')');
let exp = Range::new(r2.lower().map(|v| v.clone()),
r1.upper().map(|v| v.clone()));
2013-11-01 04:28:45 +00:00
assert_eq!(exp, r1.intersect(&r2));
assert_eq!(exp, r2.intersect(&r1));
let r2 = range!('(', 15i32 ']');
2013-11-01 04:28:45 +00:00
assert_eq!(r1, r1.intersect(&r2));
assert_eq!(r1, r2.intersect(&r1));
let r2 = range!('[' 11i32, 14i32 ')');
2013-11-01 04:28:45 +00:00
assert_eq!(r2, r1.intersect(&r2));
assert_eq!(r2, r2.intersect(&r1));
}
2013-11-01 06:10:55 +00:00
#[test]
fn test_contains_range() {
assert!(Range::<i32>::empty().contains_range(&Range::empty()));
let r1 = range!('[' 10i32, 15i32 ')');
2013-11-01 06:10:55 +00:00
assert!(r1.contains_range(&r1));
let r2 = range!('(' 10i32, ')');
2013-11-01 06:10:55 +00:00
assert!(!r1.contains_range(&r2));
assert!(!r2.contains_range(&r1));
let r2 = range!('(', 15i32 ']');
2013-11-01 06:10:55 +00:00
assert!(!r1.contains_range(&r2));
assert!(r2.contains_range(&r1));
}
2013-10-29 05:35:52 +00:00
}