home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / diverses / leda / man / basics.tex < prev    next >
Encoding:
Text File  |  1991-11-15  |  25.5 KB  |  661 lines

  1. {\magtwo 1. Basics}
  2.  
  3. We start with an example. The following program counts the number of 
  4. occurrences of each string in a sequence of strings
  5.  
  6. \#include $<$LEDA/d\_array.h$>$
  7. \medskip
  8. {\bf declare2}(d\_array,string,int)
  9. \medskip
  10. \cleartabs
  11. \+main()\cr
  12. \+$\{$\ \ &\cr
  13. \+  &d\_array(string,int) $N(0)$;\cr
  14. \smallskip
  15. \+  &string $s$;\cr
  16. \smallskip
  17. \+  &{\bf while} &(cin $>>$ $s$ \&\& $s$ != ``stop'') $N[s]++$;\cr
  18. \smallskip
  19. \+  &{\bf forall\_defined}($s,N$) 
  20.             cout $<< s <<$ ``  " $<< N[s] <<$ ``$\backslash$n";\cr
  21. \smallskip
  22. \+\ $\}$\cr
  23. \bigskip
  24.  
  25. The program can  be compiled using the LEDA library (cf. section 1.9). When
  26. executed it reads a sequence of strings from the standard input
  27. until the string ``stop'' is encountered and then prints the number
  28. of occurrences of each string on the standard output. More examples
  29. of LEDA programs can be found throughout this manual.
  30.  
  31. The program above uses the data type dictionary array ($d\_array$)
  32. from the library. This is expressed by the include statement (cf.
  33. section 1.8 for more details). The specification of the data type 
  34. $d\_array$ can be found in section~4.4. We use it also as a running
  35. example to discuss the principles underlying LEDA in sections 1.1
  36. to 1.6.
  37.  
  38.  
  39. \bigskip
  40. {\magonebf 1.1 Specifications}
  41.  
  42. In general the specification of a LEDA data type consists of five parts:
  43. a definition of the set of objects comprising the (parameterized) abstract
  44. data type, a description of how to derive a concrete data type from a
  45. parameterized data type, a description of how to create an object of the data 
  46. type, the definition of the operations available on the objects of the data 
  47. type, and finally, informations about the implementation. The five parts 
  48. appear under the headers definition, type declaration, creation, operations,  
  49. and implementation respectively.
  50.  
  51.  
  52.  
  53. \bigskip
  54. $\bullet$ {\bf Definition}
  55. \smallskip
  56. This part of the specification defines the objects (also called instances or
  57. elements) comprising the data type using standard mathematical concepts and 
  58. notation. 
  59.  
  60. {\bf Example}, the generic data type dictionary array:
  61.  
  62. An object $a$ of type $d\_array(I,E)$ is an injective function from the 
  63. data type $I$ to the set of variables of data type $E$. The types $I$ and
  64. $E$ are called the index and the element type respectively, $a$ is called
  65. a dictionary array from $I$ to $E$.
  66.  
  67.  
  68. Note that the types $I$ and $E$ are parameters in the definition above.
  69. A concrete dictionary array type is declared by a type declaration 
  70. which we discuss next.
  71.  
  72.  
  73. \bigskip
  74. $\bullet$ {\bf Type Declaration}
  75. \smallskip
  76. This part gives the syntax for deriving specific data types from 
  77. parameterized or generic data types, i.e., it shows how to set the 
  78. formal type parameters of a generic data type to concrete data types. 
  79. For a generic data type XYZ with $k$ type parameters the statement
  80.  
  81. {\bf declare}$k$(XYZ,$t_1,\dots,t_k$)
  82.  
  83. introduces a new data type with name ``XYZ($t_1,t_2,...,t_k$)'',
  84. where $t_1,\dots,t_k$ are the names of the actual type parameters.
  85. (Due to a limitation of the implementation language \CC, the number
  86. $k$ of type parameters also appears in the name of the declare-macro.)
  87. For example,
  88.  
  89. {\bf declare2}($d\_array,string,int$)
  90.  
  91. introduces a new data type with name ``$d\_array(string,int)$''. An object
  92. of data type $d\_array(string,int)$ is a injective mapping from the set of
  93. all strings to the set of variables of type $int$.
  94.  
  95.  
  96. Only simple data types are allowed as actual type parameters in declarations 
  97. of parameterized data types. Simple data types are the \CC built in types 
  98. $char$ and $int$, all \CC pointer types, the LEDA types $bool$, $real$, 
  99. $string$, $vector$, and $matrix$ (cf. section~2), all basic two-dimensional 
  100. objects from section~6.1 ($point$, $segment$, $line$, $polygon$, $circle$),
  101. and all item types (cf. section~1.5). In order to realize generic data types 
  102. with more complicated subtypes (such as dictionaries, lists, graphs, \dots) 
  103. pointers to these types must be used.
  104.  
  105.  
  106. \bigskip
  107. $\bullet$ {\bf Creation}
  108. \smallskip
  109. A variable of a (previously declared) data type is introduced by a \CC 
  110. variable declaration. For all LEDA data types variables are initialized
  111. at the time of declaration. In many cases the user has to provide arguments 
  112. used for the initialization of the variable.  In general a declaration
  113.  
  114. XYZ($t_1,\dots,t_k$)\ \ \ $y(x_1,\dots,x_\ell)$;
  115.  
  116. introduces a variable $y$ of the data type with name ``XYZ($t_1,\dots,t_k$)''
  117. and uses the arguments $x_1,\dots,x_\ell$ to initialize it. For example,
  118.  
  119. $d\_array(string,int)$  $A(0)$
  120.  
  121. introduces $A$ as a dictionary array from strings to integers, and 
  122. initializes $A$ as follows: an injective function $a$ from $string$
  123. to the set of unused variables of type $int$ is constructed, and is
  124. assigned to $A$. Moreover, all variables in the range of $a$ are 
  125. initialized to 0.
  126. The reader may wonder how LEDA handles an array of infinite size. The
  127. solution is , of course, that only that part of $A$ is explicitly stored
  128. which has been accessed already.
  129.  
  130. For most data types, in particular for the simple types, the assignment
  131. operator is available for variables of that type. However, assignment is
  132. in general not a constant time operation, e.g., if $s_1$ and $s_2$ are
  133. variables of type $string$ then the assignment $s_1 = s_2$ takes time 
  134. proportional to the length of the value of $s_2$.
  135.  
  136. {\bf Remark}: For most of the complex data types of LEDA, e.g., dictionaries,
  137. lists, and priority queues, it is convenient to interpret a variable name
  138. as the name for an object of the data type which evolves over time by means
  139. of the operations applied to it. This is appropriate, whenever the operations 
  140. on a data type only ``modify'' the values of variables, e.g., it is more 
  141. natural to say an operation on a dictionary $D$ modifies $D$ than to say that 
  142. it takes the old value of $D$, constructs a new dictionary out of it, and 
  143. assigns the new value to $D$. Of course, both interpretations are equivalent.
  144. From this more object-oriented point of view, a variable declaration, e.g.,
  145. $dictionary(string,int)$  $D$, is creating a new dictionary object with name
  146. $D$ rather than introducing a new variable of type $dictionary(string,int)$;
  147. hence the name ``creation'' for this part of a specification.
  148.  
  149.  
  150.  
  151. \bigskip
  152. $\bullet$ {\bf Operations}
  153. \smallskip
  154. In this section the operations of the data types are described. For each 
  155. operation the description consists of two parts
  156.  
  157. \beginitem
  158. \item {a)}
  159. {The interface of the operation is defined using the \CC function declaration
  160. syntax. In this syntax the result type of the operation ($void$ if there is 
  161. no result) is followed by the operation name and an argument list specifying 
  162. the type of each argument. For example,}
  163. \item{}
  164. {$list\_item$ $L$.insert ($E\ x,\ list\_item\ it,\ rel\_pos\ p=after$)\nl
  165. defines the interface of the insert operation on a list $L$ of elements of type
  166. $E$ .(cf. section~3.7). Insert takes as arguments an element $x$ of type $E$, 
  167. a $list\_item$ $it$ and an optional relative position argument $p$. It returns 
  168. a $list\_item$ as result.  }
  169. \item{}
  170. {$E\&$  $A[I\ x]$\nl
  171. defines the interface of the access operation on a dictionary array $A$. It 
  172. takes an element of $I$ as an argument and returns a variable of type $E$.
  173. }
  174.  
  175. \bigskip
  176. \item {b)} 
  177. {The effect of the operation is defined.  Often the arguments have to 
  178. fulfill certain preconditions. If such a condition is violated the effect 
  179. of the operation is not defined. Some, but not all, of these cases result 
  180. in error messages and abnormal termination of the program (see also 
  181. section~7.5). }
  182. \item{}
  183. {For the insert operation on lists this definition reads:\nl
  184. A new item with contents $x$ is inserted after (if $p=after$) or before 
  185. (if $p=before$) item $it$ into $L$. The new item is returned.
  186. ({\it precondition}: item $it$ must be in $L$)}
  187. \item{}
  188. {For the access operation on dictionary arrays the definition reads:\nl
  189. returns the variable $A(x)$.
  190. }
  191. \enditem
  192.  
  193. \bigskip
  194. $\bullet$ {\bf  Implementation}
  195. \smallskip
  196. The implementation section lists the data structures used to implement
  197. the data type and gives the time bounds for the operations and the space 
  198. requirement. For example,
  199.  
  200. Dictionary arrays are implemented by red black trees. Access operations $A[x]$
  201. take time $O(\log dom(A))$. The space requirement is $O(dom(A))$.
  202.  
  203.  
  204. \bigskip
  205. \bigskip
  206. {\magonebf 1.2 Arguments}
  207.  
  208. \bigskip
  209. $\bullet$ {\bf Optional Arguments}
  210. \smallskip
  211. The trailing arguments in the argument list of an operation may be optional.
  212. If these trailing arguments are missing in a call of an operation the default 
  213. argument values given in the specification are used. For
  214. example, if the relative position argument in the list insert operation
  215. is missing it is assumed to have the value $after$, i.e.,
  216. $L$.insert($it,y$) will insert the item $<y>$ after item $it$ into $L$.
  217.  
  218. \bigskip
  219. $\bullet$ {\bf Argument Passing}
  220. \smallskip
  221. There are two kinds of argument passing in \CC, by value and by reference.
  222. An argument $x$  of type $type$  specified by ``$type\ x$'' in the argument 
  223. list of an operation or user defined function will be passed by value, i.e.,
  224. the operation or function is provided with a copy of $x$.
  225. The syntax for specifying an argument passed by reference is ``$type\&\ x$''.
  226. In this case the operation or function works directly on $x$ ( the variable
  227. $x$ is passed not its value).
  228.  
  229. Passing by reference must always be used if the operation is to change the
  230. value of the argument. It should always be used for passing large objects
  231. such as lists, arrays, graphs and other LEDA data types to functions.
  232. Otherwise a complete copy of the actual argument is made, which takes time
  233. proportional to its size, whereas  passing by reference always takes constant
  234. time. 
  235.  
  236.  
  237. \bigskip
  238. $\bullet$ {\bf Functions as Arguments}
  239. \smallskip
  240. Some operations take functions as arguments. For instance the bucket sort 
  241. operation on lists requires a function which maps the elements of the list into
  242. an interval of integers. We use the \CC syntax to define the type of a function
  243. argument $f$:
  244. $$T\ \ (*f)(T_1, T_2,\dots, T_k)$$ declares $f$ to be a function 
  245. taking $k$ arguments of the data types $T_1$, \dots, $T_k$,
  246. respectively, and returning a result of type $T$, i.e,
  247. $f: T_1 \times \dots \times T_k \longrightarrow T$ .
  248.  
  249.  
  250. \bigskip
  251. \bigskip
  252. {\magonebf 1.3 Overloading}
  253.  
  254. Operation and function names may be overloaded, i.e., there can be different 
  255. interfaces for the same operation. An example is the translate operations
  256. for points (cf. section~6.1).
  257. \smallskip
  258. $point$  $p$.translate($vector\ v$)\nl
  259. $point$  $p$.translate($real\ \alpha,\ real\ dist$)
  260.  
  261. It can either be called with a vector as argument or with two arguments
  262. of type $real$ specifying the direction and the distance of the translation.
  263.  
  264. An important overloaded function is discussed in the next section: Function 
  265. $compare$, used to define linear orders for simple data types.
  266.  
  267. \vfill\eject
  268.  
  269. \bigskip
  270. \bigskip
  271. {\magonebf 1.4 Linear Orders}
  272.  
  273. Many data types, such as dictionaries, priority queues, and sorted sequences
  274. require linearly ordered subtypes. Whenever a type $T$ is used in such a
  275. situation, e.g. in $declare2(dictionary,T,\dots)$ the function
  276. $$int\ \ compare(T\&,T\&)$$ must be declared and must define a linear order on
  277. the data type $T$.
  278.  
  279. A binary relation $rel$ on a set $T$ is called a linear order on $T$ if for
  280. all $x,y,z\in T$:
  281. \smallskip
  282. 1) $x$ $rel$ $y$\nl
  283. 2) $x$ $rel$ $y$ and $y$ $rel$ $z$ implies $x$ $rel$ $z$\nl
  284. 3) $x$ $rel$ $y$ or  $y$ $rel$ $x$\nl
  285. 4) $x$ $rel$ $y$ and $y$ $rel$ $x$ implies $x = y$
  286.  
  287. \medskip
  288. A function $int$ $compare(T\&,T\&)$ is said to define the linear order 
  289. $rel$ on $T$ if
  290. $$compare(x,y)\ \ \cases{ < 0, &if $x$ $rel$ $y$  and  $x\ne y$\cr 
  291.                        = 0, &if $x = y$\cr 
  292.                        > 0, &if $y$ $rel$ $x$  and  $x\ne y$\cr} $$
  293.  
  294.  
  295. For each of the simple data types $char$, $int$, $real$, $string$, and $point$ 
  296. a function $compare$ is predefined and defines the so-called default ordering
  297. on that type. The default ordering is the usual $\le$ - order for $char$,
  298. $int$, and $real$, the lexicographic ordering for $string$, and for $point$
  299. the lexicographic ordering of the cartesian coordinates.
  300. For all other simple types $T$ there is no default ordering, and the user has 
  301. to provide a $compare$ function whenever a linear order on $T$ is required.
  302.  
  303.  
  304.  
  305. {\bf Example}: Suppose pairs of real numbers shall be used as keys 
  306. in a dictionary with the lexicographic order of their components.
  307. First we declare type $pair$ as the type of pointers to pairs of real
  308. numbers, and then we define the lexicographic order on $pair$ by declaring
  309. an appropriate compare function.
  310.  
  311.  
  312. \bigskip
  313. \cleartabs
  314. \+{\bf struct} Pair $\{$\cr
  315. \+\quad $real\ \ x$;\cr
  316. \+\quad $real\ \ y$;\cr
  317. \+$\}$;\cr
  318. \+\cr
  319. \+{\bf typedef} Pair$*$ \ $pair$;\cr
  320.  
  321. \vfill\eject
  322.  
  323. \+$int$\  \ \ compare($pair\&\ p,\ pair\&\ q$)\cr
  324. \+$\{$\ \ &{\bf if} ($p\rightarrow x < q\rightarrow x$) {\bf return } -&1;\cr 
  325. \+        &{\bf if} ($p\rightarrow x > q\rightarrow x$) {\bf return }  &1;\cr 
  326. \+        &{\bf if} ($p\rightarrow y < q\rightarrow y$) {\bf return } -&1;\cr 
  327. \+        &{\bf if} ($p\rightarrow y > q\rightarrow y$) {\bf return }  &1;\cr 
  328. \+        &{\bf return} 0;\cr
  329. \+$\ \}$\cr
  330.  
  331. {\bf declare2}($dictionary,\ pair,\ \dots\ $);
  332.  
  333.  
  334. \bigskip
  335. Sometimes, a user may need additional linear orders on a simple data type 
  336. $T$ which are different from the order defined by $compare$, e.g., he might 
  337. want to order points in the plane by the lexicographic ordering of their 
  338. cartesian coordinates and by their polar coordinates. In this example, the 
  339. former ordering is the default ordering for points. The user can introduce 
  340. an alternative ordering on the data type $point$ (cf. section 6.1) by defining 
  341. an appropriate comparing function $int$ $cmp(point\&,point\&)$ and then 
  342. declaring the type $POINT(cmp)$ with ``declare($POINT,cmp$)''. $POINT$  is a 
  343. parameterized data type (cf.  section 1.1) with one parameter which must be 
  344. the name of a comparing function. All data types $POINT(cmp)$ derived from 
  345. $POINT$ are equivalent to the data type $point$, with the only exception that 
  346. if $POINT(cmp)$ is used as an actual parameter in a type declaration, e.g. in
  347. ``declare2($dictionary,POINT(cmp),\dots$)'', the resulting type (
  348. $dictionary(POINT(cmp),\dots)$) is based on the linear order defined by $cmp$. 
  349. For every simple data type $t$ (except of pointer types) there exists such an 
  350. equivalent parameterized type $T$ which can be used to define additional linear
  351. orders on $t$ by declaring types $T(cmp)$ as described for points. The name of 
  352. $T$ is always the name of $t$ written with capital letters.
  353.  
  354.  
  355. In the example, we first declare a function $pol\_cmp$ and the type 
  356. $POINT(pol\_cmp)$.
  357.  
  358. \+$int$  $pol\_cmp(point\&\ x,\ point\&\ y)$\cr
  359. \+$\{$\ //lexicographic ordering on polar coordinates\cr
  360. \+\ $\}$\cr
  361. \+\cr
  362. \+{\bf declare}($POINT,\ pol\_cmp$)\cr
  363.  
  364. Now, dictionaries based on either ordering can be defined.
  365.  
  366. \+{\bf declare2}($dictionary,\ point,\ \dots\ $) \cr
  367. \+{\bf declare2}($dictionary,\ POINT(pol\_cmp),\ \dots\ $)\cr
  368.  
  369. \vfill\eject
  370.  
  371. \cleartabs
  372. \+main()\cr
  373. \+$\{$\ &\cr
  374. \+   &dictionary($POINT(pol\_cmp),\ int$) $D_1$; \quad &//polar ordering\cr
  375. \smallskip
  376. \+   &dictionary($point,\ int$) $D_0$;                 &//default ordering\cr
  377. \smallskip
  378. \+\ $\}$\cr
  379.  
  380. {\bf Remark}: We have chosen to associate a fixed linear order with most of
  381. the simple types (by predefining the function $compare$). This order is used
  382. whenever operations require a linear order on the type, e.g., the operations
  383. on a dictionary. Alternatively, we could have required the user to specify a
  384. linear order each time he uses a simple type in a situation where an ordering
  385. is needed, e.g., a user could define
  386. \smallskip
  387. \cleartabs
  388. \+\quad\quad\quad &declare3(dictionary,point,lexicographic\_ordering,\dots)\cr
  389. \+and\cr
  390. \+                &declare3(dictionary,point,polar\_ordering,\dots)\cr
  391.  
  392. This alternative would handle the cases where two or more different orderings
  393. are needed more elegantly. However, we have chosen the first alternative
  394. because of the smaller implementation effort.
  395.  
  396.  
  397.  
  398.  
  399. \bigskip
  400. \bigskip
  401. {\magonebf 1.5 Items }
  402.  
  403. Many of the advanced data types in LEDA (e.g. dictionaries), are defined in
  404. terms of so-called items. An item is a container which can hold an object
  405. relevant for the data type. For example, in the case of dictionaries a
  406. $dic\_item$ contains a pair consisting of a key and an information.
  407. A general definition of items will be given at the end of this section. 
  408.  
  409. We now discuss the role of items for the dictionary example in some detail. 
  410. A popular specification of dictionaries defines a dictionary as a partial
  411. function from some type $K$ to some other type $I$, or alternatively, as a
  412. set of pairs from $K\times I$, i.e., as the graph of the function. In an
  413. implementation each pair $(k,i)$ in the dictionary is stored in some location
  414. of the memory. Efficiency dictates that the pair $(k,i)$ cannot only be
  415. accessed through the key $k$ but sometimes also through the location where it
  416. is stored, e.g., we might want to lookup the information $i$ associated with
  417. key $k$ (this involves a search in the data structure), then compute with the
  418. value $i$ a new value $i\'$, and finally associate the new value with $k$.
  419. This either involves another search in the data structure or, if the lookup
  420. returned the location where the pair $(k,i)$ is stored, can be done by direct
  421. access. Of course, the second solution is more efficient and we therefore
  422. wanted to provide it in LEDA.
  423.  
  424. In LEDA items play the role of positions or locations in data structures.
  425. Thus an object of type $dictionary(K,I)$, where $K$ and $I$ are types, is 
  426. defined as a collection of items (type $dic\_item$) where each item contains 
  427. a pair in $K\times I$. We use $<k,i>$ to denote an item with key $k$ and 
  428. information $i$ and require that for each $k\in K$ there is at most one 
  429. $i\in I$ such that $<k,i>$ is in the dictionary. In mathematical terms this
  430. definition may be rephrased as follows: A dictionary $d$ is a partial
  431. function from the set $dic\_item$ to the set $K\times I$. Moreover, for each
  432. $k\in K$ there is at most one $i\in I$ such that the pair $(k,i)$ is in $d$.
  433.  
  434. The functionality of the operations 
  435. \smallskip
  436. \+\quad\quad &$dic\_item$ &$D$.lookup($K\ k$)\cr
  437. \+           &$I$         &$D$.inf($dic\_item\ it$)\cr
  438. \+           &$void$      &$D$.change\_inf($dic\_item\ it,\ I\ i\'$)\cr
  439. is now as follows:
  440. $D$.lookup($k$) returns an item $it$ with contents $(k,i)$, $D$.inf($it$) 
  441. extracts $i$ from $it$, and a new value $i\'$ can be associated with $k$ by
  442. $D$.change\_inf($it,i\'$).
  443.  
  444.  
  445. Let us have a look at the insert operation for dictionaries next:
  446. \smallskip
  447. \+&$dic\_item$ &$D$.insert($K\ k,\ I\ i$)\cr
  448.  
  449. There are two cases to consider. If $D$ contains an item $it$ with contents
  450. $(k,i\')$ then $i\'$ is replaced by $i$ and $it$ is returned. If $D$ contains
  451. no such item, then a new item, i.e., an item which is not contained in any 
  452. dictionary, is added to $D$, this item is made to contain $(k,i)$ and is
  453. returned. In this manual (cf. section 4.3) all of this is abbreviated to
  454.  
  455. \smallskip
  456. \+&\cleartabs
  457.    $dic\_item$\ \ \ $D$.insert($K\ k,\  I\ i$) \quad 
  458.                          &associates the information $i$ with the key $k$.\cr
  459. \+&                      &If there is an item $<k,j>$ in $D$ then $j$ is\cr
  460. \+&                      &replaced by i, else a new item $<k,i>$ is added\cr
  461. \+&                      &to $D$. In both cases the item is returned.\cr
  462.  
  463.  
  464. We now turn to a general discussion. With some LEDA types $XYZ$ there is an
  465. associated type $XYZ\_item$ of items. Nothing is known about the objects of
  466. type $XYZ\_item$ except that there are infinitely many of them. The only
  467. operations available on $XYZ\_items$ besides the one defined in the
  468. specification of type $XYZ$ is the equality predicate ``=='' and the assignment
  469. operator ``='' . The objects of type $XYZ$ are defined as sets or sequences of
  470. $XYZ\_items$ containing objects of some other type $Z$. In this situation an
  471. $XYZ\_item$ containing an object $z\in Z$ is denoted by $<z>$. A new or unused
  472. $XYZ\_item$ is any $XYZ\_item$ which is not part of any object of type $XYZ$.
  473.  
  474. {\bf Remark}: For some readers it may be useful to interpret a $dic\_item$ as
  475. a pointer to a variable of type $K\times I$. The differences are that the
  476. assignment to the variable contained in a $dic\_item$ is restricted, e.g., the
  477. $K$-component cannot be changed, and that in return for this restriction the
  478. access to $dic\_items$ is more flexible than for ordinary variables, e.g.,
  479. access through the value of the $K$-component is possible.
  480.  
  481.  
  482.  
  483. \bigskip
  484. \bigskip
  485. {\magonebf 1.6 Input and output}
  486.  
  487. Some parameterized data types (e.g. $list$) provide the operations $print$ and 
  488. $read$ for printing their contents to an output stream and for initializing
  489. an instance of this type by inserting elements read from an input stream.
  490. There are two overloaded functions which can be used for defining input and 
  491. output functions for user-defined pointer types which are used by $read$ and
  492. $print$ operations (and sometimes for error messages). 
  493.  
  494. $void$\ \  $Read(T\&,\ istream\&)$  $\{$ \dots $\}$\nl
  495. defines an input function for reading objects of type $T$ from an input stream.
  496.  
  497. $void$\ \  $Print(T\&,\ ostream\&)$  $\{$ \dots $\}$ \nl
  498. defines an output function for printing objects of type $T$ to an output stream.
  499.  
  500. \bigskip
  501. {\bf Example}: We declare the data type $list(pair)$ (see section~3.7)
  502. and want to read and print lists of pairs. Note that the $Read$ and $Print$
  503. functions have to be declared before the declaration of the list type.
  504.  
  505. $void$ $Read(pair\&\ p,\ istream\&\ IN)$  $\{$  $IN >> p\rightarrow x  >> p\rightarrow y $; $\}$
  506.  
  507. $void$ $Print(pair\&\ p,\ ostream\&\ OUT)$ $\{$  $OUT << p\rightarrow x << $" "$ << p\rightarrow y $; $\}$
  508.  
  509. {\bf declare}(list, pair)
  510. \smallskip
  511. \vdots
  512. \smallskip
  513. \cleartabs
  514. \+&\ \ \ &list(pair) $L$;\cr
  515. \smallskip
  516. \+&      &$L$.read(``L = '');\cr
  517. \+&      &$L$.print(``L = '');\cr
  518.  
  519.  
  520.  
  521. \bigskip
  522. \bigskip
  523. {\magonebf 1.7 Iteration}
  524.  
  525. For many data types LEDA provides iteration macros. These macros can be
  526. used to iterate over the elements of lists, sets and dictionaries or
  527. the nodes and edges of a graph. Iteration macros can be used similarly to 
  528. the \CC {\bf for} statement. Examples are
  529. \smallskip
  530. for lists and sets:
  531. \smallskip
  532. {\bf forall}($x,L$) $\{$ the elements of $L$ are successively assigned
  533. to $x \}$
  534. \smallskip
  535. for graphs:
  536. \smallskip
  537. {\bf forall\_nodes}($v,G$) $\{$ the nodes of $G$ are successively
  538. assigned to $v \}$
  539. \smallskip
  540. {\bf forall\_adj\_nodes}($w,v$) $\{$ the neighbor nodes of $v$ are
  541. successively assigned to $w \}$
  542.  
  543. {\bf Note}: Update operations on an object $x$ are not allowed inside
  544. the body of an iteration statement for $x$.
  545.  
  546.  
  547. \bigskip
  548. \bigskip
  549. {\magonebf 1.8 Header Files}
  550.  
  551. LEDA data types and algorithms can be used in any \CC program as described 
  552. in this manual. The specifications (class declarations) are contained
  553. in header files. To use a specific data type its header file has to be 
  554. included into the program. In general the header file for data type XYZ is 
  555. $<$LEDA/XYZ.h$>$. Exceptions are
  556.  
  557.  
  558. \smallskip
  559. $<$LEDA/basic.h$>$
  560.  
  561. This header file contains the declarations for the simple data types 
  562. $bool$, $real$, $string$ (section~2), and the functions, macros, and stream
  563. data types described in section~7.
  564.  
  565.  
  566. \smallskip
  567. $<$LEDA/graph.h$>$
  568.  
  569. contains the declarations of data types $graph$, $GRAPH$, $node\_array$ 
  570. and $edge\_array$ (section~5).
  571.  
  572. \smallskip
  573. $<$LEDA/ugraph.h$>$
  574.  
  575. contains the declarations of types $ugraph$ and $UGRAPH$ (section~5).
  576.  
  577.  
  578. \smallskip
  579. $<$LEDA/graph\_alg.h$>$
  580.  
  581. contains the declarations of all graph and network algorithms and predefines
  582. some node/edge array types (see section~5.12).
  583.  
  584.  
  585. \smallskip
  586. $<$LEDA/plane.h$>$
  587.  
  588. contains the two-dimensional objects $point$, $segment$, $line$, $polygon$, 
  589. and $circle$ (section~6)
  590.  
  591.  
  592. \smallskip
  593. $<$LEDA/plane\_alg.h$>$
  594.  
  595. contains some algorithms fortwo-dimensional geometry (section~6.1).
  596.  
  597.  
  598. \vfill\eject
  599.  
  600.  
  601. \bigskip
  602. \bigskip
  603. {\magonebf 1.9 Libraries}
  604.  
  605. The implementions of all LEDA data types and algorithms are precompiled and 
  606. contained in 5 libraries (libL.a, libG.a, libP.a, libWs.a, libWx.a) 
  607. which can be linked with \CC application programs. In the following
  608. description it is assumed that these libraries are installed in one of the 
  609. systems default library directories (e.g. /usr/lib), which allows to use 
  610. the ``-l\dots'' compiler option.
  611.  
  612. \medskip
  613. a) {\bf libL.a}
  614. is the main LEDA library, it contains the implementations of all simple 
  615. data types (section~2), basic data types (section~3), dictionaries and priority
  616. queues (section~4). A program $prog.c$ using any of these data types has to
  617. linked with the libL.a library like this:
  618.  
  619. CC $prog.c$ -lL
  620.  
  621.  
  622. \medskip
  623. b) {\bf libG.a}
  624. is the LEDA graph library. It contains the implementations of all graph 
  625. data types and algorithms (section~5). To compile a program using any graph 
  626. data type or algorithm the libG.a and libL.a library have to be used:
  627.  
  628. CC $prog.c$ -lG -lL
  629.  
  630.  
  631. \medskip
  632. c) {\bf libP.a}
  633. is the LEDA library for geometry in the plane. It contains the 
  634. implementations of all data types and algorithms for two-dimensional 
  635. geometry (section~6). To compile a program using two-dimensional data 
  636. types or algorithms the libP.a, libG.a, libL.a and maths libraries have 
  637. to be used:
  638.  
  639. CC $prog.c$ -lP -lG -lL.a -lm 
  640.  
  641. \medskip
  642. d) {\bf libWs.a}, {\bf libWx.a}
  643. are the LEDA libraries for graphic windows under the SunView or X11 
  644. (xview) window systems. Application programs using data type $window$ 
  645. (cf. section~6.7) have to be linked with one of these libraries:
  646.  
  647. \beginitem
  648. \item{ a)} 
  649. For the SunView window system:\nl
  650. CC $prog.c$ -lWs -lP -lG -lL -lm  -lsuntool -lsunwindow -lpixrect
  651.  
  652. \item{ b)} 
  653. For the X11 (xview) window system:\nl
  654. CC $prog.c$ -lWx -lP -lG -lL -lm  -lxview -lolgx -lX11 
  655.  
  656. \enditem
  657.  
  658. Note that the libraries always must be given in the order -lW? -lP -lG -lL.
  659.  
  660. \vfill\eject
  661.