1 // Copyright 2018 The Dawn Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef COMMON_SERIALMAP_H_ 16 #define COMMON_SERIALMAP_H_ 17 18 #include "common/SerialStorage.h" 19 20 #include <map> 21 #include <vector> 22 23 template <typename Serial, typename Value> 24 class SerialMap; 25 26 template <typename SerialT, typename ValueT> 27 struct SerialStorageTraits<SerialMap<SerialT, ValueT>> { 28 using Serial = SerialT; 29 using Value = ValueT; 30 using Storage = std::map<Serial, std::vector<Value>>; 31 using StorageIterator = typename Storage::iterator; 32 using ConstStorageIterator = typename Storage::const_iterator; 33 }; 34 35 // SerialMap stores a map from Serial to Value. 36 // Unlike SerialQueue, items may be enqueued with Serials in any 37 // arbitrary order. SerialMap provides useful iterators for iterating 38 // through Value items in order of increasing Serial. 39 template <typename Serial, typename Value> 40 class SerialMap : public SerialStorage<SerialMap<Serial, Value>> { 41 public: 42 void Enqueue(const Value& value, Serial serial); 43 void Enqueue(Value&& value, Serial serial); 44 void Enqueue(const std::vector<Value>& values, Serial serial); 45 void Enqueue(std::vector<Value>&& values, Serial serial); 46 }; 47 48 // SerialMap 49 50 template <typename Serial, typename Value> 51 void SerialMap<Serial, Value>::Enqueue(const Value& value, Serial serial) { 52 this->mStorage[serial].emplace_back(value); 53 } 54 55 template <typename Serial, typename Value> 56 void SerialMap<Serial, Value>::Enqueue(Value&& value, Serial serial) { 57 this->mStorage[serial].emplace_back(value); 58 } 59 60 template <typename Serial, typename Value> 61 void SerialMap<Serial, Value>::Enqueue(const std::vector<Value>& values, Serial serial) { 62 DAWN_ASSERT(values.size() > 0); 63 for (const Value& value : values) { 64 Enqueue(value, serial); 65 } 66 } 67 68 template <typename Serial, typename Value> 69 void SerialMap<Serial, Value>::Enqueue(std::vector<Value>&& values, Serial serial) { 70 DAWN_ASSERT(values.size() > 0); 71 for (const Value& value : values) { 72 Enqueue(value, serial); 73 } 74 } 75 76 #endif // COMMON_SERIALMAP_H_ 77