46#include <libdap/InternalErr.h>
47#include <libdap/debug.h>
49#include "AsciiArray.h"
50#include <libdap/util.h>
53using namespace dap_asciival;
56AsciiArray::ptr_duplicate()
58 return new AsciiArray(*
this);
61AsciiArray::AsciiArray(
const string &n, BaseType *v) : Array(n, v)
65AsciiArray::AsciiArray( Array *bt )
71 BaseType *abt = basetype_to_asciitype( bt->var() ) ;
77 Dim_iter p = bt->dim_begin();
78 while ( p != bt->dim_end() ) {
79 append_dim(bt->dimension_size(p,
true), bt->dimension_name(p));
87 set_send_p(bt->send_p());
90AsciiArray::~AsciiArray()
96 Array *bt =
dynamic_cast < Array *
>(_redirect);
101 if (bt->var()->is_simple_type()) {
102 if (dimensions(
true) > 1) {
103 print_array(strm, print_name);
105 print_vector(strm, print_name);
108 print_complex_array(strm, print_name);
113void AsciiArray::print_vector(ostream &strm,
bool print_name)
115 Array *bt =
dynamic_cast < Array *
>(_redirect);
121 strm << dynamic_cast<AsciiOutput*>(
this)->get_full_name() <<
", " ;
126 if (dimension_size(dim_begin(),
true) > 0) {
127 int64_t end = dimension_size_ll(dim_begin(),
true) - 1;
129 for (int64_t i = 0; i < end; ++i) {
130 BaseType *curr_var = basetype_to_asciitype(bt->var(i));
131 dynamic_cast<AsciiOutput &
>(*curr_var).print_ascii(strm,
false);
136 BaseType *curr_var = basetype_to_asciitype(bt->var(end));
137 dynamic_cast<AsciiOutput &
>(*curr_var).print_ascii(strm,
false);
156 Array *bt =
dynamic_cast < Array *
>(_redirect);
165 for (
int i = 0; i < number; ++i) {
166 BaseType *curr_var = basetype_to_asciitype(bt->var(index++));
167 dynamic_cast<AsciiOutput &
>(*curr_var).print_ascii(strm,
false);
172 BaseType *curr_var = basetype_to_asciitype(bt->var(index++));
173 dynamic_cast<AsciiOutput &
>(*curr_var).print_ascii(strm,
false);
183int AsciiArray::get_index(vector < int >indices)
throw(InternalErr)
185 if (indices.size() != dimensions(
true)) {
186 throw InternalErr(__FILE__, __LINE__,
187 "Index vector is the wrong size!");
194 vector < int >shape = get_shape_vector(indices.size());
197 reverse(indices.begin(), indices.end());
198 reverse(shape.begin(), shape.end());
200 vector < int >::iterator indices_iter = indices.begin();
201 vector < int >::iterator shape_iter = shape.begin();
203 int index = *indices_iter++;
205 while (indices_iter != indices.end()) {
206 multiplier *= *shape_iter++;
207 index += multiplier * *indices_iter++;
218 if (n < 1 || n > dimensions(
true)) {
219 string msg =
"Attempt to get ";
220 msg += long_to_string(n) +
" dimensions from " + name()
221 +
" which has only " + long_to_string(dimensions(
true))
224 throw InternalErr(__FILE__, __LINE__, msg);
228 Array::Dim_iter p = dim_begin();
229 for (
unsigned i = 0; i < n && p != dim_end(); ++i, ++p) {
230 shape.push_back(dimension_size(p,
true));
241 if (n > dimensions(
true) - 1) {
242 string msg =
"Attempt to get dimension ";
244 long_to_string(n + 1) +
" from `" + name() +
245 "' which has " + long_to_string(dimensions(
true)) +
247 throw InternalErr(__FILE__, __LINE__, msg);
250 return dimension_size(dim_begin() + n,
true);
253void AsciiArray::print_array(ostream &strm,
bool )
255 DBG(cerr <<
"Entering AsciiArray::print_array" << endl);
257 int dims = dimensions(
true);
259 throw InternalErr(__FILE__, __LINE__,
260 "Dimension count is <= 1 while printing multidimensional array.");
271 vector < int >state(dims - 1, 0);
277 strm << dynamic_cast <AsciiOutput *>(
this)->get_full_name() ;
279 for (
int i = 0; i < dims - 1; ++i) {
280 strm <<
"[" << state[i] <<
"]" ;
284 index =
print_row(strm, index, rightmost_dim_size - 1);
289 }
while (more_indices);
291 DBG(cerr <<
"ExitingAsciiArray::print_array" << endl);
294void AsciiArray::print_complex_array(ostream &strm,
bool )
296 DBG(cerr <<
"Entering AsciiArray::print_complex_array" << endl);
298 Array *bt =
dynamic_cast < Array *
>(_redirect);
302 int dims = dimensions(
true);
304 throw InternalErr(__FILE__, __LINE__,
305 "Dimension count is <= 1 while printing multidimensional array.");
311 vector < int >state(dims, 0);
316 strm << dynamic_cast <AsciiOutput *>(
this)->get_full_name() ;
318 for (
int i = 0; i < dims; ++i) {
319 strm <<
"[" << state[i] <<
"]" ;
324 basetype_to_asciitype(bt->var(get_index(state)));
325 dynamic_cast < AsciiOutput &
>(*curr_var).print_ascii(strm,
true);
333 }
while (more_indices);
335 DBG(cerr <<
"ExitingAsciiArray::print_complex_array" << endl);
virtual void print_ascii(ostream &strm, bool print_name=true)
int get_nth_dim_size(size_t n)
vector< int > get_shape_vector(size_t n)
int print_row(ostream &strm, int index, int number)
bool increment_state(vector< int > *state, const vector< int > &shape)