1 /*
2  * Copyright (C) 2013 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef ART_COMPILER_UTILS_DEDUPE_SET_H_
18 #define ART_COMPILER_UTILS_DEDUPE_SET_H_
19 
20 #include <stdint.h>
21 #include <memory>
22 #include <string>
23 
24 #include "base/macros.h"
25 
26 namespace art {
27 
28 class Thread;
29 
30 // A set of Keys that support a HashFunc returning HashType. Used to find duplicates of Key in the
31 // Add method. The data-structure is thread-safe through the use of internal locks, it also
32 // supports the lock being sharded.
33 template <typename InKey,
34           typename StoreKey,
35           typename Alloc,
36           typename HashType,
37           typename HashFunc,
38           HashType kShard = 1>
39 class DedupeSet {
40  public:
41   // Add a new key to the dedupe set if not present. Return the equivalent deduplicated stored key.
42   const StoreKey* Add(Thread* self, const InKey& key);
43 
44   DedupeSet(const char* set_name, const Alloc& alloc);
45 
46   ~DedupeSet();
47 
48   std::string DumpStats(Thread* self) const;
49 
50  private:
51   struct Stats;
52   class Shard;
53 
54   std::unique_ptr<Shard> shards_[kShard];
55   uint64_t hash_time_;
56 
57   DISALLOW_COPY_AND_ASSIGN(DedupeSet);
58 };
59 
60 }  // namespace art
61 
62 #endif  // ART_COMPILER_UTILS_DEDUPE_SET_H_
63