OmniSciDB  0264ff685a
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 
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:42
DEVICE T const * cbegin() const
Definition: VectorView.h:61
DEVICE void push_back(T const &value)
Definition: VectorView.h:73
DEVICE bool full() const
Definition: VectorView.h:68
DEVICE T & operator[](size_type const i)
Definition: VectorView.h:71
std::size_t size_type
Definition: VectorView.h:38
DEVICE T * data()
Definition: VectorView.h:64
DEVICE void resize(size_type const size)
Definition: VectorView.h:74
DEVICE bool empty() const
Definition: VectorView.h:66
DEVICE void clear()
Definition: VectorView.h:63
#define DEVICE
DEVICE size_type size() const
Definition: VectorView.h:83
DEVICE T const & back() const
Definition: VectorView.h:58
DEVICE VectorView(T *data, size_type const size, size_type const capacity)
Definition: VectorView.h:54
std::ptrdiff_t difference_type
Definition: VectorView.h:39
DEVICE T * begin() const
Definition: VectorView.h:59
DEVICE T * end() const
Definition: VectorView.h:67
value_type const & const_reference
Definition: VectorView.h:41
DEVICE T & front()
Definition: VectorView.h:69
size_type capacity_
Definition: VectorView.h:50
value_type const * const_pointer
Definition: VectorView.h:43
value_type & reference
Definition: VectorView.h:40
const_pointer const_iterator
Definition: VectorView.h:45
DEVICE size_type capacity() const
Definition: VectorView.h:60
DEVICE T const * cend() const
Definition: VectorView.h:62
size_type size_
Definition: VectorView.h:49
DEVICE T const & operator[](size_type const i) const
Definition: VectorView.h:72
DEVICE T const & front() const
Definition: VectorView.h:70
DEVICE T const * data() const
Definition: VectorView.h:65
DEVICE T & back()
Definition: VectorView.h:57
VectorView()=default