<commit_before>/*************************************************************************
 *
 * Copyright (c) 2012 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 ************************************************************************/

#ifndef __MDDS_MULTI_TYPE_MATRIX_HPP__
#define __MDDS_MULTI_TYPE_MATRIX_HPP__

#ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
#ifndef MDDS_MULTI_TYPE_VECTOR_DEBUG
#define MDDS_MULTI_TYPE_VECTOR_DEBUG 1
#endif
#endif

#include "multi_type_vector.hpp"
#include "multi_type_vector_trait.hpp"

namespace mdds {

namespace mtm {

/**
 * Element type for multi_type_matrix.
 */
enum element_t { element_empty, element_boolean, element_string, element_numeric };

struct std_string_trait
{
    typedef std::string string_type;
    typedef mdds::mtv::string_element_block string_element_block;
    typedef mdds::mtv::element_block_func element_block_func;

    static const mdds::mtv::element_t string_type_identifier = mdds::mtv::element_type_string;
};

}

template<typename _StringTrait>
class multi_type_matrix
{
    typedef _StringTrait string_trait;
public:
    typedef typename string_trait::string_type string_type;
    typedef size_t      size_type;

private:
    typedef mdds::multi_type_vector<typename string_trait::element_block_func> store_type;

public:
    typedef typename store_type::position_type position_type;
    typedef typename store_type::const_position_type const_position_type;

    typedef typename store_type::element_block_type element_block_type;

    typedef typename mtv::boolean_element_block boolean_block_type;
    typedef typename mtv::numeric_element_block numeric_block_type;
    typedef typename string_trait::string_element_block string_block_type;

    struct size_pair_type
    {
        size_type row;
        size_type column;
        size_pair_type() : row(0), column(0) {}
        size_pair_type(size_type _row, size_type _column) : row(_row), column(_column) {}

        bool operator== (const size_pair_type& r) const { return row == r.row && column == r.column; }
        bool operator!= (const size_pair_type& r) const { return !operator== (r); }
    };

    struct element_block_node_type
    {
        mtm::element_t type;
        size_type size;
        const element_block_type* data;

        element_block_node_type() : type(mtm::element_empty), size(0), data(NULL) {}
        element_block_node_type(const element_block_node_type& other) :
            type(other.type), size(other.size), data(other.data) {}
    };

    static mtm::element_t to_mtm_type(mdds::mtv::element_t mtv_type)
    {
        switch (mtv_type)
        {
            case mdds::mtv::element_type_numeric:
                return mdds::mtm::element_numeric;
            case string_trait::string_type_identifier:
                return mdds::mtm::element_string;
            case mdds::mtv::element_type_boolean:
                return mdds::mtm::element_boolean;
            case mdds::mtv::element_type_empty:
                return mdds::mtm::element_empty;
            default:
                throw general_error("multi_type_matrix: unknown element type.");
        }

    }

    template<typename _Func>
    struct walk_func : std::unary_function<typename store_type::const_iterator::value_type, void>
    {
        _Func& m_func;
        walk_func(_Func& func) : m_func(func) {}

        void operator() (const typename store_type::const_iterator::value_type& mtv_node)
        {
            element_block_node_type mtm_node;
            mtm_node.type = to_mtm_type(mtv_node.type);
            mtm_node.size = mtv_node.size;
            mtm_node.data = mtv_node.data;
            m_func(mtm_node);
        }
    };
<commit_msg>Share element type values where we can.<commit_after>/*************************************************************************
 *
 * Copyright (c) 2012 <NAME>
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 ************************************************************************/

#ifndef __MDDS_MULTI_TYPE_MATRIX_HPP__
#define __MDDS_MULTI_TYPE_MATRIX_HPP__

#ifdef MDDS_MULTI_TYPE_MATRIX_DEBUG
#ifndef MDDS_MULTI_TYPE_VECTOR_DEBUG
#define MDDS_MULTI_TYPE_VECTOR_DEBUG 1
#endif
#endif

#include "multi_type_vector.hpp"
#include "multi_type_vector_trait.hpp"

namespace mdds {

namespace mtm {

/**
 * Element type for multi_type_matrix.
 */
enum element_t
{
    element_empty = mdds::mtv::element_type_empty,
    element_boolean = mdds::mtv::element_type_boolean,
    element_string = mdds::mtv::element_type_string,
    element_numeric = mdds::mtv::element_type_numeric
};

struct std_string_trait
{
    typedef std::string string_type;
    typedef mdds::mtv::string_element_block string_element_block;
    typedef mdds::mtv::element_block_func element_block_func;

    static const mdds::mtv::element_t string_type_identifier = mdds::mtv::element_type_string;
};

}

template<typename _StringTrait>
class multi_type_matrix
{
    typedef _StringTrait string_trait;
public:
    typedef typename string_trait::string_type string_type;
    typedef size_t      size_type;

private:
    typedef mdds::multi_type_vector<typename string_trait::element_block_func> store_type;

public:
    typedef typename store_type::position_type position_type;
    typedef typename store_type::const_position_type const_position_type;

    typedef typename store_type::element_block_type element_block_type;

    typedef typename mtv::boolean_element_block boolean_block_type;
    typedef typename mtv::numeric_element_block numeric_block_type;
    typedef typename string_trait::string_element_block string_block_type;

    struct size_pair_type
    {
        size_type row;
        size_type column;
        size_pair_type() : row(0), column(0) {}
        size_pair_type(size_type _row, size_type _column) : row(_row), column(_column) {}

        bool operator== (const size_pair_type& r) const { return row == r.row && column == r.column; }
        bool operator!= (const size_pair_type& r) const { return !operator== (r); }
    };

    struct element_block_node_type
    {
        mtm::element_t type;
        size_type size;
        const element_block_type* data;

        element_block_node_type() : type(mtm::element_empty), size(0), data(NULL) {}
        element_block_node_type(const element_block_node_type& other) :
            type(other.type), size(other.size), data(other.data) {}
    };

    static mtm::element_t to_mtm_type(mdds::mtv::element_t mtv_type)
    {
        switch (mtv_type)
        {
            case string_trait::string_type_identifier:
                return mdds::mtm::element_string;
            case mdds::mtv::element_type_numeric:
            case mdds::mtv::element_type_boolean:
            case mdds::mtv::element_type_empty:
                // These types share the same numeric values.
                return static_cast<mtm::element_t>(mtv_type);
            default:
                throw general_error("multi_type_matrix: unknown element type.");
        }
    }

    template<typename _Func>
    struct walk_func : std::unary_function<typename store_type::const_iterator::value_type, void>
    {
        _Func& m_func;
        walk_func(_Func& func) : m_func(func) {}

        void operator() (const typename store_type::const_iterator::value_type& mtv_node)
        {
            element_block_node_type mtm_node;
            mtm_node.type = to_mtm_type(mtv_node.type);
            mtm_node.size = mtv_node.size;
            mtm_node.data = mtv_node.data;
            m_func(mtm_node);
        }
    };

{'fraction_non_alphanumeric': 0.10871602624179943, 'fraction_numerical': 0.0016661459960429033, 'mean_word_length': 3.811623246492986, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, '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': '909654', 'n_tokens_mistral': 2894, 'n_tokens_neox': 2771, 'n_words': 749}