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

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

E-mail: