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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
/*!
Structs and implementation for row-selecting data structure.

A [DataFrame](struct.DataFrame.html) is a reference to an underlying
[DataStore](../store/struct.DataStore.html) along with record-based filtering and sorting details.
*/

use std::collections::VecDeque;
use std::marker::PhantomData;

#[cfg(feature = "serialize")]
use serde::ser::{Serialize, SerializeSeq, Serializer};
use std::fmt::Debug;
use std::rc::Rc;
use std::sync::Arc;

use typenum::UTerm;

use access::{DataIndex, NRows};
use cons::Nil;
use error;
use field::{FieldData, Value};
use fieldlist::FieldCons;
use label::*;
use permute::{self, UpdatePermutation};
use select::{FieldSelect, SelectFieldByLabel};
use store::{AssocFrameLookup, AssocStorage, DataRef, DataStore, IntoView};
use view::{DataView, ViewFrameCons};

type Permutation = permute::Permutation<Vec<usize>>;

/// Type alias for label-only cons-list
pub type StoreFieldCons<L, T> = LCons<L, T>;
/// A marker struct for a frame type (Single, Melt, etc.) and the list of fields used with this
/// type (a [StoreFieldCons](type.StoreFieldCons.html) / [LabelCons](../label/type.LabelCons.html)).
pub struct StoreFieldMarkers<FrameType, StoreFieldList> {
    _marker: PhantomData<(FrameType, StoreFieldList)>,
}
/// Type alias for a cons-list which associated frame labels with their underlying store(s).
/// `FrameLabel` is a label struct. `StoreDetails` is a `StoreFieldMarkers` struct.
pub type FieldLookupCons<FrameLabel, StoreDetails, Tail> = LMCons<FrameLabel, StoreDetails, Tail>;

/// [StoreFieldMarkers](struct.StoreFieldMarkers.html) `FrameType` for typical single-source fields
pub struct Single;
/// [StoreFieldMarkers](struct.StoreFieldMarkers.html) `FrameType` for multi-source fields
/// constructed by a 'melt' call.
pub struct Melt;

/// Trait for computing the `FrameFields` [FieldLookupCons](type.FieldLookupCons.html) cons-list
/// for a standard [DataFrame](struct.DataFrame.html) (where all labels simple pass through with
/// calls to the underlying [DataStore](../store/struct.DataStore.html)).
pub trait SimpleFrameFields {
    /// The computed `FrameFields` [FieldLookupCons](type.FieldLookupCons.html) cons-list.
    type Fields;
}
impl SimpleFrameFields for Nil {
    type Fields = Nil;
}
impl<Label, Value, Tail> SimpleFrameFields for LVCons<Label, Value, Tail>
where
    Tail: SimpleFrameFields,
{
    type Fields = FieldLookupCons<
        Label,
        StoreFieldMarkers<Single, Labels![Label]>,
        <Tail as SimpleFrameFields>::Fields,
    >;
}

/// Trait for creating a [DataFrame](struct.DataFrame.html) containing a single field with label
/// `Label` which contains the `String` labels of the implementing type (a cons-list).
pub trait IntoStrFrame<Label> {
    /// Output `DataFrame` type.
    type Output;

    /// Create the `DataFrame` with the a single field labeled `Label` which contains the `String`
    /// labels from this type. The frame will have a number of rows equal to the length of the
    /// implementing cons-list.
    fn into_str_frame() -> Self::Output;
    /// Create the `DataFrame` with the a single field labeled `Label` which contains the `String`
    /// labels from this type, repeated `n` times. The frame will have a number of rows equal to the
    /// length of the implementing cons-list multiplied by `n`.
    fn into_repeated_str_frame(n: usize) -> Self::Output;
}

impl<Label, List> IntoStrFrame<Label> for List
where
    Label: Debug,
    List: StrLabels,
{
    type Output = DataFrame<
        <FieldCons<Label, String, Nil> as SimpleFrameFields>::Fields,
        DataStore<FieldCons<Label, String, Nil>>,
    >;

    fn into_str_frame() -> Self::Output {
        let strs: FieldData<String> = Self::labels().iter().map(|&s| s.to_owned()).collect();
        DataFrame::from(DataStore::<Nil>::empty().push_back_field::<Label, _>(strs))
    }

    fn into_repeated_str_frame(n: usize) -> Self::Output {
        let strs: FieldData<String> = Self::labels().iter().map(|&s| s.to_owned()).collect();
        DataFrame::from_repeated_store(
            DataStore::<Nil>::empty().push_back_field::<Label, _>(strs),
            n,
        )
    }
}

/// Helper trait for [IntoMeltFrame](trait.IntoMeltFrame.html) to generate the `FrameFields` list.
pub trait MeltFrameFields<MeltLabel> {
    /// The computed melt `FrameFields` [FieldLookupCons](type.FieldLookupCons.html) cons-list.
    type Fields;
}
impl<MeltLabel, Labels> MeltFrameFields<MeltLabel> for Labels
where
    Labels: AssocLabels,
{
    type Fields =
        FieldLookupCons<MeltLabel, StoreFieldMarkers<Melt, <Labels as AssocLabels>::Labels>, Nil>;
}

/// A data frame. A reference to the underlying data store along with record-based filtering and
/// sorting details. `FrameFields` is a [FieldLookupCons](type.FieldLookupCons.html) cons-list which
/// maps a single label to one or more underlying store labels.
#[derive(Debug)]
pub struct DataFrame<FrameFields, FramedStore> {
    permutation: Rc<Permutation>,
    fields: PhantomData<FrameFields>,
    store: Arc<FramedStore>,
}

impl<FrameFields, FramedStore> DataFrame<FrameFields, FramedStore>
where
    FramedStore: NRows,
{
    /// Returns length (number of rows) in this `DataFrame`.
    pub fn len(&self) -> usize {
        match self.permutation.len() {
            Some(len) => len,
            None => self.store.nrows(),
        }
    }
    /// Returns whether or not this `DataFrame` is empty.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }
}

impl<FrameFields, FramedStore> NRows for DataFrame<FrameFields, FramedStore>
where
    FramedStore: NRows,
{
    fn nrows(&self) -> usize {
        self.len()
    }
}

impl<FrameFields, FramedStore> Clone for DataFrame<FrameFields, FramedStore> {
    fn clone(&self) -> DataFrame<FrameFields, FramedStore> {
        DataFrame {
            permutation: self.permutation.clone(),
            fields: PhantomData,
            store: Arc::clone(&self.store),
        }
    }
}

#[cfg(test)]
pub trait StoreRefCount {
    fn store_ref_count(&self) -> usize;
}
#[cfg(test)]
impl<FrameFields, FramedStore> StoreRefCount for DataFrame<FrameFields, FramedStore> {
    fn store_ref_count(&self) -> usize {
        Arc::strong_count(&self.store)
    }
}
impl<FrameFields, FramedStore> UpdatePermutation for DataFrame<FrameFields, FramedStore> {
    fn update_permutation(mut self, new_permutation: &[usize]) -> Self {
        let perm = (*self.permutation).clone();
        self.permutation = Rc::new(perm.update_indices(new_permutation));
        self
    }
}

impl<StoreFields> From<DataStore<StoreFields>>
    for DataFrame<<StoreFields as SimpleFrameFields>::Fields, DataStore<StoreFields>>
where
    StoreFields: AssocStorage + SimpleFrameFields,
{
    fn from(
        store: DataStore<StoreFields>,
    ) -> DataFrame<<StoreFields as SimpleFrameFields>::Fields, DataStore<StoreFields>> {
        DataFrame {
            permutation: Rc::new(Permutation::default()),
            fields: PhantomData,
            store: Arc::new(store),
        }
    }
}

impl<Labels, Frames> From<DataView<Labels, Frames>>
    for DataFrame<<Labels as SimpleFrameFields>::Fields, DataView<Labels, Frames>>
where
    Labels: SimpleFrameFields,
{
    fn from(
        view: DataView<Labels, Frames>,
    ) -> DataFrame<<Labels as SimpleFrameFields>::Fields, DataView<Labels, Frames>> {
        DataFrame {
            permutation: Rc::new(Permutation::default()),
            fields: PhantomData,
            store: Arc::new(view),
        }
    }
}

/// Trait for repackaging an data store into a `DataFrame`[struct.DataFrame.html]. The output
/// `DataFrame` should have the same labels as the underlying data store.
pub trait IntoFrame {
    /// The `FrameFields` type parameter for the output `DataFrame`.
    type FrameFields;
    /// The `FramedStore` type parameter for the output `DataFrame`.
    type FramedStore;
    /// The output `DataFrame` (should always be `DataFrame<Self::FrameFields, Self::FramedStore>`).
    type Output; // = DataFrame<Self::FrameFields, Self::FramedStore>

    /// Convert `self` into a [DataFrame](struct.DataFrame.html) object.
    fn into_frame(self) -> Self::Output;
}

impl<Labels, Frames> IntoFrame for DataView<Labels, Frames>
where
    Labels: SimpleFrameFields,
    DataFrame<<Labels as SimpleFrameFields>::Fields, DataView<Labels, Frames>>:
        From<DataView<Labels, Frames>>,
{
    type FrameFields = <Labels as SimpleFrameFields>::Fields;
    type FramedStore = DataView<Labels, Frames>;

    type Output = DataFrame<Self::FrameFields, Self::FramedStore>;

    fn into_frame(self) -> Self::Output {
        self.into()
    }
}

impl<Fields> IntoFrame for DataStore<Fields>
where
    Fields: AssocStorage + SimpleFrameFields,
    DataFrame<<Fields as SimpleFrameFields>::Fields, DataStore<Fields>>: From<DataStore<Fields>>,
{
    type FrameFields = <Fields as SimpleFrameFields>::Fields;
    type FramedStore = DataStore<Fields>;

    type Output = DataFrame<Self::FrameFields, Self::FramedStore>;

    fn into_frame(self) -> Self::Output {
        self.into()
    }
}

/// Trait for repackaging an data store into a `DataFrame`[struct.DataFrame.html] as a melted data
/// structure. The output `DataFrame` will have one label, `MeltLabel`, which rotates over the
/// labels in underlying data store.
pub trait IntoMeltFrame<MeltLabel> {
    /// The `FrameFields` type parameter for the output `DataFrame`.
    type FrameFields;
    /// The `FramedStore` type parameter for the output `DataFrame`.
    type FramedStore;
    /// The output `DataFrame` (should always be `DataFrame<Self::FrameFields, Self::FramedStore>`).
    type Output; // = DataFrame<Self::FrameFields, Self::FramedStore>

    /// Convert `self` into a [DataFrame](struct.DataFrame.html) object.
    fn into_melt_frame(self) -> Self::Output;
}

impl<MeltLabel, Labels, Frames> IntoMeltFrame<MeltLabel> for DataView<Labels, Frames>
where
    Labels: MeltFrameFields<MeltLabel>,
{
    type FrameFields = <Labels as MeltFrameFields<MeltLabel>>::Fields;
    type FramedStore = DataView<Labels, Frames>;

    type Output = DataFrame<Self::FrameFields, Self::FramedStore>;

    fn into_melt_frame(self) -> Self::Output {
        DataFrame {
            permutation: Rc::new(Permutation::default()),
            fields: PhantomData,
            store: Arc::new(self),
        }
    }
}

impl<FrameFields, FramedStore> IntoView for DataFrame<FrameFields, FramedStore>
where
    FrameFields: AssocFrameLookup,
{
    type Labels = <FrameFields as AssocFrameLookup>::Output;
    type Frames = ViewFrameCons<UTerm, Self, Nil>;
    type Output = DataView<Self::Labels, Self::Frames>;

    fn into_view(self) -> Self::Output {
        DataView::new(ViewFrameCons {
            head: self.into(),
            tail: Nil,
        })
    }
}

impl<StoreFields> DataFrame<<StoreFields as SimpleFrameFields>::Fields, DataStore<StoreFields>>
where
    StoreFields: AssocStorage + SimpleFrameFields,
    DataStore<StoreFields>: NRows,
{
    pub(crate) fn from_repeated_store(
        store: DataStore<StoreFields>,
        reps: usize,
    ) -> DataFrame<<StoreFields as SimpleFrameFields>::Fields, DataStore<StoreFields>> {
        DataFrame {
            permutation: {
                //TODO: replace with slice.repeat() call when stabilized
                let mut v = Vec::with_capacity(store.nrows() * reps);
                for _ in 0..reps {
                    v.extend(0..store.nrows());
                }
                Rc::new(v.into())
            },
            fields: PhantomData,
            store: Arc::new(store),
        }
    }
}

/// Allow `DataFrame`s to be pulled from `LVCons` as `Value`s
impl<FrameFields, FramedStore> SelfValued for DataFrame<FrameFields, FramedStore> {}

/// Inner frame kind enum used in a [Framed](struct.Framed.html) struct.
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
enum FrameKind<DI> {
    Single(DI),
    Melt(Vec<DI>),
}

impl<DI> FrameKind<DI>
where
    DI: DataIndex,
{
    fn nfields(&self) -> usize {
        match *self {
            FrameKind::Single(_) => 1,
            FrameKind::Melt(ref fields) => fields.len(),
        }
    }

    fn nrows(&self) -> usize {
        assert!(!self.is_empty());
        match *self {
            FrameKind::Single(ref field) => field.len(),
            FrameKind::Melt(ref fields) => fields[0].len(),
        }
    }

    fn is_empty(&self) -> bool {
        self.nfields() == 0
    }
}

/// 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, Hash, PartialEq, Eq)]
pub struct Framed<T, DI> {
    permutation: Rc<Permutation>,
    data: FrameKind<DI>,
    _ty: PhantomData<T>,
}
impl<T, DI> Framed<T, DI> {
    /// Create a new framed view of some data, as viewed through a particular `DataFrame`.
    pub fn new(permutation: Rc<Permutation>, data: DI) -> Framed<T, DI> {
        Framed {
            permutation,
            data: FrameKind::Single(data),
            _ty: PhantomData,
        }
    }

    /// Create a new framed view of some data, rotating over data in a `Vec` of `DataIndex` objects.
    pub fn new_melt(permutation: Rc<Permutation>, data: Vec<DI>) -> Framed<T, DI> {
        Framed {
            permutation,
            data: FrameKind::Melt(data),
            _ty: PhantomData,
        }
    }
}
impl<T, DI> Clone for Framed<T, DI>
where
    DI: Clone,
{
    fn clone(&self) -> Framed<T, DI> {
        Framed {
            permutation: Rc::clone(&self.permutation),
            data: self.data.clone(),
            _ty: PhantomData,
        }
    }
}
impl<T> From<DataRef<T>> for Framed<T, DataRef<T>> {
    fn from(orig: DataRef<T>) -> Framed<T, DataRef<T>> {
        Framed {
            permutation: Rc::new(Permutation::default()),
            data: FrameKind::Single(orig),
            _ty: PhantomData,
        }
    }
}
impl<T> From<FieldData<T>> for Framed<T, DataRef<T>> {
    fn from(orig: FieldData<T>) -> Framed<T, DataRef<T>> {
        Framed {
            permutation: Rc::new(Permutation::default()),
            data: FrameKind::Single(orig.into()),
            _ty: PhantomData,
        }
    }
}

impl<T, DI> DataIndex for Framed<T, DI>
where
    T: Debug,
    DI: DataIndex<DType = T> + Debug,
{
    type DType = T;

    fn get_datum(&self, idx: usize) -> error::Result<Value<&T>> {
        assert!(!self.data.is_empty());
        match self.data {
            FrameKind::Single(ref field) => field.get_datum(self.permutation.map_index(idx)),
            FrameKind::Melt(ref fields) => {
                // when we have multiple fields in this Framed struct, we loop through through the
                // first record in each field, then the second, and so on....
                let nfields = self.data.nfields();
                fields[idx % nfields].get_datum(self.permutation.map_index(idx / nfields))
            }
        }
    }
    fn len(&self) -> usize {
        assert!(!self.data.is_empty());
        // nfields * nrows
        self.data.nfields() * self.permutation.len().unwrap_or(self.data.nrows())
    }
}

#[cfg(feature = "serialize")]
impl<T, DI> Serialize for Framed<T, DI>
where
    T: Serialize,
    Self: DataIndex<DType = T>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        let mut seq = serializer.serialize_seq(Some(self.len()))?;
        for elem in self.iter() {
            seq.serialize_element(&elem)?;
        }
        seq.end()
    }
}

/// Trait for selecting a field associated with the label `Label` from the fields in `FramedStore`.
pub trait SelectAndFrame<Label, FramedStore> {
    /// The resultant data type of the field.
    type DType: Debug;
    /// The field accessor type.
    type Field: DataIndex<DType = Self::DType>;

    /// Returns an [Framed](struct.Framed.html) struct accessing the selected field.
    fn select_and_frame(
        perm: &Rc<Permutation>,
        store: &FramedStore,
    ) -> Framed<Self::DType, Self::Field>;
}

/// Helper trait for selecting and framing fields. Used by
/// [SelectAndFrame](trait.SelectAndFrame.html). `Label` is the label to select, `FramedStore` is
/// the struct the data is stored in, and `Match` is whether or not `Label` matches the head
/// label in this type.
pub trait SelectAndFrameMatch<Label, FramedStore, Match> {
    /// The resultant data type of the field.
    type DType: Debug;
    /// The field accessor type.
    type Field: DataIndex<DType = Self::DType>;

    /// Returns an [Framed](struct.Framed.html) struct accessing the selected field.
    fn select_and_frame(
        perm: &Rc<Permutation>,
        store: &FramedStore,
    ) -> Framed<Self::DType, Self::Field>;
}

impl<TargetLabel, FrameLabel, StoreDetails, Tail, FramedStore>
    SelectAndFrame<TargetLabel, FramedStore> for FieldLookupCons<FrameLabel, StoreDetails, Tail>
where
    TargetLabel: LabelEq<FrameLabel>,
    FieldLookupCons<FrameLabel, StoreDetails, Tail>:
        SelectAndFrameMatch<TargetLabel, FramedStore, <TargetLabel as LabelEq<FrameLabel>>::Eq>,
{
    type DType = <FieldLookupCons<FrameLabel, StoreDetails, Tail> as SelectAndFrameMatch<
        TargetLabel,
        FramedStore,
        <TargetLabel as LabelEq<FrameLabel>>::Eq,
    >>::DType;
    type Field = <FieldLookupCons<FrameLabel, StoreDetails, Tail> as SelectAndFrameMatch<
        TargetLabel,
        FramedStore,
        <TargetLabel as LabelEq<FrameLabel>>::Eq,
    >>::Field;

    fn select_and_frame(
        perm: &Rc<Permutation>,
        store: &FramedStore,
    ) -> Framed<Self::DType, Self::Field> {
        <Self as SelectAndFrameMatch<
            TargetLabel,
            FramedStore,
            <TargetLabel as LabelEq<FrameLabel>>::Eq,
        >>::select_and_frame(perm, store)
    }
}

impl<TargetLabel, FrameLabel, StoreFieldList, Tail, FramedStore>
    SelectAndFrameMatch<TargetLabel, FramedStore, True>
    for FieldLookupCons<FrameLabel, StoreFieldMarkers<Single, StoreFieldList>, Tail>
where
    FramedStore: SelectFieldByLabel<TargetLabel>,
    <FramedStore as SelectFieldByLabel<TargetLabel>>::DType: Debug,
{
    type DType = <FramedStore as SelectFieldByLabel<TargetLabel>>::DType;
    type Field = <FramedStore as SelectFieldByLabel<TargetLabel>>::Output;

    fn select_and_frame(
        perm: &Rc<Permutation>,
        store: &FramedStore,
    ) -> Framed<Self::DType, Self::Field> {
        Framed::new(
            Rc::clone(perm),
            SelectFieldByLabel::<TargetLabel>::select_field(store),
        )
    }
}

impl<TargetLabel, FrameLabel, StoreFieldList, Tail, FramedStore>
    SelectAndFrameMatch<TargetLabel, FramedStore, True>
    for FieldLookupCons<FrameLabel, StoreFieldMarkers<Melt, StoreFieldList>, Tail>
where
    StoreFieldList: RotateFields<FramedStore>,
    <StoreFieldList as RotateFields<FramedStore>>::DType: Debug,
    <StoreFieldList as RotateFields<FramedStore>>::Output: Clone,
{
    type DType = <StoreFieldList as RotateFields<FramedStore>>::DType;
    type Field = <StoreFieldList as RotateFields<FramedStore>>::Output;

    fn select_and_frame(
        perm: &Rc<Permutation>,
        store: &FramedStore,
    ) -> Framed<Self::DType, Self::Field> {
        let melt_rotation = <StoreFieldList as RotateFields<FramedStore>>::add_to_rotation(store);
        Framed::new_melt(
            Rc::clone(perm),
            melt_rotation.iter().cloned().collect::<Vec<_>>(),
        )
    }
}

impl<TargetLabel, FrameLabel, StoreDetails, Tail, FramedStore>
    SelectAndFrameMatch<TargetLabel, FramedStore, False>
    for FieldLookupCons<FrameLabel, StoreDetails, Tail>
where
    Tail: SelectAndFrame<TargetLabel, FramedStore>,
{
    type DType = <Tail as SelectAndFrame<TargetLabel, FramedStore>>::DType;
    type Field = <Tail as SelectAndFrame<TargetLabel, FramedStore>>::Field;

    fn select_and_frame(
        perm: &Rc<Permutation>,
        store: &FramedStore,
    ) -> Framed<Self::DType, Self::Field> {
        <Tail as SelectAndFrame<TargetLabel, FramedStore>>::select_and_frame(perm, store)
    }
}

/// Trait for generating a collection of objects implementing
/// [DataIndex](../access/trait.DataIndex.html) with the same underlying type. Used for rotating
/// through source fields in a [Melt](struct.Melt.html)ed field.
pub trait RotateFields<FramedStore> {
    /// Underlying data type of the source fields
    type DType;
    /// Type of the individual `DataIndex`-implementing objects returned.
    type Output: DataIndex<DType = Self::DType>;

    /// Add data pointed to by this type to the data collection, drawing data from the
    /// provided `DataStore`.
    fn add_to_rotation(store: &FramedStore) -> VecDeque<Self::Output>;
}

impl<FramedStore> RotateFields<FramedStore> for Nil {
    type DType = usize;
    type Output = DataRef<usize>;

    fn add_to_rotation(_store: &FramedStore) -> VecDeque<Self::Output> {
        VecDeque::new()
    }
}
impl<FramedStore, Label, Tail> RotateFields<FramedStore> for StoreFieldCons<Label, Tail>
where
    FramedStore: SelectFieldByLabel<Label>,
    Tail: RotateFieldsTerm<
        FramedStore,
        <FramedStore as SelectFieldByLabel<Label>>::DType,
        <FramedStore as SelectFieldByLabel<Label>>::Output,
    >,
{
    type DType = <FramedStore as SelectFieldByLabel<Label>>::DType;
    type Output = <FramedStore as SelectFieldByLabel<Label>>::Output;

    fn add_to_rotation(store: &FramedStore) -> VecDeque<Self::Output> {
        let mut v = Tail::add_to_rotation(store);
        v.push_front(SelectFieldByLabel::<Label>::select_field(store));
        v
    }
}

/// Helper trait for helping terminate a [RotateFields](trait.RotateFields.html) recursion. Handles
/// taking care of the [Nil](../cons/struct.Nil.html) end-case without losing the type information
/// of the returned output collection.
pub trait RotateFieldsTerm<FramedStore, DType, Output> {
    /// Create or add data to the data collection.
    fn add_to_rotation(store: &FramedStore) -> VecDeque<Output>;
}
impl<FramedStore, DType, Output> RotateFieldsTerm<FramedStore, DType, Output> for Nil {
    fn add_to_rotation(_store: &FramedStore) -> VecDeque<Output> {
        VecDeque::new()
    }
}
impl<FramedStore, DType, Output, Label, Tail> RotateFieldsTerm<FramedStore, DType, Output>
    for StoreFieldCons<Label, Tail>
where
    StoreFieldCons<Label, Tail>: RotateFields<FramedStore, DType = DType, Output = Output>,
    Output: DataIndex<DType = DType>,
{
    fn add_to_rotation(store: &FramedStore) -> VecDeque<Output> {
        <StoreFieldCons<Label, Tail> as RotateFields<FramedStore>>::add_to_rotation(store)
    }
}

impl<FrameFields, FramedStore, Label> SelectFieldByLabel<Label>
    for DataFrame<FrameFields, FramedStore>
where
    FrameFields: SelectAndFrame<Label, FramedStore>,
{
    type DType = <FrameFields as SelectAndFrame<Label, FramedStore>>::DType;
    type Output = Framed<Self::DType, <FrameFields as SelectAndFrame<Label, FramedStore>>::Field>;

    fn select_field(&self) -> Self::Output {
        <FrameFields as SelectAndFrame<Label, FramedStore>>::select_and_frame(
            &self.permutation,
            &*self.store,
        )
    }
}

impl<FrameFields, FramedStore> FieldSelect for DataFrame<FrameFields, FramedStore> {}

#[cfg(test)]
mod tests {

    use std::path::Path;

    use csv_sniffer::metadata::Metadata;
    use serde_json;

    use super::*;

    use source::csv::{CsvReader, CsvSource, IntoCsvSrcSchema};

    fn load_csv_file<Schema>(
        filename: &str,
        schema: Schema,
    ) -> (CsvReader<Schema::CsvSrcSchema>, Metadata)
    where
        Schema: IntoCsvSrcSchema,
        <Schema as IntoCsvSrcSchema>::CsvSrcSchema: Debug,
    {
        let data_filepath = Path::new(file!()) // start as this file
            .parent()
            .unwrap() // navigate up to src directory
            .parent()
            .unwrap() // navigate up to root directory
            .join("tests") // navigate into integration tests directory
            .join("data") // navigate into data directory
            .join(filename); // navigate to target file

        let source = CsvSource::new(data_filepath).unwrap();
        (
            CsvReader::new(&source, schema).unwrap(),
            source.metadata().clone(),
        )
    }

    tablespace![
        pub table gdp {
            CountryName: String,
            CountryCode: String,
            Year1983: f64
        }
    ];

    #[test]
    fn frame_select() {
        let gdp_schema = schema![
            fieldname gdp::CountryName = "Country Name";
            fieldname gdp::CountryCode = "Country Code";
            fieldname gdp::Year1983 = "1983";
        ];

        let (mut csv_rdr, _metadata) = load_csv_file("gdp.csv", gdp_schema.clone());
        let ds = csv_rdr.read().unwrap();

        let frame = DataFrame::from(ds);
        println!("{:?}", frame.field::<gdp::CountryName>());
    }

    #[test]
    fn framed_serialize() {
        let field: FieldData<f64> = vec![5.0f64, 3.4, -1.3, 5.2, 6.0, -126.9].into();
        let framed: Framed<f64, _> = field.into();
        assert_eq!(
            serde_json::to_string(&framed).unwrap(),
            "[5.0,3.4,-1.3,5.2,6.0,-126.9]"
        );
        println!("{}", serde_json::to_string(&framed).unwrap());
    }

    tablespace![
        pub table order {
            Name: String,
            Name1: String,
            Name2: String,
            Name3: String,
        }
    ];

    #[test]
    fn repeated() {
        let field: FieldData<String> = vec!["First", "Second", "Third"]
            .iter()
            .map(|&s| s.to_owned())
            .collect();
        let store = DataStore::<Nil>::empty().push_back_field::<order::Name, _>(field);
        let frame = DataFrame::from_repeated_store(store, 5);
        assert_eq!(
            frame.field::<order::Name>().to_vec(),
            vec![
                "First", "Second", "Third", "First", "Second", "Third", "First", "Second", "Third",
                "First", "Second", "Third", "First", "Second", "Third",
            ]
        );

        let dv = frame.into_view();
        println!("{}", dv);
        assert_eq!(
            dv.field::<order::Name>().to_vec(),
            vec![
                "First", "Second", "Third", "First", "Second", "Third", "First", "Second", "Third",
                "First", "Second", "Third", "First", "Second", "Third",
            ]
        );
    }

    #[test]
    fn framed_melt() {
        let store = DataStore::<Nil>::empty().push_back_from_iter::<order::Name1, _, _, _>(
            vec!["First1", "Second1", "Third1"]
                .iter()
                .map(|&s| s.to_owned()),
        );
        let store = store.push_back_from_iter::<order::Name2, _, _, _>(
            vec!["First2", "Second2", "Third2"]
                .iter()
                .map(|&s| s.to_owned()),
        );
        let store = store.push_back_from_iter::<order::Name3, _, _, _>(
            vec!["First3", "Second3", "Third3"]
                .iter()
                .map(|&s| s.to_owned()),
        );

        let framed_data = Framed::<String, _>::new_melt(
            Rc::new(Permutation::default()),
            vec![
                store.field::<order::Name1>(),
                store.field::<order::Name2>(),
                store.field::<order::Name3>(),
            ],
        );

        println!("{:?}", framed_data.to_vec());
        assert_eq!(
            framed_data.to_vec(),
            vec![
                "First1", "First2", "First3", "Second1", "Second2", "Second3", "Third1", "Third2",
                "Third3",
            ]
        );
    }

    #[test]
    fn frame_view() {
        let store = DataStore::<Nil>::empty().push_back_from_iter::<order::Name1, _, _, _>(
            vec!["First1", "Second1", "Third1"]
                .iter()
                .map(|&s| s.to_owned()),
        );
        let dv = store.into_view();
        println!("{}", dv.nrows());
        let view_in_frame: DataFrame<_, _> = dv.into();
        println!("{}", view_in_frame.nrows());
        let final_view = view_in_frame.into_view();
        println!("{}", final_view);
    }
}