Annotation of parser3/src/types/pa_varray.h, revision 1.17

1.1       moko        1: /** @file
                      2:        Parser: @b array parser type decl.
                      3: 
                      4:        Copyright (c) 2001-2023 Art. Lebedev Studio (http://www.artlebedev.com)
                      5:        Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
                      6: */
                      7: 
                      8: #ifndef PA_VARRAY_H
                      9: #define PA_VARRAY_H
                     10: 
1.17    ! moko       11: #define IDENT_PA_VARRAY_H "$Id: pa_varray.h,v 1.16 2024/10/26 15:46:52 moko Exp $"
1.1       moko       12: 
                     13: #include "classes.h"
                     14: #include "pa_value.h"
                     15: #include "pa_array.h"
                     16: #include "pa_vhash.h"
                     17: #include "pa_vint.h"
                     18: #include "pa_globals.h"
                     19: #include "pa_symbols.h"
                     20: 
                     21: // defines
                     22: 
1.2       moko       23: #define VARRAY_TYPE "array"
1.15      moko       24: //#define DEBUG_ARRAY_USED
1.1       moko       25: 
                     26: extern Methoded* array_class;
                     27: 
1.2       moko       28: /// Sparse Array
                     29: template<typename T> class SparseArray: public Array<T> {
1.5       moko       30: 
                     31:        mutable size_t fused;
                     32: 
1.2       moko       33: public:
1.5       moko       34:        inline SparseArray(size_t initial=0) : Array<T>(initial), fused(0) {}
1.2       moko       35: 
1.6       moko       36:        inline SparseArray(size_t size, T* elements) : Array<T>(size), fused(size) {
1.16      moko       37:                memcpy(this->felements, elements, size * sizeof(T));
1.6       moko       38:                this->fsize=size;
                     39:        }
                     40: 
1.17    ! moko       41:        // note: 'append' and 'operator+=' are used directly from Array.
        !            42: 
        !            43:        // not virtual, thus Array::append does not use it, but that's OK.
        !            44:        void fit(size_t index);
        !            45: 
1.4       moko       46:        inline T get(size_t index) const {
                     47:                return index < this->count() ? this->felements[index] : NULL;
                     48:        }
                     49: 
1.5       moko       50:        inline void put(size_t index, T element){
                     51:                this->fit(index);
1.2       moko       52:                this->felements[index]=element;
1.5       moko       53:                if(index >= this->fsize){
                     54:                        this->fsize=index+1;
                     55:                }
                     56:        }
                     57: 
1.10      moko       58:        inline bool put_dont_replace(size_t index, T element){
                     59:                this->fit(index);
                     60:                if(this->felements[index])
                     61:                        return true;
                     62:                this->felements[index]=element;
                     63:                if(index >= this->fsize){
                     64:                        this->fsize=index+1;
                     65:                }
                     66:                return false;
                     67:        }
                     68: 
1.5       moko       69:        inline void insert(size_t index, T element) {
                     70:                if(index >= this->fsize){
                     71:                        this->fit(index);
                     72:                        this->felements[index]=element;
                     73:                        this->fsize=index+1;
                     74:                } else {
                     75:                        Array<T>::insert(index, element);
                     76:                }
                     77:        }
                     78: 
                     79:        size_t used() const{
1.15      moko       80: #ifndef DEBUG_ARRAY_USED
                     81:                if(!fused)
                     82: #endif
                     83:                {
                     84:                        size_t used=0;
1.5       moko       85:                        for(Array_iterator<T> i(*this); i;) {
                     86:                                if(i.next())
1.15      moko       87:                                        used++;
1.5       moko       88:                        }
1.15      moko       89: #ifdef DEBUG_ARRAY_USED
                     90:                        if(fused && fused!=used)
                     91:                                throw Exception(PARSER_RUNTIME, 0, "cached elements count %d differs from actual %d", fused, used);
                     92: #endif
                     93:                        fused=used;
1.5       moko       94:                }
                     95:                return fused;
                     96:        }
                     97: 
                     98:        inline void clear(size_t index) {
1.7       moko       99:                if(index < this->count()){
1.5       moko      100:                        this->felements[index]=NULL;
1.14      moko      101:                        if(index+1 == this->fsize){
1.7       moko      102:                                this->fsize--;
1.14      moko      103:                        }
1.2       moko      104:                }
                    105:        }
1.5       moko      106: 
                    107:        inline void clear() { Array<T>::clear(); }
                    108: 
1.8       moko      109:        inline void remove(size_t index) {
                    110:                if(index < this->count()){
                    111:                        Array<T>::remove(index);
                    112:                }
                    113:        }
                    114: 
1.5       moko      115:        inline void invalidate(){
                    116:                fused=0;
                    117:        }
                    118: 
1.11      moko      119:        inline void confirm_all_used(){
                    120:                fused=this->count();
                    121:        }
                    122: 
1.15      moko      123:        void compact(bool compact_undef){
1.13      moko      124:                T* dst=this->felements;
                    125:                T* elements_end=dst + this->fsize;
                    126: 
1.15      moko      127:                if(compact_undef){
                    128:                        for(T* src=this->felements; src < elements_end; src++)
                    129:                                if(*src && (*src)->is_defined())
                    130:                                        *dst++=*src;
                    131:                } else {
                    132:                        for(T* src=this->felements; src < elements_end; src++)
                    133:                                if(*src)
                    134:                                        *dst++=*src;
                    135:                }
                    136: 
                    137:                this->fsize=dst-this->felements;
1.13      moko      138:        }
                    139: 
1.2       moko      140: };
                    141: 
                    142: 
1.1       moko      143: class VArray: public VHashBase {
                    144: public: // value
                    145: 
                    146:        override const char* type() const { return VARRAY_TYPE; }
                    147:        override VStateless_class *get_class() { return array_class; }
                    148: 
1.5       moko      149:        /// VArray: used elements count
                    150:        override int as_int() const { return farray.used(); }
                    151:        override double as_double() const { return farray.used(); }
                    152:        override bool is_defined() const { return farray.used()!=0; }
                    153:        override bool as_bool() const { return farray.used()!=0; }
                    154:        override Value& as_expr_result() { return *new VInt(farray.used()); }
1.1       moko      155: 
                    156:        /// VArray: virtual hash
                    157:        override HashStringValue *get_hash() { return &hash(); }
                    158:        override HashStringValue* get_fields() { return &hash(); }
                    159:        override HashStringValue* get_fields_reference() { return &hash(); }
                    160: 
                    161:        /// VArray: json-string
                    162:        override const String* get_json_string(Json_options& options);
                    163: 
                    164:        /// VArray: (key)=value
                    165:        override Value* get_element(const String& aname) {
                    166:                // $element first
1.4       moko      167:                if(Value* result=farray.get(index(aname)))
1.1       moko      168:                        return result;
                    169: 
                    170:                // $fields -- pseudo field to make 'hash' and 'array' more like 'table'
                    171:                if(SYMBOLS_EQ(aname,FIELDS_SYMBOL))
                    172:                        return this;
                    173: 
                    174: #if !defined(FEATURE_GET_ELEMENT4CALL) || !defined(OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL)
                    175:                // $method, CLASS, CLASS_NAME
                    176:                if(Value* result=VStateless_object::get_element(aname))
                    177:                        return result;
                    178: #endif
                    179:                return NULL;
                    180:        }
                    181: 
                    182: #ifdef FEATURE_GET_ELEMENT4CALL
                    183:        override Value* get_element4call(const String& aname) {
                    184:                // $method first
                    185:                if(Value* result=VStateless_object::get_element(aname))
                    186:                        return result;
                    187: 
                    188:                // $element
1.12      moko      189:                if(is_index(aname))
                    190:                        if(Value* result=farray.get(index(aname)))
                    191:                                return result;
1.1       moko      192: 
                    193:                return bark("%s method not found", &aname);
                    194:        }
                    195: #endif
                    196: 
                    197:        /// VArray: (key)=value
                    198:        override const VJunction* put_element(const String& aname, Value* avalue) {
1.5       moko      199:                farray.put(index(aname), avalue);
1.14      moko      200:                farray.invalidate();
1.1       moko      201:                return 0;
                    202:        }
                    203: 
                    204: public: // VHashBase
                    205: 
                    206:        override HashStringValue& hash();
                    207:        override void set_default(Value*) { }
                    208:        override Value* get_default() { return 0; }
1.5       moko      209:        override void add(Value* avalue) { farray+=avalue; /* only json uses it, thus no need to invalidate()*/ }
1.1       moko      210: 
                    211: public: // usage
                    212: 
1.14      moko      213:        VArray(size_t initial=0): farray(initial) {}
                    214:        VArray(size_t size, Value** elements): farray(size, elements) {}
1.1       moko      215: 
                    216:        ArrayValue &array() { return farray; }
                    217: 
                    218:        static size_t index(int aindex){
                    219:                if(aindex<0)
1.9       moko      220:                        throw Exception("number.format", 0, "index out of range (negative)");
1.1       moko      221:                return aindex;
                    222:        }
                    223: 
1.7       moko      224:        static size_t index(const String::Body& aindex){ return pa_atoui(aindex.cstr()); }
                    225:        static size_t index(const String& aindex){ return pa_atoui(aindex.cstr(), 10, &aindex); }
1.1       moko      226: 
1.12      moko      227:        static bool is_index(const String& aindex){
                    228:                for(const char *pos=aindex.cstr();*pos;pos++){
                    229:                        if ((*pos < '0') || (*pos > '9'))
                    230:                                return false;
                    231:                }
                    232:                return true;
                    233:        }
                    234: 
1.5       moko      235:        bool contains(size_t index){
                    236:                return farray.get(index) != NULL;
1.1       moko      237:        }
                    238: 
                    239: private:
                    240: 
                    241:        ArrayValue farray;
                    242: 
                    243: };
                    244: 
                    245: #endif

E-mail: