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

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

E-mail: