<commit_before>#ifndef _PYVIENNACL_VECTOR_H
#define _PYVIENNACL_VECTOR_H

#include "entry_proxy.hpp"

#include <boost/numeric/ublas/vector_sparse.hpp>

#include <viennacl/vector.hpp>
#include <viennacl/vector_proxy.hpp>

namespace ublas = boost::numeric::ublas;

template <class T>
struct vector_to_list_converter
{
  static PyObject* convert(std::vector<T> const& v)
  {
    bp::list l;
    for (std::size_t i = 0; i < v.size(); ++i)
      l.append((T)v[i]);
    
    return bp::incref(l.ptr());
  }
};

// TODO: Obliterate below with the above converter
template <class SCALARTYPE>
bp::list std_vector_to_list(const std::vector<SCALARTYPE>& v)
{
  bp::list l;
  for (unsigned int i = 0; i < v.size(); ++i)
    l.append((SCALARTYPE)v[i]);
  
  return l;
}

template <class SCALARTYPE>
np::ndarray std_vector_to_ndarray(const std::vector<SCALARTYPE>& v)
{
  return np::from_object(std_vector_to_list<SCALARTYPE>(v),
			 np::dtype::get_builtin<SCALARTYPE>());
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<std::vector<SCALARTYPE> >
std_vector_init_ndarray(const np::ndarray& array)
{
  int d = array.get_nd();
  if (d != 1) {
    PyErr_SetString(PyExc_TypeError, "Can only create a vector from a 1-D array!");
    bp::throw_error_already_set();
  }
  
  vcl::vcl_size_t s = (vcl::vcl_size_t) array.shape(0);
  
  std::vector<SCALARTYPE> *v = new std::vector<SCALARTYPE>(s);
  
  for (vcl::vcl_size_t i=0; i < s; ++i)
    (*v)[i] = bp::extract<SCALARTYPE>(array[i]);
  
  return vcl::tools::shared_ptr<std::vector<SCALARTYPE> >(v);
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<std::vector<SCALARTYPE> >
std_vector_init_list(const bp::list& l)
{
  return std_vector_init_ndarray<SCALARTYPE>
    (np::from_object(l, np::dtype::get_builtin<SCALARTYPE>()));
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<std::vector<SCALARTYPE> >
std_vector_init_scalar(vcl::vcl_size_t length, SCALARTYPE value) {
  std::vector<SCALARTYPE> *v = new std::vector<SCALARTYPE>(length);
  for (vcl::vcl_size_t i=0; i < length; ++i)
    (*v)[i] = value;
  return vcl::tools::shared_ptr<std::vector<SCALARTYPE> >(v);
}

// Vector -- vcl::vector

template <class SCALARTYPE>
bp::list vcl_vector_to_list(const vcl::vector_base<SCALARTYPE>& v)
{
  std::vector<SCALARTYPE> c(v.size());
  vcl::fast_copy(v.begin(), v.end(), c.begin());

  return std_vector_to_list(c);
}

template <class SCALARTYPE>
np::ndarray vcl_vector_to_ndarray(const vcl::vector_base<SCALARTYPE>& v)
{
  return np::from_object(vcl_vector_to_list<SCALARTYPE>(v),
			 np::dtype::get_builtin<SCALARTYPE>());
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >
vcl_vector_init_mem(SCALARTYPE* ptr, vcl::memory_types mem_type,
                    vcl::vcl_size_t size, vcl::vcl_size_t start = 0,
                    vcl::vcl_ptrdiff_t stride = 1)
{
  vcl::vector<SCALARTYPE> *v = new vcl::vector<SCALARTYPE>
    (ptr, mem_type, size, start, stride);
  return vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >(v);
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >
vcl_vector_init_ndarray(const np::ndarray& array, const vcl::context& ctx)
{
  int d = array.get_nd();
  if (d != 1) {
    PyErr_SetString(PyExc_TypeError, "Can only create a vector from a 1-D array!");
    bp::throw_error_already_set();
  }
  
  vcl::vcl_size_t s = (vcl::vcl_size_t) array.shape(0);
  
  vcl::vector<SCALARTYPE> *v = new vcl::vector<SCALARTYPE>(s, ctx);
  std::vector<SCALARTYPE> cpu_vector(s);
  
  for (vcl::vcl_size_t i=0; i < s; ++i)
    cpu_vector[i] = bp::extract<SCALARTYPE>(array[i]);
  
  vcl::fast_copy(cpu_vector.begin(), cpu_vector.end(), v->begin());

  return vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >(v);
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >
vcl_vector_init_list(const bp::list& l, const vcl::context& ctx)
{
  return vcl_vector_init_ndarray<SCALARTYPE>
    (np::from_object(l, np::dtype::get_builtin<SCALARTYPE>()), ctx);
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >
vcl_vector_init_scalar(vcl::vcl_size_t length, SCALARTYPE value,
                       const vcl::context &ctx)
{
  ublas::scalar_vector<SCALARTYPE> s_v(length, value);
  vcl::vector<SCALARTYPE> *v = new vcl::vector<SCALARTYPE>(length, ctx);
  vcl::copy(s_v.begin(), s_v.end(), v->begin());<commit_msg>Change fast_copy to copy<commit_after>#ifndef _PYVIENNACL_VECTOR_H
#define _PYVIENNACL_VECTOR_H

#include "entry_proxy.hpp"

#include <boost/numeric/ublas/vector_sparse.hpp>

#include <viennacl/vector.hpp>
#include <viennacl/vector_proxy.hpp>

namespace ublas = boost::numeric::ublas;

template <class T>
struct vector_to_list_converter
{
  static PyObject* convert(std::vector<T> const& v)
  {
    bp::list l;
    for (std::size_t i = 0; i < v.size(); ++i)
      l.append((T)v[i]);
    
    return bp::incref(l.ptr());
  }
};

// TODO: Obliterate below with the above converter
template <class SCALARTYPE>
bp::list std_vector_to_list(const std::vector<SCALARTYPE>& v)
{
  bp::list l;
  for (unsigned int i = 0; i < v.size(); ++i)
    l.append((SCALARTYPE)v[i]);
  
  return l;
}

template <class SCALARTYPE>
np::ndarray std_vector_to_ndarray(const std::vector<SCALARTYPE>& v)
{
  return np::from_object(std_vector_to_list<SCALARTYPE>(v),
			 np::dtype::get_builtin<SCALARTYPE>());
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<std::vector<SCALARTYPE> >
std_vector_init_ndarray(const np::ndarray& array)
{
  int d = array.get_nd();
  if (d != 1) {
    PyErr_SetString(PyExc_TypeError, "Can only create a vector from a 1-D array!");
    bp::throw_error_already_set();
  }
  
  vcl::vcl_size_t s = (vcl::vcl_size_t) array.shape(0);
  
  std::vector<SCALARTYPE> *v = new std::vector<SCALARTYPE>(s);
  
  for (vcl::vcl_size_t i=0; i < s; ++i)
    (*v)[i] = bp::extract<SCALARTYPE>(array[i]);
  
  return vcl::tools::shared_ptr<std::vector<SCALARTYPE> >(v);
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<std::vector<SCALARTYPE> >
std_vector_init_list(const bp::list& l)
{
  return std_vector_init_ndarray<SCALARTYPE>
    (np::from_object(l, np::dtype::get_builtin<SCALARTYPE>()));
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<std::vector<SCALARTYPE> >
std_vector_init_scalar(vcl::vcl_size_t length, SCALARTYPE value) {
  std::vector<SCALARTYPE> *v = new std::vector<SCALARTYPE>(length);
  for (vcl::vcl_size_t i=0; i < length; ++i)
    (*v)[i] = value;
  return vcl::tools::shared_ptr<std::vector<SCALARTYPE> >(v);
}

// Vector -- vcl::vector

template <class SCALARTYPE>
bp::list vcl_vector_to_list(const vcl::vector_base<SCALARTYPE>& v)
{
  std::vector<SCALARTYPE> c(v.size());
  vcl::copy(v.begin(), v.end(), c.begin());

  return std_vector_to_list(c);
}

template <class SCALARTYPE>
np::ndarray vcl_vector_to_ndarray(const vcl::vector_base<SCALARTYPE>& v)
{
  return np::from_object(vcl_vector_to_list<SCALARTYPE>(v),
			 np::dtype::get_builtin<SCALARTYPE>());
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >
vcl_vector_init_mem(SCALARTYPE* ptr, vcl::memory_types mem_type,
                    vcl::vcl_size_t size, vcl::vcl_size_t start = 0,
                    vcl::vcl_ptrdiff_t stride = 1)
{
  vcl::vector<SCALARTYPE> *v = new vcl::vector<SCALARTYPE>
    (ptr, mem_type, size, start, stride);
  return vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >(v);
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >
vcl_vector_init_ndarray(const np::ndarray& array, const vcl::context& ctx)
{
  int d = array.get_nd();
  if (d != 1) {
    PyErr_SetString(PyExc_TypeError, "Can only create a vector from a 1-D array!");
    bp::throw_error_already_set();
  }
  
  vcl::vcl_size_t s = (vcl::vcl_size_t) array.shape(0);
  
  vcl::vector<SCALARTYPE> *v = new vcl::vector<SCALARTYPE>(s, ctx);
  std::vector<SCALARTYPE> cpu_vector(s);
  
  for (vcl::vcl_size_t i=0; i < s; ++i)
    cpu_vector[i] = bp::extract<SCALARTYPE>(array[i]);
  
  vcl::copy(cpu_vector.begin(), cpu_vector.end(), v->begin());

  return vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >(v);
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >
vcl_vector_init_list(const bp::list& l, const vcl::context& ctx)
{
  return vcl_vector_init_ndarray<SCALARTYPE>
    (np::from_object(l, np::dtype::get_builtin<SCALARTYPE>()), ctx);
}

template <class SCALARTYPE>
vcl::tools::shared_ptr<vcl::vector<SCALARTYPE> >
vcl_vector_init_scalar(vcl::vcl_size_t length, SCALARTYPE value,
                       const vcl::context &ctx)
{
  ublas::scalar_vector<SCALARTYPE> s_v(length, value);
  vcl::vector<SCALARTYPE> *v = new vcl::vector<SCALARTYPE>(length, ctx);
  vcl::copy(s_v.begin(), s_v.end(), v->begin());
{'fraction_non_alphanumeric': 0.16645678305666858, 'fraction_numerical': 0.002976531196336577, 'mean_word_length': 5.699386503067485, 'pattern_counts': {'":': 0, '<': 143, '<?xml version=': 0, '>': 137, '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': '7427439', 'n_tokens_mistral': 3638, 'n_tokens_neox': 3585, 'n_words': 590}