Created
August 28, 2018 08:38
-
-
Save arnobaer/9db3f38d1db5327812b7a78714371499 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
// sample container | |
struct jet | |
{ | |
typedef size_t pt_type; | |
pt_type pt; | |
}; | |
// fixed sized array | |
template<typename T, size_t N> | |
struct array | |
{ | |
typedef T value_type; | |
typedef size_t size_type; | |
value_type data[N]; | |
static size_type size() { return N; } | |
static bool empty() { return not N; } | |
value_type& operator[](size_type pos) { return data[pos]; } | |
const value_type& operator[](size_type pos) const { return data[pos]; } | |
}; | |
// variable content array of fixed size | |
// valid defines how many array elements are actually used | |
template<typename T> | |
struct va_array | |
{ | |
typedef T array_type; | |
typedef size_t size_type; | |
typedef typename T::value_type value_type; | |
array_type data; | |
size_type valid; | |
size_type capacity() const { return data.size(); } | |
size_type size() const { return valid; } | |
bool empty() const { return not valid; } | |
value_type& operator[](size_type pos) { return data[pos]; } | |
const value_type& operator[](size_type pos) const { return data[pos]; } | |
}; | |
struct cond | |
{ | |
typedef va_array<array<jet, 4> > jet_spec_type; // max 4 | |
jet_spec_type jet_spec; | |
typedef va_array<array<jet, 2> > spec_type; // max 2 | |
spec_type spec; | |
}; | |
int main() | |
{ | |
typedef array<jet, 4> jet_array; | |
jet_array base_jets = {}; | |
va_array<jet_array> init_jets = {{10, 20}, 2}; | |
std::cout << base_jets.size() << std::endl; | |
std::cout << init_jets.size() << std::endl; | |
cond c = { | |
.jet_spec={.data={40, 80}, .valid=2}, | |
.spec={.data={42}, .valid=1} | |
}; | |
jet j = {.pt=4}; | |
std::cout << c.jet_spec.size() << " of " << c.jet_spec.capacity() << std::endl; | |
std::cout << c.jet_spec[1].pt << std::endl; | |
std::cout << "spec=[" << c.spec[0].pt << ", " << c.spec[1].pt << "], valid=" << c.spec.size() << std::endl; | |
std::cout << j.pt << std::endl; | |
for (size_t i = 0; i < c.jet_spec.valid; ++i) | |
{ | |
const jet& obj = c.jet_spec[i]; | |
std::cout << ":" << obj.pt << std::endl; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment