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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
//-----------------------------------------------------------------------------
/** @file libboardgame_base/SgfNode.cpp
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#include "SgfNode.h"
#include <algorithm>
namespace libboardgame_base {
//-----------------------------------------------------------------------------
SgfNode& SgfNode::create_new_child()
{
auto node = make_unique<SgfNode>();
node->m_parent = this;
auto& result = *node;
auto last_child = get_last_child();
if (last_child == nullptr)
m_first_child = std::move(node);
else
last_child->m_sibling = std::move(node);
return result;
}
void SgfNode::delete_variations()
{
if (m_first_child)
m_first_child->m_sibling.reset(nullptr);
}
forward_list<Property>::const_iterator SgfNode::find_property(
const string& id) const
{
return find_if(m_properties.begin(), m_properties.end(),
[&](const Property& p) { return p.id == id; });
}
const vector<string>& SgfNode::get_multi_property(const string& id) const
{
auto property = find_property(id);
if (property == m_properties.end())
throw MissingProperty(id);
return property->values;
}
bool SgfNode::has_property(const string& id) const
{
return find_property(id) != m_properties.end();
}
const SgfNode& SgfNode::get_child(unsigned i) const
{
LIBBOARDGAME_ASSERT(i < get_nu_children());
auto child = m_first_child.get();
while (i > 0)
{
child = child->m_sibling.get();
--i;
}
return *child;
}
unsigned SgfNode::get_child_index(const SgfNode& child) const
{
auto current = m_first_child.get();
unsigned i = 0;
while (true)
{
if (current == &child)
return i;
current = current->m_sibling.get();
LIBBOARDGAME_ASSERT(current);
++i;
}
}
SgfNode* SgfNode::get_last_child() const
{
auto node = m_first_child.get();
if (node == nullptr)
return nullptr;
while (node->m_sibling)
node = node->m_sibling.get();
return node;
}
unsigned SgfNode::get_nu_children() const
{
unsigned n = 0;
auto child = m_first_child.get();
while (child != nullptr)
{
++n;
child = child->m_sibling.get();
}
return n;
}
const SgfNode* SgfNode::get_previous_sibling() const
{
if (m_parent == nullptr)
return nullptr;
auto child = &m_parent->get_first_child();
if (child == this)
return nullptr;
do
{
if (child->get_sibling() == this)
return child;
child = child->get_sibling();
}
while (child != nullptr);
LIBBOARDGAME_ASSERT(false);
return nullptr;
}
const string& SgfNode::get_property(const string& id) const
{
auto property = find_property(id);
if (property == m_properties.end())
throw MissingProperty(id);
return property->values[0];
}
const string& SgfNode::get_property(const string& id,
const string& default_value) const
{
auto property = find_property(id);
if (property == m_properties.end())
return default_value;
return property->values[0];
}
void SgfNode::make_first_child()
{
LIBBOARDGAME_ASSERT(has_parent());
auto current_child = m_parent->m_first_child.get();
if (current_child == this)
return;
while (true)
{
auto sibling = current_child->m_sibling.get();
if (sibling == this)
{
auto tmp = std::move(m_parent->m_first_child);
m_parent->m_first_child = std::move(current_child->m_sibling);
current_child->m_sibling = std::move(m_sibling);
m_sibling = std::move(tmp);
return;
}
current_child = sibling;
}
}
bool SgfNode::move_property_to_front(const string& id)
{
auto i = m_properties.begin();
auto previous = m_properties.end();
for ( ; i != m_properties.end(); ++i)
if (i->id == id)
break;
else
previous = i;
if (i == m_properties.begin() || i == m_properties.end())
return false;
auto property = *i;
m_properties.erase_after(previous);
m_properties.push_front(property);
return true;
}
void SgfNode::move_down()
{
LIBBOARDGAME_ASSERT(has_parent());
auto current = m_parent->m_first_child.get();
if (current == this)
{
auto tmp = std::move(m_parent->m_first_child);
m_parent->m_first_child = std::move(m_sibling);
m_sibling = std::move(m_parent->m_first_child->m_sibling);
m_parent->m_first_child->m_sibling = std::move(tmp);
return;
}
while (true)
{
auto sibling = current->m_sibling.get();
if (sibling == this)
{
if (! m_sibling)
return;
auto tmp = std::move(current->m_sibling);
current->m_sibling = std::move(m_sibling);
m_sibling = std::move(current->m_sibling->m_sibling);
current->m_sibling->m_sibling = std::move(tmp);
return;
}
current = sibling;
}
}
void SgfNode::move_up()
{
LIBBOARDGAME_ASSERT(has_parent());
auto current = m_parent->m_first_child.get();
if (current == this)
return;
SgfNode* prev = nullptr;
while (true)
{
auto sibling = current->m_sibling.get();
if (sibling == this)
{
if (prev == nullptr)
{
make_first_child();
return;
}
auto tmp = std::move(prev->m_sibling);
prev->m_sibling = std::move(current->m_sibling);
current->m_sibling = std::move(m_sibling);
m_sibling = std::move(tmp);
return;
}
prev = current;
current = sibling;
}
}
bool SgfNode::remove_property(const string& id)
{
auto previous = m_properties.end();
for (auto i = m_properties.begin() ; i != m_properties.end(); ++i)
if (i->id == id)
{
if (previous == m_properties.end())
m_properties.pop_front();
else
m_properties.erase_after(previous);
return true;
}
else
previous = i;
return false;
}
unique_ptr<SgfNode> SgfNode::remove_child(SgfNode& child)
{
auto node = &m_first_child;
unique_ptr<SgfNode>* previous = nullptr;
while (true)
{
if (node->get() == &child)
{
auto result = std::move(*node);
if (previous == nullptr)
m_first_child = std::move(child.m_sibling);
else
(*previous)->m_sibling = std::move(child.m_sibling);
result->m_parent = nullptr;
return result;
}
previous = node;
node = &(*node)->m_sibling;
}
}
//-----------------------------------------------------------------------------
} // namespace libboardgame_base
|