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

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.18    ! moko       11: #define IDENT_PA_VARRAY_H "$Id: pa_varray.h,v 1.17 2024/10/26 18:53:37 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 {
1.18    ! moko       47:                return index < this->fsize ? this->felements[index] : NULL;
1.4       moko       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.18    ! moko       99:                if(index < this->fsize){
1.5       moko      100:                        this->felements[index]=NULL;
1.18    ! moko      101:                        if(index == this->fsize-1){
1.7       moko      102:                                this->fsize--;
1.18    ! moko      103:                                locate_last_used();
1.14      moko      104:                        }
1.2       moko      105:                }
                    106:        }
1.5       moko      107: 
                    108:        inline void clear() { Array<T>::clear(); }
                    109: 
1.8       moko      110:        inline void remove(size_t index) {
1.18    ! moko      111:                if(index < this->fsize){
1.8       moko      112:                        Array<T>::remove(index);
1.18    ! moko      113:                        if(index==this->fsize)
        !           114:                                locate_last_used();
1.8       moko      115:                }
                    116:        }
                    117: 
1.18    ! moko      118:        inline T pop() {
        !           119:                if(this->fsize){
        !           120:                        T result=this->felements[this->fsize-1];
        !           121:                        this->fsize--;
        !           122:                        locate_last_used();
        !           123:                        return result;
        !           124:                }
        !           125:                return NULL;
        !           126:        }
        !           127: 
1.5       moko      128:        inline void invalidate(){
                    129:                fused=0;
                    130:        }
                    131: 
1.18    ! moko      132:        inline void locate_last_used(){
        !           133:                for(;this->fsize>0 && !this->felements[this->fsize-1];this->fsize--);
        !           134:        }
        !           135: 
1.11      moko      136:        inline void confirm_all_used(){
1.18    ! moko      137:                fused=this->fsize;
1.11      moko      138:        }
                    139: 
1.15      moko      140:        void compact(bool compact_undef){
1.13      moko      141:                T* dst=this->felements;
                    142:                T* elements_end=dst + this->fsize;
                    143: 
1.15      moko      144:                if(compact_undef){
                    145:                        for(T* src=this->felements; src < elements_end; src++)
                    146:                                if(*src && (*src)->is_defined())
                    147:                                        *dst++=*src;
                    148:                } else {
                    149:                        for(T* src=this->felements; src < elements_end; src++)
                    150:                                if(*src)
                    151:                                        *dst++=*src;
                    152:                }
                    153: 
                    154:                this->fsize=dst-this->felements;
1.13      moko      155:        }
                    156: 
1.2       moko      157: };
                    158: 
                    159: 
1.1       moko      160: class VArray: public VHashBase {
                    161: public: // value
                    162: 
                    163:        override const char* type() const { return VARRAY_TYPE; }
                    164:        override VStateless_class *get_class() { return array_class; }
                    165: 
1.5       moko      166:        /// VArray: used elements count
                    167:        override int as_int() const { return farray.used(); }
                    168:        override double as_double() const { return farray.used(); }
                    169:        override bool is_defined() const { return farray.used()!=0; }
                    170:        override bool as_bool() const { return farray.used()!=0; }
                    171:        override Value& as_expr_result() { return *new VInt(farray.used()); }
1.1       moko      172: 
                    173:        /// VArray: virtual hash
                    174:        override HashStringValue *get_hash() { return &hash(); }
                    175:        override HashStringValue* get_fields() { return &hash(); }
                    176:        override HashStringValue* get_fields_reference() { return &hash(); }
                    177: 
                    178:        /// VArray: json-string
                    179:        override const String* get_json_string(Json_options& options);
                    180: 
                    181:        /// VArray: (key)=value
                    182:        override Value* get_element(const String& aname) {
                    183:                // $element first
1.4       moko      184:                if(Value* result=farray.get(index(aname)))
1.1       moko      185:                        return result;
                    186: 
                    187:                // $fields -- pseudo field to make 'hash' and 'array' more like 'table'
                    188:                if(SYMBOLS_EQ(aname,FIELDS_SYMBOL))
                    189:                        return this;
                    190: 
                    191: #if !defined(FEATURE_GET_ELEMENT4CALL) || !defined(OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL)
                    192:                // $method, CLASS, CLASS_NAME
                    193:                if(Value* result=VStateless_object::get_element(aname))
                    194:                        return result;
                    195: #endif
                    196:                return NULL;
                    197:        }
                    198: 
                    199: #ifdef FEATURE_GET_ELEMENT4CALL
                    200:        override Value* get_element4call(const String& aname) {
                    201:                // $method first
                    202:                if(Value* result=VStateless_object::get_element(aname))
                    203:                        return result;
                    204: 
                    205:                // $element
1.12      moko      206:                if(is_index(aname))
                    207:                        if(Value* result=farray.get(index(aname)))
                    208:                                return result;
1.1       moko      209: 
                    210:                return bark("%s method not found", &aname);
                    211:        }
                    212: #endif
                    213: 
                    214:        /// VArray: (key)=value
                    215:        override const VJunction* put_element(const String& aname, Value* avalue) {
1.5       moko      216:                farray.put(index(aname), avalue);
1.14      moko      217:                farray.invalidate();
1.1       moko      218:                return 0;
                    219:        }
                    220: 
                    221: public: // VHashBase
                    222: 
                    223:        override HashStringValue& hash();
                    224:        override void set_default(Value*) { }
                    225:        override Value* get_default() { return 0; }
1.5       moko      226:        override void add(Value* avalue) { farray+=avalue; /* only json uses it, thus no need to invalidate()*/ }
1.1       moko      227: 
                    228: public: // usage
                    229: 
1.14      moko      230:        VArray(size_t initial=0): farray(initial) {}
                    231:        VArray(size_t size, Value** elements): farray(size, elements) {}
1.1       moko      232: 
                    233:        ArrayValue &array() { return farray; }
                    234: 
                    235:        static size_t index(int aindex){
                    236:                if(aindex<0)
1.9       moko      237:                        throw Exception("number.format", 0, "index out of range (negative)");
1.1       moko      238:                return aindex;
                    239:        }
                    240: 
1.7       moko      241:        static size_t index(const String::Body& aindex){ return pa_atoui(aindex.cstr()); }
                    242:        static size_t index(const String& aindex){ return pa_atoui(aindex.cstr(), 10, &aindex); }
1.1       moko      243: 
1.12      moko      244:        static bool is_index(const String& aindex){
                    245:                for(const char *pos=aindex.cstr();*pos;pos++){
                    246:                        if ((*pos < '0') || (*pos > '9'))
                    247:                                return false;
                    248:                }
                    249:                return true;
                    250:        }
                    251: 
1.5       moko      252:        bool contains(size_t index){
                    253:                return farray.get(index) != NULL;
1.1       moko      254:        }
                    255: 
                    256: private:
                    257: 
                    258:        ArrayValue farray;
                    259: 
                    260: };
                    261: 
                    262: #endif

E-mail: