File: parametertree.cc

package info (click to toggle)
dune-common 2.11.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,048 kB
  • sloc: cpp: 54,403; python: 4,136; sh: 1,657; makefile: 17
file content (246 lines) | stat: -rw-r--r-- 6,381 bytes parent folder | download | duplicates (2)
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
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
// SPDX-FileCopyrightInfo: Copyright © DUNE Project contributors, see file LICENSE.md in module root
// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception

#include <cstdlib>
#include <iostream>
#include <ostream>
#include <string>
#include <sstream>
#include <fstream>
#include <set>
#include <algorithm>

#include <dune/common/exceptions.hh>
#include <dune/common/parametertree.hh>

using namespace Dune;

ParameterTree::ParameterTree()
{}

// Since this (internal) tree is static and constant, its prefix cannot be changed even
// though we report it as the sub-tree of another one (in `.sub(prefix) const`).
// Thus, we set the prefix as "<unknown>" to inform users that we could not construct the real prefix.
ParameterTree::ParameterTree(ParameterTree::EmptyTag)
: prefix_{"<unknown>"}
{}

const ParameterTree ParameterTree::empty_ = ParameterTree{ParameterTree::EmptyTag()};

void ParameterTree::report(std::ostream& stream, const std::string& prefix) const
{
  typedef std::map<std::string, std::string>::const_iterator ValueIt;
  ValueIt vit = values_.begin();
  ValueIt vend = values_.end();

  for(; vit!=vend; ++vit)
    stream << vit->first << " = \"" << vit->second << "\"" << std::endl;

  typedef std::map<std::string, ParameterTree>::const_iterator SubIt;
  SubIt sit = subs_.begin();
  SubIt send = subs_.end();
  for(; sit!=send; ++sit)
  {
    stream << "[ " << prefix + prefix_ + sit->first << " ]" << std::endl;
    (sit->second).report(stream, prefix);
  }
}

bool ParameterTree::hasKey(const std::string& key) const
{
  std::string::size_type dot = key.find(".");

  if (dot != std::string::npos)
  {
    std::string prefix = key.substr(0,dot);
    if (subs_.count(prefix) == 0)
      return false;

    if (values_.count(prefix) > 0)
      DUNE_THROW(RangeError,"key " << prefix << " occurs as value and as subtree");

    const ParameterTree& s = sub(prefix);
    return s.hasKey(key.substr(dot+1));
  }
  else
    if (values_.count(key) != 0)
      {
        if (subs_.count(key) > 0)
          DUNE_THROW(RangeError,"key " << key << " occurs as value and as subtree");
        return true;
      }
    else
      return false;

}

bool ParameterTree::hasSub(const std::string& key) const
{
  std::string::size_type dot = key.find(".");

  if (dot != std::string::npos)
  {
    std::string prefix = key.substr(0,dot);
    if (subs_.count(prefix) == 0)
      return false;

    if (values_.count(prefix) > 0)
      DUNE_THROW(RangeError,"key " << prefix << " occurs as value and as subtree");

    const ParameterTree& s = sub(prefix);
    return s.hasSub(key.substr(dot+1));
  }
  else
    if (subs_.count(key) != 0)
      {
        if (values_.count(key) > 0)
          DUNE_THROW(RangeError,"key " << key << " occurs as value and as subtree");
        return true;
      }
    else
      return false;
}

ParameterTree& ParameterTree::sub(const std::string& key)
{
  std::string::size_type dot = key.find(".");

  if (dot != std::string::npos)
  {
    ParameterTree& s = sub(key.substr(0,dot));
    return s.sub(key.substr(dot+1));
  }
  else
  {
    if (values_.count(key) > 0)
      DUNE_THROW(RangeError,"key " << key << " occurs as value and as subtree");
    if (subs_.count(key) == 0)
      subKeys_.push_back(key.substr(0,dot));
    subs_[key].prefix_ = prefix_ + key + ".";
    return subs_[key];
  }
}

const ParameterTree& ParameterTree::sub(const std::string& key, bool fail_if_missing) const
{
  std::string::size_type dot = key.find(".");

  if (dot != std::string::npos)
  {
    const ParameterTree& s = sub(key.substr(0,dot));
    return s.sub(key.substr(dot+1),fail_if_missing);
  }
  else
  {
    if (values_.count(key) > 0)
      DUNE_THROW(RangeError,"key " << key << " occurs as value and as subtree");
    if (subs_.count(key) == 0)
      {
        if (fail_if_missing)
          {
            DUNE_THROW(Dune::RangeError, "SubTree '" << key
                       << "' not found in ParameterTree (prefix " + prefix_ + ")");
          }
        else
          return empty_;
      }
    return subs_.find(key)->second;
  }
}

std::string& ParameterTree::operator[] (const std::string& key)
{
  std::string::size_type dot = key.find(".");

  if (dot != std::string::npos)
  {
    ParameterTree& s = sub(key.substr(0,dot));
    return s[key.substr(dot+1)];
  }
  else
  {
    if (! hasKey(key))
      valueKeys_.push_back(key);
    return values_[key];
  }
}

const std::string& ParameterTree::operator[] (const std::string& key) const
{
  std::string::size_type dot = key.find(".");

  if (dot != std::string::npos)
  {
    const ParameterTree& s = sub(key.substr(0,dot));
    return s[key.substr(dot+1)];
  }
  else
  {
    if (! hasKey(key))
      DUNE_THROW(Dune::RangeError, "Key '" << key
        << "' not found in ParameterTree (prefix " + prefix_ + ")");
    return values_.find(key)->second;
  }
}

std::string ParameterTree::get(const std::string& key, const std::string& defaultValue) const
{
  if (hasKey(key))
    return (*this)[key];
  else
    return defaultValue;
}

std::string ParameterTree::get(const std::string& key, const char* defaultValue) const
{
  if (hasKey(key))
    return (*this)[key];
  else
    return defaultValue;
}

std::string ParameterTree::ltrim(const std::string& s)
{
  std::size_t firstNonWS = s.find_first_not_of(" \t\n\r");

  if (firstNonWS!=std::string::npos)
    return s.substr(firstNonWS);
  return std::string();
}

std::string ParameterTree::rtrim(const std::string& s)
{
  std::size_t lastNonWS = s.find_last_not_of(" \t\n\r");

  if (lastNonWS!=std::string::npos)
    return s.substr(0, lastNonWS+1);
  return std::string();
}

std::vector<std::string> ParameterTree::split(const std::string & s) {
  std::vector<std::string> substrings;
  std::size_t front = 0, back = 0, size = 0;

  while (front != std::string::npos)
  {
    // find beginning of substring
    front = s.find_first_not_of(" \t\n\r", back);
    back  = s.find_first_of(" \t\n\r", front);
    size  = back - front;
    if (size > 0)
      substrings.push_back(s.substr(front, size));
  }
  return substrings;
}

const ParameterTree::KeyVector& ParameterTree::getValueKeys() const
{
  return valueKeys_;
}

const ParameterTree::KeyVector& ParameterTree::getSubKeys() const
{
  return subKeys_;
}