<commit_before>/*
 * Copyright (c) 2017-2018 OFFIS Institute for Information Technology
 *                          Oldenburg, Germany
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \brief  VCD traits
 * \author <NAME> <<EMAIL>>,
 *         <NAME> <<EMAIL>>
 */

#include <boost/format.hpp>

#include <ostream>
#include <iomanip>
#include <climits>

template<typename T>
struct vcd_traits
{
  using value_type = T;
  using this_type = vcd_traits<value_type>;

  static constexpr const char* header_identifier_value = "real";
  static constexpr const char* trace_identifier_value = "r";
  static constexpr uint16_t bitwidth_value = sizeof(value_type) * CHAR_BIT;

  static void print(std::ostream& out, value_type const& val) { out << val; }
};

template struct vcd_traits<uint16_t>;
template struct vcd_traits<uint32_t>;
template struct vcd_traits<int>;

template<>
struct vcd_traits<bool>
{
  using value_type = bool;
  using this_type = vcd_traits<value_type>;

  static constexpr const char* header_identifier_value = "wire";
  static constexpr const char* trace_identifier_value = "b";
  static constexpr uint16_t bitwidth_value = 1;

  static void print(std::ostream& out, value_type const& val)
  {
    out << (val ? "1" : "0");
  }
};

template<>
struct vcd_traits<double>
{
  using value_type = double;
  using this_type = vcd_traits<value_type>;

  static constexpr const char* header_identifier_value = "real";
  static constexpr const char* trace_identifier_value = "r";
  static constexpr uint16_t bitwidth_value = sizeof(value_type) * CHAR_BIT;

  static void print(std::ostream& out, value_type const& val)
  {
    out << boost::format("%.16g") % val;
  }
};
<commit_msg>vcd: add trait for streams with std::string<commit_after>/*
 * Copyright (c) 2017-2018 OFFIS Institute for Information Technology
 *                          Oldenburg, Germany
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * \brief  VCD traits
 * \author <NAME> <<EMAIL>>,
 *         <NAME> <<EMAIL>>
 */

#include <boost/format.hpp>

#include <algorithm>
#include <bitset>
#include <climits>
#include <iomanip>
#include <ostream>

template<typename T>
struct vcd_traits
{
  using value_type = T;
  using this_type = vcd_traits<value_type>;

  static constexpr const char* header_identifier_value = "real";
  static constexpr const char* trace_identifier_value = "r";
  static constexpr uint16_t bitwidth_value = sizeof(value_type) * CHAR_BIT;

  static void print(std::ostream& out, value_type const& val) { out << val; }
};

template struct vcd_traits<uint16_t>;
template struct vcd_traits<uint32_t>;
template struct vcd_traits<int>;

template<>
struct vcd_traits<bool>
{
  using value_type = bool;
  using this_type = vcd_traits<value_type>;

  static constexpr const char* header_identifier_value = "wire";
  static constexpr const char* trace_identifier_value = "b";
  static constexpr uint16_t bitwidth_value = 1;

  static void print(std::ostream& out, value_type const& val)
  {
    out << (val ? "1" : "0");
  }
};

template<>
struct vcd_traits<double>
{
  using value_type = double;
  using this_type = vcd_traits<value_type>;

  static constexpr const char* header_identifier_value = "real";
  static constexpr const char* trace_identifier_value = "r";
  static constexpr uint16_t bitwidth_value = sizeof(value_type) * CHAR_BIT;

  static void print(std::ostream& out, value_type const& val)
  {
    out << boost::format("%.16g") % val;
  }
};

template<>
struct vcd_traits<std::string>
{
  using value_type = std::string;
  using this_type = vcd_traits<value_type>;

  static constexpr const char* header_identifier_value = "wire";
  static constexpr const char* trace_identifier_value = "b";
  static constexpr uint16_t bitwidth_value = 800;

  static void print(std::ostream& out, value_type const& val)
  {
    for (std::size_t i = 0; i < bitwidth_value/8; i++) {
      char data;
      if (i < val.size()) {
        data = val[i];
      } else {
        data = 0;
      }
      out << std::bitset<8>(data);
    }
  }
};

{'fraction_non_alphanumeric': 0.10659297275957363, 'fraction_numerical': 0.012435846821950257, 'mean_word_length': 4.031777557100298, 'pattern_counts': {'":': 0, '<': 67, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6804945', 'n_tokens_mistral': 1638, 'n_tokens_neox': 1540, 'n_words': 512}