SerialQueue/Map: Take the serial type as type paramater.

This is in preparation for follow-up CLs that will use typed integers
for the various serial types.

Bug: dawn:442

Change-Id: I5296546e96acd6ac9f7a0bfc46dc7eba40cb3cf5
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/28921
Reviewed-by: Jiawei Shao <jiawei.shao@intel.com>
Reviewed-by: Austin Eng <enga@chromium.org>
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Corentin Wallez
2020-09-28 12:28:13 +00:00
committed by Commit Bot service account
parent ed2b465f86
commit 145f115c54
20 changed files with 121 additions and 81 deletions

View File

@@ -20,54 +20,55 @@
#include <map>
#include <vector>
template <typename T>
template <typename Serial, typename Value>
class SerialMap;
template <typename T>
struct SerialStorageTraits<SerialMap<T>> {
using Value = T;
using Storage = std::map<Serial, std::vector<T>>;
template <typename SerialT, typename ValueT>
struct SerialStorageTraits<SerialMap<SerialT, ValueT>> {
using Serial = SerialT;
using Value = ValueT;
using Storage = std::map<Serial, std::vector<Value>>;
using StorageIterator = typename Storage::iterator;
using ConstStorageIterator = typename Storage::const_iterator;
};
// SerialMap stores a map from Serial to T.
// SerialMap stores a map from Serial to Value.
// Unlike SerialQueue, items may be enqueued with Serials in any
// arbitrary order. SerialMap provides useful iterators for iterating
// through T items in order of increasing Serial.
template <typename T>
class SerialMap : public SerialStorage<SerialMap<T>> {
// through Value items in order of increasing Serial.
template <typename Serial, typename Value>
class SerialMap : public SerialStorage<SerialMap<Serial, Value>> {
public:
void Enqueue(const T& value, Serial serial);
void Enqueue(T&& value, Serial serial);
void Enqueue(const std::vector<T>& values, Serial serial);
void Enqueue(std::vector<T>&& values, Serial serial);
void Enqueue(const Value& value, Serial serial);
void Enqueue(Value&& value, Serial serial);
void Enqueue(const std::vector<Value>& values, Serial serial);
void Enqueue(std::vector<Value>&& values, Serial serial);
};
// SerialMap
template <typename T>
void SerialMap<T>::Enqueue(const T& value, Serial serial) {
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(const Value& value, Serial serial) {
this->mStorage[serial].emplace_back(value);
}
template <typename T>
void SerialMap<T>::Enqueue(T&& value, Serial serial) {
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(Value&& value, Serial serial) {
this->mStorage[serial].emplace_back(value);
}
template <typename T>
void SerialMap<T>::Enqueue(const std::vector<T>& values, Serial serial) {
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(const std::vector<Value>& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
for (const T& value : values) {
for (const Value& value : values) {
Enqueue(value, serial);
}
}
template <typename T>
void SerialMap<T>::Enqueue(std::vector<T>&& values, Serial serial) {
template <typename Serial, typename Value>
void SerialMap<Serial, Value>::Enqueue(std::vector<Value>&& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
for (const T& value : values) {
for (const Value& value : values) {
Enqueue(value, serial);
}
}

View File

@@ -19,65 +19,65 @@
#include <vector>
template <typename T>
template <typename Serial, typename Value>
class SerialQueue;
template <typename T>
struct SerialStorageTraits<SerialQueue<T>> {
using Value = T;
using SerialPair = std::pair<Serial, std::vector<T>>;
template <typename SerialT, typename ValueT>
struct SerialStorageTraits<SerialQueue<SerialT, ValueT>> {
using Serial = SerialT;
using Value = ValueT;
using SerialPair = std::pair<Serial, std::vector<Value>>;
using Storage = std::vector<SerialPair>;
using StorageIterator = typename Storage::iterator;
using ConstStorageIterator = typename Storage::const_iterator;
};
// SerialQueue stores an associative list mapping a Serial to T.
// SerialQueue stores an associative list mapping a Serial to Value.
// It enforces that the Serials enqueued are strictly non-decreasing.
// This makes it very efficient iterate or clear all items added up
// to some Serial value because they are stored contiguously in memory.
template <typename T>
class SerialQueue : public SerialStorage<SerialQueue<T>> {
template <typename Serial, typename Value>
class SerialQueue : public SerialStorage<SerialQueue<Serial, Value>> {
public:
using SerialPair = typename SerialStorageTraits<SerialQueue<T>>::SerialPair;
// The serial must be given in (not strictly) increasing order.
void Enqueue(const T& value, Serial serial);
void Enqueue(T&& value, Serial serial);
void Enqueue(const std::vector<T>& values, Serial serial);
void Enqueue(std::vector<T>&& values, Serial serial);
void Enqueue(const Value& value, Serial serial);
void Enqueue(Value&& value, Serial serial);
void Enqueue(const std::vector<Value>& values, Serial serial);
void Enqueue(std::vector<Value>&& values, Serial serial);
};
// SerialQueue
template <typename T>
void SerialQueue<T>::Enqueue(const T& value, Serial serial) {
template <typename Serial, typename Value>
void SerialQueue<Serial, Value>::Enqueue(const Value& value, Serial serial) {
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
if (this->Empty() || this->mStorage.back().first < serial) {
this->mStorage.emplace_back(serial, std::vector<T>{});
this->mStorage.emplace_back(serial, std::vector<Value>{});
}
this->mStorage.back().second.push_back(value);
}
template <typename T>
void SerialQueue<T>::Enqueue(T&& value, Serial serial) {
template <typename Serial, typename Value>
void SerialQueue<Serial, Value>::Enqueue(Value&& value, Serial serial) {
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
if (this->Empty() || this->mStorage.back().first < serial) {
this->mStorage.emplace_back(serial, std::vector<T>{});
this->mStorage.emplace_back(serial, std::vector<Value>{});
}
this->mStorage.back().second.push_back(std::move(value));
}
template <typename T>
void SerialQueue<T>::Enqueue(const std::vector<T>& values, Serial serial) {
template <typename Serial, typename Value>
void SerialQueue<Serial, Value>::Enqueue(const std::vector<Value>& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
this->mStorage.emplace_back(serial, values);
}
template <typename T>
void SerialQueue<T>::Enqueue(std::vector<T>&& values, Serial serial) {
template <typename Serial, typename Value>
void SerialQueue<Serial, Value>::Enqueue(std::vector<Value>&& values, Serial serial) {
DAWN_ASSERT(values.size() > 0);
DAWN_ASSERT(this->Empty() || this->mStorage.back().first <= serial);
this->mStorage.emplace_back(serial, values);

View File

@@ -27,6 +27,7 @@ struct SerialStorageTraits {};
template <typename Derived>
class SerialStorage {
private:
using Serial = typename SerialStorageTraits<Derived>::Serial;
using Value = typename SerialStorageTraits<Derived>::Value;
using Storage = typename SerialStorageTraits<Derived>::Storage;
using StorageIterator = typename SerialStorageTraits<Derived>::StorageIterator;
@@ -158,13 +159,13 @@ void SerialStorage<Derived>::ClearUpTo(Serial serial) {
}
template <typename Derived>
Serial SerialStorage<Derived>::FirstSerial() const {
typename SerialStorage<Derived>::Serial SerialStorage<Derived>::FirstSerial() const {
DAWN_ASSERT(!Empty());
return mStorage.begin()->first;
}
template <typename Derived>
Serial SerialStorage<Derived>::LastSerial() const {
typename SerialStorage<Derived>::Serial SerialStorage<Derived>::LastSerial() const {
DAWN_ASSERT(!Empty());
return mStorage.back().first;
}