home *** CD-ROM | disk | FTP | other *** search
- /*******************************************************************************
- +
- + LEDA 2.1.1 11-15-1991
- +
- +
- + _basic.c
- +
- +
- + Copyright (c) 1991 by Max-Planck-Institut fuer Informatik
- + Im Stadtwald, 6600 Saarbruecken, FRG
- + All rights reserved.
- +
- *******************************************************************************/
-
-
- #include <LEDA/basic.h>
-
- #include <stdarg.h>
- #include <time.h>
- #include <string.h>
- #include <sys/types.h>
-
- #ifndef __TURBOC__
- #include <sys/times.h>
- #endif
-
-
- const char* leda_version_string =
- "LEDA 2.1 (c) 1991 Max-Planck-Institut fuer Informatik, Saarbruecken, FRG";
-
-
- LEDA_INIT LEDA;
-
-
- LEDA_INIT::LEDA_INIT()
- {
-
- init_list = getenv("LEDA_INIT");
-
- if (init_list) fprintf(stderr,"%s\n",leda_version_string);
-
- init_random();
- }
-
-
- LEDA_INIT::~LEDA_INIT()
- {
- if (init_list && strcmp(init_list,"statistics")==0) print_statistics();
- }
-
-
- //------------------------------------------------------------------------------
- // Bool
- //------------------------------------------------------------------------------
-
- const int false = 0;
- const int true = 1;
-
-
- //------------------------------------------------------------------------------
- // Real
- //------------------------------------------------------------------------------
-
- int compare(double& r1, double& r2)
- { if (r1 < r2) return -1;
- if (r1 > r2) return 1;
- return 0;
- }
-
- int compare(real& r1, real& r2)
- { if (r1 < r2) return -1;
- if (r1 > r2) return 1;
- return 0;
- }
-
- rrep::rrep(double x)
- {
- d = x;
- n = 1;
- }
-
-
- real& real::operator=(const double x)
- { if (p->n > 1) { p->n--; p = new rrep(0); }
- p->d = x;
- return *this;
- }
-
- real& real::operator=(const real& x)
- { x.p->n++;
- if (--p->n == 0) delete p;
- p = x.p;
- return *this;
- }
-
- istream& operator>>(istream& s, real& r)
- { double d; s >> d; r = d; return s;}
-
- ostream& operator<<(ostream& s, const real& r)
- { return s << r.p->d; }
-
- //------------------------------------------------------------------------------
- // Memory Management
- //------------------------------------------------------------------------------
-
- const int memory_word_size = 4; // size of word (bytes)
- const int memory_block_bytes = 4092; // size of block (bytes) 2^k - 4
- const int memory_block_words = 1023; // size of block (in words)
- const int memory_max_size = 64; // maximal size of structures (words)
-
- memory_elem_ptr memory_free_list[65]; //memory_max_size + 1
- long int memory_total_count[65]; //memory_max_size + 1
-
-
- memory_elem_ptr memory_block_list = 0;
- int memory_block_count = 0;
-
-
- memory_elem_ptr memory_allocate_block(int size)
- { // fills memory_free_list[size] with l new elements of size "size"
-
- register memory_elem_ptr q;
- register memory_elem_ptr p;
-
-
- if ( (q=memory_elem_ptr(malloc(memory_block_bytes))) == 0 )
- { cout << string("memory_alloc: out of memory\n");
- print_statistics();
- abort();
- }
-
- memory_block_count++;
-
- q->next = memory_block_list;
- memory_block_list = q;
-
- q++;
-
- memory_free_list[size] = q;
-
- int l = (memory_block_words-1)/size;
- memory_total_count[size] += l;
-
- p = q + (l-1)*size;
- p->next = 0;
-
- while (q < p) q = (q->next = q+size);
-
-
- /*
- // rest
- int r;
- p = p+size;
- r = memory_block_bytes%(size * memory_word_size);
- if (r = r/memory_word_size)
- { p->next = memory_free_list[r];
- memory_free_list[r] = p;
- memory_total_count[r]++;
- }
- */
-
- return memory_free_list[size];
- }
-
-
- void memory_free_blocks()
- {
- register memory_elem_ptr p;
-
- while (memory_block_list)
- { p = memory_block_list;
- memory_block_list = p->next;
- free((char*)p);
- }
-
- for(int i=0; i <= memory_max_size; i++)
- { memory_free_list[i] = 0;
- memory_total_count[i] = 0;
- }
-
- memory_block_count = 0;
- }
-
-
- memory_elem_ptr allocate_words(int size)
- { memory_elem_ptr p = memory_free_list[size];
- if (p==0) p = memory_allocate_block(size);
- memory_free_list[size] = p->next;
- return p;
- }
-
- void deallocate_words(void* p, int size)
- { memory_elem_ptr(p)->next = memory_free_list[size];
- memory_free_list[size] = memory_elem_ptr(p);
- }
-
- void deallocate_list(void* head,void* tail, int size)
- { memory_elem_ptr(tail)->next = memory_free_list[size];
- memory_free_list[size] = memory_elem_ptr(head);
- }
-
- memory_elem_ptr Allocate_words(int size)
- { memory_elem_ptr p = memory_free_list[size];
- if (p==0) p = memory_allocate_block(size);
- memory_free_list[size] = p->next;
- cout << "Allocate "<< int(p) << "(" << size << ")\n";
- return p;
- }
-
- void Deallocate_words(void* p, int size)
- { cout << "Deallocate " << int(p) << "(" << size << ")\n";
-
- if (int(p)<=0 )
- error_handler(1,"deallocate: illegal pointer");
-
- memory_elem_ptr(p)->next = memory_free_list[size];
- memory_free_list[size] = memory_elem_ptr(p);
- }
-
- void print_statistics()
- {
- cout.flush();
-
- int i,size;;
- long int total,free,used;
- long int total_bytes=0, free_bytes=0, used_bytes=0, b;
- char* format=" | %3d %5ld %5ld %5ld %8ld bytes |\n";
-
- printf("\n");
- printf(" +-----------------------------------------------+\n");
- printf(" | size free used total space |\n");
- printf(" +-----------------------------------------------+\n");
-
- for (i=1;i<=memory_max_size;i++)
- if ((total = memory_total_count[i]) > 0 || memory_free_list[i])
- { memory_elem_ptr p = memory_free_list[i];
- size = memory_word_size*i;
- free = 0;
- while (p) { free++; p = p->next; }
- b = total*size;
- used = total - free;
- free_bytes = free_bytes + free*size;
- used_bytes = used_bytes + used*size;
- total_bytes = total_bytes + b;
- printf(format,i,free,used,total,b);
- }
-
- printf(" +-----------------------------------------------+\n");
- printf(" | %3d blocks %7.2f kb |\n",memory_block_count,float(total_bytes)/1024);
- printf(" +-----------------------------------------------+\n");
- printf("\n");
- fflush(stdout);
-
- }
-
-
-
- //------------------------------------------------------------------------------
- // Error Handling
- //------------------------------------------------------------------------------
-
-
- PEH p_error_handler = default_error_handler;
-
- void default_error_handler(int i, char* s)
- { if (i==0)
- cout << "(warning) " << s << "\n";
- else
- { cout << "ERROR "<< s << "\n";
- cout.flush();
- abort();
- }
- }
-
- PEH set_error_handler(PEH handler)
- { PEH old = error_handler;
- p_error_handler = handler;
- return old;
- }
-
- //------------------------------------------------------------------------------
- // usefull functions
- //------------------------------------------------------------------------------
-
- int interrupt_handler()
- { string cmd;
-
- cout << "\n INTERRUPT \n";
-
- for(;;)
- { newline;
- cmd=read_string("m(emory) / t(ime) / !(cmd) / c(ontinue) / i(nterrupt): ");
- switch(cmd[0])
- { case 'c': { cout << "execution continued\n";
- return(0);
- }
- case 'i': { cout << "execution terminated\n";
- exit(1);
- }
- case 'm': { print_statistics();
- newline;
- break;
- }
- case 't': { newline;
-
- #ifndef __TURBOC__
- print_time();
- #endif
- break;
- }
- case '!': { cmd[0] = ' ';
- system(cmd.cstring());
- newline;
- break;
- }
- }
- }
- }
-
-
-
- void init_random(int seed)
- { long l = seed;
- if (l==0) time(&l);
- srandom(int(l));
- }
-
- double rrandom() { return double(random())/Infinity; }
-
-
- #ifndef __TURBOC__
-
- double used_time()
- { tms x;
- times(&x);
- return double(x.tms_utime)/60;
- }
-
- double used_time(real& T)
- { double t = T;
- tms x;
- times(&x);
- T = double(x.tms_utime)/60;
- return double(T)-t;
- }
-
- double used_time(double& T)
- { double t = T;
- tms x;
- times(&x);
- T = double(x.tms_utime)/60;
- return double(T)-t;
- }
-
- float used_time(float& T)
- { float t = T;
- tms x;
- times(&x);
- T = float(x.tms_utime)/60;
- return float(T)-t;
- }
-
- void print_time(string s)
- {
- tms x;
- times(&x);
- float ut = float(x.tms_utime)/60;
- float st = float(x.tms_stime)/60;
- cerr << s;
- cerr << string("user time: %.2f sec system time: %.2f sec \n",ut,st);
- }
-
- #else /* TURBOC */
-
- double used_time()
- { clock_t x = clock();
- return double(x/CLK_TCK);
- }
-
- double used_time(real& T)
- { double t = T;
- T = used_time();
- return double(T)-t;
- }
-
- double used_time(double& T)
- { double t = T;
- T = used_time();
- return T-t;
- }
-
- float used_time(float& T)
- { float t = T;
- T = used_time();
- return T-t;
- }
-
- void print_time(string s)
- { cerr << s;
- cerr << string("user time: %.2f sec system time: 0.0 sec \n",used_time());
- }
-
-
- #endif
-
- //------------------------------------------------------------------------------
- // Input/Ouput
- //------------------------------------------------------------------------------
-
-
- file_ostream::file_ostream(string fname) : ostream(&fb)
- { if (fb.open(~fname,output)==0)
- error_handler(1,"cannot open file ");
- }
-
- void file_ostream::close() { fb.close(); clear(); }
-
- bool file_ostream::open(string fname)
- { close();
- return (fb.open(~fname,output) !=0);
- }
-
-
-
- file_istream::file_istream(string fname) : istream(&fb)
- { if (fb.open(~fname,input)==0)
- error_handler(1,"cannot open file");
- }
-
- void file_istream::close() { fb.close(); clear(); }
-
- bool file_istream::open(string fname)
- { close();
- return (fb.open(~fname,input) !=0);
- }
-
- string_ostream::string_ostream() : ostream(2048,buf) {}
-
- string string_ostream::str()
- { return buf; }
-
-
- string_istream::string_istream(string s) : istream(2048,buf)
- { for(int i=0;i<2048;i++) buf[i] = 0;
- strncpy(buf,~s,2048);
- }
-
- string_istream::string_istream(char* s) : istream(2048,buf)
- { for(int i=0;i<2048;i++) buf[i] = ' ';
- strncpy(buf,s,2048);
- }
-
- string_istream::string_istream(int argc, char** argv) : istream(2048,buf)
- { for(int i=0;i<2048;i++) buf[i] = 0;
- char* b = buf;
- int rest = 2048;
- for(i=0;i<argc;i++)
- { int l = strlen(argv[i]) + 1;
- strncpy(b,argv[i],rest);
- rest -= l;
- b += l;
- b[-1] = ' ';
- }
- }
-
-
- #ifndef __TURBOC__
-
- #ifdef __GNUG__
-
- cmd_ostream::cmd_ostream(string cmd) : ostream(popen(~cmd,"w")) {}
- cmd_istream::cmd_istream(string cmd) : istream(popen(~cmd,"r")) {}
-
- #else
-
- cmd_ostream::cmd_ostream(string cmd) : b(popen(~cmd,"w")), ostream(&b) {}
- cmd_istream::cmd_istream(string cmd) : b(popen(~cmd,"r")), istream(&b) {}
-
- #endif
-
- #endif
-
-
- int Yes(string s)
- { char answer = read_char(s);
- return ((answer == 'y') || (answer == 'Y'));
- }
-
- int read_int(string s)
- { int answer;
- char c;
- int success = 0;
- while (!success)
- { cout << s;
- cin >> answer;
- if (!cin) { cin.clear();
- cin.get(c);
- cout << string("read_int: illegal input \"%c\"\n",c);
- while (c!='\n') cin.get(c); //skip rest of line
- }
- else success=1;
- }
- cin.get(c); // eat '\n'
-
- return answer;
- }
-
- char read_char(string s)
- { char c;
- cout << s;
- cin.get(c);
- if (c!='\n') read_line(cin);
- return c;
- }
-
- double read_real(string s)
- { double answer;
- cout << s;
- cin >> answer;
- read_line(cin);
- return answer;
- }
-
- string read_line(istream& s)
- { char buf[256];
- char* p = buf;
-
- while (s.get(*p))
- { if (*p == '\n') break;
- p++;
- }
- *p = '\0';
- return string(buf);
- }
-
- string read_string(string s)
- { cout << s;
- return read_line(cin);
- }
-
- int Yes() { return Yes(""); }
- int read_int() { return read_int(""); }
- char read_char() { return read_char(""); }
- double read_real() { return read_real(""); }
- string read_string() { return read_string(""); }
- string read_line() { return read_line(cin); }
-
-