File: manual045.html

package info (click to toggle)
ocaml-doc 2.04-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,820 kB
  • ctags: 997
  • sloc: makefile: 38; sh: 12
file content (136 lines) | stat: -rw-r--r-- 4,834 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
            "http://www.w3.org/TR/REC-html40/loose.dtd">
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset= ISO-8859-1">
<TITLE>
 Module Map: association tables over ordered types
</TITLE>
</HEAD>
<BODY >
<A HREF="manual044.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual046.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
<HR>

<H2>17.15&nbsp;&nbsp; Module <TT>Map</TT>: association tables over ordered types</H2><A NAME="s:Map"></A>
<A NAME="@manual384"></A><BLOCKQUOTE>
This module implements applicative association tables, also known as
finite maps or dictionaries, given a total ordering function
over the keys.
All operations over maps are purely applicative (no side-effects).
The implementation uses balanced binary trees, and therefore searching
and insertion take time logarithmic in the size of the map. 
</BLOCKQUOTE>
<PRE>
module type OrderedType =
  sig
    type t
    val compare: t -&gt; t -&gt; int
  end
</PRE>
<A NAME="@manual385"></A><BLOCKQUOTE>
The input signature of the functor <CODE>Map.Make</CODE>.
<CODE>t</CODE> is the type of the map keys.
<CODE>compare</CODE> is a total ordering function over the keys.
This is a two-argument function <CODE>f</CODE> such that
<CODE>f e1 e2</CODE> is zero if the keys <CODE>e1</CODE> and <CODE>e2</CODE> are equal,
<CODE>f e1 e2</CODE> is strictly negative if <CODE>e1</CODE> is smaller than <CODE>e2</CODE>,
and <CODE>f e1 e2</CODE> is strictly positive if <CODE>e1</CODE> is greater than <CODE>e2</CODE>.
Example: a suitable ordering function is
the generic structural comparison function <CODE>compare</CODE>. 
</BLOCKQUOTE>
<PRE>
module type S =
  sig
    type key
</PRE>
<BLOCKQUOTE>
The type of the map keys. 
</BLOCKQUOTE>
<PRE>
    type 'a t
</PRE>
<BLOCKQUOTE>
The type of maps from type <CODE>key</CODE> to type <CODE>'a</CODE>. 
</BLOCKQUOTE>
<PRE>
    val empty: 'a t
</PRE>
<BLOCKQUOTE>
The empty map. 
</BLOCKQUOTE>
<PRE>
    val add: key -&gt; 'a -&gt; 'a t -&gt; 'a t
</PRE>
<BLOCKQUOTE>
<CODE>add x y m</CODE> returns a map containing the same bindings as
<CODE>m</CODE>, plus a binding of <CODE>x</CODE> to <CODE>y</CODE>. If <CODE>x</CODE> was already bound
in <CODE>m</CODE>, its previous binding disappears. 
</BLOCKQUOTE>
<PRE>
    val find: key -&gt; 'a t -&gt; 'a
</PRE>
<BLOCKQUOTE>
<CODE>find x m</CODE> returns the current binding of <CODE>x</CODE> in <CODE>m</CODE>,
or raises <CODE>Not_found</CODE> if no such binding exists. 
</BLOCKQUOTE>
<PRE>
    val remove: key -&gt; 'a t -&gt; 'a t
</PRE>
<BLOCKQUOTE>
<CODE>remove x m</CODE> returns a map containing the same bindings as
<CODE>m</CODE>, except for <CODE>x</CODE> which is unbound in the returned map. 
</BLOCKQUOTE>
<PRE>
    val mem:  key -&gt; 'a t -&gt; bool
</PRE>
<BLOCKQUOTE>
<CODE>mem x m</CODE> returns <CODE>true</CODE> if <CODE>m</CODE> contains a binding for <CODE>m</CODE>,
and <CODE>false</CODE> otherwise. 
</BLOCKQUOTE>
<PRE>
    val iter: (key -&gt; 'a -&gt; unit) -&gt; 'a t -&gt; unit
</PRE>
<BLOCKQUOTE>
<CODE>iter f m</CODE> applies <CODE>f</CODE> to all bindings in map <CODE>m</CODE>.
<CODE>f</CODE> receives the key as first argument, and the associated value
as second argument. The order in which the bindings are passed to
<CODE>f</CODE> is unspecified. Only current bindings are presented to <CODE>f</CODE>:
bindings hidden by more recent bindings are not passed to <CODE>f</CODE>. 
</BLOCKQUOTE>
<PRE>
    val map: ('a -&gt; 'b) -&gt; 'a t -&gt; 'b t
</PRE>
<BLOCKQUOTE>
<CODE>map f m</CODE> returns a map with same domain as <CODE>m</CODE>, where the
associated value <CODE>a</CODE> of all bindings of <CODE>m</CODE> has been
replaced by the result of the application of <CODE>f</CODE> to <CODE>a</CODE>.
The order in which the associated values are passed to <CODE>f</CODE>
is unspecified. 
</BLOCKQUOTE>
<PRE>
    val fold: (key -&gt; 'a -&gt; 'b -&gt; 'b) -&gt; 'a t -&gt; 'b -&gt; 'b
</PRE>
<BLOCKQUOTE>
<CODE>fold f m a</CODE> computes <CODE>(f kN dN ... (f k1 d1 a)...)</CODE>,
where <CODE>k1 ... kN</CODE> are the keys of all bindings in <CODE>m</CODE>,
and <CODE>d1 ... dN</CODE> are the associated data.
The order in which the bindings are presented to <CODE>f</CODE> is
unspecified. 
</BLOCKQUOTE>
<PRE>
  end
module Make(Ord: OrderedType): (S with type key = Ord.t)
</PRE>
<A NAME="@manual386"></A><BLOCKQUOTE>
Functor building an implementation of the map structure
given a totally ordered type. 
</BLOCKQUOTE>

<HR>
<A HREF="manual044.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A>
<A HREF="manual046.html"><IMG SRC ="next_motif.gif" ALT="Next"></A>
<A HREF="manual030.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A>
</BODY>
</HTML>