home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / src / graph / _g_array.c next >
Encoding:
C/C++ Source or Header  |  1991-11-15  |  3.3 KB  |  131 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  2.1.1                                                 11-15-1991
  4. +
  5. +
  6. +  _g_array.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/graph.h>
  16.  
  17.  
  18. //------------------------------------------------------------------------------
  19. // node and edge arrays       
  20. //------------------------------------------------------------------------------
  21.  
  22. #define graph_arrayimplement(itype)\
  23. \
  24. ent& graph_array(itype)::entry(itype x)\
  25. { if (g != graph_of(x) || index(x) > max_i)\
  26.          error_handler(102,"(node/edge)_array[x]: not defined for x");\
  27.  return arr[index(x)];\
  28. }\
  29. \
  30. ent  graph_array(itype)::inf(itype x) const\
  31. { if (g != graph_of(x) || index(x) > max_i)\
  32.          error_handler(102,"(node/edge)_array[x]: not defined for x");\
  33.  return arr[index(x)];\
  34. }\
  35. \
  36. void graph_array(itype)::init(const graph& G, int n, ent i)\
  37. { if (g!=0) clear();\
  38.   g = (graph*)&G;\
  39.   max_i=n-1;\
  40.   arr=new ent[n];\
  41.   if (arr==0)  error_handler(104,"(node/edge)_array.init(G): out of memory");\
  42.   if (max_i<0) error_handler(0,"(node/edge)_array.init(G): empty graph");\
  43.   for (int j=0;j<=max_i;j++) { copy_entry(i);\
  44.                                arr[j] = i;\
  45.                               }\
  46. }\
  47. \
  48. \
  49. void graph_array(itype)::init(const graph& G, ent i)\
  50. { init(G,name3(G.max_,itype,_i)()+1,i); }\
  51. \
  52. void graph_array(itype)::init(const graph_array(itype)& A)\
  53. { if (g!=0) clear();\
  54.   g = A.g;\
  55.   max_i=A.max_i;\
  56.   arr=new ent[max_i+1];\
  57.   if (arr==0)  error_handler(1,"(node/edge)_array.init: out of memory");\
  58.   for (int j=0;j<=max_i;j++)\
  59.   { ent x = A.arr[j];\
  60.     copy_entry(x);\
  61.     arr[j] = x;\
  62.    }\
  63. }\
  64. \
  65. void graph_array(itype)::clear()\
  66. { int j;\
  67.   for (j=0;j<=max_i;j++) clear_entry(arr[j]);\
  68.   if (arr) delete arr;\
  69.   arr=0; g=0; max_i = -1;\
  70. }\
  71.  
  72.  
  73. implement(graph_array,node)
  74. implement(graph_array,edge)
  75.  
  76. //------------------------------------------------------------------------------
  77. // node matrices
  78. //------------------------------------------------------------------------------
  79.  
  80. graph_array(node)& Node_Matrix::operator[](node v)
  81. { return *(M[v]); }
  82.  
  83. ent& Node_Matrix::entry(node v, node w)     { return M[v]->entry(w); }
  84. ent  Node_Matrix::inf(node v, node w) const { return M[v]->inf(w); }
  85.  
  86. void Node_Matrix::init(const graph& G, int n, ent x) 
  87. { int i,j;
  88.   g = (graph*)&G;
  89.   M.init(G,n,0);
  90.   for(i=0;i<M.size();i++) 
  91.     { M.elem(i) = new graph_array(node);
  92.       M.elem(i) -> init(G,n,0);
  93.       for(j=0;j<M.elem(i)->size();j++) 
  94.         { copy_entry(x);
  95.           M.elem(i)->elem(j) = x;
  96.          }
  97.      }
  98. }
  99.  
  100. void Node_Matrix::init(const graph& G, ent x) 
  101. {  init(G,G.max_node_i()+1,x); }
  102.  
  103. void Node_Matrix::init(Node_Matrix& A) 
  104. { int i,j;
  105.   g = A.g;
  106.   M.init(*g,0);
  107.   for(i=0;i<M.size();i++) 
  108.     { M.elem(i) = new graph_array(node);
  109.       M.elem(i) -> init(*g,0);
  110.       for(j=0;j<M.elem(i)->size();j++) 
  111.         { ent x = A.M.elem(i)->elem(j);
  112.           copy_entry(x);
  113.           M.elem(i)->elem(j) = x;
  114.          }
  115.      }
  116. }
  117.  
  118. void Node_Matrix::clear()
  119. { int i,j;
  120.   for(i=0;i<M.size();i++) 
  121.    { for(j=0;j<M.elem(i)->size();j++) 
  122.       { ent x = M.elem(i)->elem(j);
  123.         clear_entry(x); 
  124.        }
  125.      delete M.elem(i);
  126.     }
  127.   M.clear();
  128. }
  129.  
  130.