KOSMIndoorMap

mapcssresult.cpp
1 /*
2  SPDX-FileCopyrightText: 2020 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "mapcssresult_p.h"
8 
9 #include <algorithm>
10 
11 using namespace KOSMIndoorMap;
12 
13 MapCSSResultItem::MapCSSResultItem() = default;
14 MapCSSResultItem::~MapCSSResultItem() = default;
15 
16 void MapCSSResultItem::clear()
17 {
18  m_declarations.clear();
19  m_classes.clear();
20  m_flags = MapCSSDeclaration::NoFlag;
21  m_layer = {};
22 }
23 
24 bool MapCSSResultItem::hasAreaProperties() const
25 {
26  return m_flags & MapCSSDeclaration::AreaProperty;
27 }
28 
29 bool MapCSSResultItem::hasLineProperties() const
30 {
31  return m_flags & MapCSSDeclaration::LineProperty;
32 }
33 
34 bool MapCSSResultItem::hasLabelProperties() const
35 {
36  return m_flags & MapCSSDeclaration::LabelProperty;
37 }
38 
39 const MapCSSDeclaration* MapCSSResultItem::declaration(MapCSSDeclaration::Property prop) const
40 {
41  const auto it = std::lower_bound(m_declarations.begin(), m_declarations.end(), prop, [](auto lhs, auto rhs) {
42  return lhs->property() < rhs;
43  });
44  if (it == m_declarations.end() || (*it)->property() != prop) {
45  return nullptr;
46  }
47  return (*it);
48 }
49 
50 const std::vector<const MapCSSDeclaration*>& MapCSSResultItem::declarations() const
51 {
52  return m_declarations;
53 }
54 
55 void MapCSSResultItem::addDeclaration(const MapCSSDeclaration *decl)
56 {
57  const auto it = std::lower_bound(m_declarations.begin(), m_declarations.end(), decl, [](auto lhs, auto rhs) {
58  return lhs->property() < rhs->property();
59  });
60  if (it == m_declarations.end() || (*it)->property() != decl->property()) {
61  m_declarations.insert(it, decl);
62  } else {
63  (*it) = decl;
64  }
65 
66  m_flags |= decl->propertyFlags();
67 }
68 
69 void MapCSSResultItem::addClass(ClassSelectorKey cls)
70 {
71  const auto it = std::lower_bound(m_classes.begin(), m_classes.end(), cls);
72  if (it == m_classes.end() || (*it) != cls) {
73  m_classes.insert(it, cls);
74  }
75 }
76 
77 bool MapCSSResultItem::hasClass(ClassSelectorKey cls) const
78 {
79  return std::binary_search(m_classes.begin(), m_classes.end(), cls);
80 }
81 
82 LayerSelectorKey MapCSSResultItem::layerSelector() const
83 {
84  return m_layer;
85 }
86 
87 void MapCSSResultItem::setLayerSelector(LayerSelectorKey layer)
88 {
89  m_layer = layer;
90 }
91 
92 
93 MapCSSResult::MapCSSResult() = default;
94 MapCSSResult::~MapCSSResult() = default;
95 
96 void MapCSSResult::clear()
97 {
98  std::move(m_results.begin(), m_results.end(), std::back_inserter(m_inactivePool));
99  m_results.clear();
100  std::for_each(m_inactivePool.begin(), m_inactivePool.end(), std::mem_fn(&MapCSSResultItem::clear));
101 }
102 
103 const std::vector<MapCSSResultItem>& MapCSSResult::results() const
104 {
105  return m_results;
106 }
107 
108 MapCSSResultItem& MapCSSResult::operator[](LayerSelectorKey layer)
109 {
110  const auto it = std::find_if(m_results.begin(), m_results.end(), [layer](const auto &res) {
111  return res.layerSelector() == layer;
112  });
113  if (it != m_results.end()) {
114  return *it;
115  }
116 
117  if (!m_inactivePool.empty()) {
118  auto res = std::move(m_inactivePool.back());
119  m_inactivePool.pop_back();
120  res.setLayerSelector(layer);
121  m_results.push_back(std::move(res));
122  } else {
123  MapCSSResultItem res;
124  res.setLayerSelector(layer);
125  m_results.push_back(std::move(res));
126  }
127  return m_results.back();
128 }
129 
130 const MapCSSResultItem& MapCSSResult::operator[](LayerSelectorKey layer) const
131 {
132  const auto it = std::find_if(m_results.begin(), m_results.end(), [layer](const auto &res) {
133  return res.layerSelector() == layer;
134  });
135  if (it != m_results.end()) {
136  return *it;
137  }
138 
139  if (m_inactivePool.empty()) {
140  m_inactivePool.push_back(MapCSSResultItem());
141  }
142  return m_inactivePool.back();
143 }
OSM-based multi-floor indoor maps for buildings.
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Oct 25 2021 23:04:00 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.