home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / basic / _basic.bak < prev    next >
Encoding:
Text File  |  1991-11-25  |  11.6 KB  |  548 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _basic.c
  7. +
  8. +
  9. +  Copyright (c) 1991  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #include <LEDA/basic.h>
  16.  
  17. #include <stdarg.h>
  18. #include <time.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21.  
  22. #ifndef __TURBOC__
  23. #include <sys/times.h>
  24. #endif
  25.  
  26.  
  27. const char* leda_version_string = 
  28.   "LEDA 2.1 (c) 1991  Max-Planck-Institut fuer Informatik, Saarbruecken, FRG";
  29.  
  30.  
  31. LEDA_INIT LEDA;
  32.  
  33.  
  34. LEDA_INIT::LEDA_INIT()
  35.  
  36.   init_list = getenv("LEDA_INIT");
  37.  
  38.   if (init_list) fprintf(stderr,"%s\n",leda_version_string);
  39.  
  40.   init_random();
  41.  }
  42.  
  43.  
  44. LEDA_INIT::~LEDA_INIT() 
  45.   if (init_list && strcmp(init_list,"statistics")==0) print_statistics(); 
  46.  } 
  47.  
  48.  
  49. //------------------------------------------------------------------------------
  50. // Bool
  51. //------------------------------------------------------------------------------
  52.  
  53. const int false = 0;
  54. const int true  = 1;
  55.  
  56.  
  57. //------------------------------------------------------------------------------
  58. // Real
  59. //------------------------------------------------------------------------------
  60.  
  61. int compare(double& r1, double& r2) 
  62. { if (r1 < r2)  return -1;  
  63.   if (r1 > r2)  return  1;  
  64.   return 0; 
  65. }
  66.  
  67. int compare(real& r1, real& r2) 
  68. { if (r1 < r2)  return -1;  
  69.   if (r1 > r2)  return  1;  
  70.   return 0; 
  71. }
  72.  
  73. rrep::rrep(double x) 
  74.   d = x; 
  75.   n = 1;
  76. }
  77.  
  78.  
  79. real& real::operator=(const double x)
  80. { if (p->n > 1) { p->n--; p = new rrep(0); }
  81.   p->d = x;
  82.   return *this;
  83. }
  84.  
  85. real& real::operator=(const real& x)
  86. { x.p->n++;
  87.   if (--p->n == 0)  delete p;
  88.   p = x.p;
  89.   return *this;
  90. }
  91.  
  92. istream& operator>>(istream& s, real& r) 
  93. { double d; s >> d; r = d; return s;}
  94.  
  95. ostream& operator<<(ostream& s, const real& r) 
  96. { return s << r.p->d; }
  97.  
  98. //------------------------------------------------------------------------------
  99. // Memory Management
  100. //------------------------------------------------------------------------------
  101.  
  102. const int memory_word_size = 4;     // size of word  (bytes)
  103. const int memory_block_bytes = 4092; // size of block (bytes) 2^k - 4
  104. const int memory_block_words = 1023; // size of block (in words)
  105. const int memory_max_size = 64;     // maximal size of structures (words)
  106.  
  107. memory_elem_ptr memory_free_list[65];     //memory_max_size + 1
  108. long int        memory_total_count[65];   //memory_max_size + 1
  109.  
  110.  
  111. memory_elem_ptr memory_block_list = 0;
  112. int             memory_block_count = 0;
  113.  
  114.  
  115. memory_elem_ptr memory_allocate_block(int size)
  116. { // fills memory_free_list[size] with l new elements of size "size" 
  117.  
  118.   register memory_elem_ptr q;
  119.   register memory_elem_ptr p;
  120.  
  121.  
  122.   if ( (q=memory_elem_ptr(malloc(memory_block_bytes))) == 0 )
  123.    { cout << string("memory_alloc: out of memory\n");
  124.      print_statistics();
  125.      abort();
  126.     }
  127.  
  128.   memory_block_count++;
  129.  
  130.   q->next = memory_block_list;
  131.   memory_block_list = q;
  132.  
  133.   q++; 
  134.  
  135.   memory_free_list[size] = q;
  136.  
  137.   int l = (memory_block_words-1)/size;
  138.   memory_total_count[size] += l;
  139.  
  140.   p = q + (l-1)*size;
  141.   p->next = 0;
  142.  
  143.   while (q < p) q = (q->next = q+size);
  144.  
  145.  
  146. /*
  147.   // rest
  148.   int r;
  149.   p = p+size;
  150.   r = memory_block_bytes%(size * memory_word_size);
  151.   if (r = r/memory_word_size)
  152.   { p->next = memory_free_list[r];
  153.     memory_free_list[r] = p;
  154.     memory_total_count[r]++;
  155.    }
  156. */
  157.  
  158.  return memory_free_list[size];
  159. }
  160.  
  161.  
  162. void memory_free_blocks()
  163.   register memory_elem_ptr p;
  164.  
  165.   while (memory_block_list)
  166.   { p = memory_block_list;
  167.     memory_block_list = p->next;
  168.     free((char*)p);
  169.   }
  170.  
  171.   for(int i=0; i <= memory_max_size; i++)
  172.   { memory_free_list[i] = 0;
  173.     memory_total_count[i] = 0;
  174.    }
  175.  
  176.   memory_block_count = 0;
  177. }
  178.  
  179.  
  180. memory_elem_ptr allocate_words(int size)
  181. { memory_elem_ptr p = memory_free_list[size];
  182.   if (p==0) p = memory_allocate_block(size);
  183.   memory_free_list[size] = p->next;
  184.   return p;
  185. }
  186.  
  187. void deallocate_words(void* p, int size)
  188. { memory_elem_ptr(p)->next = memory_free_list[size];
  189.   memory_free_list[size] = memory_elem_ptr(p);
  190.  }
  191.  
  192. void deallocate_list(void* head,void* tail, int size)
  193. { memory_elem_ptr(tail)->next = memory_free_list[size];
  194.   memory_free_list[size] = memory_elem_ptr(head);
  195.  }
  196.  
  197. memory_elem_ptr Allocate_words(int size)
  198. { memory_elem_ptr p = memory_free_list[size];
  199.   if (p==0) p = memory_allocate_block(size);
  200.   memory_free_list[size] = p->next;
  201.   cout << "Allocate   "<< int(p) << "(" << size << ")\n";
  202.   return p;
  203. }
  204.  
  205. void Deallocate_words(void* p, int size)
  206. { cout << "Deallocate  " << int(p) << "(" << size << ")\n";
  207.  
  208.   if (int(p)<=0 ) 
  209.     error_handler(1,"deallocate: illegal pointer");  
  210.  
  211.   memory_elem_ptr(p)->next = memory_free_list[size];
  212.   memory_free_list[size] = memory_elem_ptr(p);
  213. }
  214.  
  215. void print_statistics()
  216. {
  217.   cout.flush();
  218.  
  219.   int i,size;;
  220.   long int total,free,used;
  221.   long int total_bytes=0, free_bytes=0, used_bytes=0, b;
  222.   char* format="     | %3d    %5ld   %5ld   %5ld   %8ld bytes |\n";     
  223.  
  224.   printf("\n");
  225.   printf("     +-----------------------------------------------+\n");
  226.   printf("     | size    free    used   total      space       |\n"); 
  227.   printf("     +-----------------------------------------------+\n");
  228.  
  229.   for (i=1;i<=memory_max_size;i++)
  230.     if ((total = memory_total_count[i]) > 0 || memory_free_list[i])
  231.     { memory_elem_ptr p = memory_free_list[i];
  232.       size = memory_word_size*i;
  233.       free = 0;
  234.       while (p) { free++; p = p->next; }
  235.       b = total*size; 
  236.       used  = total - free;
  237.       free_bytes  = free_bytes  + free*size;
  238.       used_bytes  = used_bytes  + used*size;
  239.       total_bytes = total_bytes + b;
  240.       printf(format,i,free,used,total,b);
  241.      }
  242.  
  243.  printf("     +-----------------------------------------------+\n");
  244.  printf("     | %3d blocks                      %7.2f kb    |\n",memory_block_count,float(total_bytes)/1024);
  245.  printf("     +-----------------------------------------------+\n");
  246.  printf("\n");
  247.  fflush(stdout);
  248.  
  249. }
  250.  
  251.  
  252.  
  253. //------------------------------------------------------------------------------
  254. // Error Handling
  255. //------------------------------------------------------------------------------
  256.  
  257.  
  258. PEH p_error_handler = default_error_handler;
  259.  
  260. void default_error_handler(int i, char* s)
  261. { if (i==0) 
  262.    cout << "(warning) " << s << "\n";
  263.   else 
  264.    { cout << "ERROR "<< s << "\n";
  265.      cout.flush();
  266.      abort();
  267.     }
  268. }
  269.  
  270. PEH set_error_handler(PEH handler)
  271. { PEH old = error_handler;
  272.   p_error_handler = handler;
  273.   return old;
  274. }
  275.  
  276. //------------------------------------------------------------------------------
  277. // usefull functions
  278. //------------------------------------------------------------------------------
  279.  
  280. int interrupt_handler()
  281. { string cmd;
  282.  
  283.   cout << "\n INTERRUPT \n";
  284.  
  285.   for(;;) 
  286.   { newline;
  287.     cmd=read_string("m(emory) / t(ime) / !(cmd) / c(ontinue) / i(nterrupt): ");
  288.     switch(cmd[0]) 
  289.      { case 'c': { cout << "execution continued\n";
  290.                    return(0);
  291.                   }
  292.        case 'i': { cout << "execution terminated\n";
  293.                    exit(1);
  294.                   }
  295.        case 'm': { print_statistics(); 
  296.                    newline;
  297.                    break;
  298.                   }
  299.        case 't': { newline;
  300.  
  301. #ifndef __TURBOC__
  302.                    print_time();
  303. #endif
  304.                    break;
  305.                   }
  306.        case '!': { cmd[0] = ' ';
  307.                    system(cmd.cstring());
  308.                    newline;
  309.                    break;
  310.                   }
  311.       }
  312.     }
  313. }
  314.  
  315.  
  316.  
  317. void init_random(int seed)
  318. { long l = seed;
  319.   if (l==0) time(&l);
  320.   srandom(int(l));
  321. }
  322.  
  323. double rrandom()                { return double(random())/Infinity; }
  324.  
  325.  
  326. #ifndef __TURBOC__
  327.  
  328. double used_time()
  329. { tms x;
  330.   times(&x);
  331.   return  double(x.tms_utime)/60;
  332. }
  333.  
  334. double used_time(real& T)
  335. { double t = T;
  336.   tms x;
  337.   times(&x);
  338.   T =  double(x.tms_utime)/60;
  339.   return  double(T)-t;
  340. }
  341.  
  342. double used_time(double& T)
  343. { double t = T;
  344.   tms x;
  345.   times(&x);
  346.   T =  double(x.tms_utime)/60;
  347.   return  double(T)-t;
  348. }
  349.  
  350. float used_time(float& T)
  351. { float t = T;
  352.   tms x;
  353.   times(&x);
  354.   T =  float(x.tms_utime)/60;
  355.   return  float(T)-t;
  356. }
  357.  
  358. void print_time(string s)
  359. {
  360.   tms x;
  361.   times(&x);
  362.   float ut = float(x.tms_utime)/60;
  363.   float st = float(x.tms_stime)/60;
  364.   cerr << s;
  365.   cerr << string("user time: %.2f sec   system time: %.2f sec \n",ut,st);
  366. }
  367.  
  368. #else  /*  TURBOC */
  369.   
  370. double used_time()
  371. { clock_t x = clock();
  372.   return  double(x/CLK_TCK);
  373. }
  374.  
  375. double used_time(real& T)
  376. { double t = T;
  377.   T =  used_time();
  378.   return  double(T)-t;
  379. }
  380.  
  381. double used_time(double& T)
  382. { double t = T;
  383.   T =  used_time();
  384.   return  T-t;
  385. }
  386.  
  387. float used_time(float& T)
  388. { float t = T;
  389.   T =  used_time();
  390.   return  T-t;
  391. }
  392.  
  393. void print_time(string s)
  394. { cerr << s;
  395.   cerr << string("user time: %.2f sec   system time: 0.0 sec \n",used_time());
  396. }
  397.  
  398.  
  399. #endif
  400.  
  401. //------------------------------------------------------------------------------
  402. // Input/Ouput
  403. //------------------------------------------------------------------------------
  404.  
  405.  
  406. file_ostream::file_ostream(string fname) : ostream(&fb)
  407. { if (fb.open(~fname,output)==0)
  408.   error_handler(1,"cannot open file ");
  409.  }
  410.  
  411. void file_ostream::close() { fb.close(); clear(); }
  412.  
  413. bool file_ostream::open(string fname)
  414. { close();
  415.   return (fb.open(~fname,output) !=0);
  416.  }
  417.  
  418.  
  419.  
  420. file_istream::file_istream(string fname) : istream(&fb)
  421. { if (fb.open(~fname,input)==0)
  422.   error_handler(1,"cannot open file");
  423.  }
  424.  
  425. void file_istream::close() { fb.close(); clear(); }
  426.  
  427. bool file_istream::open(string fname)
  428. { close();
  429.   return (fb.open(~fname,input) !=0);
  430.  }
  431.  
  432. string_ostream::string_ostream() : ostream(2048,buf) {}
  433.  
  434. string string_ostream::str() 
  435. { return buf; }
  436.  
  437.  
  438. string_istream::string_istream(string s) : istream(2048,buf) 
  439. { for(int i=0;i<2048;i++) buf[i] = 0; 
  440.   strncpy(buf,~s,2048); 
  441.  }
  442.  
  443. string_istream::string_istream(char* s) : istream(2048,buf) 
  444. { for(int i=0;i<2048;i++) buf[i] = ' '; 
  445.   strncpy(buf,s,2048); 
  446.  }
  447.  
  448. string_istream::string_istream(int argc, char** argv) : istream(2048,buf) 
  449. { for(int i=0;i<2048;i++) buf[i] = 0; 
  450.   char* b = buf;
  451.   int rest = 2048;
  452.   for(i=0;i<argc;i++) 
  453.   { int l = strlen(argv[i]) + 1;
  454.     strncpy(b,argv[i],rest);
  455.     rest -= l;
  456.     b += l;
  457.     b[-1] = ' ';
  458.    }
  459.  }
  460.  
  461.  
  462. #ifndef __TURBOC__
  463.  
  464. #ifdef __GNUG__
  465.  
  466. cmd_ostream::cmd_ostream(string cmd) : ostream(popen(~cmd,"w")) {}
  467. cmd_istream::cmd_istream(string cmd) : istream(popen(~cmd,"r")) {}
  468.  
  469. #else
  470.  
  471. cmd_ostream::cmd_ostream(string cmd) : b(popen(~cmd,"w")), ostream(&b) {}
  472. cmd_istream::cmd_istream(string cmd) : b(popen(~cmd,"r")), istream(&b) {}
  473.  
  474. #endif
  475.  
  476. #endif
  477.  
  478.   
  479. int Yes(string s)
  480. { char answer = read_char(s);
  481.   return ((answer == 'y') || (answer == 'Y'));
  482. }
  483.  
  484. int read_int(string s)
  485. { int answer;
  486.   char c;
  487.   int success = 0;
  488.   while (!success)
  489.   { cout << s;
  490.     cin >> answer;
  491.     if (!cin) { cin.clear();
  492.                 cin.get(c);
  493.                 cout << string("read_int: illegal input \"%c\"\n",c);
  494.                 while (c!='\n') cin.get(c); //skip rest of line
  495.                }
  496.     else  success=1;
  497.    }
  498.   cin.get(c);   // eat '\n'
  499.  
  500.   return answer;
  501. }
  502.  
  503. char read_char(string s)
  504. { char c;
  505.   cout << s;
  506.   cin.get(c);
  507.   if (c!='\n') read_line(cin);
  508.   return c;
  509. }
  510.  
  511. double read_real(string s)
  512. { double answer;
  513.   cout << s;
  514.   cin >> answer;
  515.   read_line(cin);
  516.   return answer;
  517. }
  518.  
  519. string  read_line(istream& s)
  520. { char buf[256];
  521.   char* p = buf;
  522.  
  523.   while (s.get(*p))
  524.   { if (*p == '\n') break;
  525.     p++;
  526.    } 
  527.   *p = '\0';
  528.   return string(buf);
  529. }
  530.  
  531. string read_string(string s)
  532. { cout << s;
  533.   return read_line(cin); 
  534.  }
  535.  
  536. int     Yes()              { return Yes(""); }
  537. int     read_int()         { return read_int(""); }
  538. char    read_char()        { return read_char(""); }
  539. double  read_real()        { return read_real(""); }
  540. string  read_string()      { return read_string(""); } 
  541. string  read_line()        { return read_line(cin); }
  542.  
  543.