home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + array.h
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
-
-
-
- #ifndef ARRAYH
- #define ARRAYH
-
- #include <LEDA/basic.h>
-
- //------------------------------------------------------------------------------
- // array
- //
- // Stefan Naeher (1989)
- //------------------------------------------------------------------------------
-
-
-
- class ent_array {
- ent* v;
- int sz;
- int Low;
- int High;
- int t;
- int dir;
- void quick_sort(int l, int r);
- void quick_sort(int l, int r, CMP_ENT f);
-
- virtual int cmp(ent& x, ent& y) { return int(x)-int(y); };
- virtual void print_el(ent& x,ostream& out) const { out << int(x); }
- virtual void read_el(ent& x,istream& in) const { in >> *(int*)&x; }
- virtual void clear_entry(ent& x) { x = 0; }
- virtual void copy_entry(ent& x) { x = 0; }
- virtual void init_entry(ent& x) { x = 0; }
-
- protected:
- void clear();
-
- public:
- void init();
- ~ent_array() { delete v; }
-
- ent_array(int, int);
- ent_array(int);
- ent_array(ent_array&);
- ent_array& operator=(ent_array&);
- int size() { return sz; }
- int low() { return Low; }
- int high() { return High; }
- ent& elem(int i) { return v[i]; }
- ent elem(int i) const { return v[i]; }
- ent& entry(int i)
- { if (i<Low || i>High)
- error_handler(2,"array::entry index out of range");
- return v[i-Low];
- }
- ent inf(int i) const
- { if (i<Low || i>High)
- error_handler(2,"array::inf index out of range");
- return v[i-Low];
- }
-
- void permute(int,int);
-
- void permute() { permute(Low,High); }
-
- void sort(CMP_ENT f, int l, int h, bool randomize)
- { if (randomize) permute();
- quick_sort(l-Low,h-Low,f);
- }
-
- void sort(CMP_ENT f, bool randomize)
- { if (randomize) permute();
- quick_sort(0,sz-1,f);
- }
-
- int binary_search(ent);
- int binary_search(ent,CMP_ENT);
-
- void print(ostream&,string, char space) const;
- void print(ostream& out,char space=' ') const { print(out,"",space); }
- void print(string s, char space=' ') const { print(cout,s,space); }
- void print(char space=' ') const { print(cout,"",space); }
-
-
- void read(istream&,string);
- void read(istream& in) { read(in,""); }
- void read(string s ) { read(cin,s); }
- void read() { read(cin,""); }
-
-
- };
-
-
- #define array(type) name2(type,array)
-
- #define arraydeclare(type)\
- \
- typedef int (*name2(type,_array_cmp))(type&,type&);\
- \
- class array(type) : public ent_array {\
- \
- /*int cmp(ent& x, ent& y) { return compare(type(x),type(y)); }*/\
- \
- void print_el(ent& x,ostream& out) const { Print(*(type*)&x,out);}\
- void read_el(ent& x,istream& in) const { type y=0; Read(y,in); x = Copy(y); }\
- void clear_entry(ent& x) { Clear(*(type*)&x); }\
- void copy_entry(ent& x) { Copy(*(type*)&x); }\
- void init_entry(ent& x) { type y=0; x = Copy(y); }\
- \
- public:\
- array(type)(int a, int b) : ent_array(a,b) { init(); }\
- array(type)(int n) : ent_array(n) { init(); }\
- array(type)(array(type)& A) : ent_array((ent_array&) A) {}\
- ~array(type)() { clear(); }\
- \
- array(type)& operator=(array(type)& A)\
- { return (array(type)&)ent_array::operator=((ent_array&) A); }\
- type& operator[](int i) { return *(type*)&ent_array::entry(i);}\
- type operator[](int i) const { return type(ent_array::inf(i));}\
- void sort(name2(type,_array_cmp) f, bool rand=true)\
- { CMP_ENT g = CMP_ENT(f);\
- ent_array::sort(g,rand); }\
- void sort(name2(type,_array_cmp) f,int l, int h, bool rand=true)\
- { CMP_ENT g = CMP_ENT(f);\
- ent_array::sort(g,l,h,rand); }\
- int binary_search(type x) { return ent_array::binary_search(Ent(x));}\
- int binary_search(type x,name2(type,_array_cmp) f)\
- { return ent_array::binary_search(Ent(x),CMP_ENT(f));}\
- };
-
- /*------------------------------------------------------------------------*/
- /* 2 dimensional arrays */
- /*------------------------------------------------------------------------*/
-
-
-
- class ent_array2 {
- ent_array A;
- int Low1, Low2, High1, High2;
- virtual void clear_entry(ent& x) { x = 0; }
- virtual void copy_entry(ent& x) { x = 0; }
- virtual void init_entry(ent& x) { x = 0; }
-
- protected:
- void clear();
- ent_array* row(int i) const { return (ent_array*)A.inf(i); }
-
- public:
- void init(int,int,int,int);
- int low1() { return Low1; }
- int low2() { return Low2; }
- int high1() { return High1; }
- int high2() { return High2; }
- ent_array2(int,int,int,int);
- ent_array2(int,int);
- ~ent_array2();
- };
-
-
- #define array2(type) name2(type,array2)
-
- #define array2declare(type)\
- \
- struct array2(type) : public ent_array2 {\
- \
- void clear_entry(ent& x) { Clear(*(type*)&x); }\
- void copy_entry(ent& x) { x = Copy(*(type*)&x); }\
- void init_entry(ent& x) { type y=0; x = Copy(y); }\
- \
- type& operator()(int i, int j) { return *(type*)&(row(i)->entry(j)); }\
- type operator()(int i, int j) const { return type(row(i)->entry(j)); }\
- \
- array2(type)(int a,int b,int c,int d) :ent_array2(a,b,c,d){ init(a,b,c,d);}\
- array2(type)(int n,int m) :ent_array2(n,m) { init(0,n-1,0,m-1);}\
- ~array2(type)() { clear(); }\
- };
-
- #endif
-