/* SCRIPTOL LIBRARY (c) 2002-2003 by Denis G. Sureau http://www.scriptol.com LICENCE Along with the free Scriptol to native compiler is provided the libsol.a or libsol.lib library, it contains the TEXT, DYN and ARRAY classes and lot of functions compatibles with the Php library. This library is the original work of Denis G. Sureau. It has been registered at INPI 25 june 2002. The Php compatible functions are original work also. They have nothing to do with Php sources. The library is provided to allow the user to link it with its Scriptol compiled program. He is allowed to include the library in binary form into its program and distribute its programs with the library included into, with no restriction or condition or fee due. He is not allowed to distribute the compiled library as it, but as a part of an open-source program. The compiled library may be included among other files of the open source distribution and headers of the file to include, that must hold my name and the address of the Scriptol site. The source of the library is provided to users to allow them either to modify it according to their need, or to compile it in a different file format, for a different compiler. The users are not permitted to distribute the source of the library in no any case, even modified. */ #ifndef _DYN_ARRAY #define _DYN_ARRAY #define TYPE_UNKNOW 0 #define TYPE_INT 1 #define TYPE_NUMBER 2 #define TYPE_NATURAL 3 #define TYPE_REAL 4 #define TYPE_BYTE 5 #define TYPE_BOOL 6 #define TYPE_LONG 7 #define TYPE_DOUBLE 8 #define TYPE_COMPLEX 9 #define TYPE_TEXT 16 #define TYPE_ARRAY 32 #define TYPE_DICT 33 #define TYPE_PAIR 34 #define TYPE_FILE 40 #define TYPE_DIR 41 #define TYPE_OBJECT 50 #define TYPE_TARRAY 60 extern int _default_capacity; /** Fast indexer for typed arrays of 32 bits objects: integers, strings of chars, pointers on objects... */ class _arr32 : public gc_cleanup { static const char *nothing; public: void **data; /** Number of elements (less or equal to the capacity) */ int len; /** Default capacity at creation of the typed array */ int capacity; /** Used by the builtin iterator */ int indexer; int type; /** default constructor */ _arr32(); _arr32(int); _arr32(int, int); _arr32(int, int, int); /** copy constructor */ _arr32(_arr32 *); _arr32(const _arr32 &); /** Constructor from an array of texts, with text type, size of the array */ _arr32(char *a[]); /** Constructor with type and one element */ _arr32(int, void *); /** Complete constructor with one element. In order, the type, the starting capcity, and the element */ _arr32(int, int, void *); /** Complete constructor with a string item. */ _arr32(int, int, const char *); /** Complete constructor with a text item. */ _arr32(int, int, text &); /** Returns the number of elements */ inline int size() { return len; } /** Returns true if the array is empty */ inline bool empty() { return len == 0; } void resize(int); /** Clears the content. */ void clear(); int compare(void *, void *); bool same(void *, void *); void set(); void set(int); void set(int, int); void set(const _arr32 &); void set(_arr32 *); void set(char *[]); void set(void *); void operator=(const _arr32 &); void operator=(_arr32 *); void operator=(void *); void operator=(int); void operator=(const char *); void setAt(int, void *); inline void setAt(int i, bool b) { erase(i); } // set nil inline void setAt(int i, int n) { setAt(i, (void *) n); } inline void setAt(int i, const char *s) { setAt(i, (void *) s); } //void setAt(int, const char *); inline void setAt(int i, text &t) { setAt(i, (void *) t.data); } /** Adds a second array having the same type. */ void add(_arr32); void * &operator[](int); //void * &operator[](text); void * &operator[](const char *); void * at(int); void * at(text); void * at(const char *); bool operator==(_arr32); bool operator==(bool); inline bool operator!=(_arr32 a) { return !operator==(a); } inline bool operator!=(bool b) { return !operator==(b); } bool operator<(_arr32); bool operator>(_arr32); inline bool operator>=(_arr32 a) { return !operator<(a); } inline bool operator<=(_arr32 b) { return !operator>(b); } _arr32 operator+(_arr32); _arr32 operator+(void *); _arr32 operator+(text x); inline _arr32 operator+(int x) { return operator+((void *) x); } inline _arr32 operator+(char *x) { return operator+((void *) x); } _arr32 operator-(_arr32); _arr32 operator-(void *); inline _arr32 operator-(text x) { return operator-((void *) x.data); } inline _arr32 operator-(int x) { return operator-((void *) x); } inline _arr32 operator-(char *x) { return operator-((void *) x); } _arr32 operator&(_arr32); _arr32 operator&(void *); _arr32 operator&(text x); inline _arr32 operator&(int x) { return operator&((void *) x); } inline _arr32 operator&(char *x) { return operator&((void *) x); } _arr32 operator|(_arr32); _arr32 operator|(void *); _arr32 operator|(text x); inline _arr32 operator|(int x) { return operator|((void *) x); } inline _arr32 operator|(char *x) { return operator|((void *) x); } _arr32 operator^(_arr32); void operator+=(_arr32); void operator+=(void *); inline void operator+=(int x) { operator+=((void *) x); } inline void operator+=(text t) { operator+=((void *) t.data); } inline void operator+=(char *x) { operator+=((void *) x); } void operator-=(_arr32); void operator-=(void *); inline void operator-=(int x) { operator-=((void *) x); } inline void operator-=(text t) { operator-=((void *) t.data); } inline void operator-=(char *x) { operator-=((void *) x); } void operator&=(_arr32); void operator&=(void *); inline void operator&=(int i) { operator&=((void *) i); } inline void operator&=(text t) { operator&=((void *) t.data); } inline void operator&=(char *s) { operator&=((void *) s); } void operator|=(_arr32); void operator|=(void *); inline void operator|=(int i) { operator|=((void *) i); } inline void operator|=(text t) { operator|=((void *) t.data); } inline void operator|=(char *s) { operator|=((void *) s); } void operator^=(_arr32); bool operator!(); //===== util bool in(void *); signed int hasStrKey(const text &); signed int findKey(const text &); signed int findKey(int); signed int findValue(void *); /** Finds the position of an element inside the array. Return the position or -1 if not found. */ inline int find(void *x) { return findValue(x); } /** Displays the array */ void display(); void display(int); /** Joins the elements of the array into a string. Numbers are converted to strings of digits. */ char *toStr(); char *toStr(int); /** Joins the elements of the array into a text. */ text toText(); //===== iterator /** Points out the indexer at the first element. */ void *begin(); /** Points out the indexer at the last element. */ void *end(); /** Restarts the iterator at first element. */ void reset(); bool outOfBound(); bool bound(); /** Returns the current element and increments the indexer to the next position. */ void *inc(); /** Returns the current element and decrements the indexer to the previous position. */ void *dec(); /** Returns the current position of the iterator. Use index() instead. */ int key(); int key(int); /** Returns the currently pointer out element. */ void *value(); void *value(int); void *current(); /** Return the current location of the indexer. */ int index(); //===== stack /** Pushes an object at the end of the array. */ void push(void *); /** Pushes an integer at the end of the array. */ void push(int); /** Pushes a string at the end of the array. */ void push(const char *); /** Pushes a text at the end of the array. */ void push(text); void pushUnic(void *); /** Returns the last element, and remove it. */ void *pop(); /** Returns the first element, and remove it. */ void *shift(); /** Inserts an object at beginning. */ void unshift(void *); /** Inserts an integer at beginning. */ inline void unshift(int i) { unshift((void *) i); } /** Inserts a string at beginning. */ inline void unshift(char *s) { unshift((void *) s); } /** Inserts a text at beginning. */ inline void unshift(text &t) { unshift((void *) t.data); } //===== ranges /** Inserts an object at position. */ void insert(int, void *); /** Inserts an array at position. The elements of the array are inserted, not the array itself. */ void insert(int, _arr32); /** Removes a range of elements for the first to the last given positions, included. Use splicing instead. */ void erase(int, int); /** Removes the element at position. */ void erase(int); _arr32 slice(int, int, int); inline _arr32 _arr32::slice(int f) { return slice(f, len, 0); } /** Extracts a sub-array from the array. Parameters are the position of the first and last element included. */ inline _arr32 _arr32::slice(int f, int l) { return slice(f, l, 1); } void splice(int); /** Erases a range of elements. Parameters are the position of the first and last element included. */ void splice(int, int); void splice(int, int, int); void splice(int, int, _arr32, int); void splice(int, int, void *, int); void splice(int, int, bool, int); //===== transform /** Remove all null records from the array */ void pack(); void swapData(int, int); void dynSort(int,int); /** Reverses the content of the array */ _arr32 reverse(); /** Sorts the array in ascending order. */ void sort(); void aSort(); void kSort() { return; } /** Removes doubloons */ void unique(); /** This method returns the array itself and is useless. */ _arr32 values(); /** Returns the indices as an array of integers. */ _arr32 keys(); //===== operations /** Copies into another array. */ void clone(_arr32 *); /** Returns the smaller element. If elements are texts, they are considered to represent integers, and non-digits text have 0 value. */ int min(); /** Returns the higher element. If elements are texts, they are considered to represent integers. */ int max(); /** Returns the sum of elements. If elements are texts, they are considered to represent integers. */ int sum(); /** Joins the elements into a text when they are string and thus, for an array of texts only. */ text join(const char *); //===== I/O /** Fills an array from a file (one element per line). */ void load(const char *); /** Store an array into file. The end of line code must be added to elements of the array. Elements are stored as text if they are texts, as binary if they are scalars. Object are not handled for now. */ bool store(const char *); static void boundError(const text &); }; std::ostream& operator<< (std::ostream &, _arr32 &); //_arr32* operator+(_arr32*, void *); #endif