File: TestArray.cc

package info (click to toggle)
libdap 3.20.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,568 kB
  • sloc: cpp: 50,809; sh: 41,536; xml: 23,511; ansic: 20,030; yacc: 2,508; exp: 1,544; makefile: 990; lex: 309; perl: 52; fortran: 8
file content (579 lines) | stat: -rw-r--r-- 16,918 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

// (c) COPYRIGHT URI/MIT 1995-1996,1999
// Please read the full copyright statement in the file COPYRIGHT_URI.
//
// Authors:
//      jhrg,jimg       James Gallagher <jgallagher@gso.uri.edu>

// Implementation for TestArray. See TestByte.cc
//
// jhrg 1/12/95

#include "config.h"

#include <cstring>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#ifndef WIN32
#else
#include <io.h>
#include <fcntl.h>
#include <process.h>
#endif

//#define DODS_DEBUG

#include "util.h"
#include "debug.h"

#include "TestInt8.h"

#include "TestByte.h"
#include "TestInt16.h"
#include "TestUInt16.h"
#include "TestInt32.h"
#include "TestUInt32.h"

#include "TestInt64.h"
#include "TestUInt64.h"

#include "TestD4Enum.h"

#include "TestFloat32.h"
#include "TestFloat64.h"

#include "TestStr.h"

#include "TestArray.h"
#include "TestCommon.h"

using std::cerr;
using std::endl;

extern int test_variable_sleep_interval;

void TestArray::_duplicate(const TestArray &ts)
{
    d_series_values = ts.d_series_values;
}

BaseType *
TestArray::ptr_duplicate()
{
    return new TestArray(*this);
}

TestArray::TestArray(const string &n, BaseType *v, bool is_dap4) :
        Array(n, v, is_dap4), d_series_values(false)
{
}

TestArray::TestArray(const string &n, const string &d, BaseType *v, bool is_dap4) :
        Array(n, d, v, is_dap4), d_series_values(false)
{
}

TestArray::TestArray(const TestArray &rhs) :
        Array(rhs), TestCommon(rhs)
{
    _duplicate(rhs);
}

TestArray::~TestArray()
{
}

TestArray &
TestArray::operator=(const TestArray &rhs)
{
    if (this == &rhs) return *this;

    Array::operator=(rhs);

    _duplicate(rhs);

    return *this;
}

// This code calls 'output_values()' because print_val() does not test
// the value of send_p(). We need to wrap a method around the calls to
// print_val() to ensure that only values for variables with send_p() set
// are called. In the serialize/deserialize case, the 'client' DDS only
// has variables sent by the 'server' but in the intern_data() case, the
// whole DDS is still present but only variables selected in the CE have
// values.

unsigned int TestArray::m_print_array(ostream &out, unsigned int index, unsigned int dims, unsigned int shape[])
{
    if (dims == 1) {
        out << "{";

        // Added this test for zero-length arrays. jhrg 1/28/16
        if (shape[0] >= 1) {
            for (unsigned i = 0; i < shape[0] - 1; ++i) {
                dynamic_cast<TestCommon&>(*var(index++)).output_values(out);
                out << ", ";
            }

            dynamic_cast<TestCommon&>(*var(index++)).output_values(out);
        }

        out << "}";

        return index;
    }
    else {
        out << "{";
        // Fixed an off-by-one error in the following loop. Since the array
        // length is shape[dims-1]-1 *and* since we want one less dimension
        // than that, the correct limit on this loop is shape[dims-2]-1. From
        // Todd Karakasian.
        //
        // The saga continues; the loop test should be `i < shape[0]-1'. jhrg
        // 9/12/96.
        //
        // Added this (primitive) guard against errors when a zero-length array
        // is declared with a shape like [0][4]. jhrg 1/28/16
        if (shape[0] > 0) {
            for (unsigned i = 0; i < shape[0] - 1; ++i) {
                index = m_print_array(out, index, dims - 1, shape + 1);
                out << ",";
            }

            index = m_print_array(out, index, dims - 1, shape + 1);
        }

        out << "}";

        return index;
    }
}

void TestArray::output_values(std::ostream &out)
{
    //unsigned int *shape = new unsigned int[dimensions(true)];

    vector<unsigned int> shape(dimensions(true));
    unsigned int index = 0;
    for (Dim_iter i = dim_begin(); i != dim_end() && index < dimensions(true); ++i)
        shape[index++] = dimension_size(i, true);

    m_print_array(out, 0, dimensions(true), shape.data());

    //delete[] shape;
    //shape = 0;
}

/** Special names are ones that start with 'lat' or 'lon'. These indicate
 that the vector (this is only for vectors) is a vector of latitude or
 longitude values. */
bool TestArray::m_name_is_special()
{
    return (name().find("lat") != string::npos || name().find("lon") != string::npos);
}

void TestArray::m_build_special_values()
{
    if (name().find("lat_reversed") != string::npos) {
        int array_len = length();
        //double *lat_data = new double[array_len];
        vector<double> lat_data(array_len);
        for (int i = 0; i < array_len; ++i) {
            lat_data[i] = -89 + (180 / array_len) * (i + 1);
        }
        libdap::set_array_using_double(this, lat_data.data(), array_len);
    }
    else if (name().find("lat") != string::npos) {
        int array_len = length();
        // double *lat_data = new double[array_len];
        vector<double> lat_data(array_len);
        for (int i = 0; i < array_len; ++i) {
            lat_data[i] = 90 - (180 / array_len) * (i + 1);
        }
        libdap::set_array_using_double(this, lat_data.data(), array_len);
    }
    else if (name().find("lon") != string::npos) {
        int array_len = length();
        //double *lon_data = new double[array_len];
        vector<double> lon_data(array_len);
        for (int i = 0; i < array_len; ++i) {
            lon_data[i] = (360 / array_len) * (i + 1);
        }
        libdap::set_array_using_double(this, lon_data.data(), array_len);
    }
    else {
        throw InternalErr(__FILE__, __LINE__, "Unrecognized name");
    }
}

int TestArray::m_offset(int y, Dim_iter X, int x)
{
    return y * dimension_size(X, false) + x;
}

/**
 * @brief Load an 2D array with values.
 * Use the read() function for the prototype element of the array to
 * get values and load them into an array, then constrain the array.
 * Thus if 'series values' are used and the array is constrained, the
 * result will 'make sense'
 *
 * @param constrained_array
 */
template<typename T, class C>
void TestArray::m_constrained_matrix(vector<T>&constrained_array)
{
    int unconstrained_size = 1;
    Dim_iter d = dim_begin();
    while (d != dim_end())
        unconstrained_size *= dimension_size(d++, false);

    vector<T> whole_array(unconstrained_size);
    for (int i = 0; i < unconstrained_size; ++i) {
        T v;
        var()->read();
#if 0
        if (var()->type() == dods_enum_c)
        static_cast<C*>(var())->value(&v);
        else
#endif
        v = static_cast<C*>(var())->value();

        whole_array[i] = v;
        var()->set_read_p(false); // pick up the next value
    }

    DBG(cerr << "whole_array: "; copy(whole_array.begin(), whole_array.end(), ostream_iterator<T>(cerr, ", ")); cerr << endl);

    Dim_iter Y = dim_begin();
    Dim_iter X = Y + 1;

    DBG(cerr << "dimension_start(Y): " << dimension_start(Y) << endl); DBG(cerr << "dimension_stop(Y): " << dimension_stop(Y) << endl); DBG(cerr << "dimension_start(X): " << dimension_start(X) << endl); DBG(cerr << "dimension_stop(X): " << dimension_stop(X) << endl);

    int constrained_size = 0;
    int y = dimension_start(Y);
    while (y < dimension_stop(Y) + 1) {
        int x = dimension_start(X);

        while (x < dimension_stop(X) + 1) {
            constrained_array[constrained_size++] = whole_array[m_offset(y, X, x)];
            x += dimension_stride(X);
        }

        y += dimension_stride(Y);
    }
}

template<typename T>
void TestArray::m_enum_constrained_matrix(vector<T>&constrained_array)
{
    int unconstrained_size = 1;
    Dim_iter d = dim_begin();
    while (d != dim_end())
        unconstrained_size *= dimension_size(d++, false);

    vector<T> whole_array(unconstrained_size);
    for (int i = 0; i < unconstrained_size; ++i) {
        T v;
        var()->read();
        static_cast<D4Enum*>(var())->value(&v);
        whole_array[i] = v;
        var()->set_read_p(false); // pick up the next value
    }

    DBG(cerr << "whole_array: "; copy(whole_array.begin(), whole_array.end(), ostream_iterator<T>(cerr, ", ")); cerr << endl);

    Dim_iter Y = dim_begin();
    Dim_iter X = Y + 1;

    DBG(cerr << "dimension_start(Y): " << dimension_start(Y) << endl); DBG(cerr << "dimension_stop(Y): " << dimension_stop(Y) << endl); DBG(cerr << "dimension_start(X): " << dimension_start(X) << endl); DBG(cerr << "dimension_stop(X): " << dimension_stop(X) << endl);

    int constrained_size = 0;
    int y = dimension_start(Y);
    while (y < dimension_stop(Y) + 1) {
        int x = dimension_start(X);

        while (x < dimension_stop(X) + 1) {
            constrained_array[constrained_size++] = whole_array[m_offset(y, X, x)];
            x += dimension_stride(X);
        }

        y += dimension_stride(Y);
    }
}

/**
 * Load the variable's internal data buffer with values, simulating a read()
 * call to some data store. A private method.
 */
template<typename T, class C>
void TestArray::m_cardinal_type_read_helper()
{
    if (get_series_values()) {
        // Special case code for vectors that have specific names.
        // This is used to test code that works with lat/lon data.
        if (dimensions() == 1 && m_name_is_special()) {
            m_build_special_values();
        }
        else if (dimensions() == 2) {
            vector<T> tmp(length());
            m_constrained_matrix<T, C>(tmp);
            set_value(tmp, length());
        }
        else {
            vector<T> tmp(length());
            for (int64_t i = 0, end = length(); i < end; ++i) {
                var()->read();
                tmp[i] = static_cast<C*>(var())->value();
                var()->set_read_p(false); // pick up the next value
            }
            set_value(tmp, length());
        }
    }
    else {
        // read a value into the Array's prototype element
        var()->read();
        T value = static_cast<C*>(var())->value();
        vector<T> tmp(length());
        for (int64_t i = 0, end = length(); i < end; ++i) {
            tmp[i] = value;
        }

        set_value(tmp, length());
    }
}

/**
 * Load the variable's internal data buffer with values, simulating a read()
 * call to some data store. A private method.
 */
template<typename T>
void TestArray::m_enum_type_read_helper()
{
    if (get_series_values()) {
        if (dimensions() == 2) {
            vector<T> tmp(length());
            m_enum_constrained_matrix<T>(tmp);
            set_value(tmp, length());
        }
        else {
            vector<T> tmp(length());
            for (int64_t i = 0, end = length(); i < end; ++i) {
                var()->read();
                T v;
                static_cast<D4Enum*>(var())->value(&v);

                tmp[i] = v;
                var()->set_read_p(false); // pick up the next value
            }
            set_value(tmp, length());
        }
    }
    else {
        // read a value into the Array's prototype element
        var()->read();
        T value;
        static_cast<D4Enum*>(var())->value(&value);

        vector<T> tmp(length());
        for (int64_t i = 0, end = length(); i < end; ++i) {
            tmp[i] = value;
        }

        set_value(tmp, length());
    }
}

bool TestArray::read()
{
    if (read_p()) return true;

    if (test_variable_sleep_interval > 0) sleep(test_variable_sleep_interval);

    int64_t array_len = length(); // elements in the array

    switch (var()->type()) {
    // These are the DAP2 types and the classes that implement them all define
    // the old buf2val() and val2buf() methods. For the new DAP4 types see below.
    //case dods_byte_c:
    //case dods_uint8_c:
    case dods_int16_c:
        m_cardinal_type_read_helper<dods_int16, Int16>();
        set_read_p(true);
        break;

    case dods_uint16_c:
        m_cardinal_type_read_helper<dods_uint16, UInt16>();
        set_read_p(true);
        break;

    case dods_int32_c:
        m_cardinal_type_read_helper<dods_int32, Int32>();
        set_read_p(true);
        break;

    case dods_uint32_c:
        m_cardinal_type_read_helper<dods_uint32, UInt32>();
        set_read_p(true);
        break;

    case dods_float32_c:
        m_cardinal_type_read_helper<dods_float32, Float32>();
        set_read_p(true);
        break;

    case dods_float64_c:
        m_cardinal_type_read_helper<dods_float64, Float64>();
        set_read_p(true);
        break;

    case dods_int8_c:
        m_cardinal_type_read_helper<dods_int8, Int8>();
        set_read_p(true);
        break;

    case dods_byte_c:
    case dods_char_c:
    case dods_uint8_c:
        m_cardinal_type_read_helper<dods_byte, Byte>();
        set_read_p(true);
        break;

    case dods_int64_c:
        m_cardinal_type_read_helper<dods_int64, Int64>();
        set_read_p(true);
        break;

    case dods_uint64_c:
        m_cardinal_type_read_helper<dods_uint64, UInt64>();
        set_read_p(true);
        break;

    case dods_enum_c:
        switch (static_cast<D4Enum*>(var())->element_type()) {
        case dods_byte_c:
        case dods_char_c:
        case dods_uint8_c:
            m_enum_type_read_helper<dods_byte>();
            break;
        case dods_int8_c:
            m_enum_type_read_helper<dods_int8>();
            break;
        case dods_int16_c:
            m_enum_type_read_helper<dods_int16>();
            break;
        case dods_uint16_c:
            m_enum_type_read_helper<dods_uint16>();
            break;
        case dods_int32_c:
            m_enum_type_read_helper<dods_int32>();
            break;
        case dods_uint32_c:
            m_enum_type_read_helper<dods_uint32>();
            break;
        case dods_int64_c:
            m_enum_type_read_helper<dods_int64>();
            break;
        case dods_uint64_c:
            m_enum_type_read_helper<dods_uint64>();
            break;
        default:
            throw InternalErr(__FILE__, __LINE__, "Enum with undefined type.");
        }
        set_read_p(true);
        break;

    case dods_str_c:
    case dods_url_c: {
        vector<string> tmp(array_len);

        if (get_series_values()) {
            for (int64_t i = 0; i < array_len; ++i) {
                var()->read();
                // URL isa Str
                tmp[i] = static_cast<Str*>(var())->value();
                var()->set_read_p(false); // pick up the next value
            }
        }
        else {
            var()->read();
            string value = static_cast<Str*>(var())->value();

            for (unsigned i = 0; i < array_len; ++i)
                tmp[i] = value;
        }

        set_value(tmp, array_len);
        set_read_p(true);
        break;
    }

    case dods_opaque_c:
    case dods_structure_c:
        vec_resize(array_len);
        for (unsigned i = 0; i < array_len; ++i) {
            // Copy the prototype and read a value into it
            BaseType *elem = var()->ptr_duplicate();
            elem->read();
            // Load the new value into this object's array
            set_vec_nocopy(i, elem);   // Use set_vec_nocopy() TODO (and below)
        }
        set_read_p(true);
        break;

    case dods_sequence_c:
        // No sequence arrays in DAP2
        if (!is_dap4()) throw InternalErr(__FILE__, __LINE__, "Bad data type");

        vec_resize(array_len);
        for (unsigned i = 0; i < array_len; ++i) {
            // Load the new BaseType (a D4Sequence) into the array element
            set_vec_nocopy(i, var()->ptr_duplicate());
        }

        break;

        // No Grids in DAP4; No arrays of arrays and no null-typed vars in DAP2 or 4
    case dods_grid_c:
    case dods_array_c:
    case dods_null_c:
    default:
        throw InternalErr(__FILE__, __LINE__, "Bad data type");
    }

    // set_read_p(true);

    return true;
}

void TestArray::set_series_values(bool sv)
{
    dynamic_cast<TestCommon&>(*var()).set_series_values(sv);
    d_series_values = sv;
}