<commit_before>use definitions::{LiteralValue, ColumnDef, ColumnConstraint};

use std::collections::BTreeMap;
use std::fmt;

pub type TableRow = Vec<LiteralValue>;
pub type TableHeader = Vec<ColumnDef>;
pub type PkType = uint;

pub struct RowFormat<'a>(pub &'a TableRow);
pub struct HeaderFormat<'a>(pub &'a TableHeader);

pub struct Table {
    pub name: String,
    pub header: TableHeader,
    pub data: BTreeMap<PkType, TableRow>,
    pub pk: Option<PkType>,
}

impl Table {
    pub fn new_result_table(header: TableHeader) -> Table {
        Table {
            name: "".to_string(),
            header: header,
            data: BTreeMap::new(),
            pk: None,
        }
    }
    pub fn get_column_def_by_name(&self, name: &String) -> Option<&ColumnDef> {
        self.header.iter().find(|&cols| &cols.name == name)
    }

    pub fn get_column_index(&self, name: &String) -> Option<uint> {
        self.header.iter().position(|ref cols| &cols.name == name)
    }

    pub fn has_row(&self, pk: PkType) -> bool {
        self.data.contains_key(&pk)
    }

    pub fn assert_size(&self) {
        let header_size = self.header.len();

        for row in self.data.values() {
            assert!(row.len() == header_size);
        }
    }

    pub fn add_column(&mut self, column_def: ColumnDef) {
        self.header.push(column_def);

        for (_, row) in self.data.iter_mut() {
            row.push(LiteralValue::Null);
        }
    }

    pub fn add_columns(&mut self, column_defs: Vec<ColumnDef>) {
        for def in column_defs.into_iter() {
            self.add_column(def);
        }
    }

    pub fn insert(&mut self, column_data: Vec<TableRow>,
                  specified_columns: &Option<Vec<String>>) {
        for column_data in column_data.into_iter() {
            if let &Some(ref column_names) = specified_columns {
                assert!(column_names.len() == column_data.len());
                let mut row = Vec::from_elem(self.header.len(), LiteralValue::Null);

                for (name, data) in column_names.iter().zip(column_data.into_iter()) {
                    row[self.get_column_index(name).unwrap()] = data;
                }

                self.push_row(row);
            } else {
                self.push_row(column_data);
            }
        }
    }

    pub fn push_row(&mut self, row: TableRow) {
        if let Some(i) = self.pk {
            let pk = row[i].clone().to_uint();
            self.data.insert(pk, row);
        } else {
            let len = self.data.len();
            self.data.insert(len, row);
        }
    }<commit_msg>Fix deprecated warning on latest nightly<commit_after>use definitions::{LiteralValue, ColumnDef, ColumnConstraint};

use std::collections::BTreeMap;
use std::fmt;
use std::iter::repeat;

pub type TableRow = Vec<LiteralValue>;
pub type TableHeader = Vec<ColumnDef>;
pub type PkType = uint;

pub struct RowFormat<'a>(pub &'a TableRow);
pub struct HeaderFormat<'a>(pub &'a TableHeader);

pub struct Table {
    pub name: String,
    pub header: TableHeader,
    pub data: BTreeMap<PkType, TableRow>,
    pub pk: Option<PkType>,
}

impl Table {
    pub fn new_result_table(header: TableHeader) -> Table {
        Table {
            name: "".to_string(),
            header: header,
            data: BTreeMap::new(),
            pk: None,
        }
    }
    pub fn get_column_def_by_name(&self, name: &String) -> Option<&ColumnDef> {
        self.header.iter().find(|&cols| &cols.name == name)
    }

    pub fn get_column_index(&self, name: &String) -> Option<uint> {
        self.header.iter().position(|ref cols| &cols.name == name)
    }

    pub fn has_row(&self, pk: PkType) -> bool {
        self.data.contains_key(&pk)
    }

    pub fn assert_size(&self) {
        let header_size = self.header.len();

        for row in self.data.values() {
            assert!(row.len() == header_size);
        }
    }

    pub fn add_column(&mut self, column_def: ColumnDef) {
        self.header.push(column_def);

        for (_, row) in self.data.iter_mut() {
            row.push(LiteralValue::Null);
        }
    }

    pub fn add_columns(&mut self, column_defs: Vec<ColumnDef>) {
        for def in column_defs.into_iter() {
            self.add_column(def);
        }
    }

    pub fn insert(&mut self, column_data: Vec<TableRow>,
                  specified_columns: &Option<Vec<String>>) {
        for column_data in column_data.into_iter() {
            if let &Some(ref column_names) = specified_columns {
                assert!(column_names.len() == column_data.len());
                let mut row: TableRow = repeat(LiteralValue::Null).take(self.header.len()).collect();

                for (name, data) in column_names.iter().zip(column_data.into_iter()) {
                    row[self.get_column_index(name).unwrap()] = data;
                }

                self.push_row(row);
            } else {
                self.push_row(column_data);
            }
        }
    }

    pub fn push_row(&mut self, row: TableRow) {
        if let Some(i) = self.pk {
            let pk = row[i].clone().to_uint();
            self.data.insert(pk, row);
        } else {
            let len = self.data.len();
            self.data.insert(len, row);
        }
    }
{'fraction_non_alphanumeric': 0.1351658767772512, 'fraction_numerical': 0.0, 'mean_word_length': 2.1404761904761904, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2520728', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1639, 'n_words': 363}