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

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

E-mail: