forked from Oshlack/Corset
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringSet.h
More file actions
51 lines (42 loc) · 1.69 KB
/
StringSet.h
File metadata and controls
51 lines (42 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2013 Nadia Davidson for Murdoch Childrens Research
// Institute Australia. This program is distributed under the GNU
// General Public License. We also ask that you cite this software in
// publications where you made use of it for any part of the data
// analysis.
// A template container storing a pointer to an object
// alongside its name (ID string). Used for Read and
// Transcript lookup by name during I/O; destroyed once
// super-clusters are built to conserve memory.
//
// Original author: Nadia Davidson
// Last modified 21 February 2026, Martin Paliocha, martin.paliocha@nmbu.no
#pragma once
#include <string>
#include <cstdlib>
#include <unordered_map>
template <class T>
class StringSet {
using map_type = std::unordered_map<std::string, T *>;
map_type set_map;
public:
using iterator = typename map_type::iterator;
// Look up an object by name. Returns nullptr if not found.
[[nodiscard]] T *find(const std::string &name) {
auto it = set_map.find(name);
return (it != set_map.end()) ? it->second : nullptr;
}
// Insert a new object keyed by name. If the name already exists,
// return the existing object pointer.
// Uses try_emplace for a single hash lookup (find + insert combined).
T *insert(const std::string &name) {
auto [it, inserted] = set_map.try_emplace(name, nullptr);
if (!inserted)
return it->second;
it->second = new T(name);
return it->second;
}
iterator begin() { return set_map.begin(); }
iterator end() { return set_map.end(); }
void clear() { set_map.clear(); }
[[nodiscard]] int size() const { return static_cast<int>(set_map.size()); }
};