The Sparta Modeling Framework
Loading...
Searching...
No Matches
FrontArray.hpp
Go to the documentation of this file.
1// <FrontArray.hpp> -*- C++ -*-
2
10#pragma once
11
13
14namespace sparta{
15
28 template <class DataT, ArrayType ArrayT = ArrayType::AGED>
29 class FrontArray : public Array<DataT, ArrayT>
30 {
31 //Typedef for the base type we inherit from
33
34 public:
42 FrontArray(const std::string& name, uint32_t num_entries, const Clock* clk,
43 StatisticSet* statset = nullptr) :
44 BaseArray(name, num_entries, clk, statset)
45 {
46 }
47
49 const DataT& readValid(const uint32_t nth=0) const
50 {
51 sparta_assert(nth < BaseArray::capacity(), "Cannot read at index larger than the size of the array");
52 sparta_assert(nth < BaseArray::numValid(), "Asked for an idx that is not valid");
53 // We need to iterate over the underlaying vector, array_.
54 // Then count how many valid entries we have passed until
55 // we are at the nth'th valid entree.
56 uint32_t n = 0;
57 uint32_t i = 0;
58 while(n <= nth)
59 {
61 {
62 ++n;
63 }
64 ++i;
65 }
66 // if i is not > 0, then we are going to fail that return
67 // line with i-1
68 sparta_assert(i > 0);
69 return BaseArray::read(i - 1);
70 }
71
79 uint32_t writeFront(const DataT& dat)
80 {
81 return writeFrontImpl_(dat);
82 }
83
91 uint32_t writeFront(DataT&& dat)
92 {
93 return writeFrontImpl_(std::move(dat));
94 }
95
103 uint32_t writeBack(const DataT& dat)
104 {
105 return writeBackImpl_(dat);
106 }
107
115 uint32_t writeBack(DataT&& dat)
116 {
117 return writeBackImpl_(std::move(dat));
118 }
119
121 // STL compatibility -- kinda
122
128 uint32_t push_front(const DataT & dat) { return writeFront(dat); }
129
135 uint32_t push_front(DataT&& dat) { return writeFront(std::forward(dat)); }
136
142 uint32_t push_back(const DataT & dat) { return writeBack(dat); }
143
149 uint32_t push_back(DataT && dat) { return writeBack(std::forward(dat)); }
150
151 private:
152 template<typename U>
153 uint32_t writeFrontImpl_(U&& dat)
154 {
155 uint32_t i;
156 for(i = 0; i < BaseArray::capacity(); ++i)
157 {
158 if(!BaseArray::isValid(i))
159 {
160 break; //we found an entry that is invalid. This is the front!
161 }
162 }
164 "Cannot write to the front of the Array. There are no free entries.");
165 BaseArray::write(i, std::forward<U>(dat));
166 return i;
167 }
168
169 template<typename U>
170 uint32_t writeBackImpl_(U&& dat)
171 {
172 int32_t i;
173 for(i = BaseArray::capacity()-1; i>=0;--i)
174 {
175 if(!BaseArray::isValid(i))
176 {
177 break; //we found an entry that is invalid. This is the back!
178 }
179 }
180 sparta_assert(i >= 0, "Cannot write to the back of the array. There are no free entries.");
181 BaseArray::write(i, std::forward<U>(dat));
182 return i;
183 }
184 };
185}
Defines the Array class.
#define sparta_assert(...)
Simple variadic assertion that will throw a sparta_exception if the condition fails.
Array is essentially a fixed size vector, maintains a concept of validity of its indexes,...
Definition Array.hpp:58
size_type capacity() const
Return the maximum number of elements this Array can hold.
Definition Array.hpp:680
void write(const uint32_t idx, const DataT &dat)
Write data to the array.
Definition Array.hpp:770
bool isValid(const uint32_t idx) const
Determine whether an index is currently valid.
Definition Array.hpp:541
const DataT & read(const uint32_t idx) const
Read (only) the data at an index.
Definition Array.hpp:550
size_type numValid() const
The number of valid entries contained.
Definition Array.hpp:688
A representation of simulated time.
Definition Clock.hpp:51
a type of Array with special allocation policies to support writing to the front most valid entry in ...
const DataT & readValid(const uint32_t nth=0) const
read the nth valid object from the front of the array.
uint32_t writeFront(const DataT &dat)
a public method that can be used to write data to the first invalidate entry in the array....
uint32_t writeBack(DataT &&dat)
a public method that can be used to write data to the last invalidate entry in the array....
uint32_t writeBack(const DataT &dat)
a public method that can be used to write data to the last invalidate entry in the array....
uint32_t push_back(DataT &&dat)
Push the first available index in the Array starting from the back.
uint32_t push_front(DataT &&dat)
Push the first available index in the Array.
FrontArray(const std::string &name, uint32_t num_entries, const Clock *clk, StatisticSet *statset=nullptr)
Construct the array.
uint32_t push_back(const DataT &dat)
Push the first available index in the Array starting from the back.
uint32_t push_front(const DataT &dat)
Push the first available index in the Array.
uint32_t writeFront(DataT &&dat)
a public method that can be used to write data to the first invalidate entry in the array....
Set of StatisticDef and CounterBase-derived objects for visiblility through a sparta Tree.
Macros for handling exponential backoff.