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
|
//-----------------------------------------------------------------------------
/** @file libboardgame_base/SgfUtil.cpp
@author Markus Enzenberger
@copyright GNU General Public License version 3 or later */
//-----------------------------------------------------------------------------
#include "SgfUtil.h"
#include <algorithm>
#include <sstream>
namespace libboardgame_base {
//-----------------------------------------------------------------------------
const SgfNode& back_to_main_variation(const SgfNode& node)
{
if (is_main_variation(node))
return node;
auto current = &node;
while (! is_main_variation(*current))
current = ¤t->get_parent();
return current->get_first_child();
}
const SgfNode& beginning_of_branch(const SgfNode& node)
{
auto current = node.get_parent_or_null();
if (current == nullptr)
return node;
while (true)
{
auto parent = current->get_parent_or_null();
if (parent == nullptr || ! parent->has_single_child())
break;
current = parent;
}
return *current;
}
const SgfNode* find_next_comment(const SgfNode& node)
{
auto current = get_next_node(node);
while (current != nullptr)
{
if (has_comment(*current))
return current;
current = get_next_node(*current);
}
return nullptr;
}
const SgfNode& find_root(const SgfNode& node)
{
auto current = &node;
while (current->has_parent())
current = ¤t->get_parent();
return *current;
}
const SgfNode& get_last_node(const SgfNode& node)
{
auto n = &node;
while (n->has_children())
n = &n->get_first_child();
return *n;
}
unsigned get_depth(const SgfNode& node)
{
unsigned depth = 0;
auto current = &node;
while (current->has_parent())
{
current = ¤t->get_parent();
++depth;
}
return depth;
}
const SgfNode* get_next_earlier_variation(const SgfNode& node)
{
auto child = &node;
auto current = node.get_parent_or_null();
while (current != nullptr && (child->get_sibling() == nullptr))
{
child = current;
current = current->get_parent_or_null();
}
if (current == nullptr)
return nullptr;
return child->get_sibling();
}
const SgfNode* get_next_node(const SgfNode& node)
{
auto child = node.get_first_child_or_null();
if (child != nullptr)
return child;
return get_next_earlier_variation(node);
}
void get_path_from_root(const SgfNode& node, vector<const SgfNode*>& path)
{
auto current = &node;
path.assign(1, current);
while(current->has_parent())
{
current = ¤t->get_parent();
path.push_back(current);
}
reverse(path.begin(), path.end());
}
string get_variation_string(const SgfNode& node)
{
string result;
auto current = &node;
unsigned depth = get_depth(*current);
while (current->has_parent())
{
auto& parent = current->get_parent();
if (parent.get_nu_children() > 1)
{
unsigned index = parent.get_child_index(*current);
if (index > 0)
{
ostringstream s;
s << depth << get_letter_coord(index);
if (! result.empty())
s << '-' << result;
result = s.str();
}
}
current = &parent;
--depth;
}
return result;
}
bool has_comment(const SgfNode& node)
{
return node.has_property("C");
}
bool has_earlier_variation(const SgfNode& node)
{
auto current = node.get_parent_or_null();
if (current == nullptr)
return false;
while (true)
{
auto parent = current->get_parent_or_null();
if (parent == nullptr)
return false;
if (! parent->has_single_child())
return true;
current = parent;
}
}
bool is_empty(const SgfTree& tree)
{
auto& root = tree.get_root();
if (root.has_children())
return false;
for (auto& p : root.get_properties())
{
auto& id = p.id;
if (id != "GM" && id != "CA" && id != "AP" && id != "DT")
return false;
}
return true;
}
bool is_main_variation(const SgfNode& node)
{
auto current = &node;
while (current->has_parent())
{
auto& parent = current->get_parent();
if (current != &parent.get_first_child())
return false;
current = &parent;
}
return true;
}
//-----------------------------------------------------------------------------
} // namespace libboardgame_base
|