<commit_before>/** \file
 *
 *  \brief            Routines that don't fit elsewhere.
 *
 *  \date             Created:  Jul 17, 2014
 *  \date             Modified: $Date$
 *
 *  \authors          mauro <<EMAIL>>
 *
 *  \version          $Revision$
 */
#ifndef LINALG_UTILITIES_MISC_H_
#define LINALG_UTILITIES_MISC_H_

#include <fstream>    // std::fstream
#include <limits>     // std::numeric_limits
#include <string>     // std::string
#include <vector>     // std::vector

#include "../profiling.h"
#include "../exceptions.h"

namespace LinAlg {

namespace Utilities {

/** \brief            A routine that jumps to a specific line in a fstream
 *
 *  \param[in,out]    stream
 *                    The fstream on which to advance to the requested line.
 *
 *  \param[in]        line
 *                    The line to advance to. The next getline call on the
 *                    stream will return the contents of line number \<line\>.
 */
inline void go_to_line(std::ifstream& stream, unsigned int line) {

  PROFILING_FUNCTION_HEADER

  stream.seekg(std::ios::beg);

  for (unsigned int i = 0; i < line - 1; ++i) {

    stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

  }

}

/** \brief            A routine to read a vector from a file
 *
 *  \param[in]        filename
 *                    The name of the file to read from. Contents can be 
 *                    whitespace separated or one per line. The type is 
 *                    inferred from the type of the vector argument.
 *
 *  \param[in|out]    vector
 *                    The vector to store the file's content.
 */
template <typename T>
void read_vector(std::string filename, std::vector<T>& vector) {

  std::ifstream file(filename);

  if (file.is_open()) {
  
    T tmp;

    while (file >> tmp) {
    
      vector.push_back(tmp);
    
    }

    file.close();
  
  } else {
  
    throw excBadArgument("read_vector(): unable to open file (%s) for "
                         "reading.", filename.c_str());
  
  }

}

} /* namespace Utilities */

} /* namespace LinAlg */

#endif /* LINALG_UTILITIES_MISC_H_ */<commit_msg>Add function for writing out a vector<commit_after>/** \file
 *
 *  \brief            Routines that don't fit elsewhere.
 *
 *  \date             Created:  Jul 17, 2014
 *  \date             Modified: $Date$
 *
 *  \authors          mauro <<EMAIL>>
 *
 *  \version          $Revision$
 */
#ifndef LINALG_UTILITIES_MISC_H_
#define LINALG_UTILITIES_MISC_H_

#include <fstream>    // std::fstream
#include <limits>     // std::numeric_limits
#include <string>     // std::string
#include <vector>     // std::vector

#include "../profiling.h"
#include "../exceptions.h"

namespace LinAlg {

namespace Utilities {

/** \brief            A routine that jumps to a specific line in a fstream
 *
 *  \param[in,out]    stream
 *                    The fstream on which to advance to the requested line.
 *
 *  \param[in]        line
 *                    The line to advance to. The next getline call on the
 *                    stream will return the contents of line number \<line\>.
 */
inline void go_to_line(std::ifstream& stream, unsigned int line) {

  PROFILING_FUNCTION_HEADER

  stream.seekg(std::ios::beg);

  for (unsigned int i = 0; i < line - 1; ++i) {

    stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

  }

}

/** \brief            A routine to read a vector from a file
 *
 *  \param[in]        filename
 *                    The name of the file to read from. Contents can be 
 *                    whitespace separated or one per line. The type is 
 *                    inferred from the type of the vector argument.
 *
 *  \param[in|out]    vector
 *                    The vector to store the file's content.
 */
template <typename T>
void read_vector(const std::string filename, std::vector<T>& vector) {

  PROFILING_FUNCTION_HEADER

  std::ifstream file(filename);

  if (file.is_open()) {
  
    T tmp;

    while (file >> tmp) {
    
      vector.push_back(tmp);
    
    }

    file.close();
  
  } else {
  
    throw excBadArgument("read_vector(): unable to open file (%s) for "
                         "reading.", filename.c_str());
  
  }

}

/** \brief            A routine to write a vector to a file
 *
 *  \param[in]        filename
 *                    The name of the file to write the vector to. Entries are 
 *                    separated by newlines.
 *
 *  \param[in]        vector
 *                    The vector containing the data to write.
 */
template <typename T>
void write_vector(const std::string filename, const std::vector<T>& vector) {

  PROFILING_FUNCTION_HEADER

  std::ofstream file(filename);

  if (file.is_open()) {
  
    for (const auto& element : vector) file << element << std::endl;

    file.close();
  
  } else {
  
    throw excBadArgument("write_vector(): unable to open file (%s) for "
                         "writing.", filename.c_str());
  
  }

}


} /* namespace Utilities */

} /* namespace LinAlg */

#endif /* LINALG_UTILITIES_MISC_H_ */
{'fraction_non_alphanumeric': 0.11761217631943072, 'fraction_numerical': 0.003162680371614944, 'mean_word_length': 1.9923122412773506, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 29, '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': '2325729', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1526, 'n_words': 471}