Line data Source code
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/. */
4 :
5 : #ifndef mozilla_SimpleMap_h
6 : #define mozilla_SimpleMap_h
7 :
8 : #include "mozilla/Pair.h"
9 : #include "nsTArray.h"
10 :
11 : namespace mozilla {
12 :
13 : template<typename T>
14 0 : class SimpleMap
15 : {
16 : public:
17 : typedef Pair<int64_t, T> Element;
18 :
19 0 : SimpleMap() : mMutex("SimpleMap") { }
20 :
21 : // Insert Key and Value pair at the end of our map.
22 0 : void Insert(int64_t aKey, const T& aValue)
23 : {
24 0 : MutexAutoLock lock(mMutex);
25 0 : mMap.AppendElement(MakePair(aKey, aValue));
26 0 : }
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 0 : bool Find(int64_t aKey, T& aValue)
31 : {
32 0 : MutexAutoLock lock(mMutex);
33 0 : for (uint32_t i = 0; i < mMap.Length(); i++) {
34 0 : Element& element = mMap[i];
35 0 : if (element.first() == aKey) {
36 0 : aValue = element.second();
37 0 : mMap.RemoveElementAt(i);
38 0 : return true;
39 : }
40 : }
41 0 : return false;
42 : }
43 : // Remove all elements of the map.
44 0 : void Clear()
45 : {
46 0 : MutexAutoLock lock(mMutex);
47 0 : mMap.Clear();
48 0 : }
49 :
50 : private:
51 : Mutex mMutex; // To protect mMap.
52 : AutoTArray<Element, 16> mMap;
53 : };
54 :
55 : } // namespace mozilla
56 :
57 : #endif // mozilla_SimpleMap_h
|