MAK Legion 1.1 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 #pragma once
10 
11 #include <stdexcept>
12 
13 #include <vector>
14 
15 namespace Legion
16 {
21  template <typename DATA_TYPE>
22  class Span
23  {
24  public:
26  Span()
27  :myCount(0)
28  , myData(0)
29  {
30  }
31 
33  Span(const DATA_TYPE* data, int count)
34  :myCount(count)
35  , myData(data)
36  {
37  }
38 
40  Span(const std::vector<DATA_TYPE>& dataVector)
41  :myCount(dataVector.size())
42  , myData(dataVector.data())
43  {
44  }
45 
48  {
49  }
50 
52  Span(const Span& orig)
53  {
54  myData = orig.myData;
55  myCount = orig.myCount;
56  }
57 
59  Span& operator=(const Span& orig)
60  {
61  if (this != &orig)
62  {
63  myData = orig.myData;
64  myCount = orig.myCount;
65  }
66 
67  return *this;
68  }
69 
71  inline bool operator==(const Span<DATA_TYPE>& rhs) const
72  {
73  return myData == rhs.myData && myCount == rhs.myCount;
74  }
75 
77  inline bool operator!=(const Span<DATA_TYPE>& rhs) const
78  {
79  return !(*this == rhs);
80  }
81 
83  inline int size() const { return myCount; }
84 
86  inline int sizeInBytes() const { return myCount * sizeof(DATA_TYPE); }
87 
89  inline const DATA_TYPE* data() const {return myData;}
90 
92  const DATA_TYPE& operator[](int index) const
93  {
94 #if _DEBUG
95  if (index < 0 || index > myCount)
96  {
97  throw std::runtime_error("Index out of range");
98  }
99 #endif
100 
101  return myData[index];
102  }
103 
106  Span slice(int offset)
107  {
108  return Span(myData + offset, myCount - offset);
109  }
110 
113  Span slice(int offset, int count)
114  {
115 #if _DEBUG
116  if ((offset + count) > myCount)
117  {
118  throw std::runtime_error("Subspan out of range");
119  }
120 #endif
121 
122  return Span(myData + offset, count);
123  }
124 
127  inline std::vector<DATA_TYPE>& assignTo(std::vector<DATA_TYPE>& destination) const
128  {
129  if (size() > 0)
130  destination.assign(data(), data() + size());
131  else
132  destination.clear();
133  return destination;
134  }
135 
138  inline std::vector<DATA_TYPE>& appendTo(std::vector<DATA_TYPE>& destination) const
139  {
140  if (size() > 0)
141  destination.insert(destination.end(), data(), data() + size());
142  return destination;
143  }
144 
145 
146  protected:
147  int myCount;
148  const DATA_TYPE* myData;
149  };
150 }
Span(const DATA_TYPE *data, int count)
constructor for raw data
Definition: span.h:33
~Span()
destructor
Definition: span.h:47
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:138
bool operator!=(const Span< DATA_TYPE > &rhs) const
inequality operator
Definition: span.h:77
int myCount
Definition: span.h:147
Span & operator=(const Span &orig)
assignment operator
Definition: span.h:59
bool operator==(const Span< DATA_TYPE > &rhs) const
equality operator
Definition: span.h:71
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:106
Span(const std::vector< DATA_TYPE > &dataVector)
constructor for complete vector
Definition: span.h:40
A span represents a contiguous array of objects in memory.
Definition: span.h:22
const DATA_TYPE & operator[](int index) const
Array access to the elements in the array.
Definition: span.h:92
int size() const
Returns the count of objects in the array.
Definition: span.h:83
const DATA_TYPE * data() const
Returns a pointer to the underlying data.
Definition: span.h:89
const DATA_TYPE * myData
Definition: span.h:148
Span()
default constructor
Definition: span.h:26
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:113
Span(const Span &orig)
copy constructor
Definition: span.h:52
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:127
int sizeInBytes() const
Returns the size of the array in bytes.
Definition: span.h:86

Document ID: Generated on Wed May 26 15:17:05 EDT 2021 from SVN revision 229676
Copyright © 2005-2021 MAK Technologies. All Rights Reserved (www.mak.com)