TECA
teca_array_collection.h
1 #ifndef teca_array_collection_h
2 #define teca_array_collection_h
3 
4 #include "teca_array_collection_fwd.h"
5 #include "teca_variant_array.h"
6 #include <map>
7 #include <vector>
8 #include <string>
9 
10 // a collection of named arrays
15 {
16 public:
17  // construct on heap
18  static
19  p_teca_array_collection New()
20  { return p_teca_array_collection(new teca_array_collection()); }
21 
22  // reset to empty state
23  void clear();
24 
25  // declare a set of arrays. requires name,type pairs
26  // for ex. define("c1",int(),"c2",float()) creates
27  // 2 arrays in the collection the first storing int's
28  // the second storing float's.
29  template<typename nT, typename aT, typename... oT>
30  void declare_set(nT &&a_name, aT a_type, oT &&...args);
31 
32  // declare a single array
33  template<typename nT, typename aT>
34  void declare(nT &&a_name, aT a_type);
35 
36  // add, return the index of the new entry.
37  // or -1 if the array name already exists.
38  int append(p_teca_variant_array array);
39  int append(const std::string &name, p_teca_variant_array array);
40 
41  // replace the ith array, return 0 on success.
42  // the name of the array is not changed.
43  int set(unsigned int i, p_teca_variant_array array);
44 
45  // add or replace the named array, returns 0 on success.
46  int set(const std::string &name, p_teca_variant_array array);
47 
48  // remove
49  int remove(unsigned int i);
50  int remove(const std::string &name);
51 
52  // number of
53  unsigned int size() const noexcept
54  { return m_arrays.size(); }
55 
56  // access by id
57  p_teca_variant_array get(unsigned int i)
58  { return m_arrays[i]; }
59 
60  const_p_teca_variant_array get(unsigned int i) const
61  { return m_arrays[i]; }
62 
63  // test for array
64  bool has(const std::string &name) const;
65 
66  // access by name
67  p_teca_variant_array get(const std::string &name);
68  const_p_teca_variant_array get(const std::string &name) const;
69 
70  p_teca_variant_array operator[](const std::string &name)
71  { return this->get(name); }
72 
73  const_p_teca_variant_array operator[](const std::string &name) const
74  { return this->get(name); }
75 
76  // access names
77  std::string &get_name(unsigned int i)
78  { return m_names[i]; }
79 
80  const std::string &get_name(unsigned int i) const
81  { return m_names[i]; }
82 
83  // return a unique string identifier
84  std::string get_class_name() const
85  { return "teca_array_collection"; }
86 
87  // return an integer identifier uniquely naming the dataset type
88  int get_type_code() const
89  { return -1; }
90 
91  // copy
92  void copy(const const_p_teca_array_collection &other);
93  void shallow_copy(const p_teca_array_collection &other);
94 
95  // append
96  int append(const const_p_teca_array_collection &other);
97  int shallow_append(const p_teca_array_collection &other);
98 
99  // swap
100  void swap(p_teca_array_collection &other);
101 
102  // serialize the data to/from the given stream
103  // for I/O or communication
104  int to_stream(teca_binary_stream &s) const;
105  int from_stream(teca_binary_stream &s);
106 
107  // stream to/from human readable representation
108  int to_stream(std::ostream &) const;
109 
110 protected:
111  teca_array_collection() = default;
113  teca_array_collection(const teca_array_collection &&) = delete;
114  void operator=(const teca_array_collection &) = delete;
115  void operator=(const teca_array_collection &&) = delete;
116  void declare_set(){}
117 
118 private:
119  using name_vector_t = std::vector<std::string>;
120  using array_vector_t = std::vector<p_teca_variant_array>;
121  using name_array_map_t = std::map<std::string,unsigned int>;
122 
123  name_vector_t m_names;
124  array_vector_t m_arrays;
125  name_array_map_t m_name_array_map;
126 };
127 
128 // --------------------------------------------------------------------------
129 template<typename nT, typename aT, typename... oT>
130 void teca_array_collection::declare_set(nT &&a_name, aT a_type, oT &&... args)
131 {
132  this->declare(std::forward<nT>(a_name), a_type);
133  this->declare_set(args...);
134 }
135 
136 // --------------------------------------------------------------------------
137 template<typename nT, typename aT>
138 void teca_array_collection::declare(nT &&a_name, aT)
139 {
140  unsigned int id = m_arrays.size();
141  m_names.emplace_back(std::forward<nT>(a_name));
142  m_arrays.emplace_back(teca_variant_array_impl<aT>::New());
143  m_name_array_map.emplace(std::forward<nT>(a_name), id);
144 }
145 
146 #endif
teca_binary_stream
Definition: teca_binary_stream.h:16
teca_variant_array_impl
Definition: teca_variant_array.h:269
teca_array_collection
Definition: teca_array_collection.h:15