Stroika Library 3.0d16
 
Loading...
Searching...
No Matches
Atom.cpp
1/*
2 * Copyright(c) Sophist Solutions, Inc. 1990-2025. All rights reserved
3 */
4#include "Stroika/Foundation/StroikaPreComp.h"
5
8#include "Stroika/Foundation/Containers/Mapping.h"
9#include "Stroika/Foundation/Containers/Sequence.h"
10#include "Stroika/Foundation/Execution/Common.h"
12
13#include "Atom.h"
14
15using namespace Stroika::Foundation;
18
19// @todo Consider using Synchronized<> for Sequence<>
20
21namespace {
22 struct AtomManager_Default_Rep_ {
23 // VERY CRUDDY (but close to what we use in HF) impl - to get started...
24 /*
25 * Use SpinLock since locks very short lived. COULD use shared_mutex because much more reads than writes. But since locks so short, little point.
26 */
27 Execution::SpinLock fCritSec; // lock needed here to keep map and sequence in sync
30 };
31 inline AtomManager_Default_Rep_& GetAtomManager_Default_Rep_ ()
32 {
33 static AtomManager_Default_Rep_ sRep_{};
34 return sRep_;
35 }
36}
37namespace {
38 struct AtomManager_CaseInsensitive_Rep_ {
39 // VERY CRUDDY (but close to what we use in HF) impl - to get started...
40 /*
41 * Use SpinLock since locks very short lived. COULD use shared_mutex because much more reads than writes. But since locks so short, little point.
42 */
43 Execution::SpinLock fCritSec; // lock needed here to keep map and sequence in sync
47 };
48 inline AtomManager_CaseInsensitive_Rep_& GetAtomManager_CaseInsensitive_Rep_ ()
49 {
50 static AtomManager_CaseInsensitive_Rep_ sRep_{};
51 return sRep_;
52 }
53}
54
55/*
56 ********************************************************************************
57 ******************** DataExchange::AtomManager_Default *************************
58 ********************************************************************************
59 */
60auto AtomManager_Default::Intern (const String& s) -> AtomInternalType
61{
62 AtomInternalType v;
63 if (s.empty ()) {
64 v = kEmpty;
65 }
66 else {
67 auto& mgr = GetAtomManager_Default_Rep_ ();
68 [[maybe_unused]] lock_guard critSec{mgr.fCritSec};
69 auto i = mgr.fMap.Lookup (s);
70 if (i.has_value ()) {
71 return *i;
72 }
73 v = mgr.fSeq.size ();
74 mgr.fSeq.Append (s);
75 mgr.fMap.Add (s, v);
76 }
77 Ensure (Extract (v) == s);
78 return v;
79}
80
81String AtomManager_Default::Extract (AtomInternalType atomI)
82{
83 if (atomI == kEmpty) {
84 return String{};
85 }
86 auto& mgr = GetAtomManager_Default_Rep_ ();
87 [[maybe_unused]] lock_guard critSec{mgr.fCritSec};
88 return (mgr.fSeq)[atomI];
89}
90
91/*
92 ********************************************************************************
93 ****************** DataExchange::AtomManager_CaseInsensitive *******************
94 ********************************************************************************
95 */
96auto AtomManager_CaseInsensitive::Intern (const String& s) -> AtomInternalType
97{
98 AtomInternalType v;
99 if (s.empty ()) {
100 v = kEmpty;
101 }
102 else {
103 auto& mgr = GetAtomManager_CaseInsensitive_Rep_ ();
104 [[maybe_unused]] lock_guard critSec{mgr.fCritSec};
105 auto i = mgr.fMap.Lookup (s);
106 if (i.has_value ()) {
107 return *i;
108 }
109 v = mgr.fSeq.size ();
110 mgr.fSeq.Append (s);
111 mgr.fMap.Add (s, v);
112 }
113 Ensure (Extract (v) == s);
114 return v;
115}
116
117String AtomManager_CaseInsensitive::Extract (AtomInternalType atomI)
118{
119 if (atomI == kEmpty) {
120 return String{};
121 }
122 auto& mgr = GetAtomManager_CaseInsensitive_Rep_ ();
123 [[maybe_unused]] lock_guard critSec{mgr.fCritSec};
124 return (mgr.fSeq)[atomI];
125}
String is like std::u32string, except it is much easier to use, often much more space efficient,...
Definition String.h:201
SortedMapping_stdmap<KEY_TYPE,MAPPED_VALUE_TYPE> is an std::map-based concrete implementation of the ...
A generalization of a vector: a container whose elements are keyed by the natural numbers.
Definition Sequence.h:187
very similar to ThreeWayComparer but returns true if less
Definition String.h:1856