Philote-Cpp
C++ bindings for the Philote MDO standard
Loading...
Searching...
No Matches
variable.h
Go to the documentation of this file.
1/*
2 Philote C++ Bindings
3
4 Copyright 2022-2025 Christopher A. Lupp
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17
18 This work has been cleared for public release, distribution unlimited, case
19 number: AFRL-2023-5716.
20
21 The views expressed are those of the authors and do not reflect the
22 official guidance or position of the United States Government, the
23 Department of Defense or of the United States Air Force.
24
25 Statement from DoD: The Appearance of external hyperlinks does not
26 constitute endorsement by the United States Department of Defense (DoD) of
27 the linked websites, of the information, products, or services contained
28 therein. The DoD does not exercise any editorial, security, or other
29 control over the information you may find at these locations.
30*/
31#pragma once
32
33#include <string>
34#include <map>
35#include <vector>
36
37#include <data.pb.h>
38#include <disciplines.grpc.pb.h>
39
40namespace philote
41{
85 {
86 public:
91 Variable() = default;
92
99 Variable(const philote::VariableType &type,
100 const std::vector<size_t> &shape);
101
107 explicit Variable(const philote::VariableMetaData &meta);
108
114 explicit Variable(const philote::PartialsMetaData &meta);
115
120 ~Variable() noexcept = default;
121
138 void Segment(const size_t &start, const size_t &end,
139 const std::vector<double> &data);
140
149 std::vector<double> Segment(const size_t &start, const size_t &end) const;
150
157 std::vector<size_t> Shape() const noexcept;
158
164 size_t Size() const noexcept;
165
172 double operator()(const size_t &i) const;
173
180 double &operator()(const size_t &i);
181
189 philote::Array CreateChunk(const size_t &start, const size_t &end) const;
190
196 void Send(std::string name,
197 std::string subname,
198 grpc::ClientReaderWriter<::philote::Array, ::philote::Array> *stream,
199 const size_t &chunk_size) const;
200
210 void Send(std::string name,
211 std::string subname,
212 grpc::ServerReaderWriterInterface<::philote::Array, ::philote::Array> *stream,
213 const size_t &chunk_size,
214 grpc::ServerContext* context = nullptr) const;
215
221 void Send(std::string name,
222 std::string subname,
223 grpc::ClientReaderWriterInterface<::philote::Array, ::philote::Array> *stream,
224 const size_t &chunk_size) const;
225
232 void AssignChunk(const Array &data);
233
234 private:
236 philote::VariableType type_;
237
239 std::vector<size_t> shape_;
240
242 std::vector<double> data_;
243
245 std::vector<int64_t> discrete_data_;
246 };
247
290 template<typename T>
292 {
293 public:
297 PairDict() = default;
298
302 PairDict(const PairDict&) = default;
303
307 PairDict& operator=(const PairDict&) = default;
308
312 PairDict(PairDict&&) noexcept = default;
313
317 PairDict& operator=(PairDict&&) noexcept = default;
318
322 ~PairDict() noexcept = default;
323
331 T& operator()(const std::string& key1, const std::string& key2)
332 {
333 return data_[std::make_pair(key1, key2)];
334 }
335
343 const T& operator()(const std::string& key1, const std::string& key2) const
344 {
345 return data_.at(std::make_pair(key1, key2));
346 }
347
355 bool contains(const std::string& key1, const std::string& key2) const noexcept
356 {
357 return data_.find(std::make_pair(key1, key2)) != data_.end();
358 }
359
365 size_t size() const noexcept
366 {
367 return data_.size();
368 }
369
375 bool empty() const noexcept
376 {
377 return data_.empty();
378 }
379
383 void clear()
384 {
385 data_.clear();
386 }
387
391 auto begin() { return data_.begin(); }
392 auto begin() const { return data_.begin(); }
393
397 auto end() { return data_.end(); }
398 auto end() const { return data_.end(); }
399
400 private:
401 std::map<std::pair<std::string, std::string>, T> data_;
402 };
403
404 typedef std::map<std::string, philote::Variable> Variables;
405 typedef std::map<std::pair<std::string, std::string>, philote::Variable> Partials;
407}
Dictionary for storing values with respect to two keys.
Definition variable.h:292
const T & operator()(const std::string &key1, const std::string &key2) const
Access value using two keys (const version)
Definition variable.h:343
auto begin() const
Definition variable.h:392
auto begin()
Get iterator to beginning.
Definition variable.h:391
bool empty() const noexcept
Check if the dictionary is empty.
Definition variable.h:375
PairDict()=default
Default constructor.
bool contains(const std::string &key1, const std::string &key2) const noexcept
Check if a key pair exists.
Definition variable.h:355
PairDict & operator=(const PairDict &)=default
Copy assignment operator.
size_t size() const noexcept
Get the size of the dictionary.
Definition variable.h:365
void clear()
Clear all entries.
Definition variable.h:383
auto end()
Get iterator to end.
Definition variable.h:397
auto end() const
Definition variable.h:398
PairDict(const PairDict &)=default
Copy constructor.
PairDict(PairDict &&) noexcept=default
Move constructor.
A class for storing continuous and discrete variables.
Definition variable.h:85
Variable(const philote::VariableMetaData &meta)
Construct a new Variable object.
void AssignChunk(const Array &data)
Assigns a chunk to the variable.
Variable(const philote::PartialsMetaData &meta)
Construct a new Variable object.
std::vector< size_t > Shape() const noexcept
Returns the shape of the array.
void Segment(const size_t &start, const size_t &end, const std::vector< double > &data)
Assigns a segment of the array given a subvector.
Variable(const philote::VariableType &type, const std::vector< size_t > &shape)
Construct a new Variable object.
size_t Size() const noexcept
Returns the size of the array.
~Variable() noexcept=default
Destroy the Variables object.
void Send(std::string name, std::string subname, grpc::ClientReaderWriter<::philote::Array, ::philote::Array > *stream, const size_t &chunk_size) const
Sends the variable from the client to the server.
Variable()=default
Construct a new Variables object.
philote::Array CreateChunk(const size_t &start, const size_t &end) const
Create a Chunk of the variable.
Definition discipline.h:43
std::map< std::string, philote::Variable > Variables
Definition variable.h:404
PairDict< philote::Variable > PartialsPairDict
Definition variable.h:406
std::map< std::pair< std::string, std::string >, philote::Variable > Partials
Definition variable.h:405