OmniSciDB  d2f719934e
 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 (c) 2020 OmniSci, 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  * @author Matt Pulver <matt.pulver@omnisci.com>
20  * @description Like string_view but as a vector.
21  * Useful for splitting memory among thread workers.
22  *
23  */
24 
25 #pragma once
26 
27 #include "funcannotations.h"
28 
29 #include <cassert>
30 
34 template <typename T>
35 class VectorView {
36  public:
37  using value_type = T;
38  using size_type = std::size_t;
39  using difference_type = std::ptrdiff_t;
41  using const_reference = value_type const&;
42  using pointer = value_type*;
43  using const_pointer = value_type const*;
44  using iterator = pointer;
46 
47  private:
48  T* data_{nullptr};
51 
52  public:
53  VectorView() = default;
55  : data_(data), size_(size), capacity_(capacity) {}
56  DEVICE VectorView(T* data, size_type const size) : VectorView(data, size, size) {}
57 
58  DEVICE T& back() { return data_[size_ - 1]; }
59  DEVICE T const& back() const { return data_[size_ - 1]; }
60  DEVICE T* begin() const { return data_; }
61  DEVICE size_type capacity() const { return capacity_; }
62  DEVICE T const* cbegin() const { return data_; }
63  DEVICE T const* cend() const { return data_ + size_; }
64  DEVICE void clear() { size_ = 0; }
65  DEVICE T* data() { return data_; }
66  DEVICE T const* data() const { return data_; }
67  DEVICE bool empty() const { return size_ == 0; }
68  DEVICE T* end() const { return data_ + size_; }
69  DEVICE bool full() const { return size_ == capacity_; }
70  DEVICE T& front() { return *data_; }
71  DEVICE T const& front() const { return *data_; }
72  DEVICE T& operator[](size_type const i) { return data_[i]; }
73  DEVICE T const& operator[](size_type const i) const { return data_[i]; }
74  DEVICE void push_back(T const& value) { data_[size_++] = value; }
75  DEVICE void resize(size_type const size) {
76  assert(size <= capacity_);
77  size_ = size;
78  }
79  // Does not change capacity_.
80  DEVICE void set(T* data, size_type const size) {
81  resize(size);
82  data_ = data;
83  }
84  DEVICE size_type size() const { return size_; }
85 };
value_type * pointer
Definition: VectorView.h:42
DEVICE VectorView(T *data, size_type const size)
Definition: VectorView.h:56
DEVICE void push_back(T const &value)
Definition: VectorView.h:74
DEVICE size_type capacity() const
Definition: VectorView.h:61
DEVICE T const & front() const
Definition: VectorView.h:71
DEVICE T & operator[](size_type const i)
Definition: VectorView.h:72
std::size_t size_type
Definition: VectorView.h:38
DEVICE T * data()
Definition: VectorView.h:65
DEVICE void resize(size_type const size)
Definition: VectorView.h:75
DEVICE void clear()
Definition: VectorView.h:64
#define DEVICE
DEVICE size_type size() const
Definition: VectorView.h:84
DEVICE void set(T *data, size_type const size)
Definition: VectorView.h:80
DEVICE VectorView(T *data, size_type const size, size_type const capacity)
Definition: VectorView.h:54
DEVICE T * begin() const
Definition: VectorView.h:60
DEVICE T const & back() const
Definition: VectorView.h:59
std::ptrdiff_t difference_type
Definition: VectorView.h:39
DEVICE T const * cend() const
Definition: VectorView.h:63
DEVICE T const * cbegin() const
Definition: VectorView.h:62
value_type const & const_reference
Definition: VectorView.h:41
DEVICE T & front()
Definition: VectorView.h:70
size_type capacity_
Definition: VectorView.h:50
value_type const * const_pointer
Definition: VectorView.h:43
DEVICE T const & operator[](size_type const i) const
Definition: VectorView.h:73
value_type & reference
Definition: VectorView.h:40
const_pointer const_iterator
Definition: VectorView.h:45
size_type size_
Definition: VectorView.h:49
DEVICE T const * data() const
Definition: VectorView.h:66
DEVICE bool full() const
Definition: VectorView.h:69
DEVICE T & back()
Definition: VectorView.h:58
VectorView()=default
DEVICE bool empty() const
Definition: VectorView.h:67
DEVICE T * end() const
Definition: VectorView.h:68