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

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.10    ! moko       11: #define IDENT_PA_VARRAY_H "$Id: pa_varray.h,v 1.9 2024/09/22 13:56:10 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.1       moko       24: 
                     25: extern Methoded* array_class;
                     26: 
1.2       moko       27: /// Sparse Array
                     28: template<typename T> class SparseArray: public Array<T> {
1.5       moko       29: 
                     30:        mutable size_t fused;
                     31: 
1.2       moko       32: public:
1.5       moko       33:        inline SparseArray(size_t initial=0) : Array<T>(initial), fused(0) {}
1.2       moko       34: 
1.6       moko       35:        inline SparseArray(size_t size, T* elements) : Array<T>(size), fused(size) {
                     36:                T* elements_end=elements + size;
                     37:                for(T* dst=this->felements; elements < elements_end;)
                     38:                        *dst++=*elements++;
                     39:                this->fsize=size;
                     40:        }
                     41: 
1.4       moko       42:        inline T get(size_t index) const {
                     43:                return index < this->count() ? this->felements[index] : NULL;
                     44:        }
                     45: 
1.9       moko       46:        void fit(size_t index);
                     47: 
1.5       moko       48:        inline void put(size_t index, T element){
                     49:                this->fit(index);
1.2       moko       50:                this->felements[index]=element;
1.5       moko       51:                if(index >= this->fsize){
                     52:                        this->fsize=index+1;
                     53:                }
                     54:        }
                     55: 
1.10    ! moko       56:        inline bool put_dont_replace(size_t index, T element){
        !            57:                this->fit(index);
        !            58:                if(this->felements[index])
        !            59:                        return true;
        !            60:                this->felements[index]=element;
        !            61:                if(index >= this->fsize){
        !            62:                        this->fsize=index+1;
        !            63:                }
        !            64:                return false;
        !            65:        }
        !            66: 
1.5       moko       67:        inline void insert(size_t index, T element) {
                     68:                if(index >= this->fsize){
                     69:                        this->fit(index);
                     70:                        this->felements[index]=element;
                     71:                        this->fsize=index+1;
                     72:                } else {
                     73:                        Array<T>::insert(index, element);
                     74:                }
                     75:        }
                     76: 
                     77:        size_t used() const{
                     78:                if(!fused){
                     79:                        for(Array_iterator<T> i(*this); i;) {
                     80:                                if(i.next())
                     81:                                        fused++;
                     82:                        }
                     83:                }
                     84:                return fused;
                     85:        }
                     86: 
                     87:        inline void clear(size_t index) {
1.7       moko       88:                if(index < this->count()){
1.5       moko       89:                        this->felements[index]=NULL;
1.7       moko       90:                        if(index+1 == this->count())
                     91:                                this->fsize--;
1.2       moko       92:                }
                     93:        }
1.5       moko       94: 
                     95:        inline void clear() { Array<T>::clear(); }
                     96: 
1.8       moko       97:        inline void remove(size_t index) {
                     98:                if(index < this->count()){
                     99:                        Array<T>::remove(index);
                    100:                }
                    101:        }
                    102: 
1.5       moko      103:        inline void invalidate(){
                    104:                fused=0;
                    105:        }
                    106: 
1.2       moko      107: };
                    108: 
                    109: 
1.1       moko      110: class VArray: public VHashBase {
                    111: public: // value
                    112: 
                    113:        override const char* type() const { return VARRAY_TYPE; }
                    114:        override VStateless_class *get_class() { return array_class; }
                    115: 
1.5       moko      116:        /// VArray: used elements count
                    117:        override int as_int() const { return farray.used(); }
                    118:        override double as_double() const { return farray.used(); }
                    119:        override bool is_defined() const { return farray.used()!=0; }
                    120:        override bool as_bool() const { return farray.used()!=0; }
                    121:        override Value& as_expr_result() { return *new VInt(farray.used()); }
1.1       moko      122: 
                    123:        /// VArray: virtual hash
                    124:        override HashStringValue *get_hash() { return &hash(); }
                    125:        override HashStringValue* get_fields() { return &hash(); }
                    126:        override HashStringValue* get_fields_reference() { return &hash(); }
                    127: 
                    128:        /// VArray: json-string
                    129:        override const String* get_json_string(Json_options& options);
                    130: 
                    131:        /// VArray: (key)=value
                    132:        override Value* get_element(const String& aname) {
                    133:                // $element first
1.4       moko      134:                if(Value* result=farray.get(index(aname)))
1.1       moko      135:                        return result;
                    136: 
                    137:                // $fields -- pseudo field to make 'hash' and 'array' more like 'table'
                    138:                if(SYMBOLS_EQ(aname,FIELDS_SYMBOL))
                    139:                        return this;
                    140: 
                    141: #if !defined(FEATURE_GET_ELEMENT4CALL) || !defined(OPTIMIZE_BYTECODE_GET_ELEMENT__SPECIAL)
                    142:                // $method, CLASS, CLASS_NAME
                    143:                if(Value* result=VStateless_object::get_element(aname))
                    144:                        return result;
                    145: #endif
                    146:                return NULL;
                    147:        }
                    148: 
                    149: #ifdef FEATURE_GET_ELEMENT4CALL
                    150:        override Value* get_element4call(const String& aname) {
                    151:                // $method first
                    152:                if(Value* result=VStateless_object::get_element(aname))
                    153:                        return result;
                    154: 
                    155:                // $element
1.4       moko      156:                if(Value* result=farray.get(index(aname)))
1.1       moko      157:                        return result;
                    158: 
                    159:                return bark("%s method not found", &aname);
                    160:        }
                    161: #endif
                    162: 
                    163:        /// VArray: (key)=value
                    164:        override const VJunction* put_element(const String& aname, Value* avalue) {
1.5       moko      165:                farray.put(index(aname), avalue);
                    166:                invalidate();
1.1       moko      167:                return 0;
                    168:        }
                    169: 
                    170: public: // VHashBase
                    171: 
                    172:        override HashStringValue& hash();
                    173:        override void set_default(Value*) { }
                    174:        override Value* get_default() { return 0; }
1.5       moko      175:        override void add(Value* avalue) { farray+=avalue; /* only json uses it, thus no need to invalidate()*/ }
1.1       moko      176: 
                    177: public: // usage
                    178: 
1.5       moko      179:        VArray(size_t initial=0): farray(initial), fhash(0) {}
1.6       moko      180:        VArray(size_t size, Value** elements): farray(size, elements), fhash(0) {}
1.5       moko      181:        ~VArray() { invalidate(); }
1.1       moko      182: 
                    183:        ArrayValue &array() { return farray; }
                    184: 
                    185:        static size_t index(int aindex){
                    186:                if(aindex<0)
1.9       moko      187:                        throw Exception("number.format", 0, "index out of range (negative)");
1.1       moko      188:                return aindex;
                    189:        }
                    190: 
1.7       moko      191:        static size_t index(const String::Body& aindex){ return pa_atoui(aindex.cstr()); }
                    192:        static size_t index(const String& aindex){ return pa_atoui(aindex.cstr(), 10, &aindex); }
1.1       moko      193: 
1.5       moko      194:        bool contains(size_t index){
                    195:                return farray.get(index) != NULL;
1.1       moko      196:        }
                    197: 
1.5       moko      198:        void invalidate() {
1.1       moko      199: #ifdef USE_DESTRUCTORS
                    200:                if(fhash)
                    201:                        delete(fhash);
                    202: #endif
                    203:                fhash=0;
1.5       moko      204:                farray.invalidate();
1.1       moko      205:        }
                    206: 
                    207: private:
                    208: 
                    209:        ArrayValue farray;
                    210:        HashStringValue *fhash;
                    211: 
                    212: };
                    213: 
                    214: #endif

E-mail: