OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
VectorView.h
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
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 
17 /*
18  * @file VectorView.h
19  * @description Like string_view but as a vector.
20  * Useful for splitting memory among thread workers.
21  *
22  */
23 
24 #pragma once
25 
26 #include "funcannotations.h"
27 
28 #include <cassert>
29 
33 template <typename T>
34 class VectorView {
35  public:
36  using value_type = T;
37  using size_type = std::size_t;
38  using difference_type = std::ptrdiff_t;
40  using const_reference = value_type const&;
41  using pointer = value_type*;
42  using const_pointer = value_type const*;
43  using iterator = pointer;
45 
46  private:
47  T* data_{nullptr};
50 
51  public:
52  VectorView() = default;
54  : data_(data), size_(size), capacity_(capacity) {}
55  DEVICE VectorView(T* data, size_type const size) : VectorView(data, size, size) {}
56 
57  DEVICE T& back() { return data_[size_ - 1]; }
58  DEVICE T const& back() const { return data_[size_ - 1]; }
59  DEVICE T* begin() const { return data_; }
60  DEVICE size_type capacity() const { return capacity_; }
61  DEVICE T const* cbegin() const { return data_; }
62  DEVICE T const* cend() const { return data_ + size_; }
63  DEVICE void clear() { size_ = 0; }
64  DEVICE T* data() { return data_; }
65  DEVICE T const* data() const { return data_; }
66  DEVICE bool empty() const { return size_ == 0; }
67  DEVICE T* end() const { return data_ + size_; }
68  DEVICE bool full() const { return size_ == capacity_; }
69  DEVICE T& front() { return *data_; }
70  DEVICE T const& front() const { return *data_; }
71  DEVICE T& operator[](size_type const i) { return data_[i]; }
72  DEVICE T const& operator[](size_type const i) const { return data_[i]; }
73  DEVICE void push_back(T const& value) { data_[size_++] = value; }
74  DEVICE void resize(size_type const size) {
75  assert(size <= capacity_);
76  size_ = size;
77  }
78  // Does not change capacity_.
79  DEVICE void set(T* data, size_type const size) {
80  resize(size);
81  data_ = data;
82  }
83  DEVICE size_type size() const { return size_; }
84 };
value_type * pointer
Definition: VectorView.h:41
DEVICE VectorView(T *data, size_type const size)
Definition: VectorView.h:55
DEVICE void push_back(T const &value)
Definition: VectorView.h:73
DEVICE size_type capacity() const
Definition: VectorView.h:60
DEVICE T const & front() const
Definition: VectorView.h:70
DEVICE T & operator[](size_type const i)
Definition: VectorView.h:71
std::size_t size_type
Definition: VectorView.h:37
DEVICE T * data()
Definition: VectorView.h:64
DEVICE void resize(size_type const size)
Definition: VectorView.h:74
DEVICE void clear()
Definition: VectorView.h:63
#define DEVICE
DEVICE size_type size() const
Definition: VectorView.h:83
DEVICE void set(T *data, size_type const size)
Definition: VectorView.h:79
DEVICE VectorView(T *data, size_type const size, size_type const capacity)
Definition: VectorView.h:53
DEVICE T * begin() const
Definition: VectorView.h:59
DEVICE T const & back() const
Definition: VectorView.h:58
std::ptrdiff_t difference_type
Definition: VectorView.h:38
DEVICE T const * cend() const
Definition: VectorView.h:62
DEVICE T const * cbegin() const
Definition: VectorView.h:61
value_type const & const_reference
Definition: VectorView.h:40
DEVICE T & front()
Definition: VectorView.h:69
size_type capacity_
Definition: VectorView.h:49
value_type const * const_pointer
Definition: VectorView.h:42
DEVICE T const & operator[](size_type const i) const
Definition: VectorView.h:72
value_type & reference
Definition: VectorView.h:39
const_pointer const_iterator
Definition: VectorView.h:44
size_type size_
Definition: VectorView.h:48
DEVICE T const * data() const
Definition: VectorView.h:65
DEVICE bool full() const
Definition: VectorView.h:68
DEVICE T & back()
Definition: VectorView.h:57
VectorView()=default
DEVICE bool empty() const
Definition: VectorView.h:66
DEVICE T * end() const
Definition: VectorView.h:67