1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef mozilla_SimpleMap_h
6 #define mozilla_SimpleMap_h
8 #include "mozilla/Mutex.h"
18 typedef std::pair
<int64_t, T
> Element
;
20 SimpleMap() : mMutex("SimpleMap") {}
22 // Insert Key and Value pair at the end of our map.
23 void Insert(int64_t aKey
, const T
& aValue
) {
24 MutexAutoLock
lock(mMutex
);
25 mMap
.AppendElement(std::make_pair(aKey
, aValue
));
27 // Sets aValue matching aKey and remove it from the map if found.
28 // The element returned is the first one found.
29 // Returns true if found, false otherwise.
30 bool Find(int64_t aKey
, T
& aValue
) {
31 MutexAutoLock
lock(mMutex
);
32 for (uint32_t i
= 0; i
< mMap
.Length(); i
++) {
33 Element
& element
= mMap
[i
];
34 if (element
.first
== aKey
) {
35 aValue
= element
.second
;
36 mMap
.RemoveElementAt(i
);
42 // Remove all elements of the map.
44 MutexAutoLock
lock(mMutex
);
49 Mutex mMutex MOZ_UNANNOTATED
; // To protect mMap.
50 AutoTArray
<Element
, 16> mMap
;
53 } // namespace mozilla
55 #endif // mozilla_SimpleMap_h