TECA
The Toolkit for Extreme Climate Analysis
teca_table_collection.h
1 #ifndef teca_table_collection_h
2 #define teca_table_collection_h
3 
4 #include "teca_config.h"
5 #include "teca_table.h"
6 #include "teca_shared_object.h"
7 
8 #include <map>
9 #include <vector>
10 #include <string>
11 
12 TECA_SHARED_OBJECT_FORWARD_DECL(teca_table_collection)
13 
14 /// A collection of named tables.
16 {
17 public:
18  using allocator = teca_dataset::allocator;
19 
20  // construct on heap
21  static
22  p_teca_table_collection New()
23  { return p_teca_table_collection(new teca_table_collection()); }
24 
25  // reset to empty state
26  void clear();
27 
28  // declare a set of tables, from a list of names
29  template<typename nT, typename... oT>
30  void declare_set(nT &&a_name, oT &&...args);
31 
32  // declare a single array
33  template<typename nT>
34  void declare(nT &&a_name);
35 
36  // add, return the index of the new entry.
37  // or -1 if the array name already exists.
38  int append(p_teca_table array);
39  int append(const std::string &name, p_teca_table array);
40 
41  // set, return 0 on success.
42  int set(unsigned int i, p_teca_table array);
43  int set(const std::string &name, p_teca_table array);
44 
45  // remove
46  int remove(unsigned int i);
47  int remove(const std::string &name);
48 
49  // number of
50  unsigned int size() const noexcept
51  { return m_tables.size(); }
52 
53  // access by id
54  p_teca_table get(unsigned int i)
55  { return m_tables[i]; }
56 
57  const_p_teca_table get(unsigned int i) const
58  { return m_tables[i]; }
59 
60  // test for array
61  bool has(const std::string &name) const;
62 
63  // access by name
64  p_teca_table get(const std::string &name);
65  const_p_teca_table get(const std::string &name) const;
66 
67  p_teca_table operator[](const std::string &name)
68  { return this->get(name); }
69 
70  const_p_teca_table operator[](const std::string &name) const
71  { return this->get(name); }
72 
73  // access names
74  std::string &get_name(unsigned int i)
75  { return m_names[i]; }
76 
77  const std::string &get_name(unsigned int i) const
78  { return m_names[i]; }
79 
80  // return a unique string identifier
81  std::string get_class_name() const
82  { return "teca_table_collection"; }
83 
84  // return an integer identifier uniquely naming the dataset type
85  int get_type_code() const
86  { return -1; }
87 
88  /// deep copy using the passed allocator for new allocations
89  void copy(const const_p_teca_table_collection &other,
90  allocator alloc = allocator::malloc);
91 
92  /// shallow copy
93  void shallow_copy(const p_teca_table_collection &other);
94 
95  // swap
96  void swap(p_teca_table_collection &other);
97 
98  // serialize the data to/from the given stream
99  // for I/O or communication
100  int to_stream(teca_binary_stream &s) const;
101  int from_stream(teca_binary_stream &s);
102 
103  // stream to/from human readable representation
104  int to_stream(std::ostream &) const;
105  int from_stream(std::istream &) { return -1; }
106 
107 protected:
108  teca_table_collection() = default;
110  teca_table_collection(const teca_table_collection &&) = delete;
111  void operator=(const teca_table_collection &) = delete;
112  void operator=(const teca_table_collection &&) = delete;
113  void declare_set(){}
114 
115 private:
116  using name_vector_t = std::vector<std::string>;
117  using array_vector_t = std::vector<p_teca_table>;
118  using name_array_map_t = std::map<std::string,unsigned int>;
119 
120  name_vector_t m_names;
121  array_vector_t m_tables;
122  name_array_map_t m_name_array_map;
123 };
124 
125 // --------------------------------------------------------------------------
126 template<typename nT, typename... oT>
127 void teca_table_collection::declare_set(nT &&a_name, oT &&... args)
128 {
129  this->declare(std::forward<nT>(a_name));
130  this->declare_set(args...);
131 }
132 
133 // --------------------------------------------------------------------------
134 template<typename nT>
135 void teca_table_collection::declare(nT &&a_name)
136 {
137  unsigned int id = m_tables.size();
138  m_names.emplace_back(std::forward<nT>(a_name));
139  m_tables.emplace_back(teca_table::New());
140  m_name_array_map.emplace(std::forward<nT>(a_name), id);
141 }
142 
143 #endif
Serialize objects into a binary stream.
Definition: teca_binary_stream.h:17
A collection of named tables.
Definition: teca_table_collection.h:16
void shallow_copy(const p_teca_table_collection &other)
shallow copy
void copy(const const_p_teca_table_collection &other, allocator alloc=allocator::malloc)
deep copy using the passed allocator for new allocations
p_teca_error_handler error_handler TECA_EXPORT
The global error handler instance.
TECA_EXPORT bool append(teca_variant_array *varr, PyObject *obj)
Append values from the object to the variant array.
Definition: teca_py_array.h:221
TECA_EXPORT bool set(teca_variant_array *varr, unsigned long i, PyObject *obj)
Set i'th element of the variant array to the value of the object.
Definition: teca_py_array.h:305
auto New(size_t n_elem, teca_variant_array::allocator alloc=teca_variant_array::allocator::malloc)
Definition: teca_variant_array_util.h:269