FastLED 3.9.15
Loading...
Searching...
No Matches
string_interner.h
Go to the documentation of this file.
1#pragma once
2
3// String interner: stores unique strings as heap-allocated fl::string instances
4// for efficient comparison and deduplication. Uses a hash map for O(1) average
5// lookup time instead of linear search.
6//
7// Implementation: unordered_map<string_view, shared_ptr<StringHolder>>
8// The string_view keys point into the StringHolder values' data (self-referential).
9// This is safe because StringHolder data is heap-allocated and never moves.
10//
11// All interned strings are heap-allocated to ensure stable pointers.
12// Copying interned strings is cheap (shared_ptr increment).
13// Outstanding strings survive interner.clear() due to reference counting.
14
15#include "fl/stl/int.h"
17#include "fl/stl/shared_ptr.h"
18#include "fl/stl/noexcept.h"
19
20namespace fl {
21
22class string;
23class string_view; // IWYU pragma: keep
24class StringHolder;
25template <typename T, fl::size Extent> class span; // IWYU pragma: keep
26
27// StringInterner: Manages a pool of unique strings as fl::string instances.
28// Interned strings are heap-allocated and reference-counted, making copies cheap.
29// Uses hash map for O(1) average lookup time (vs O(N) linear search).
30// Thread-safety: NOT thread-safe. Synchronize externally if needed.
32 public:
35
36 // Intern a string_view (primary API) - returns a fl::string with heap allocation.
37 // If the string already exists, returns the existing fl::string (cheap shared_ptr copy).
38 // The returned fl::string is reference-counted via shared_ptr.
39 // All interned strings use heap allocation to ensure stable pointers.
40 // Performance: O(1) average time complexity via hash map lookup.
41 fl::string intern(const string_view& sv);
42
43 // Convenience overload: intern from fl::string
44 fl::string intern(const fl::string& str);
45
46 // Convenience overload: intern from C string
47 fl::string intern(const char* str);
48
49 // Convenience overload: intern from span<const char>
51
52 // Check if a string is already interned
53 // Performance: O(1) average time complexity via hash map lookup.
54 bool contains(const string_view& sv) const;
55 bool contains(const char* str) const;
56
57 // Get the number of unique interned strings
58 fl::size size() const;
59
60 // Check if the interner is empty
61 bool empty() const;
62
63 // Clear all interned strings
64 // Outstanding fl::string copies survive due to shared_ptr reference counting
65 void clear();
66
67 // Reserve capacity for expected number of strings
68 void reserve(fl::size count);
69
70 private:
73};
74
75// Global string interner singleton (optional convenience)
76// Uses fl::Singleton to ensure the destructor is never called, avoiding
77// static destruction order issues and unnecessary cleanup in embedded systems.
78// Usage: fl::global_interner().intern("my string")
80
81// Convenience functions for global interning
82// Returns heap-allocated fl::string with stable pointer (cheap to copy via shared_ptr)
83// Performance: O(1) average time complexity via hash map lookup.
85fl::string intern(const fl::string& str);
86fl::string intern(const char* str);
88
89} // namespace fl
fl::shared_ptr< StringHolder > StringHolderPtr
void reserve(fl::size count)
fl::string intern(const string_view &sv)
bool contains(const string_view &sv) const
fl::unordered_map< string_view, StringHolderPtr > mEntries
StringInterner & global_interner()
fl::string intern(const string_view &sv)
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NOEXCEPT