IODA Bundle
Span.cc
Go to the documentation of this file.
1 /*
2  * (C) Copyright 1996-2012 ECMWF.
3  *
4  * This software is licensed under the terms of the Apache Licence Version 2.0
5  * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
6  * In applying this licence, ECMWF does not waive the privileges and immunities
7  * granted to it by virtue of its status as an intergovernmental organisation nor
8  * does it submit to any jurisdiction.
9  */
10 
11 #include "odc/core/Span.h"
12 
13 #include <cstring>
14 
15 #include "eckit/exception/Exceptions.h"
16 #include "eckit/types/Types.h"
17 
18 
19 using namespace eckit;
20 
21 namespace odc {
22 namespace core {
23 
24 //----------------------------------------------------------------------------------------------------------------------
25 
26 Span::Span(Offset start, Length length) :
27  start_(start),
28  length_(length) {}
29 
30 Span::~Span() {}
31 
32 void Span::extend(const Span& other) {
33 
34  if (start_ != other.start_) {
35  length_ += other.length_;
36  }
37 
38  for (const auto& kv : other.integerValues_) addValues(kv.first, kv.second);
39  for (const auto& kv : other.realValues_) addValues(kv.first, kv.second);
40  for (const auto& kv : other.stringValues_) addValues(kv.first, kv.second);
41 }
42 
43 void Span::extend(Length length) {
44  length_ += length;
45 }
46 
47 void Span::addValue(const std::string& column, api::ColumnType t, double val) {
48 
49  switch (t) {
50 
51  case api::INTEGER:
52  case api::BITFIELD:
53  ASSERT(realValues_.find(column) == realValues_.end());
54  ASSERT(stringValues_.find(column) == stringValues_.end());
55  integerValues_[column].insert(static_cast<int64_t>(val));
56  break;
57 
58  case api::REAL:
59  case api::DOUBLE:
60  ASSERT(integerValues_.find(column) == integerValues_.end());
61  ASSERT(stringValues_.find(column) == stringValues_.end());
62  realValues_[column].insert(val);
63  break;
64 
65  case api::STRING: {
66  ASSERT(realValues_.find(column) == realValues_.end());
67  ASSERT(integerValues_.find(column) == integerValues_.end());
68  const char* c = reinterpret_cast<const char*>(&val);
69  stringValues_[column].insert(std::string(c, ::strnlen(c, sizeof(double))));
70  }
71  break;
72 
73  case api::IGNORE:
74  default: {
75  std::stringstream ss;
76  ss << "Unsupported column type " << t << " found for column '" << column << "'";
77  throw SeriousBug(ss.str(), Here());
78  }
79  };
80 }
81 
82 void Span::addValues(const std::string& column, const std::set<double>& vals) {
83 
84  ASSERT(integerValues_.find(column) == integerValues_.end());
85  ASSERT(stringValues_.find(column) == stringValues_.end());
86  realValues_[column].insert(vals.cbegin(), vals.cend());
87 }
88 
89 void Span::addValues(const std::string& column, const std::set<long>& vals) {
90 
91  ASSERT(realValues_.find(column) == realValues_.end());
92  ASSERT(stringValues_.find(column) == stringValues_.end());
93  integerValues_[column].insert(vals.begin(), vals.end());
94 }
95 
96 void Span::addValues(const std::string& column, const std::set<std::string>& vals) {
97 
98  ASSERT(integerValues_.find(column) == integerValues_.end());
99  ASSERT(realValues_.find(column) == realValues_.end());
100  stringValues_[column].insert(vals.begin(), vals.end());
101 }
102 
103 //----------------------------------------------------------------------------------------------------------------------
104 
105 } // namespace core
106 } // namespace odc
107 
std::map< std::string, std::set< std::string > > stringValues_
Definition: Span.h:64
eckit::Length length_
Definition: Span.h:60
eckit::Offset start_
Definition: Span.h:59
std::map< std::string, std::set< long > > integerValues_
Definition: Span.h:62
std::map< std::string, std::set< double > > realValues_
Definition: Span.h:63
@ BITFIELD
Definition: ColumnType.h:27
Definition: ColumnInfo.h:23