1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
use { crate::{ col::{ColProxy,Col}, rename_col::RenameCol, header::{Header}, record::Record, }, tylisp::{ HCons, HNil, sexpr, defun_nocalc, non_calc_literal, engine::Eval, ops::{ list::{Union, SupersetP, EmptyP, Tail}, }, marker_traits::List }, }; use std::ops::{Bound,RangeBounds}; use std::any::Any; pub trait QueryFilter: Clone { type ReqCols: Header; fn test_record(&self, _:&impl Record)->bool; fn bounds_of<C:Col+Ord>(&self)->(Bound<&'_ C>, Bound<&'_ C>) { (Bound::Unbounded, Bound::Unbounded) } } pub trait IntoFilter { type Result:QueryFilter + List; fn into_filter(self)->Self::Result; } impl IntoFilter for HNil { type Result = Self; fn into_filter(self)->Self { self } } impl<H:ColProxy+PartialEq, T:IntoFilter> IntoFilter for HCons<H,T> where HCons<Exact<H>, T::Result>: QueryFilter { type Result = HCons<Exact<H>, T::Result>; fn into_filter(self)->Self::Result { HCons { head: Exact(self.head), tail: self.tail.into_filter() } } } #[derive(Copy,Clone,Default)] pub struct FilterForHeader; defun_nocalc!{() FilterForHeader { (H: Header, F:QueryFilter) { _:H, _:F } => {SupersetP, @H, @F::ReqCols}; }} #[derive(Copy,Clone,Default)] pub struct SingleColFilter; defun_nocalc!{() SingleColFilter { (F:QueryFilter) {_:F} => {EmptyP, {Tail, @F::ReqCols}}; }} #[test] fn test_filter_for_header() { col!{A: usize} col!{B: usize} col!{C: usize} use tylisp::{typenum as tn, eval, ops::list::BuildList}; assert_type_eq! { tn::True : eval!{FilterForHeader, @{A,B}, @Exact<A>}} assert_type_eq! { tn::True : eval!{FilterForHeader, @{A,B}, @Exact<B>}} assert_type_eq! { tn::False: eval!{FilterForHeader, @{A,B}, @Exact<C>}} assert_type_eq! { tn::True : eval!{FilterForHeader, @{A,B}, @HNil}} assert_type_eq! { tn::True : eval!{FilterForHeader, @{A,B}, {BuildList, @Exact<A>, @Exact<B>}}} assert_type_eq! { tn::True : eval!{FilterForHeader, @{A,B}, {BuildList, @Exact<B>, @Exact<A>}}} assert_type_eq! { tn::False: eval!{FilterForHeader, @{A,B}, {BuildList, @Exact<C>, @Exact<A>}}} assert_type_eq! { tn::False: eval!{FilterForHeader, @{A,B}, {BuildList, @Exact<A>, @Exact<C>}}} } #[derive(Copy, Clone, Debug)] pub struct Exact<T>(pub T); non_calc_literal!({T} Exact<T>); impl<A,B,T> RenameCol<A,B> for Exact<T> where A: Col, B:Col<Inner = A::Inner>, T: RenameCol<A,B>, Exact<T::Renamed>: QueryFilter { type Renamed = Exact<T::Renamed>; fn rename_col(self)->Exact<T::Renamed> { Exact(self.0.rename_col()) } } impl<T:ColProxy+Clone> QueryFilter for Exact<T> where T::For: PartialEq { type ReqCols = sexpr!{T::For}; fn test_record(&self, rec:&impl Record)->bool { match rec.col_opt::<T::For>() { Some(c) => *c == *self.0.col_ref(), None => true } } fn bounds_of<C:Col+Ord>(&self)->(Bound<&'_ C>, Bound<&'_ C>) { let c: Option<&C> = (self.0.col_ref() as &dyn Any).downcast_ref(); match c { Some(c) => (Bound::Included(c), Bound::Included(c)), None => (Bound::Unbounded, Bound::Unbounded) } } } impl QueryFilter for HNil { type ReqCols = HNil; fn test_record(&self, _:&impl Record)->bool { true } fn bounds_of<C:Col+Ord>(&self)->(Bound<&'_ C>, Bound<&'_ C>) { (Bound::Unbounded, Bound::Unbounded) } } impl<H,T, UnionCols> QueryFilter for HCons<H,T> where H:QueryFilter, T:QueryFilter, sexpr!{Union, @H::ReqCols, @T::ReqCols}: Eval<Result=UnionCols>, UnionCols: Header, { type ReqCols = UnionCols; #[inline(always)] fn test_record(&self, rec:&impl Record)->bool { self.head.test_record(rec) && self.tail.test_record(rec) } fn bounds_of<C:Col+Ord>(&self)->(Bound<&'_ C>, Bound<&'_ C>) { let (lo_1, hi_1) = self.head.bounds_of::<C>(); let (lo_2, hi_2) = self.tail.bounds_of::<C>(); use Bound::*; let lo = match (lo_1, lo_2) { (Unbounded, _) => lo_2, (_, Unbounded) => lo_1, (Included(a), Included(b)) => Included(a.max(b)), (Excluded(a), Excluded(b)) => Excluded(a.max(b)), (Included(a), Excluded(b)) => if a > b { Included(a) } else { Excluded(b) }, (Excluded(a), Included(b)) => if b > a { Included(b) } else { Excluded(a) }, }; let hi = match (hi_1, hi_2) { (Unbounded, _) => hi_2, (_, Unbounded) => hi_1, (Included(a), Included(b)) => Included(a.min(b)), (Excluded(a), Excluded(b)) => Excluded(a.min(b)), (Included(a), Excluded(b)) => if a < b { Included(a) } else { Excluded(b) }, (Excluded(a), Included(b)) => if b < a { Included(b) } else { Excluded(a) }, }; (lo, hi) } }