1 #ifndef OPENMW_COMPONENTS_SCENEUTIL_TEXTKEYMAP
2 #define OPENMW_COMPONENTS_SCENEUTIL_TEXTKEYMAP
3 
4 #include <algorithm>
5 #include <map>
6 #include <set>
7 #include <string>
8 
9 namespace SceneUtil
10 {
11     class TextKeyMap
12     {
13     public:
14         using ConstIterator = std::multimap<float, std::string>::const_iterator;
15 
begin() const16         auto begin() const noexcept
17         {
18             return mTextKeyByTime.begin();
19         }
20 
end() const21         auto end() const noexcept
22         {
23             return mTextKeyByTime.end();
24         }
25 
rbegin() const26         auto rbegin() const noexcept
27         {
28             return mTextKeyByTime.rbegin();
29         }
30 
rend() const31         auto rend() const noexcept
32         {
33             return mTextKeyByTime.rend();
34         }
35 
lowerBound(float time) const36         auto lowerBound(float time) const
37         {
38             return mTextKeyByTime.lower_bound(time);
39         }
40 
upperBound(float time) const41         auto upperBound(float time) const
42         {
43             return mTextKeyByTime.upper_bound(time);
44         }
45 
emplace(float time,std::string && textKey)46         void emplace(float time, std::string&& textKey)
47         {
48             const auto separator = textKey.find(": ");
49             if (separator != std::string::npos)
50                 mGroups.emplace(textKey.substr(0, separator));
51 
52             mTextKeyByTime.emplace(time, std::move(textKey));
53         }
54 
empty() const55         bool empty() const noexcept
56         {
57             return mTextKeyByTime.empty();
58         }
59 
findGroupStart(const std::string & groupName) const60         auto findGroupStart(const std::string &groupName) const
61         {
62             return std::find_if(mTextKeyByTime.begin(), mTextKeyByTime.end(), IsGroupStart{groupName});
63         }
64 
hasGroupStart(const std::string & groupName) const65         bool hasGroupStart(const std::string &groupName) const
66         {
67             return mGroups.count(groupName) > 0;
68         }
69 
70     private:
71         struct IsGroupStart
72         {
73             const std::string &mGroupName;
74 
operator ()SceneUtil::TextKeyMap::IsGroupStart75             bool operator ()(const std::multimap<float, std::string>::value_type& value) const
76             {
77                 return value.second.compare(0, mGroupName.size(), mGroupName) == 0 &&
78                         value.second.compare(mGroupName.size(), 2, ": ") == 0;
79             }
80         };
81 
82         std::set<std::string> mGroups;
83         std::multimap<float, std::string> mTextKeyByTime;
84     };
85 }
86 
87 #endif
88