MAK Legion API Documentation
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
span.h
Go to the documentation of this file.
1 /*********************************************************************************
2 ** Copyright (c) 2020 MAK Technologies, Inc.
3 ** All rights reserved.
4 *********************************************************************************/
5 
8 
9 
10 #pragma once
11 
12 #include <vector>
13 
14 namespace Legion
15 {
20  template <typename DATA_TYPE>
21  class Span
22  {
23  public:
25  Span()
26  :myCount(0)
27  , myData(0)
28  {
29  }
30 
32  Span(const DATA_TYPE* data, int count)
33  :myCount(count)
34  , myData(data)
35  {
36  }
37 
39  Span(const std::vector<DATA_TYPE>& dataVector)
40  :myCount(dataVector.size())
41  , myData(dataVector.data())
42  {
43  }
44 
47  {
48  }
49 
51  Span(const Span& orig)
52  {
53  myData = orig.myData;
54  myCount = orig.myCount;
55  }
56 
58  Span& operator=(const Span& orig)
59  {
60  if (this != &orig)
61  {
62  myData = orig.myData;
63  myCount = orig.myCount;
64  }
65 
66  return *this;
67  }
68 
70  inline bool operator==(const Span<DATA_TYPE>& rhs) const
71  {
72  return myData == rhs.myData && myCount == rhs.myCount;
73  }
74 
76  inline bool operator!=(const Span<DATA_TYPE>& rhs) const
77  {
78  return !(*this == rhs);
79  }
80 
82  inline int size() const { return myCount; }
83 
85  inline int sizeInBytes() const { return myCount * sizeof(DATA_TYPE); }
86 
88  inline const DATA_TYPE* data() const {return myData;}
89 
91  const DATA_TYPE& operator[](int index) const
92  {
93 #if _DEBUG
94  if (index < 0 || index > myCount)
95  {
96  throw std::exception("Index out of range");
97  }
98 #endif
99 
100  return myData[index];
101  }
102 
105  Span slice(int offset)
106  {
107  return Span(myData + offset, myCount - offset);
108  }
109 
112  Span slice(int offset, int count)
113  {
114 #if _DEBUG
115  if ((offset + count) > myCount)
116  {
117  throw std::exception("Subspan out of range");
118  }
119 #endif
120 
121  return Span(myData + offset, count);
122  }
123 
126  inline std::vector<DATA_TYPE>& assignTo(std::vector<DATA_TYPE>& destination) const
127  {
128  if (size() > 0)
129  destination.assign(data(), data() + size());
130  else
131  destination.clear();
132  return destination;
133  }
134 
137  inline std::vector<DATA_TYPE>& appendTo(std::vector<DATA_TYPE>& destination) const
138  {
139  if (size() > 0)
140  destination.insert(destination.end(), data(), data() + size());
141  return destination;
142  }
143 
144 
145  protected:
146  int myCount;
147  const DATA_TYPE* myData;
148  };
149 }
Span(const DATA_TYPE *data, int count)
constructor for raw data
Definition: span.h:32
~Span()
destructor
Definition: span.h:46
std::vector< DATA_TYPE > & appendTo(std::vector< DATA_TYPE > &destination) const
Append the contents of the span to the end of the given vector Return the given vector.
Definition: span.h:137
bool operator!=(const Span< DATA_TYPE > &rhs) const
inequality operator
Definition: span.h:76
int myCount
Definition: span.h:146
Span & operator=(const Span &orig)
assignment operator
Definition: span.h:58
bool operator==(const Span< DATA_TYPE > &rhs) const
equality operator
Definition: span.h:70
Span slice(int offset)
Returns a new span that is a subset of the existing span from the offset to the end of the array...
Definition: span.h:105
Span(const std::vector< DATA_TYPE > &dataVector)
constructor for complete vector
Definition: span.h:39
A span represents a contiguous array of objects in memory.
Definition: span.h:21
const DATA_TYPE & operator[](int index) const
Array access to the elements in the array.
Definition: span.h:91
int size() const
Returns the count of objects in the array.
Definition: span.h:82
const DATA_TYPE * data() const
Returns a pointer to the underlying data.
Definition: span.h:88
const DATA_TYPE * myData
Definition: span.h:147
Span()
default constructor
Definition: span.h:25
Span slice(int offset, int count)
Returns a new span that is a subset of the existing span from the offset for a given count of the arr...
Definition: span.h:112
Span(const Span &orig)
copy constructor
Definition: span.h:51
std::vector< DATA_TYPE > & assignTo(std::vector< DATA_TYPE > &destination) const
Assign the contents of the span to the given vector replacing its contents Return the given vector...
Definition: span.h:126
int sizeInBytes() const
Returns the size of the array in bytes.
Definition: span.h:85

Document ID: Generated on Mon Mar 8 15:11:30 EST 2021 from SVN revision 225633
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)