/* SCRIPTOL LIBRARY (c) 2002 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 and 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. */ #if !defined(_SOL_ARRAY) #define _SOL_ARRAY #define REVERSE true class dict; class sol_pair : public gc_cleanup { public: text key; dyn *value; sol_pair() { key = text(); value = new dyn(); } sol_pair(sol_pair *s) { key = s->key; value = s->value; } sol_pair(int a, dyn b) { key = text(a); value = new dyn(b); } sol_pair(const char *a, dyn b) { key = text(a); value = new dyn(b); } sol_pair(text a, dyn b) { key = a; value = new dyn(b); } text toText(); char *toStr(); }; /** Associative, Php compatible dynamic array. Base class also of dict. */ class array : public gc_cleanup { std::vector > keyList; std::vector > data; int freeKey; // first free numeric key public: int indexer; /** Return the number of elements */ inline int size() { return (int) data.size(); } /** Return true if the array holds no any element, false otherwise */ inline bool empty() { return data.empty(); } void clear(); void set(const array &); void set(_arr32); void set(array *); void set(dyn); void set(char *[]); array(); /** Build an associative array from an array of strings of chars. Indexes are generated from 0 to n. */ array(char *a[]); /** Build an array with a single element. */ array(dyn); array(const array &); array(array *); /** Build an associative array from a typed indiced array. */ array(_arr32); ~array(); void operator=(const array &); void operator=(array *); void operator=(dyn); void operator=(_arr32 &); void add(array *); void add(const array &); dyn get(const text &); dyn get(const char *); dyn get(int); inline array operator+(array x) { return operator+(&x); } array operator+(array *); array operator+(dyn); inline array operator-(array x) { return operator-(&x); } array operator-(array *); array operator-(dyn); inline array operator&(array x) { return operator&(&x); } array operator&(array *); inline array operator|(array x) { return operator|(&x); } array operator|(array *); inline array operator^(array x) { return operator^(&x); } array operator^(array *); inline array &operator+=(array x) { return operator+=(&x); } array &operator+=(array *); array &operator+=(dyn); void operator-=(array x) { operator-=(&x); } void operator-=(array *); void operator-=(dyn); inline bool operator==(array x) { return operator==(&x); } bool operator==(array *); bool operator==(bool); inline bool operator!=(array x) { return operator!=(&x); } bool operator!=(array *); bool operator!=(bool); inline bool operator<(array x) { return operator<(&x); } bool operator<(array *); inline bool operator>(array x) { return operator>(&x); } bool operator>(array *); inline bool operator>=(array a) { return !operator<(&a); } inline bool operator>=(array *a) { return !operator<(a); } inline bool operator<=(array b) { return !operator>(&b); } inline bool operator<=(array *b) { return !operator>(b); } inline void operator&=(array x) { operator&=(&x); } void operator&=(array *); inline void operator|=(array x) { operator|=(&x); } void operator|=(array *); inline void operator^=(array x) { operator^=(&x); } void operator^=(array *); bool operator!(); array &operator,(dyn); //===== util bool in(const dyn &); bool in(dyn *); signed int hasStrKey(const text &); signed int findKey(const text &); signed int findKey(const char *); signed int findKey(int); signed int findValue(const dyn &); signed int findValue(dyn *); inline int find(dyn x) { return findValue(x); } inline int find(dyn *x) { return findValue(x); } void display(); void display(int); text toText(); void pushKey(const text &); //===== iterator /** Restart the iterator (at first element). */ void reset(); bool outOfBound(); bool bound(); /** Return the current element, and move index to next one. */ dyn inc(); /** Return the current element, and move index to previous one. */ dyn dec(); /** Return the currently indiced element. */ dyn current(); /** Return the key of the currently pointed out element. */ text key(); /** Return the key of the element at position in argument. */ text key(int); /** Return the value of the currently pointed out element. */ dyn value(); /** Return the value of the element at position in argument. */ dyn value(int); /** Set iterator at first element and return it. */ dyn begin(); /** Set iterator at last element and return it. */ dyn end(); /** Return the current indice of the iterator. */ int index(); //===== stack /** Push a dyn on top of the array, it becomes the last element and the size increases from one. */ void push(const dyn &); void push(dyn *); void push(sol_pair *); void push(const char *); void push(const text &, dyn); void pushAdd(const text &, dyn); void pushAdd(int, dyn); void pushUnic(const text &, dyn); /** Return the last element, and remove it from the array. If empty, return false. */ dyn pop(); /** Return the first element and remove it. All remaining element are shift for one position. Indices are renumbered if integers. */ dyn shift(); /** Insert an element at the first position. */ void unshift(dyn); /** Insert an element at given position. The first parameter is the postion, the second one the element. */ void insert(int, dyn); //===== ranges void erase(int, int); void erase(int); array slice(int, int, int); void splice(int, int, array, int); void splice(int, int, array *, int); void splice(int, int, int); void splice(int, int); void splice(int); void splice(int, int, dyn, int); void splice(int, int, bool, int); //===== transform /** Renumber the key if they are numbers. Textual keys are ignored. */ void pack(); void swapData(int, int); void swapElem(int, int); void keysSort(int, int); void dynSort(int, int); /** Reverse the content of the array */ array reverse(); /** Sort the values of the array in ascending order. */ void sort(); /** Sort the keys of the array, preserving the associations with values. Ascending order. */ void kSort(); void aSort(); /** Remove doubloons in values. */ void unique(); /** Return the values as an array. Keys are generated, from 0 to n. */ array values(); /** Return the keys as an array. Integer keys are generated. */ array keys(); //===== operations void clone(array *); // copy to another array /** Return the minimal value among elements. */ dyn min(); /** Return the greatest values among elements. */ dyn max(); /** Calculate the sum of elements and return it. */ double sum(); /** Concatenate all the element (providing they are texts) and use the text given as argument as separator. */ text join(const char *); int nomatch(); //===== I/O /** Load a file into the array. The argument is the filename. Each line of the file become an element. The end of line code (10, 13 or the twice) are stored also. */ void load(const char *); /** Save the whole array into the file, the name of which is given as argument. An end of line must be added to each element if necessary. */ bool store(const char *); #ifdef DEBUG void image(); #endif array *to(int); array *to(const char *); array *to(text); dyn &operator[](int); dyn &operator[](const char *); dyn &operator[](text); dyn &at(int); dyn &at(text); dyn &at(const char *); void setAt(int, dyn *); void setAt(int, dyn); void setAt(text, dyn *); void setAt(text, dyn); void boundError(const text &); friend class dict; }; class dict : public array { public: /** Build a dict with a single element. */ dict(); dict(dyn x) : array(x) {}; dict(const array &x) : array(x) {}; dict(array *x) : array(x) {}; dict(const dict &); dict(dict *); void set(const dict &); void set(dict *); void setAt(const text &, dyn); void setAt(const text &, dyn *); void sort() { aSort(); } dyn &operator[](const char *); dyn &operator[](const text &); void erase(const char *); inline void erase(const text t) { erase(t.data); } }; inline array operator+(array *a, const array &b) { return a->operator+(b); } std::ostream& operator<< (std::ostream& out, array *); std::ostream& operator<< (std::ostream& out, array); #endif