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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*!
Structs and implementation for Frame-level data structure. A `DataFrame` is a reference to an
underlying data store, along with record-based filtering and sorting details.
*/
use std::fmt::Debug;
use std::sync::Arc;
use std::marker::PhantomData;
use serde::{Serialize, Serializer};

use filter::{Filter, DataFilter};
use store::DataStore;
use data_types::*;
use field::{FieldIdent};
use access::{OwnedOrRef, DataIndex};
use select::{SelectField, Field};
use apply::sort::SortOrderFn;
use error;
use field::{Value};

/// A data frame. A `DataStore` reference along with record-based filtering and sorting details.
#[derive(Debug, Clone)]
pub struct DataFrame<DTypes>
    where DTypes: DTypeList
{
    pub(crate) permutation: Option<Vec<usize>>,
    pub(crate) store: Arc<DataStore<DTypes>>,
}
impl<DTypes> DataFrame<DTypes>
    where DTypes: DTypeList
{
    /// Number of rows that pass the filter in this frame.
    pub fn nrows(&self) -> usize
        where DTypes::Storage: MaxLen<DTypes>
    {
        self.len()
    }
    #[cfg(test)]
    pub(crate) fn store_ref_count(&self) -> usize {
        Arc::strong_count(&self.store)
    }
    /// Get the field type of a particular field in the underlying `DataStore`.
    pub fn get_field_type(&self, ident: &FieldIdent) -> Option<DTypes::DType> {
        self.store.get_field_type(ident)
    }
    pub(crate) fn has_same_store(&self, other: &DataFrame<DTypes>) -> bool {
        Arc::ptr_eq(&self.store, &other.store)
    }
    /// Returns `true` if this `DataFrame` contains this field.
    pub fn has_field(&self, s: &FieldIdent) -> bool {
        self.store.has_field(s)
    }
    pub(crate) fn update_permutation(&mut self, new_permutation: &[usize]) {
        // check if we already have a permutation
        self.permutation = match self.permutation {
            Some(ref prev_perm) => {
                // we already have a permutation, map the filter indices through it
                Some(new_permutation.iter().map(|&new_idx| prev_perm[new_idx]).collect())
            },
            None => Some(new_permutation.to_vec())
        };
    }

    /// Applies the provided `Func` to the data in the specified field. This `Func` must be
    /// implemented for all types in `DTypes`.
    ///
    /// Fails if the specified identifier is not found in this `DataFrame`.
    pub fn map<F, FOut>(&self, ident: &FieldIdent, f: F)
        -> error::Result<FOut>
        where DTypes::Storage: FramedMap<DTypes, F, FOut>,
    {
        self.store.map(ident, FramedFunc::new(self, f))
    }

    /// Applies the provided `Func` to the data in the specified field. This `Func` must be
    /// implemented for type `T`.
    ///
    /// Fails if the specified identifier is not found in this `DataFrame` or the incorrect type `T`
    /// is used.
    pub fn tmap<T, F>(&self, ident: &FieldIdent, f: F)
        -> error::Result<F::Output>
        where F: Func<DTypes, T>,
              T: DataType<DTypes>,
              DTypes::Storage: MaxLen<DTypes> + FramedTMap<DTypes, T, F>,
    {
        self.store.tmap(ident, FramedFunc::new(self, f))
    }

    /// Applies the provided `FuncExt` to the data in the specified field. This `FuncExt` must be
    /// implemented for all types in `DTypes`.
    ///
    /// Fails if the specified identifier is not found in this `DataFrame`.
    pub fn map_ext<F, FOut>(&self, ident: &FieldIdent, f: F)
        -> error::Result<FOut>
        where DTypes::Storage: FramedMapExt<DTypes, F, FOut>,
    {
        self.store.map_ext(ident, FramedFunc::new(self, f))
    }

    /// Applies the provided `FuncPartial` to the data in the specified field.
    ///
    /// Fails if the specified identifier is not found in this `DataFrame`.
    pub fn map_partial<F>(&self, ident: &FieldIdent, f: F)
        -> error::Result<Option<F::Output>>
        where DTypes::Storage: MapPartial<DTypes, F> + MaxLen<DTypes>,
              F: FuncPartial<DTypes>
    {
        self.store.map_partial(ident, self, f)
    }

    /// Returns the permutation (list of indices in sorted order) of values in field identified
    /// by `ident`.
    ///
    /// Fails if the field is not found in this `DataFrame`.
    pub fn sort_by(&mut self, ident: &FieldIdent) -> error::Result<Vec<usize>>
        where DTypes::Storage: FramedMap<DTypes, SortOrderFn, Vec<usize>>
    {
        let sort_order = self.sort_order_by(ident)?;
        self.update_permutation(&sort_order);
        Ok(sort_order)
    }

    fn sort_order_by(&self, ident: &FieldIdent) -> error::Result<Vec<usize>>
        where DTypes::Storage: FramedMap<DTypes, SortOrderFn, Vec<usize>>,
    {
        self.map(ident, SortOrderFn)
    }
}

/// Marker trait for a storage structure that implements [Map](../data_types/trait.Map.html), as
/// accessed through a [DataFrame](struct.DataFrame.html).
pub trait FramedMap<DTypes, F, FOut>:
    for<'a> Map<DTypes, FramedFunc<'a, DTypes, F>, FOut>
    where DTypes: AssocTypes
{}
impl<DTypes, F, FOut, T> FramedMap<DTypes, F, FOut> for T
    where T: for<'a> Map<DTypes, FramedFunc<'a, DTypes, F>, FOut>,
          DTypes: AssocTypes
{}

/// Marker trait for a storage structure that implements [TMap](../data_types/trait.TMap.html), as
/// accessed through a [DataFrame](struct.DataFrame.html).
pub trait FramedTMap<DTypes, T, F>:
    for<'a> TMap<DTypes, T, FramedFunc<'a, DTypes, F>>
    where DTypes: AssocTypes,
          T: DataType<DTypes>
{}
impl<DTypes, T, F, U> FramedTMap<DTypes, T, F> for U
    where U: for<'a> TMap<DTypes, T, FramedFunc<'a, DTypes, F>>,
          DTypes: AssocTypes,
          T: DataType<DTypes>
{}

/// Marker trait for a storage structure that implements [MapExt](../data_types/trait.MapExt.html),
/// as accessed through a [DataFrame](struct.DataFrame.html).
pub trait FramedMapExt<DTypes, F, FOut>:
    for<'a> MapExt<DTypes, FramedFunc<'a, DTypes, F>, FOut>
    where DTypes: AssocTypes
{}
impl<DTypes, F, FOut, T> FramedMapExt<DTypes, F, FOut> for T
    where T: for<'a> MapExt<DTypes, FramedFunc<'a, DTypes, F>, FOut>,
          DTypes: AssocTypes
{}

/// Trait for a data structure that re-indexes data and provides methods for accessing that
/// reorganized data.
pub trait Reindexer<DTypes: DTypeList>: Debug {
    /// Returns the length of this field.
    fn len(&self) -> usize;
    /// Returns `true` if this field is empty.
    fn is_empty(&self) -> bool { self.len() == 0 }
    /// Returns the re-organized index of a requested index.
    fn map_index(&self, requested: usize) -> usize;
    /// Returns a [Reindexed](struct.Reindexed.html) structure implementing
    /// [DataIndex](../access/trait.DataIndex.html) that provides access to the reorganized data.
    fn reindex<'a, 'b, DI>(&'a self, data_index: &'b DI) -> Reindexed<'a,'b, Self, DI>
        where DI: 'b + DataIndex<DTypes>,
              Self: Sized
    {
        Reindexed {
            orig: data_index,
            reindexer: self
        }
    }
}

impl<DTypes> Reindexer<DTypes> for DataFrame<DTypes>
    where DTypes: DTypeList,
          DTypes::Storage: MaxLen<DTypes>
{
    fn len(&self) -> usize
    {
        match self.permutation {
            Some(ref perm) => perm.len(),
            None => self.store.nrows()
        }
    }

    fn map_index(&self, requested: usize) -> usize {
        match self.permutation {
            Some(ref perm) => perm[requested],
            None => requested
        }
    }
}

/// Data structure that provides [DataIndex](../access/trait.DataIndex.html) access to a reorganized
/// (sorted / shuffled) data field.
#[derive(Debug)]
pub struct Reindexed<'a, 'b, R: 'a, DI: 'b>
{
    reindexer: &'a R,
    orig: &'b DI,
}
impl<'a, 'b, DI, R, DTypes> DataIndex<DTypes> for Reindexed<'a, 'b, R, DI>
    where DTypes: DTypeList,
          R: 'a + Reindexer<DTypes>,
          DI: 'b + DataIndex<DTypes>,
{
    type DType = DI::DType;
    fn get_datum(&self, idx: usize) -> error::Result<Value<&Self::DType>> {
        self.orig.get_datum(self.reindexer.map_index(idx))
    }
    fn len(&self) -> usize {
        self.reindexer.len()
    }
}

impl<'a, DTypes, T> SelectField<'a, T, DTypes>
    for DataFrame<DTypes>
    where DTypes: 'a + DTypeList,
          DTypes::Storage: 'a + MaxLen<DTypes>,
          T: 'static + DataType<DTypes>
{
    type Output = Framed<'a, DTypes, T>;

    fn select(&'a self, ident: FieldIdent)
        -> error::Result<Framed<'a, DTypes, T>>
        where DTypes::Storage: TypeSelector<DTypes, T>
    {
        Ok(Framed::new(&self, self.store.select(ident)?))
    }
}
impl<DTypes> Field<DTypes> for DataFrame<DTypes>
    where DTypes: DTypeList
{}

/// Wrapper for a [Func](../data_types/trait.Func.html) that calls the underlying `Func` with the
/// field data organized by this [DataFrame](struct.DataFrame.html).
pub struct FramedFunc<'a, DTypes, F>
    where DTypes: 'a + DTypeList,
{
    func: F,
    frame: &'a DataFrame<DTypes>,
}
impl<'a, DTypes, F> FramedFunc<'a, DTypes, F>
    where DTypes: 'a + DTypeList,
{
    fn new(frame: &'a DataFrame<DTypes>, func: F) -> FramedFunc<'a, DTypes, F> {
        FramedFunc {
            func,
            frame,
        }
    }
}

impl<'a, DTypes, T, F> Func<DTypes, T> for FramedFunc<'a, DTypes, F>
    where F: Func<DTypes, T>,
          T: DataType<DTypes>,
          DTypes: DTypeList,
          DTypes::Storage: 'a + MaxLen<DTypes>
{
    type Output = F::Output;
    fn call(
        &mut self,
        type_data: &dyn DataIndex<DTypes, DType=T>,
    )
        -> F::Output
    {
        self.func.call(&Framed::new(self.frame, OwnedOrRef::Ref(type_data)))
    }
}

impl<'a, DTypes, T, F> FuncExt<DTypes, T> for FramedFunc<'a, DTypes, F>
    where F: FuncExt<DTypes, T>,
          T: DataType<DTypes>,
          DTypes: DTypeList,
          DTypes::Storage: 'a + MaxLen<DTypes>
{
    type Output = F::Output;
    fn call<L>(
        &mut self,
        type_data: &dyn DataIndex<DTypes, DType=T>,
        locator: &L,
            )
        -> F::Output
        where L: FieldLocator<DTypes>
    {
        self.func.call(&Framed::new(self.frame, OwnedOrRef::Ref(type_data)), locator)
    }
}

impl<DTypes, T> Filter<DTypes, T> for DataFrame<DTypes>
    where DTypes: DTypeList,
          DTypes::Storage: MaxLen<DTypes> + TypeSelector<DTypes, T>,
          T: 'static + DataType<DTypes>,
          Self: Field<DTypes>
{
    fn filter<I: Into<FieldIdent>, F: Fn(&T) -> bool>(&mut self, ident: I, pred:F)
        -> error::Result<Vec<usize>>
    {
        let filter = self.field(ident)?.data_filter(pred);
        self.update_permutation(&filter);
        Ok(filter)
    }
}

impl<DTypes> From<DataStore<DTypes>> for DataFrame<DTypes>
    where DTypes: DTypeList
{
    fn from(store: DataStore<DTypes>) -> DataFrame<DTypes> {
        DataFrame {
            permutation: None,
            store: Arc::new(store),
        }
    }
}

/// Structure to hold references to a data structure (e.g. DataStore) and a frame used to view
/// that structure. Provides DataIndex for the underlying data structure, as viewed through the
/// frame.
#[derive(Debug)]
pub struct Framed<'a, DTypes, T>
    where T: 'a + DataType<DTypes>,
          DTypes: 'a + DTypeList
{
    frame: &'a DataFrame<DTypes>,
    data: OwnedOrRef<'a, DTypes, T>,
    dtype: PhantomData<T>,
}
impl<'a, DTypes, T> Framed<'a, DTypes, T>
    where DTypes: DTypeList,
          T: DataType<DTypes>
{
    /// Create a new framed view of some data, as view through a particular `DataFrame`.
    pub fn new(frame: &'a DataFrame<DTypes>, data: OwnedOrRef<'a, DTypes, T>)
        -> Framed<'a, DTypes, T>
    {
        Framed { frame, data, dtype: PhantomData }
    }
}

impl<'a, DTypes, T> DataIndex<DTypes> for Framed<'a, DTypes, T>
    where T: DataType<DTypes>,
          DTypes: DTypeList,
          DTypes::Storage: MaxLen<DTypes>
{
    type DType = T;

    fn get_datum(&self, idx: usize) -> error::Result<Value<&T>> {
        self.data.get_datum(self.frame.map_index(idx))
    }
    fn len(&self) -> usize
    {
        self.frame.nrows()
    }
}

pub(crate) struct SerializedField<'a, DTypes>
    where DTypes: 'a + DTypeList
{
    ident: FieldIdent,
    frame: &'a DataFrame<DTypes>,
}
impl<'a, DTypes> SerializedField<'a, DTypes>
    where DTypes: DTypeList
{
    pub fn new(ident: FieldIdent, frame: &'a DataFrame<DTypes>) -> SerializedField<'a, DTypes> {
        SerializedField {
            ident,
            frame,
        }
    }
}

impl<'a, DTypes> Serialize for SerializedField<'a, DTypes>
    where DTypes: DTypeList,
          DTypes::Storage: MaxLen<DTypes> + FieldSerialize<DTypes>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer,
    {
        self.frame.store.serialize_field(&self.ident, self.frame, serializer)
    }
}