-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslhaea.h
2670 lines (2364 loc) · 74.8 KB
/
slhaea.h
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SLHAea - containers for SUSY Les Houches Accord input/output
// Copyright © 2009-2011 Frank S. Thomas <[email protected]>
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#ifndef SLHAEA_H
#define SLHAEA_H
#include <algorithm>
#include <cctype>
#include <cstddef>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/join.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/lexical_cast.hpp>
#if __cplusplus <= 199711L
#define MEM_FN std::mem_fun_ref
#else
#define MEM_FN std::mem_fn
#endif
namespace SLHAea {
// auxiliary functions
/**
* \brief Converts an object of type \c Source to an object of type
* \c Target.
* \param arg Object that will be converted.
* \return Result of the conversion of \p arg to \c Target.
*
* This function is a wrapper for
* \c boost::lexical_cast<Target>().
*/
template<class Target, class Source> inline Target
to(const Source& arg)
{ return boost::lexical_cast<Target>(arg); }
/**
* \brief Converts an object of type \c Source to a string.
* \param arg Object that will be converted.
* \return Result of the conversion of \p arg to \c std::string.
*
* This function is a wrapper for
* \c boost::lexical_cast<std::string>().
*/
template<class Source> inline std::string
to_string(const Source& arg)
{ return boost::lexical_cast<std::string>(arg); }
/**
* \brief Converts an object of type \c Source to a string.
* \param arg Object that will be converted.
* \param precision Precision of float values that are written in
* scientific notation.
* \return Result of the conversion of \p arg to \c std::string.
*
* This function is equivalent to \c to_string() except that all
* floating-point numbers are written in scientific notation with the
* given precision.
*/
template<class Source> inline std::string
to_string(const Source& arg, int precision)
{
std::ostringstream output;
output << std::setprecision(precision) << std::scientific << arg;
return output.str();
}
namespace detail {
inline bool
is_all_whitespace(const std::string& str)
{ return str.find_first_not_of(" \t\n\v\f\r") == std::string::npos; }
inline std::string
to_upper_copy(const std::string& str)
{
std::string str_upper(str.length(), char());
std::transform(str.begin(), str.end(), str_upper.begin(),
static_cast<int (*)(int)>(std::toupper));
return str_upper;
}
inline void
trim_left(std::string& str)
{
const std::size_t startpos = str.find_first_not_of(" \t\n\v\f\r");
if (startpos != std::string::npos) str.erase(0, startpos);
else str.clear();
}
inline void
trim_right(std::string& str)
{
const std::size_t endpos = str.find_last_not_of(" \t\n\v\f\r");
if (endpos != std::string::npos) str.erase(endpos + 1);
else str.clear();
}
} // namespace detail
// forward declarations
class Line;
class Block;
class Coll;
struct Key;
inline std::ostream& operator<<(std::ostream& os, const Line& line);
inline std::ostream& operator<<(std::ostream& os, const Block& block);
inline std::ostream& operator<<(std::ostream& os, const Coll& coll);
inline std::ostream& operator<<(std::ostream& os, const Key& key);
/**
* Container of strings that represents a line in a SLHA structure.
* This class is a container of strings that represents a line in a
* SLHA structure. The elements of a %Line are the so called fields of
* an ordinary SLHA line, which are its whitespace-separated
* substrings and the comment. For example, if a %Line is constructed
* from the string <tt>" 1 2 0.123 # a comment"</tt> its elements
* would be \c "1", \c "2", \c "0.123", and \c "# a comment".
* Array-style access to the elements with integer indices is provided
* by the operator[]() and at() functions. %Line also provides
* introspective functions to find out whether it is a comment or data
* line for example. Introspective functions that check if an element
* is a block specifier (\c "BLOCK" or \c "DECAY") always perform
* case-insensitive comparisons.
*
* In addition to storing the fields of a SLHA line, a %Line also
* stores its formatting (the exact position of the fields in the
* line). A formatted representation of a %Line can be produced with
* str() const. The reformat() function clears the previous formatting
* and indents all elements with an appropriate number of spaces.
*/
class Line
{
private:
typedef std::vector<std::string> impl_type;
public:
typedef std::string value_type;
typedef std::string& reference;
typedef const std::string& const_reference;
typedef impl_type::iterator iterator;
typedef impl_type::const_iterator const_iterator;
typedef impl_type::reverse_iterator reverse_iterator;
typedef impl_type::const_reverse_iterator const_reverse_iterator;
typedef impl_type::pointer pointer;
typedef impl_type::const_pointer const_pointer;
typedef impl_type::difference_type difference_type;
typedef impl_type::size_type size_type;
// NOTE: The compiler-generated copy constructor and assignment
// operator for this class are just fine, so we don't need to
// write our own.
/** Constructs an empty %Line. */
Line() : impl_(), columns_() {}
/**
* \brief Constructs a %Line from a string.
* \param line String whose fields are used as content of the %Line.
* \sa str()
*/
Line(const std::string& line) : impl_(), columns_()
{ str(line); }
/**
* \brief Assigns content from a string to the %Line.
* \param line String whose fields are used as content of the %Line.
* \return Reference to \c *this.
*
* This function is an alias for str().
*/
Line&
operator=(const std::string& line)
{
str(line);
return *this;
}
/**
* \brief Appends a string to the end of the %Line.
* \param arg String that is appended to the %Line.
* \return Reference to \c *this.
*
* This function is an alias for append().
*/
Line&
operator+=(const std::string& arg)
{
append(arg);
return *this;
}
/**
* \brief Inserts an element at the end of the %Line.
* \param field Element that is inserted at the end of the %Line.
* \return Reference to \c *this.
*
* This function inserts an element at the end of the %Line. If the
* the %Line contains a comment, \p field is only appended to the
* last element and thus size() remains unchanged.
*/
template<class T> Line&
operator<<(const T& field)
{
std::string field_str = to_string(field);
detail::trim_right(field_str);
if (field_str.empty()) return *this;
if (contains_comment())
{ back() += field_str; }
else
{
detail::trim_left(field_str);
impl_.push_back(field_str);
reformat();
}
return *this;
}
/**
* \brief Appends a string to the end of the %Line.
* \param arg String that is appended to the %Line.
* \return Reference to \c *this.
*
* This function appends \p arg to the output of str() const and
* uses this temporary string as input for str(). Based on the
* temporary string, size() is increased or remains unchanged.
*/
Line&
append(const std::string& arg)
{
str(str() + arg);
return *this;
}
/**
* \brief Assigns content to the %Line based on a string.
* \param line String whose fields are used as content of the %Line.
* \return Reference to \c *this.
*
* This function parses \p line and sets the found fields as content
* of the %Line. If \p line contains newlines, everything after the
* first newline is ignored.
*
* The exact formatting of \p line is stored internally and can be
* reproduced with str() const.
*/
Line&
str(const std::string& line)
{
clear();
static const std::string whitespace = " \t\v\f\r";
const std::size_t last_non_ws =
line.substr(0, line.find('\n')).find_last_not_of(whitespace);
if (last_non_ws == std::string::npos) return *this;
const std::string trimmed_line = line.substr(0, last_non_ws + 1);
const std::size_t comment_pos = trimmed_line.find('#');
const std::string data = trimmed_line.substr(0, comment_pos);
std::size_t pos1 = data.find_first_not_of(whitespace, 0);
std::size_t pos2 = data.find_first_of(whitespace, pos1);
while (pos1 != std::string::npos)
{
impl_.push_back(data.substr(pos1, pos2 - pos1));
columns_.push_back(pos1);
pos1 = data.find_first_not_of(whitespace, pos2);
pos2 = data.find_first_of(whitespace, pos1);
}
if (comment_pos != std::string::npos)
{
impl_.push_back(trimmed_line.substr(comment_pos));
columns_.push_back(comment_pos);
}
return *this;
}
/** Returns a formatted string representation of the %Line. */
std::string
str() const
{
if (empty()) return "";
std::ostringstream output;
int length = 0, spaces = 0;
const_iterator field = begin();
std::vector<std::size_t>::const_iterator column = columns_.begin();
for (; field != end() && column != columns_.end(); ++field, ++column)
{
spaces = std::max(0, static_cast<int>(*column) - length + 1);
length += spaces + field->length();
output << std::setw(spaces) << " " << *field;
}
return output.str().substr(1);
}
// element access
/**
* \brief Subscript access to the strings contained in the %Line.
* \param n Index of the string which should be accessed.
* \return Read/write reference to the accessed string.
*
* This operator allows for easy, array-style, data access. Note
* that data access with this operator is unchecked and out_of_range
* lookups are not defined. (For checked lookups see at().)
*/
reference
operator[](size_type n)
{ return impl_[n]; }
/**
* \brief Subscript access to the strings contained in the %Line.
* \param n Index of the string which should be accessed.
* \return Read-only (constant) reference to the accessed string.
*
* This operator allows for easy, array-style, data access. Note
* that data access with this operator is unchecked and out_of_range
* lookups are not defined. (For checked lookups see at().)
*/
const_reference
operator[](size_type n) const
{ return impl_[n]; }
/**
* \brief Provides access to the strings contained in the %Line.
* \param n Index of the string which should be accessed.
* \return Read/write reference to the accessed string.
* \throw std::out_of_range If \p n is an invalid index.
*/
reference
at(size_type n)
{ return impl_.at(n); }
/**
* \brief Provides access to the strings contained in the %Line.
* \param n Index of the string which should be accessed.
* \return Read-only (constant) reference to the accessed string.
* \throw std::out_of_range If \p n is an invalid index.
*/
const_reference
at(size_type n) const
{ return impl_.at(n); }
/**
* Returns a read/write reference to the first element of the %Line.
*/
reference
front()
{ return impl_.front(); }
/**
* Returns a read-only (constant) reference to the first element of
* the %Line.
*/
const_reference
front() const
{ return impl_.front(); }
/**
* Returns a read/write reference to the last element of the %Line.
*/
reference
back()
{ return impl_.back(); }
/**
* Returns a read-only (constant) reference to the last element of
* the %Line.
*/
const_reference
back() const
{ return impl_.back(); }
// iterators
/**
* Returns a read/write iterator that points to the first element in
* the %Line. Iteration is done in ordinary element order.
*/
iterator
begin()
{ return impl_.begin(); }
/**
* Returns a read-only (constant) iterator that points to the first
* element in the %Line. Iteration is done in ordinary element
* order.
*/
const_iterator
begin() const
{ return impl_.begin(); }
/**
* Returns a read-only (constant) iterator that points to the first
* element in the %Line. Iteration is done in ordinary element
* order.
*/
const_iterator
cbegin() const
{ return impl_.begin(); }
/**
* Returns a read/write iterator that points one past the last
* element in the %Line. Iteration is done in ordinary element
* order.
*/
iterator
end()
{ return impl_.end(); }
/**
* Returns a read-only (constant) iterator that points one past the
* last element in the %Line. Iteration is done in ordinary element
* order.
*/
const_iterator
end() const
{ return impl_.end(); }
/**
* Returns a read-only (constant) iterator that points one past the
* last element in the %Line. Iteration is done in ordinary element
* order.
*/
const_iterator
cend() const
{ return impl_.end(); }
/**
* Returns a read/write reverse iterator that points to the last
* element in the %Line. Iteration is done in reverse element order.
*/
reverse_iterator
rbegin()
{ return impl_.rbegin(); }
/**
* Returns a read-only (constant) reverse iterator that points to
* the last element in the %Line. Iteration is done in reverse
* element order.
*/
const_reverse_iterator
rbegin() const
{ return impl_.rbegin(); }
/**
* Returns a read-only (constant) reverse iterator that points to
* the last element in the %Line. Iteration is done in reverse
* element order.
*/
const_reverse_iterator
crbegin() const
{ return impl_.rbegin(); }
/**
* Returns a read/write reverse iterator that points to one before
* the first element in the %Line. Iteration is done in reverse
* element order.
*/
reverse_iterator
rend()
{ return impl_.rend(); }
/**
* Returns a read-only (constant) reverse iterator that points to
* one before the first element in the %Line. Iteration is done in
* reverse element order.
*/
const_reverse_iterator
rend() const
{ return impl_.rend(); }
/**
* Returns a read-only (constant) reverse iterator that points to
* one before the first element in the %Line. Iteration is done in
* reverse element order.
*/
const_reverse_iterator
crend() const
{ return impl_.rend(); }
// introspection
/**
* Returns true if the %Line begins with \c "BLOCK" or \c "DECAY"
* followed by a block name.
*/
bool
is_block_def() const
{
if (size() < 2) return false;
const_iterator field = begin();
return is_block_specifier(*field) && !is_comment(*++field);
}
/** Returns true if the %Line begins with \c "#". */
bool
is_comment_line() const
{ return !empty() && is_comment(front()); }
/**
* Returns true if the %Line is not empty and if it does not begin
* with \c "#", \c "BLOCK" or \c "DECAY".
*/
bool
is_data_line() const
{ return !empty() && !is_comment(front()) && !is_block_specifier(front()); }
// capacity
/** Returns the number of elements in the %Line. */
size_type
size() const
{ return impl_.size(); }
/**
* Returns the number of elements without the comment in the %Line.
*/
size_type
data_size() const
{ return std::distance(begin(), std::find_if(begin(), end(), is_comment)); }
/** Returns the size() of the largest possible %Line. */
size_type
max_size() const
{ return impl_.max_size(); }
/** Returns true if the %Line is empty. */
bool
empty() const
{ return impl_.empty(); }
// modifiers
/**
* \brief Swaps data with another %Line.
* \param line %Line to be swapped with.
*/
void
swap(Line& line)
{
impl_.swap(line.impl_);
columns_.swap(line.columns_);
}
/** Erases all the elements in the %Line. */
void
clear()
{
impl_.clear();
columns_.clear();
}
/** Reformats the string representation of the %Line. */
void
reformat()
{
if (empty()) return;
columns_.clear();
const_iterator field = begin();
std::size_t pos1 = 0, pos2 = 0;
if (is_block_specifier(*field))
{
pos1 = 0;
pos2 = pos1 + field->length();
columns_.push_back(pos1);
if (++field == end()) return;
pos1 = pos2 + 1;
pos2 = pos1 + field->length();
columns_.push_back(pos1);
}
else if (is_comment(*field))
{
pos1 = 0;
pos2 = pos1 + field->length();
columns_.push_back(pos1);
}
else
{
pos1 = shift_width_;
pos2 = pos1 + field->length();
columns_.push_back(pos1);
}
while (++field != end())
{
pos1 = pos2 + calc_spaces_for_indent(pos2);
if (starts_with_sign(*field)) --pos1;
pos2 = pos1 + field->length();
columns_.push_back(pos1);
}
}
/**
* Comments the %Line. This function prefixes the %Line with a
* \c "#" and packetizes all its elements into one.
*/
void
comment()
{ if (!empty()) str("#" + str()); }
/**
* Uncomments the %Line. This function removes the first character
* of the %Line if it is a \c "#" and splits the former comment into
* the corresponding number of fields.
*/
void
uncomment()
{
if (!empty() && is_comment(front()))
{
front().erase(0, 1);
str(str());
}
}
private:
bool
contains_comment() const
{ return std::find_if(rbegin(), rend(), is_comment) != rend(); }
static std::size_t
calc_spaces_for_indent(const std::size_t& pos)
{
std::size_t width = shift_width_ - (pos % shift_width_);
if (width < min_width_) width += shift_width_;
return width;
}
static bool
is_block_specifier(const value_type& field)
{
static const std::size_t specifier_length = 5;
if (field.length() != specifier_length) return false;
const value_type field_upper = detail::to_upper_copy(field);
return (field_upper == "BLOCK") || (field_upper == "DECAY");
}
static bool
is_comment(const value_type& field)
{ return !field.empty() && field[0] == '#'; }
template<class T> Line&
insert_fundamental_type(const T& arg)
{
static const int digits = std::numeric_limits<T>::digits10;
return *this << to_string(arg, digits);
}
static bool
starts_with_sign(const value_type& field)
{ return !field.empty() && (field[0] == '-' || field[0] == '+'); }
private:
impl_type impl_;
std::vector<std::size_t> columns_;
static const std::size_t shift_width_ = 4;
static const std::size_t min_width_ = 2;
};
template<> inline Line&
Line::operator<< <float>(const float& number)
{
insert_fundamental_type(number);
return *this;
}
template<> inline Line&
Line::operator<< <double>(const double& number)
{
insert_fundamental_type(number);
return *this;
}
template<> inline Line&
Line::operator<< <long double>(const long double& number)
{
insert_fundamental_type(number);
return *this;
}
/**
* Container of Lines that resembles a block in a SLHA structure.
* This class is a named container of Lines that resembles a block in
* a SLHA structure. Unlike a block in a SLHA structure, a %Block can
* contain any number of block definitions or it can be completely
* empty.
*
* Access to the elements of the %Block is provided by the
* operator[]() and at() functions. These take one or more strings
* resp. ints as argument(s) and compare them against the first
* strings of the contained Lines (the ints are converted to strings
* before comparison). The first Line that matches the provided
* arguments is then returned, or if no matching Line is found, an
* empty Line is appended to the %Block (operator[]()) or
* \c std::out_of_range is thrown (at()). The special argument
* \c "(any)" will be considered equal to all strings in the Lines.
* For example, <tt>at("(any)", "2")</tt> returns the first Line whose
* second element is \c "2".
*/
class Block
{
private:
typedef std::vector<Line> impl_type;
public:
typedef std::vector<std::string> key_type;
typedef Line value_type;
typedef Line& reference;
typedef const Line& const_reference;
typedef impl_type::iterator iterator;
typedef impl_type::const_iterator const_iterator;
typedef impl_type::reverse_iterator reverse_iterator;
typedef impl_type::const_reverse_iterator const_reverse_iterator;
typedef impl_type::pointer pointer;
typedef impl_type::const_pointer const_pointer;
typedef impl_type::difference_type difference_type;
typedef impl_type::size_type size_type;
// NOTE: The compiler-generated copy constructor and assignment
// operator for this class are just fine, so we don't need to
// write our own.
/**
* \brief Constructs an empty %Block.
* \param name Name of the %Block.
*/
explicit
Block(const std::string& name = "") : name_(name), impl_() {}
/**
* \brief Constructs a %Block with content from an input stream.
* \param is Input stream to read content from.
* \sa read()
*/
explicit
Block(std::istream& is) : name_(), impl_()
{ read(is); }
/**
* \brief Constructs a %Block with content from a string.
* \param block String to read content from.
*/
static Block
from_str(const std::string& block)
{
std::istringstream input(block);
return Block(input);
}
/**
* \brief Sets the name of the %Block.
* \param newName New name of the %Block.
*
* Notice that this function only changes a property of the %Block.
* No contained Line (in particular no block definition) is changed.
*/
void
name(const std::string& newName)
{ name_ = newName; }
/** Returns the name of the %Block. */
const std::string&
name() const
{ return name_; }
/**
* \brief Changes the name and definition of the %Block.
* \param newName New name of the %Block.
*
* In contrast to name() this function changes the name of the
* %Block and its first block definition (if it exists).
*/
void
rename(const std::string& newName)
{
name(newName);
iterator block_def = find_block_def();
if (block_def != end()) (*block_def)[1] = newName;
}
/**
* \brief Assigns content from an input stream to the %Block.
* \param is Input stream to read content from.
* \return Reference to \c *this.
*
* This function reads non-empty lines from \p is, transforms them
* into Lines and adds them to the end of the %Block. Lines from
* \p is are read until the second block definition is encountered
* or until the end of the stream, whatever comes first. If \p is
* contains a block definition and the current name of the %Block is
* empty, it is changed accordingly.
*/
Block&
read(std::istream& is)
{
std::string line_str;
value_type line;
std::size_t def_count = 0;
bool nameless = name().empty();
while (std::getline(is, line_str))
{
if (detail::is_all_whitespace(line_str)) continue;
line.str(line_str);
if (line.is_block_def())
{
if (++def_count > 1)
{
is.seekg(-static_cast<std::ptrdiff_t>(line_str.length() + 1), std::ios_base::cur);
break;
}
if (nameless)
{
name(line[1]);
nameless = false;
}
}
push_back(line);
}
return *this;
}
/**
* \brief Assigns content from a string to the %Block.
* \param block String that is used as content for the %Block.
* \return Reference to \c *this.
*
* This function clears the name and content of the %Block and adds
* every non-empty line found in \p block as Line to the end of the
* %Block. If \p block contains a block definition, the name of the
* %Block is set accordingly. If \p block contains more than two
* block definitions, only the lines before the second block
* definition are added to the %Block.
*/
Block&
str(const std::string& block)
{
std::istringstream input(block);
clear();
read(input);
return *this;
}
/** Returns a string representation of the %Block. */
std::string
str() const
{
std::ostringstream output;
output << *this;
return output.str();
}
// element access
/**
* \brief Locates a Line in the %Block.
* \param key First strings of the Line to be located.
* \return Read/write reference to sought-after Line.
*
* This function takes a key (which is a vector of strings) and
* locates the Line whose first strings are equal to the strings in
* \p key. If no such Line exists, this function creates an empty
* Line at the end of the %Block and returns a reference to it.
*/
reference
operator[](const key_type& key)
{
iterator line = find(key);
if (line != end()) return *line;
push_back(value_type());
return back();
}
/**
* \brief Locates a Line in the %Block.
* \param key Integers that are used to locate the Line.
* \return Read/write reference to sought-after Line.
*
* This function takes a key (which is a vector of ints) and locates
* the Line whose first strings are equal to the to strings
* converted ints in \p key. If no such Line exists, this function
* creates an empty Line at the end of the %Block and returns a
* reference to it.
*/
reference
operator[](const std::vector<int>& key)
{ return (*this)[cont_to_key(key)]; }
/**
* \brief Locates a Line in the %Block.
* \param key String that is used to locate the Line.
* \return Read/write reference to sought-after Line.
*
* This function locates the Line whose first string is equal to
* \p key. If no such Line exists, this function creates an empty
* Line at the end of the %Block and returns a reference to it.
*/
reference
operator[](const std::string& key)
{ return (*this)[key_type(1, key)]; }
/**
* \brief Locates a Line in the %Block.
* \param key Integer that is used to locate the Line.
* \return Read/write reference to sought-after Line.
*
* This function locates the Line whose first string is equal to the
* to string converted \p key. If no such Line exists, this function
* creates an empty Line at the end of the %Block and returns a
* reference to it.
*/
reference
operator[](int key)
{ return (*this)[key_type(1, to_string(key))]; }
/**
* \brief Locates a Line in the %Block.
* \param key First strings of the Line to be located.
* \return Read/write reference to sought-after Line.
* \throw std::out_of_range If \p key does not match any Line.
*
* This function takes a key (which is a vector of strings) and
* locates the Line whose first strings are equal to the strings in
* \p key. If no such Line exists, \c std::out_of_range is thrown.
*/
reference
at(const key_type& key)
{
iterator line = find(key);
if (line != end()) return *line;
throw std::out_of_range(
"SLHAea::Block::at(‘" + boost::join(key, ",") + "’)");
}
/**
* \brief Locates a Line in the %Block.
* \param key First strings of the Line to be located.
* \return Read-only (constant) reference to sought-after Line.
* \throw std::out_of_range If \p key does not match any Line.
*
* This function takes a key (which is a vector of strings) and
* locates the Line whose first strings are equal to the strings in
* \p key. If no such Line exists, \c std::out_of_range is thrown.
*/
const_reference
at(const key_type& key) const
{
const_iterator line = find(key);
if (line != end()) return *line;
throw std::out_of_range(
"SLHAea::Block::at(‘" + boost::join(key, ",") + "’)");
}
/**
* \brief Locates a Line in the %Block.
* \param key Integers that are used to locate the Line.
* \return Read/write reference to sought-after Line.
* \throw std::out_of_range If \p key does not match any Line.
*
* This function takes a vector of ints and locates the Line whose
* first strings are equal to the to strings converted ints in
* \p key. If no such Line exists, \c std::out_of_range is thrown.
*/
reference
at(const std::vector<int>& key)
{ return at(cont_to_key(key)); }