Fork me on GitHub
 All Classes Files Functions Variables Groups Pages
array1d.h
Go to the documentation of this file.
1 /*
2  * Copyright 2008-2014 NVIDIA Corporation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
22 #pragma once
23 
24 #include <cusp/detail/config.h>
25 
26 #include <cusp/memory.h>
27 #include <cusp/detail/format.h>
28 #include <cusp/exception.h>
29 
31 
32 #include <thrust/copy.h>
33 #include <thrust/host_vector.h>
34 #include <thrust/device_vector.h>
35 #include <thrust/detail/vector_base.h>
36 #include <thrust/iterator/constant_iterator.h>
37 #include <thrust/iterator/counting_iterator.h>
38 
39 namespace cusp
40 {
41 
42 // forward declaration of array1d_view
43 template <typename RandomAccessIterator> class array1d_view;
44 
97 template <typename T, typename MemorySpace>
98 class array1d : public thrust::detail::vector_base<T, typename cusp::default_memory_allocator<T, MemorySpace>::type>
99 {
100 private:
101  typedef typename cusp::default_memory_allocator<T, MemorySpace>::type Alloc;
102  typedef typename thrust::detail::vector_base<T,Alloc> Parent;
103 
104 public:
106  typedef MemorySpace memory_space;
107  typedef cusp::array1d_format format;
108 
109  typedef typename Parent::iterator iterator;
110  typedef typename Parent::const_iterator const_iterator;
111  typedef typename Parent::value_type value_type;
112  typedef typename Parent::size_type size_type;
113 
114  typedef cusp::array1d<T,MemorySpace> container;
115  typedef typename cusp::array1d_view<iterator> view;
116  typedef typename cusp::array1d_view<const_iterator> const_view;
117 
118  template<typename MemorySpace2>
119  struct rebind {
120  typedef cusp::array1d<T, MemorySpace2> type;
121  };
126  array1d(void)
127  : Parent() {}
128 
133  explicit array1d(size_type n)
134  : Parent(n) {}
135 
141  explicit array1d(size_type n, const value_type &value)
142  : Parent(n, value) {}
143 
147  array1d(const array1d &v)
148  : Parent(v) {}
149 
154  { Parent::operator=(v); return *this; }
155 
161  template<typename OtherT, typename OtherMem>
163  : Parent(v) {}
164 
170  template<typename OtherT, typename OtherMem>
172  { Parent::operator=(v); return *this; }
173 
179  template<typename OtherT, typename OtherAlloc>
180  array1d(const std::vector<OtherT, OtherAlloc> &v)
181  : Parent(v) {}
182 
188  template<typename OtherT, typename OtherAlloc>
189  array1d &operator=(const std::vector<OtherT, OtherAlloc> &v)
190  { Parent::operator=(v); return *this; }
191 
197  template<typename OtherT, typename OtherAlloc>
198  array1d(const thrust::host_vector<OtherT, OtherAlloc> &v)
199  : Parent(v) {}
200 
206  template<typename OtherT, typename OtherAlloc>
207  array1d &operator=(const thrust::host_vector<OtherT, OtherAlloc> &v)
208  { Parent::operator=(v); return *this; }
209 
215  template<typename OtherT, typename OtherAlloc>
216  array1d(const thrust::device_vector<OtherT, OtherAlloc> &v)
217  : Parent(v) {}
218 
224  template<typename OtherT, typename OtherAlloc>
225  array1d &operator=(const thrust::device_vector<OtherT, OtherAlloc> &v)
226  { Parent::operator=(v); return *this; }
227 
233  template<typename InputIterator>
234  array1d(InputIterator first, InputIterator last)
235  : Parent(first, last) {}
236 
241  template<typename InputIterator>
243  : Parent(v.begin(), v.end()) {}
244 
275  view subarray(size_type start_index, size_type num_entries);
276 
307  const_view subarray(size_type start_index, size_type num_entries) const;
308 
309 }; // end class array1d
360 template<typename RandomAccessIterator>
361 class array1d_view : public thrust::iterator_adaptor<array1d_view<RandomAccessIterator>, RandomAccessIterator>
362 {
363 private :
364  typedef thrust::iterator_adaptor<array1d_view<RandomAccessIterator>, RandomAccessIterator> Parent;
365 
366  friend class thrust::iterator_core_access;
367 
368 public :
369 
371  typedef RandomAccessIterator iterator;
372  typedef cusp::array1d_format format;
373 
374  typedef typename thrust::iterator_value<RandomAccessIterator>::type value_type;
375  typedef typename thrust::iterator_system<RandomAccessIterator>::type memory_space;
376  typedef typename thrust::iterator_pointer<RandomAccessIterator>::type pointer;
377  typedef typename thrust::iterator_reference<RandomAccessIterator>::type reference;
378  typedef typename thrust::iterator_difference<RandomAccessIterator>::type difference_type;
379  // typedef typename thrust::iterator_difference<RandomAccessIterator>::type size_type;
380  typedef size_t size_type;
381 
382  typedef typename cusp::array1d<value_type,memory_space> container;
383 
384  // TODO: Fixed view types, view == const_view???
385  typedef typename cusp::array1d_view<RandomAccessIterator> view;
386 
387  typedef RandomAccessIterator const_iterator;
388  typedef typename cusp::array1d_view<const_iterator> const_view;
393  array1d_view(void)
394  : m_size(0), m_capacity(0) {}
395 
400  template<typename ArrayType>
401  array1d_view(ArrayType &v)
402  : Parent(v.begin()), m_size(v.size()), m_capacity(v.capacity()) {}
403 
408  template<typename InputIterator>
409  array1d_view(InputIterator begin, InputIterator end)
410  : Parent(begin), m_size(end-begin), m_capacity(end-begin) {}
411 
417 
422  reference front(void) const;
423 
428  reference back(void) const;
429 
438  reference operator[](size_type n) const;
439 
444  iterator begin(void) const;
445 
450  iterator end(void) const;
451 
454  size_type size(void) const;
455 
459  size_type capacity(void) const;
460 
461  // Thrust does not export iterator pointer types so data is not valid
462  // see http://thrust.github.io/doc/classthrust_1_1iterator__facade.html
463  // pointer data(void)
464  // {
465  // return &front();
466  // }
467 
476  void resize(size_type new_size);
477 
509  view subarray(size_type start_index, size_type num_entries);
510 
511 
516  void swap(array1d_view &v);
517 
518 protected:
522  size_type m_size;
523 
527  size_type m_capacity;
528 };
529 
562 template <typename ValueType>
563 class counting_array
564  : public cusp::array1d_view< thrust::counting_iterator<ValueType> >
565 {
566 private:
567 
569 
570 public:
571 
573  typedef typename Parent::iterator iterator;
574  typedef typename Parent::size_type size_type;
582  counting_array(size_type size, ValueType init = ValueType(0))
583  : Parent(iterator(init), iterator(init) + size) {}
584 };
585 
612 template <typename ValueType>
613 class constant_array
614  : public cusp::array1d_view< thrust::constant_iterator<ValueType> >
615 {
616 private:
617 
619 
620 public:
621 
623  typedef typename Parent::iterator iterator;
624  typedef typename Parent::size_type size_type;
632  constant_array(size_type size, ValueType value)
633  : Parent(iterator(value), iterator(value) + size) {}
634 };
635 
660 template <typename ValueType>
661 class random_array :
662  public cusp::array1d_view< typename cusp::random_iterator<ValueType>::iterator >
663 {
664 private:
665 
667 
668 public:
669 
671  typedef typename cusp::random_iterator<ValueType> rand;
672  typedef typename Parent::size_type size_type;
680  random_array(size_type size, size_type seed = 0)
681  : Parent(rand(seed).begin(), rand(seed).begin() + size) {}
682 };
683 
684 
693 template <typename Iterator>
694 array1d_view<Iterator> make_array1d_view(Iterator first, Iterator last)
695 {
696  array1d_view<Iterator> view(first, last);
697 
698  return view;
699 }
700 
709 template <typename T, typename MemorySpace>
711 {
712  return make_array1d_view(v.begin(), v.end());
713 }
714 
722 template <typename Iterator>
724 {
725  return make_array1d_view(v.begin(), v.end());
726 }
727 
735 template <typename Iterator>
737 {
738  return make_array1d_view(v.begin(), v.end());
739 }
740 
749 template <typename T, typename MemorySpace>
751 {
752  return make_array1d_view(v.begin(), v.end());
753 }
757 } // end namespace cusp
758 
759 #include <cusp/detail/array1d.inl>
760 
reference front(void) const
array1d_view(void)
Definition: array1d.h:392
The array1d class is a 1D vector container that may contain elements stored in "host" or "device" mem...
Definition: array1d.h:98
iterator end(void) const
view subarray(size_type start_index, size_type num_entries)
array1d & operator=(const thrust::device_vector< OtherT, OtherAlloc > &v)
Definition: array1d.h:225
array1d(const array1d &v)
Definition: array1d.h:147
array1d(size_type n, const value_type &value)
Definition: array1d.h:141
The array1d_view class is a 1D vector container that wraps data from various iterators in a array1d d...
Definition: array1d.h:43
array1d(const array1d_view< InputIterator > &v)
Definition: array1d.h:242
array1d_view & operator=(const array1d_view &v)
size_type size(void) const
size_type m_capacity
Definition: array1d.h:526
array1d(void)
Definition: array1d.h:126
array1d(const std::vector< OtherT, OtherAlloc > &v)
Definition: array1d.h:180
void swap(array1d_view &v)
array1d(size_type n)
Definition: array1d.h:133
reference operator[](size_type n) const
Subscript access to the data contained in this array1d_view.
array1d(const thrust::device_vector< OtherT, OtherAlloc > &v)
Definition: array1d.h:216
counting_array(size_type size, ValueType init=ValueType(0))
Definition: array1d.h:581
constant_array(size_type size, ValueType value)
Definition: array1d.h:631
array1d & operator=(const thrust::host_vector< OtherT, OtherAlloc > &v)
Definition: array1d.h:207
Iterator for generating random values.
array1d< T, MemorySpace >::const_view make_array1d_view(const array1d< T, MemorySpace > &v)
Definition: array1d.h:749
array1d(InputIterator first, InputIterator last)
Definition: array1d.h:234
size_type capacity(void) const
reference back(void) const
array1d(const thrust::host_vector< OtherT, OtherAlloc > &v)
Definition: array1d.h:198
array1d & operator=(const array1d &v)
Definition: array1d.h:153
iterator begin(void) const
An iterator which generates random entries.
void resize(size_type new_size)
Resizes this array1d_view to the specified number of elements.
Specialized array1d_view wrapping thrust::constant_iterator.
Definition: array1d.h:612
random_array(size_type size, size_type seed=0)
Definition: array1d.h:679
array1d(const array1d< OtherT, OtherMem > &v)
Definition: array1d.h:162
array1d & operator=(const array1d< OtherT, OtherMem > &v)
Definition: array1d.h:171
view subarray(size_type start_index, size_type num_entries)
array1d & operator=(const std::vector< OtherT, OtherAlloc > &v)
Definition: array1d.h:189
size_type m_size
Definition: array1d.h:521
Specialized array1d_view wrapping thrust::counting_iterator.
Definition: array1d.h:562
Specialized array1d_view wrapping cusp::random_iterator.
Definition: array1d.h:660