• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 T>
24 class SerialMap;
25 
26 template <typename T>
27 struct SerialStorageTraits<SerialMap<T>> {
28     using Value = T;
29     using Storage = std::map<Serial, std::vector<T>>;
30     using StorageIterator = typename Storage::iterator;
31     using ConstStorageIterator = typename Storage::const_iterator;
32 };
33 
34 // SerialMap stores a map from Serial to T.
35 // Unlike SerialQueue, items may be enqueued with Serials in any
36 // arbitrary order. SerialMap provides useful iterators for iterating
37 // through T items in order of increasing Serial.
38 template <typename T>
39 class SerialMap : public SerialStorage<SerialMap<T>> {
40   public:
41     void Enqueue(const T& value, Serial serial);
42     void Enqueue(T&& value, Serial serial);
43     void Enqueue(const std::vector<T>& values, Serial serial);
44     void Enqueue(std::vector<T>&& values, Serial serial);
45 };
46 
47 // SerialMap
48 
49 template <typename T>
50 void SerialMap<T>::Enqueue(const T& value, Serial serial) {
51     this->mStorage[serial].emplace_back(value);
52 }
53 
54 template <typename T>
55 void SerialMap<T>::Enqueue(T&& value, Serial serial) {
56     this->mStorage[serial].emplace_back(value);
57 }
58 
59 template <typename T>
60 void SerialMap<T>::Enqueue(const std::vector<T>& values, Serial serial) {
61     DAWN_ASSERT(values.size() > 0);
62     for (const T& value : values) {
63         Enqueue(value, serial);
64     }
65 }
66 
67 template <typename T>
68 void SerialMap<T>::Enqueue(std::vector<T>&& values, Serial serial) {
69     DAWN_ASSERT(values.size() > 0);
70     for (const T& value : values) {
71         Enqueue(value, serial);
72     }
73 }
74 
75 #endif  // COMMON_SERIALMAP_H_
76