variant.hpp 29.3 KB
Newer Older
Victor Yacovlev's avatar
Victor Yacovlev committed
1 2 3
#ifndef VARIANT_HPP
#define VARIANT_HPP

4

Victor Yacovlev's avatar
Victor Yacovlev committed
5 6 7 8 9 10 11 12
#ifdef DO_NOT_DECLARE_STATIC
#define DO_NOT_DECLARE_STATIC_VARIANT
#endif

#define DO_NOT_DECLARE_STATIC
#include "stdlib/kumirstdlib.hpp"
#include "vm_enums.h"

13 14 15 16




Victor Yacovlev's avatar
Victor Yacovlev committed
17 18 19 20 21 22 23
namespace VM {

using Kumir::Char;
using Kumir::String;
using Kumir::real;
using namespace Bytecode;

Victor Yacovlev's avatar
Victor Yacovlev committed
24
typedef std::vector< class AnyValue > Record;
25

Victor Yacovlev's avatar
Victor Yacovlev committed
26 27 28 29 30 31 32 33 34 35 36
class AnyValue
{
    friend class Variable;
public:
    inline explicit AnyValue() { __init__(); }
    inline explicit AnyValue(ValueType t) { __init__(); tp = t;  svalue = t==VT_string? new String() : 0; ivalue = 0; }
    inline explicit AnyValue(int v) { __init__(); tp = VT_int;  ivalue = v; }
    inline explicit AnyValue(real v) { __init__(); tp = VT_real;  rvalue = v; }
    inline explicit AnyValue(bool v) { __init__(); tp = VT_bool; bvalue = v; }
    inline explicit AnyValue(Char v) { __init__(); tp = VT_char; cvalue = v; }
    inline explicit AnyValue(const String & v) { tp = VT_string; svalue = new String(v); }
Victor Yacovlev's avatar
Victor Yacovlev committed
37
    inline explicit AnyValue(const Record & value) { __init__(); tp = VT_record; uvalue = value; }
Victor Yacovlev's avatar
Victor Yacovlev committed
38 39 40 41 42 43 44

    inline void operator=(ValueType t) { __init__(); tp = t;  svalue = t==VT_string? new String() : 0; }
    inline void operator=(int v) { __init__(); tp = VT_int;  ivalue = v; }
    inline void operator=(real v) { __init__(); tp = VT_real; rvalue = v; }
    inline void operator=(bool v) { __init__(); tp = VT_bool; bvalue = v; }
    inline void operator=(Char v) { __init__(); tp = VT_char; cvalue = v; }
    inline void operator=(const String & v) { __init__(); tp = VT_string; svalue = new String(v); }
Victor Yacovlev's avatar
Victor Yacovlev committed
45
    inline void operator=(const Record & value) {
46
        __init__();
Victor Yacovlev's avatar
Victor Yacovlev committed
47
        tp = VT_record;
48 49
        uvalue = value;
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
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

    inline int toInt() const {
        if (tp==VT_bool) return bvalue? 1 : 0;
        else if (tp==VT_char) return static_cast<int>(cvalue);
        else return ivalue;
    }
    inline real toReal() const {
        if (tp==VT_bool || tp==VT_int) return static_cast<real>(toInt());
        else return rvalue;
    }
    inline bool toBool() const {
        if (tp==VT_int) return ivalue!=0;
        else if (tp==VT_real) return rvalue!=0.0;
        else return bvalue;
    }
    inline Char toChar() const {
        if (tp==VT_int) return static_cast<Char>(ivalue);
        else if (tp==VT_string && svalue && svalue->length()==1) return svalue->at(0);
        else return cvalue;
    }
    inline String toString() const {
        if (tp==VT_int) return Kumir::Converter::sprintfInt(ivalue, 10, 0, 0);
        else if (tp==VT_real) return Kumir::Converter::sprintfReal(rvalue, '.', false, 0, 0, 0);
        else if (tp==VT_bool) return bvalue? Kumir::Core::fromUtf8("да") : Kumir::Core::fromUtf8("нет");
        else if (tp==VT_char) {
            String sval;
            sval.push_back(cvalue);
            return sval;
        }
        else if (tp==VT_void) return String();
        else return *svalue;
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
82 83 84 85 86
    inline const Record & toRecord() const {
        return uvalue;
    }
    inline Record & toRecord() {
        return uvalue;
87
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
88

Victor Yacovlev's avatar
Victor Yacovlev committed
89 90 91 92 93 94 95 96 97 98 99 100 101 102
    template <typename T>
    inline T toUserValue() const {
        size_t fields = strlen(T::_());
        T value;
        void * ptr = &value;
        for (size_t i=0; i<fields; i++) {
            void * fieldptr = 0;
            size_t copySize = 0;
            char tp = T::_()[i];
            switch (tp) {
            case 'i': {
                int val = uvalue.at(i).toInt();
                fieldptr = &val;
                copySize = sizeof(int);
Denis Khachko's avatar
Denis Khachko committed
103 104
                ptr = (void*)((size_t)(ptr)+copySize);
             //   ptr +=(uint)copySize;
Victor Yacovlev's avatar
Victor Yacovlev committed
105 106 107 108 109 110
                memcpy(ptr, fieldptr, copySize);
                break;
            }
            case 'd': {
                double val = uvalue.at(i).toReal();
                fieldptr = &val;
Denis Khachko's avatar
Denis Khachko committed
111 112
                copySize = sizeof(double);
                ptr = (void*)((size_t)(ptr)+copySize);
Victor Yacovlev's avatar
Victor Yacovlev committed
113 114 115 116 117 118
                memcpy(ptr, fieldptr, copySize);
                break;
            }
            case 'b': {
                bool val = uvalue.at(i).toBool();
                fieldptr = &val;
Denis Khachko's avatar
Denis Khachko committed
119 120
                copySize = sizeof(bool);
                ptr = (void*)((size_t)(ptr)+copySize);
Victor Yacovlev's avatar
Victor Yacovlev committed
121 122 123 124 125 126 127
                memcpy(ptr, fieldptr, copySize);
                break;
            }
            case 'c': {
                Char val = uvalue.at(i).toChar();
                fieldptr = &val;
                copySize = sizeof(Char);
Denis Khachko's avatar
Denis Khachko committed
128 129

                ptr = (void*)((size_t)(ptr)+copySize);
Victor Yacovlev's avatar
Victor Yacovlev committed
130 131 132 133 134
                break;
            }
            case 's': {
                String val = uvalue.at(i).toString();
                fieldptr = &val;
Denis Khachko's avatar
Denis Khachko committed
135 136
                copySize = sizeof(String);
                ptr = (void*)((size_t)(ptr)+copySize);
Victor Yacovlev's avatar
Victor Yacovlev committed
137 138 139 140 141 142 143 144 145 146 147
                String * stringRef = reinterpret_cast<String*>(fieldptr);
                stringRef->assign(val); // copy container data
                break;
            }
            default:
                break;
            }
        }
        return value;
    }

Victor Yacovlev's avatar
Victor Yacovlev committed
148
    inline bool isValid() const { return tp!=VT_void || avalue.size()>0; }
Victor Yacovlev's avatar
Victor Yacovlev committed
149 150 151 152 153 154 155

    inline ValueType type() const { return tp; }
    inline const AnyValue & at(size_t index) const { return avalue[index]; }
    inline const AnyValue & operator[](size_t index) const { return at(index); }
    inline AnyValue & at(size_t index) { return avalue[index]; }
    inline AnyValue & operator[](size_t index) { return at(index); }

Victor Yacovlev's avatar
Victor Yacovlev committed
156
    inline size_t rawSize() const { return avalue.size(); }
Victor Yacovlev's avatar
Victor Yacovlev committed
157 158 159 160 161 162


protected:

    inline void resize(size_t size) {
        if (size==0) {
Victor Yacovlev's avatar
Victor Yacovlev committed
163 164
            if (avalue.size())
                avalue.clear();
Victor Yacovlev's avatar
Victor Yacovlev committed
165 166
        }
        else {
Victor Yacovlev's avatar
Victor Yacovlev committed
167 168 169
            if (size != avalue.size()) {
                size_t asize = avalue.size();
                avalue.resize(size);
Victor Yacovlev's avatar
Victor Yacovlev committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
            }
        }
    }

private:
    inline void __init__() {
        tp = VT_void;
        svalue = 0;
        ivalue = 0;
    }

    ValueType tp;
    union {
        int ivalue;
        real rvalue;
        Char cvalue;
        bool bvalue;
    };
Victor Yacovlev's avatar
Victor Yacovlev committed
188
    Record uvalue;
Victor Yacovlev's avatar
Victor Yacovlev committed
189
    String * svalue;
Victor Yacovlev's avatar
Victor Yacovlev committed
190
    std::vector<class AnyValue> avalue;
Victor Yacovlev's avatar
Victor Yacovlev committed
191 192 193
};


194

Victor Yacovlev's avatar
Victor Yacovlev committed
195 196 197 198 199 200 201 202
class Variable
{
public:

    inline explicit Variable() { create(); }

    inline void create()
    {
Victor Yacovlev's avatar
Victor Yacovlev committed
203 204 205
        l_referenceIndeces[0] = l_referenceIndeces[1] = l_referenceIndeces[2] =         l_referenceIndeces[3] = 0;
        l_bounds[0] = l_bounds[1] = l_bounds[2] = l_bounds[3] = l_bounds[4] = l_bounds[5] = l_bounds[6] = 0;
        l_restrictedBounds[0] = l_restrictedBounds[1] = l_restrictedBounds[2] = l_restrictedBounds[3] = l_restrictedBounds[4] = l_restrictedBounds[5] = l_restrictedBounds[6] = 0;
Victor Yacovlev's avatar
Victor Yacovlev committed
206 207 208 209 210 211 212 213 214 215 216 217 218 219
        m_value = VT_void;
        i_dimension = 0;
        e_baseType = VT_void;
        m_reference = 0;
        b_constant = false;
    }

    static bool ignoreUndefinedError;

    inline explicit Variable(int v) { create() ; e_baseType = VT_int; m_value = v; }
    inline explicit Variable(double v) { create(); e_baseType = VT_real; m_value = v; }
    inline explicit Variable(Char & v) { create(); e_baseType = VT_char; m_value = v; }
    inline explicit Variable(const String & v) { create(); e_baseType = VT_string; m_value = v; }
    inline explicit Variable(bool v) { create(); e_baseType = VT_bool; m_value = v; }
Victor Yacovlev's avatar
Victor Yacovlev committed
220
    inline explicit Variable(const Record & value) {
221
        create();
Victor Yacovlev's avatar
Victor Yacovlev committed
222
        e_baseType = VT_record;
223 224
        m_value = value;
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
225 226 227 228 229 230 231 232 233
    inline explicit Variable(Variable * ref) { create(); m_reference = ref; }
    inline explicit Variable(const AnyValue & v) { create(); e_baseType = v.type(); m_value = v; }

    inline bool isValid() const { return e_baseType!=VT_void; }

    inline bool isConstant() const { return m_reference? m_reference->isConstant() : b_constant; }
    inline void setConstantFlag(bool value) { b_constant = value; }

    inline void init();
Victor Yacovlev's avatar
Victor Yacovlev committed
234
    inline uint8_t dimension() const { return m_reference? m_reference->dimension() : i_dimension; }
Victor Yacovlev's avatar
Victor Yacovlev committed
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
    inline void setDimension(uint8_t v) { i_dimension = v; }
    inline void setName(const String & n) {
        s_name = n;
    }
    inline String fullReferenceName() const;
    inline const String & name() const {
        if(m_reference)
            return m_reference->name();
        else
            return s_name;
    }
    inline const String & myName() const {
        return s_name;
    }
    inline void setAlgorhitmName(const String & n) {
        s_algorhitmName = n;
    }
    inline const String & algorhitmName() const {
        if(m_reference)
            return m_reference->algorhitmName();
        else
            return s_algorhitmName;
    }

    inline void setBounds(int bounds[7]);
    inline void updateBounds(int bounds[7]);
    inline void getBounds(/*out*/ int bounds[7]) const { memcpy(bounds, l_bounds, 7*sizeof(int)); }
    inline void getEffectiveBounds(/*out*/ int bounds[7]) const { memcpy(bounds, l_restrictedBounds, 7*sizeof(int)); }

    inline bool hasValue() const;
    inline bool hasValue(int indeces[4]) const;
    inline bool hasValue(int index0) const;
    inline bool hasValue(int index0, int index1) const;
    inline bool hasValue(int index0, int index1, int index2) const;

    inline AnyValue value() const;
    inline AnyValue value(int index0) const;
    inline AnyValue value(int index0, int index1) const;
    inline AnyValue value(int index0, int index1, int index2) const;

    inline AnyValue value(int indeces[4]) const;

    inline size_t rawSize() const { return m_value.rawSize(); }
    inline const AnyValue & at(size_t index) const { return m_value.avalue[index]; }
Victor Yacovlev's avatar
Victor Yacovlev committed
279
    inline const AnyValue & operator[](size_t index) const { return at(index); }
Victor Yacovlev's avatar
Victor Yacovlev committed
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
    inline AnyValue & at(size_t index) { return m_value.avalue[index]; }
    inline AnyValue & operator[](size_t index) { return at(index); }

    inline bool isReference() const { return m_reference!=0; }
    inline void setReference(Variable * r, int effectiveBounds[7]) {
        m_reference = r;
        memcpy(l_bounds, effectiveBounds, 7*sizeof(int));
        memcpy(l_restrictedBounds, effectiveBounds, 7*sizeof(int));
    }
    inline Variable * reference() { return m_reference; }

    inline void setReferenceIndeces(int v[4]) {
        memcpy(l_referenceIndeces, v, 4*sizeof(int));
    }
    inline void getReferenceIndeces(int result[4]) const {
        memcpy(result, l_referenceIndeces, 4*sizeof(int));
    }

    inline int toInt() const { return value().toInt(); }
    inline real toReal() const { return value().toReal(); }
    inline double toDouble() const { return toReal(); }
    inline bool toBool() const { return value().toBool(); }
    inline Char toChar() const { return value().toChar(); }
    inline String toString() const;
    inline String toString(int indeces[4]) const;
305 306
    inline const Record toRecord() const { return value().toRecord(); }
    inline Record & toRecord() { return value().toRecord(); }
Victor Yacovlev's avatar
Victor Yacovlev committed
307

Victor Yacovlev's avatar
Victor Yacovlev committed
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    inline Variable toReference();
    inline static Variable toConstReference(const AnyValue & value);
    inline Variable toReference(int indeces[4]);
    inline void setValue(const AnyValue & value);
    inline void setValue(int index0, const AnyValue & value);
    inline void setValue(int index0, int index1, const AnyValue & value);
    inline void setValue(int index0, int index1, int index2, const AnyValue & value);
    inline void setValue(int indeces[4], const AnyValue & value);

    inline void setConstValue(const Variable & ctab);

    inline ValueType baseType() const { return m_reference? m_reference->baseType() : e_baseType; }
    inline void setBaseType(ValueType vt) { e_baseType = vt; }

    inline static void unsetError() { Kumir::Core::unsetError(); }
private:
    inline size_t linearIndex(int a) const;
    inline size_t linearIndex(int a, int b) const;
    inline size_t linearIndex(int a, int b, int c) const;
    AnyValue m_value;
Victor Yacovlev's avatar
Victor Yacovlev committed
328 329 330 331 332 333
    uint8_t i_dimension;
    int l_bounds[7];
    int l_restrictedBounds[7];
    ValueType e_baseType;
    Variable * m_reference;
    int l_referenceIndeces[4];
Victor Yacovlev's avatar
Victor Yacovlev committed
334 335
    String s_name;
    String s_algorhitmName;
Victor Yacovlev's avatar
Victor Yacovlev committed
336
    bool b_constant;
Victor Yacovlev's avatar
Victor Yacovlev committed
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
};

/* ----------------------- IMPLEMENTATION ----------------------*/

bool Variable::hasValue() const
{
    if (m_reference) {
        if (l_referenceIndeces[3]==0)
            return m_reference->hasValue();
        else if (l_referenceIndeces[3]==1)
            return m_reference->hasValue(l_referenceIndeces[0]);
        else if (l_referenceIndeces[3]==2)
            return m_reference->hasValue(l_referenceIndeces[0], l_referenceIndeces[1]);
        else
            return m_reference->hasValue(l_referenceIndeces[0], l_referenceIndeces[1], l_referenceIndeces[2]);
    }
    else
        return m_value.isValid();
}


bool Variable::hasValue(int indeces[4]) const
{
    if (indeces[3]==1)
        return hasValue(indeces[0]);
    else if (indeces[3]==2)
        return hasValue(indeces[0], indeces[1]);
    else if (indeces[3]==3)
        return hasValue(indeces[0], indeces[1], indeces[2]);
    else
        return hasValue();
}

AnyValue Variable::value() const
{
    if (m_reference) {
        if (l_referenceIndeces[3]==0) {
            return m_reference->value();
        }
        else if (l_referenceIndeces[3]==1) {
            return m_reference->value(l_referenceIndeces[0]);
        }
        else if (l_referenceIndeces[3]==2) {
            return m_reference->value(l_referenceIndeces[0], l_referenceIndeces[1]);
        }
        else if (l_referenceIndeces[3]==3) {
            return m_reference->value(l_referenceIndeces[0], l_referenceIndeces[1], l_referenceIndeces[2]);
        }
    }
    else {
        if (!m_value.isValid() && !ignoreUndefinedError)
            Kumir::Core::abort(Kumir::Core::fromUtf8("Нет значения у величины"));
        return m_value;
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
391
    return m_value;
Victor Yacovlev's avatar
Victor Yacovlev committed
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
}

void Variable::setValue(const AnyValue &v)
{
    if (m_reference) {
        if (l_referenceIndeces[3]==0) {
            m_reference->setValue(v);
        }
        else if (l_referenceIndeces[3]==1) {
            m_reference->setValue(l_referenceIndeces[0], v);
        }
        else if (l_referenceIndeces[3]==2) {
            m_reference->setValue(l_referenceIndeces[0],l_referenceIndeces[1], v);
        }
        else if (l_referenceIndeces[3]==3) {
            m_reference->setValue(l_referenceIndeces[0],l_referenceIndeces[1],l_referenceIndeces[2], v);
        }

    }
    else {
        m_value = v;
    }

}




String Variable::toString() const
{
    String result;
    switch (e_baseType)
    {
    case VT_bool:
        if (value().toBool())
            result = Kumir::Core::fromUtf8("да");
        else
            result = Kumir::Core::fromUtf8("нет");
        break;
    case VT_real:
        result = Kumir::Converter::sprintfReal(value().toReal(), '.', false, 0,0,0);
        break;
    case VT_int:
        result = Kumir::Converter::sprintfInt(value().toInt(), 10, 0, 0);
        break;
    case VT_char:
        result.push_back(value().toChar());
        break;
    case VT_string:
        result = value().toString();
        break;
    default:
        break;
    }
    return result;
}

String Variable::toString(int indeces[4]) const
{
    String result;
    switch (e_baseType)
    {
    case VT_bool:
        if (value(indeces).toBool())
            result = Kumir::Core::fromUtf8("да");
        else
            result = Kumir::Core::fromUtf8("нет");
        break;
    case VT_real:
        result = Kumir::Converter::sprintfReal(value(indeces).toReal(), '.', false, 0,0,0);
        break;
    case VT_int:
        result = Kumir::Converter::sprintfInt(value(indeces).toInt(), 10, 0, 0);
        break;
    case VT_char:
        result.push_back(value(indeces).toChar());
        break;
    case VT_string:
        result = value(indeces).toString();
        break;
    default:
        break;
    }
    return result;
}

AnyValue Variable::value(int indeces[4]) const
{
    if (indeces[3]==1)
        return value(indeces[0]);
    else if (indeces[3]==2)
        return value(indeces[0], indeces[1]);
    else if (indeces[3]==3)
        return value(indeces[0], indeces[1], indeces[2]);
    else
        return value();
}

void Variable::setValue(int indeces[4], const AnyValue &value)
{
    if (indeces[3]==1)
        setValue(indeces[0], value);
    else if (indeces[3]==2)
        setValue(indeces[0], indeces[1], value);
    else if (indeces[3]==3)
        setValue(indeces[0], indeces[1], indeces[2], value);
    else
        setValue(value);
}


size_t Variable::linearIndex(int a) const
{
    return a-l_bounds[0];
}

size_t Variable::linearIndex(int a, int b) const
{
    int size0 = l_bounds[3]-l_bounds[2]+1;
    int offset0 = (a - l_bounds[0]) * size0;
    int result = offset0 + b-l_bounds[2];
    return result;
}

size_t Variable::linearIndex(int a, int b, int c) const
{
    int size0 = l_bounds[3]-l_bounds[2]+1;
    int size1 = l_bounds[5]-l_bounds[4]+1;
    return (a-l_bounds[0])*size0*size1 + (b-l_bounds[2])*size1 + c-l_bounds[4];
}


void Variable::setConstValue(const Variable & ctab)
{
    if (isReference()) {
        m_reference->setConstValue(ctab);
        return;
    }
    const int dim = ctab.dimension();
    int cbounds[7];
    if (dim>0) {
        ctab.getBounds(cbounds);
        for (int i=0; i<dim; i++) {
            const int mysize = l_bounds[2*i+1] - l_bounds[2*i];
            const int csize  = cbounds [2*i+1] - cbounds [2*i];
            if (mysize < csize) {
                Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
                return;
            }
        }
    }
    switch (dim)
    {
    case 0: {
        setValue(ctab.value());
        break;
    }
    case 1: {
        const int cx = cbounds [0];
        const int mx = l_bounds[0];
        const int sx = cbounds [1] - cbounds [0];
        for (int x=0; x<=sx; x++) {
            setValue(mx+x, ctab.value(cx+x));
        }
        break;
    }
    case 2: {
        const int cy = cbounds [0];
        const int my = l_bounds[0];
        const int cx = cbounds [2];
        const int mx = l_bounds[2];
        const int sy = cbounds [1] - cbounds [0];
        const int sx = cbounds [3] - cbounds [2];
        for (int y=0; y<=sy; y++) {
            for (int x=0; x<=sx; x++) {
                setValue(my+y, mx+x, ctab.value(cy+y, cx+x));
568
                unsetError();
Victor Yacovlev's avatar
Victor Yacovlev committed
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
            }
        }
        break;
    }
    case 3: {
        const int cz = cbounds [0];
        const int mz = l_bounds[0];
        const int cy = cbounds [2];
        const int my = l_bounds[2];
        const int cx = cbounds [4];
        const int mx = l_bounds[4];
        const int sz = cbounds [1] - cbounds [0];
        const int sy = cbounds [3] - cbounds [2];
        const int sx = cbounds [5] - cbounds [4];
        for (int z=0; z<sz; z++) {
            for (int y=0; y<=sy; y++) {
                for (int x=0; x<=sx; x++) {
                    setValue(mz+z, my+y, mx+x, ctab.value(cz+z, cy+y, cx+x));
                }
            }
        }
        break;
        }
        default: {
        break;
    }
    }
}

// DIM = 1

bool Variable::hasValue(int index0) const
{
    if (m_reference)
        return m_reference->hasValue(index0);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<1) {
        return false;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1]) {
        return false;
    }
    int index = linearIndex(index0);
    return m_value.isValid() && m_value[index].isValid();
}

AnyValue Variable::value(int index0) const
{
    if (m_reference)
        return m_reference->value(index0);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<1) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return AnyValue(VT_void);
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return AnyValue(VT_void);
    }
    int index = linearIndex(index0);
    if (m_value[index].type()==VT_void) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Значение элемента таблицы не определено"));
        return AnyValue(VT_void);
    }
    return m_value[index];
}

void Variable::setValue(int index0, const AnyValue &value)
{
    if (!m_reference && (m_value.rawSize()==0 || l_restrictedBounds[6]<1)) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return ;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return ;
    }
    if (m_reference) {
        m_reference->setValue(index0, value);
        return;
    }
    size_t index = linearIndex(index0);
    m_value[index] = value;
}



// DIM = 2

bool Variable::hasValue(int index0, int index1) const
{
    if (m_reference)
        return m_reference->hasValue(index0, index1);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<2) {
        return false;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2]|| index1>l_restrictedBounds[3]) {
        return false;
    }
    size_t index = linearIndex(index0, index1);
    return m_value.isValid() && m_value[index].isValid();
}

AnyValue Variable::value(int index0, int index1) const
{
    if (m_reference)
        return m_reference->value(index0, index1);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<2) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return AnyValue(VT_void);
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2] || index1>l_restrictedBounds[3]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return AnyValue(VT_void);
    }
    size_t index = linearIndex(index0, index1);
    if (m_value[index].type()==VT_void) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Значение элемента таблицы не определено"));
        return AnyValue(VT_void);
    }
    return m_value[index];
}

void Variable::setValue(int index0, int index1, const AnyValue &value)
{
    if (!m_reference && (m_value.rawSize()==0 || l_restrictedBounds[6]<2)) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return ;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2] || index1>l_restrictedBounds[3]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return ;
    }
    if (m_reference) {
        m_reference->setValue(index0, index1, value);
        return;
    }
    size_t index = linearIndex(index0, index1);
    m_value[index] = value;
}


// DIM = 3

bool Variable::hasValue(int index0, int index1, int index2) const
{
    if (m_reference)
        return m_reference->hasValue(index0, index1, index2);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<3) {
        return false;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2]|| index1>l_restrictedBounds[3]|| index2<l_restrictedBounds[4] || index2>l_restrictedBounds[5]) {
        return false;
    }
    size_t index = linearIndex(index0, index1, index2);
    return m_value.isValid() && m_value[index].isValid();
}

AnyValue Variable::value(int index0, int index1, int index2) const
{
    if (m_reference)
        return m_reference->value(index0, index1, index2);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<3) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return AnyValue(VT_void);
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2] || index1>l_restrictedBounds[3]|| index2<l_restrictedBounds[4] || index2>l_restrictedBounds[5]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return AnyValue(VT_void);
    }
    size_t index = linearIndex(index0, index1, index2);
    if (m_value[index].type()==VT_void) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Значение элемента таблицы не определено"));
        return AnyValue(VT_void);
    }
    return m_value[index];
}

void Variable::setValue(int index0, int index1, int index2, const AnyValue &value)
{
    if (!m_reference && (m_value.rawSize()==0 || l_restrictedBounds[6]<3)) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return ;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2] || index1>l_restrictedBounds[3]|| index2<l_restrictedBounds[4] || index2>l_restrictedBounds[5]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return ;
    }
    if (m_reference) {
        m_reference->setValue(index0, index1, index2, value);
        return;
    }
    size_t index = linearIndex(index0, index1, index2);
    m_value[index] = value;
}

void Variable::init()
{
    if (m_reference) {
        m_reference->init();
        return;
    }
    if (i_dimension==0) {
        m_value = VT_void;
    }
    else {
        if (i_dimension==1) {
            for (int x=l_restrictedBounds[0]; x<=l_restrictedBounds[1]; x++) {
                size_t index = linearIndex(x);
                m_value[index] = VT_void;
            }
        }
        else if (i_dimension==2) {
            for (int y=l_restrictedBounds[0]; y<=l_restrictedBounds[1]; y++) {
                for (int x=l_restrictedBounds[2]; x<=l_restrictedBounds[3]; x++) {
                    size_t index = linearIndex(y, x);
                    m_value[index] = VT_void;
                }
            }
        }
        else if (i_dimension==3) {
            for (int z=l_restrictedBounds[0]; z<=l_restrictedBounds[1]; z++) {
                for (int y=l_restrictedBounds[2]; y<=l_restrictedBounds[3]; y++) {
                    for (int x=l_restrictedBounds[4]; x<=l_restrictedBounds[5]; x++) {
                        size_t index = linearIndex(z, y, x);
                        m_value[index] = VT_void;
                    }
                }
            }
        }

    }
}

void Variable::setBounds(int bounds[7])
{
    size_t size = 0;
    i_dimension = bounds[6]/2;
    if (i_dimension>=1) {
        size = bounds[1]-bounds[0]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }
    if (i_dimension>=2) {
        size *= bounds[3]-bounds[2]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }
    if (i_dimension>=3) {
        size *= bounds[5]-bounds[4]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }

    m_value.resize(size);

    memcpy(l_bounds, bounds, 7*sizeof(int));
    memcpy(l_restrictedBounds, l_bounds, 7*sizeof(int));
}

void Variable::updateBounds(int bounds[7])
{
    size_t size = 0;
    i_dimension = bounds[6]/2;
    if (i_dimension>=1) {
        size = bounds[1]-bounds[0]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }
    if (i_dimension>=2) {
        size *= bounds[3]-bounds[2]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }
    if (i_dimension>=3) {
        size *= bounds[5]-bounds[4]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }

    l_restrictedBounds[6] = bounds[6];

    for (int i=0; i<bounds[6]; i+=2) {
        l_restrictedBounds[i] = l_bounds[6]
Victor Yacovlev's avatar
Victor Yacovlev committed
863
                ? Kumir::Math::imax(l_bounds[i], bounds[i])
Victor Yacovlev's avatar
Victor Yacovlev committed
864 865 866 867
                : bounds[i];
    }
    for (int i=1; i<bounds[6]; i+=2) {
        l_restrictedBounds[i] = l_bounds[6]
Victor Yacovlev's avatar
Victor Yacovlev committed
868
                ? Kumir::Math::imin(l_bounds[i], bounds[i])
Victor Yacovlev's avatar
Victor Yacovlev committed
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
                : bounds[i];
    }

}

Variable Variable::toReference()
{
    Variable result;
    if (m_reference) {
        result.m_reference = m_reference;
    }
    else {
        result.m_reference = this;
    }
    memcpy(result.l_bounds, l_restrictedBounds, 7*sizeof(int));
    memcpy(result.l_restrictedBounds, l_restrictedBounds, 7*sizeof(int));
    return result;
}



Variable Variable::toReference(int indeces[4])
{
    Variable result;
    if (m_reference) {
        result.m_reference = m_reference;
    }
    else {
        result.m_reference = this;
    }
    memcpy(result.l_referenceIndeces, indeces, 4*sizeof(int));
    return result;
}

String Variable::fullReferenceName() const
{
    if (m_reference) {
        String result = name();
        if (l_referenceIndeces[3]) {
            result.push_back('[');
            for (size_t i=0; i<l_referenceIndeces[3]; i++) {
                result += Kumir::Converter::sprintfInt(l_referenceIndeces[i],10,0,0);
                if (i<l_referenceIndeces[3]-1)
                    result.push_back(',');
            }
            result.push_back(']');
        }
        return result;
    }
    else {
        return name();
    }
}

#ifndef DO_NOT_DECLARE_STATIC_VARIANT
bool Variable::ignoreUndefinedError = false;
#endif

}


#endif