blob: 53c1afa69839bc95e44e4be35047a8f2c3cde4bb [file] [log] [blame]
Mathieu Chartier193bad92013-08-29 18:46:00 -07001/*
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 <set>
Ian Rogersd133b972013-09-05 11:01:30 -070021#include <string>
Mathieu Chartier193bad92013-08-29 18:46:00 -070022
23#include "base/mutex.h"
24#include "base/stl_util.h"
25
26namespace art {
27
Ian Rogersd133b972013-09-05 11:01:30 -070028// A set of Keys that support a HashFunc returning HashType. Used to find duplicates of Key in the
29// Add method. The data-structure is thread-safe through the use of internal locks, it also
30// supports the lock being sharded.
31template <typename Key, typename HashType, typename HashFunc, HashType kShard = 1>
Mathieu Chartier193bad92013-08-29 18:46:00 -070032class DedupeSet {
33 typedef std::pair<HashType, Key*> HashedKey;
34
35 class Comparator {
36 public:
37 bool operator()(const HashedKey& a, const HashedKey& b) const {
Ian Rogersd133b972013-09-05 11:01:30 -070038 if (a.first != b.first) {
39 return a.first < b.first;
40 } else {
41 return *a.second < *b.second;
42 }
Mathieu Chartier193bad92013-08-29 18:46:00 -070043 }
44 };
45
Mathieu Chartier193bad92013-08-29 18:46:00 -070046 public:
Mathieu Chartier193bad92013-08-29 18:46:00 -070047 Key* Add(Thread* self, const Key& key) {
Ian Rogersd133b972013-09-05 11:01:30 -070048 HashType raw_hash = HashFunc()(key);
49 HashType shard_hash = raw_hash / kShard;
50 HashType shard_bin = raw_hash % kShard;
51 HashedKey hashed_key(shard_hash, const_cast<Key*>(&key));
52 MutexLock lock(self, *lock_[shard_bin]);
53 auto it = keys_[shard_bin].find(hashed_key);
54 if (it != keys_[shard_bin].end()) {
Mathieu Chartier193bad92013-08-29 18:46:00 -070055 return it->second;
56 }
57 hashed_key.second = new Key(key);
Ian Rogersd133b972013-09-05 11:01:30 -070058 keys_[shard_bin].insert(hashed_key);
Mathieu Chartier193bad92013-08-29 18:46:00 -070059 return hashed_key.second;
60 }
61
Ian Rogersd133b972013-09-05 11:01:30 -070062 explicit DedupeSet(const char* set_name) {
63 for (HashType i = 0; i < kShard; ++i) {
64 lock_name_[i] = StringPrintf("%s lock %d", set_name, i);
65 lock_[i].reset(new Mutex(lock_name_[i].c_str()));
66 }
Mathieu Chartier193bad92013-08-29 18:46:00 -070067 }
68
69 ~DedupeSet() {
Ian Rogersd133b972013-09-05 11:01:30 -070070 for (HashType i = 0; i < kShard; ++i) {
71 STLDeleteValues(&keys_[i]);
72 }
Mathieu Chartier193bad92013-08-29 18:46:00 -070073 }
74
75 private:
Ian Rogersd133b972013-09-05 11:01:30 -070076 std::string lock_name_[kShard];
77 UniquePtr<Mutex> lock_[kShard];
78 std::set<HashedKey, Comparator> keys_[kShard];
79
Mathieu Chartier193bad92013-08-29 18:46:00 -070080 DISALLOW_COPY_AND_ASSIGN(DedupeSet);
81};
82
83} // namespace art
84
85#endif // ART_COMPILER_UTILS_DEDUPE_SET_H_