File size: 34,419 Bytes
9375c9a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - bottom_up_cluster.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2015  Davis E. King (davis@dlib.net)
</font><font color='#009900'>// License: Boost Software License   See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_BOTTOM_uP_CLUSTER_Hh_
<font color='#0000FF'>#define</font> DLIB_BOTTOM_uP_CLUSTER_Hh_

<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>queue<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>map<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='bottom_up_cluster_abstract.h.html'>bottom_up_cluster_abstract.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../matrix.h.html'>../matrix.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../disjoint_subsets.h.html'>../disjoint_subsets.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../graph_utils.h.html'>../graph_utils.h</a>"


<font color='#0000FF'>namespace</font> dlib
<b>{</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>namespace</font> buc_impl
    <b>{</b>
        <font color='#0000FF'>inline</font> <font color='#0000FF'><u>void</u></font> <b><a name='merge_sets'></a>merge_sets</b> <font face='Lucida Console'>(</font>
            matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> dists,
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> dest,
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> src
        <font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#979000'>0</font>; r <font color='#5555FF'>&lt;</font> dists.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font>
                <font color='#BB00BB'>dists</font><font face='Lucida Console'>(</font>dest,r<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#BB00BB'>dists</font><font face='Lucida Console'>(</font>r,dest<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> std::<font color='#BB00BB'>max</font><font face='Lucida Console'>(</font><font color='#BB00BB'>dists</font><font face='Lucida Console'>(</font>r,dest<font face='Lucida Console'>)</font>, <font color='#BB00BB'>dists</font><font face='Lucida Console'>(</font>r,src<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#0000FF'>struct</font> <b><a name='compare_dist'></a>compare_dist</b>
        <b>{</b>
            <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font face='Lucida Console'>(</font>
                <font color='#0000FF'>const</font> sample_pair<font color='#5555FF'>&amp;</font> a,
                <font color='#0000FF'>const</font> sample_pair<font color='#5555FF'>&amp;</font> b
            <font face='Lucida Console'>)</font> <font color='#0000FF'>const</font>
            <b>{</b>
                <font color='#0000FF'>return</font> a.<font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&gt;</font> b.<font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <b>}</b>
        <b>}</b>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>template</font> <font color='#5555FF'>&lt;</font>
        <font color='#0000FF'>typename</font> EXP
        <font color='#5555FF'>&gt;</font>
    <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> <b><a name='bottom_up_cluster'></a>bottom_up_cluster</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> matrix_exp<font color='#5555FF'>&lt;</font>EXP<font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> dists_,
        std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> labels,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> min_num_clusters,
        <font color='#0000FF'><u>double</u></font> max_dist <font color='#5555FF'>=</font> std::numeric_limits<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font>::<font color='#BB00BB'>infinity</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        matrix<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font> dists <font color='#5555FF'>=</font> matrix_cast<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font><font face='Lucida Console'>(</font>dists_<font face='Lucida Console'>)</font>;
        <font color='#009900'>// make sure requires clause is not broken
</font>        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font>dists.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> dists.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&amp;</font><font color='#5555FF'>&amp;</font> min_num_clusters <font color='#5555FF'>&gt;</font> <font color='#979000'>0</font>, 
            "<font color='#CC0000'>\t unsigned long bottom_up_cluster()</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t Invalid inputs were given to this function.</font>"
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t dists.nr(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> dists.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> 
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t dists.nc(): </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> dists.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> 
            <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>\n\t min_num_clusters: </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> min_num_clusters 
            <font face='Lucida Console'>)</font>;

        <font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> buc_impl;

        labels.<font color='#BB00BB'>resize</font><font face='Lucida Console'>(</font>dists.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        disjoint_subsets sets;
        sets.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font>dists.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>labels.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font> <font color='#979000'>0</font>;

        <font color='#009900'>// push all the edges in the graph into a priority queue so the best edges to merge
</font>        <font color='#009900'>// come first.
</font>        std::priority_queue<font color='#5555FF'>&lt;</font>sample_pair, std::vector<font color='#5555FF'>&lt;</font>sample_pair<font color='#5555FF'>&gt;</font>, compare_dist<font color='#5555FF'>&gt;</font> que;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#979000'>0</font>; r <font color='#5555FF'>&lt;</font> dists.<font color='#BB00BB'>nr</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font>
            <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> c <font color='#5555FF'>=</font> r<font color='#5555FF'>+</font><font color='#979000'>1</font>; c <font color='#5555FF'>&lt;</font> dists.<font color='#BB00BB'>nc</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>c<font face='Lucida Console'>)</font>
                que.<font color='#BB00BB'>push</font><font face='Lucida Console'>(</font><font color='#BB00BB'>sample_pair</font><font face='Lucida Console'>(</font>r,c,<font color='#BB00BB'>dists</font><font face='Lucida Console'>(</font>r,c<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        <font color='#009900'>// Now start merging nodes.
</font>        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> iter <font color='#5555FF'>=</font> min_num_clusters; iter <font color='#5555FF'>&lt;</font> sets.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>iter<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// find the next best thing to merge.
</font>            <font color='#0000FF'><u>double</u></font> best_dist <font color='#5555FF'>=</font> que.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> a <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>que.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>index1</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> b <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>que.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>index2</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            que.<font color='#BB00BB'>pop</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <font color='#009900'>// we have been merging and modifying the distances, so make sure this distance
</font>            <font color='#009900'>// is still valid and these guys haven't been merged already.
</font>            <font color='#0000FF'>while</font><font face='Lucida Console'>(</font>a <font color='#5555FF'>=</font><font color='#5555FF'>=</font> b <font color='#5555FF'>|</font><font color='#5555FF'>|</font> best_dist <font color='#5555FF'>&lt;</font> <font color='#BB00BB'>dists</font><font face='Lucida Console'>(</font>a,b<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#009900'>// Haven't merged it yet, so put it back in with updated distance for
</font>                <font color='#009900'>// reconsideration later.
</font>                <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>a <font color='#5555FF'>!</font><font color='#5555FF'>=</font> b<font face='Lucida Console'>)</font>
                    que.<font color='#BB00BB'>push</font><font face='Lucida Console'>(</font><font color='#BB00BB'>sample_pair</font><font face='Lucida Console'>(</font>a, b, <font color='#BB00BB'>dists</font><font face='Lucida Console'>(</font>a, b<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

                best_dist <font color='#5555FF'>=</font> que.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                a <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>que.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>index1</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                b <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>que.<font color='#BB00BB'>top</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>.<font color='#BB00BB'>index2</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
                que.<font color='#BB00BB'>pop</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
            <b>}</b>


            <font color='#009900'>// now merge these sets if the best distance is small enough
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>best_dist <font color='#5555FF'>&gt;</font> max_dist<font face='Lucida Console'>)</font>
                <font color='#0000FF'>break</font>;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> news <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>merge_sets</font><font face='Lucida Console'>(</font>a,b<font face='Lucida Console'>)</font>;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> olds <font color='#5555FF'>=</font> <font face='Lucida Console'>(</font>news<font color='#5555FF'>=</font><font color='#5555FF'>=</font>a<font face='Lucida Console'>)</font>?b:a;
            <font color='#BB00BB'>merge_sets</font><font face='Lucida Console'>(</font>dists, news, olds<font face='Lucida Console'>)</font>;
        <b>}</b>

        <font color='#009900'>// figure out which cluster each element is in.  Also make sure the labels are
</font>        <font color='#009900'>// contiguous.
</font>        std::map<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font>, <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font><font color='#5555FF'>&gt;</font> relabel;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> r <font color='#5555FF'>=</font> <font color='#979000'>0</font>; r <font color='#5555FF'>&lt;</font> labels.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>r<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> l <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>r<font face='Lucida Console'>)</font>;
            <font color='#009900'>// relabel to make contiguous
</font>            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>relabel.<font color='#BB00BB'>count</font><font face='Lucida Console'>(</font>l<font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> next <font color='#5555FF'>=</font> relabel.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
                relabel[l] <font color='#5555FF'>=</font> next;
            <b>}</b>
            labels[r] <font color='#5555FF'>=</font> relabel[l];
        <b>}</b>


        <font color='#0000FF'>return</font> relabel.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>struct</font> <b><a name='snl_range'></a>snl_range</b>
    <b>{</b>
        <b><a name='snl_range'></a>snl_range</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> <font color='#0000FF'>default</font>;
        <b><a name='snl_range'></a>snl_range</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> val<font face='Lucida Console'>)</font> : lower<font face='Lucida Console'>(</font>val<font face='Lucida Console'>)</font>, upper<font face='Lucida Console'>(</font>val<font face='Lucida Console'>)</font> <b>{</b><b>}</b>
        <b><a name='snl_range'></a>snl_range</b><font face='Lucida Console'>(</font><font color='#0000FF'><u>double</u></font> l, <font color='#0000FF'><u>double</u></font> u<font face='Lucida Console'>)</font> : lower<font face='Lucida Console'>(</font>l<font face='Lucida Console'>)</font>, upper<font face='Lucida Console'>(</font>u<font face='Lucida Console'>)</font> <b>{</b> <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>lower <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> upper<font face='Lucida Console'>)</font><b>}</b>

        <font color='#0000FF'><u>double</u></font> lower <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
        <font color='#0000FF'><u>double</u></font> upper <font color='#5555FF'>=</font> <font color='#979000'>0</font>;

        <font color='#0000FF'><u>double</u></font> <b><a name='width'></a>width</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> upper<font color='#5555FF'>-</font>lower; <b>}</b>
        <font color='#0000FF'><u>bool</u></font> <b><a name='operator'></a>operator</b><font color='#5555FF'>&lt;</font><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> snl_range<font color='#5555FF'>&amp;</font> item<font face='Lucida Console'>)</font> <font color='#0000FF'>const</font> <b>{</b> <font color='#0000FF'>return</font> lower <font color='#5555FF'>&lt;</font> item.lower; <b>}</b>
    <b>}</b>;

    <font color='#0000FF'>inline</font> snl_range <b><a name='merge'></a>merge</b><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> snl_range<font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> snl_range<font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> <font color='#BB00BB'>snl_range</font><font face='Lucida Console'>(</font>std::<font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>a.lower, b.lower<font face='Lucida Console'>)</font>, std::<font color='#BB00BB'>max</font><font face='Lucida Console'>(</font>a.upper, b.upper<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'>inline</font> <font color='#0000FF'><u>double</u></font> <b><a name='distance'></a>distance</b> <font face='Lucida Console'>(</font><font color='#0000FF'>const</font> snl_range<font color='#5555FF'>&amp;</font> a, <font color='#0000FF'>const</font> snl_range<font color='#5555FF'>&amp;</font> b<font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#0000FF'>return</font> std::<font color='#BB00BB'>max</font><font face='Lucida Console'>(</font>a.lower,b.lower<font face='Lucida Console'>)</font> <font color='#5555FF'>-</font> std::<font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>a.upper,b.upper<font face='Lucida Console'>)</font>;
    <b>}</b>

    <font color='#0000FF'>inline</font> std::ostream<font color='#5555FF'>&amp;</font> <b><a name='operator'></a>operator</b><font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font face='Lucida Console'>(</font>std::ostream<font color='#5555FF'>&amp;</font> out, <font color='#0000FF'>const</font> snl_range<font color='#5555FF'>&amp;</font> item <font face='Lucida Console'>)</font>
    <b>{</b>
        out <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>[</font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font>item.lower<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font>"<font color='#CC0000'>,</font>"<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font>item.upper<font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font>"<font color='#CC0000'>]</font>";
        <font color='#0000FF'>return</font> out;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
    <font color='#0000FF'>inline</font> std::vector<font color='#5555FF'>&lt;</font>snl_range<font color='#5555FF'>&gt;</font> <b><a name='segment_number_line'></a>segment_number_line</b> <font face='Lucida Console'>(</font>
        <font color='#0000FF'>const</font> std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>double</u></font><font color='#5555FF'>&gt;</font><font color='#5555FF'>&amp;</font> x,
        <font color='#0000FF'>const</font> <font color='#0000FF'><u>double</u></font> max_range_width
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#BB00BB'>DLIB_CASSERT</font><font face='Lucida Console'>(</font>max_range_width <font color='#5555FF'>&gt;</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>;

        <font color='#009900'>// create initial ranges, one for each value in x.  So initially, all the ranges have
</font>        <font color='#009900'>// width of 0.
</font>        std::vector<font color='#5555FF'>&lt;</font>snl_range<font color='#5555FF'>&gt;</font> ranges;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'>auto</font> v : x<font face='Lucida Console'>)</font>
            ranges.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>v<font face='Lucida Console'>)</font>;
        std::<font color='#BB00BB'>sort</font><font face='Lucida Console'>(</font>ranges.<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, ranges.<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        std::vector<font color='#5555FF'>&lt;</font>snl_range<font color='#5555FF'>&gt;</font> greedy_final_ranges;
        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>ranges.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font> greedy_final_ranges;
        <font color='#009900'>// We will try two different clustering strategies.  One that does a simple greedy left
</font>        <font color='#009900'>// to right sweep and another that does a bottom up agglomerative clustering.  This
</font>        <font color='#009900'>// first loop runs the greedy left to right sweep.  Then at the end of this routine we
</font>        <font color='#009900'>// will return the results that produced the tightest clustering.
</font>        greedy_final_ranges.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>ranges[<font color='#979000'>0</font>]<font face='Lucida Console'>)</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>size_t</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>1</font>; i <font color='#5555FF'>&lt;</font> ranges.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>auto</font> m <font color='#5555FF'>=</font> <font color='#BB00BB'>merge</font><font face='Lucida Console'>(</font>greedy_final_ranges.<font color='#BB00BB'>back</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, ranges[i]<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>m.<font color='#BB00BB'>width</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> max_range_width<font face='Lucida Console'>)</font>
                greedy_final_ranges.<font color='#BB00BB'>back</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font> m;
            <font color='#0000FF'>else</font>
                greedy_final_ranges.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>ranges[i]<font face='Lucida Console'>)</font>;
        <b>}</b>


        <font color='#009900'>// Here we do the bottom up clustering.  So compute the edges connecting our ranges.
</font>        <font color='#009900'>// We will simply say there are edges between ranges if and only if they are
</font>        <font color='#009900'>// immediately adjacent on the number line.
</font>        std::vector<font color='#5555FF'>&lt;</font>sample_pair<font color='#5555FF'>&gt;</font> edges;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>size_t</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>1</font>; i <font color='#5555FF'>&lt;</font> ranges.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
            edges.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font><font color='#BB00BB'>sample_pair</font><font face='Lucida Console'>(</font>i<font color='#5555FF'>-</font><font color='#979000'>1</font>,i, <font color='#BB00BB'>distance</font><font face='Lucida Console'>(</font>ranges[i<font color='#5555FF'>-</font><font color='#979000'>1</font>],ranges[i]<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
        std::<font color='#BB00BB'>sort</font><font face='Lucida Console'>(</font>edges.<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, edges.<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, order_by_distance<font color='#5555FF'>&lt;</font>sample_pair<font color='#5555FF'>&gt;</font><font face='Lucida Console'>)</font>;

        disjoint_subsets sets;
        sets.<font color='#BB00BB'>set_size</font><font face='Lucida Console'>(</font>ranges.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

        <font color='#009900'>// Now start merging nodes.
</font>        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'>auto</font> edge : edges<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// find the next best thing to merge.
</font>            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> a <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>edge.<font color='#BB00BB'>index1</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
            <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> b <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>edge.<font color='#BB00BB'>index2</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;

            <font color='#009900'>// merge it if it doesn't result in an interval that's too big.
</font>            <font color='#0000FF'>auto</font> m <font color='#5555FF'>=</font> <font color='#BB00BB'>merge</font><font face='Lucida Console'>(</font>ranges[a], ranges[b]<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>m.<font color='#BB00BB'>width</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> max_range_width<font face='Lucida Console'>)</font>
            <b>{</b>
                <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> news <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>merge_sets</font><font face='Lucida Console'>(</font>a,b<font face='Lucida Console'>)</font>;
                ranges[news] <font color='#5555FF'>=</font> m;
            <b>}</b>
        <b>}</b>

        <font color='#009900'>// Now create a list of the final ranges.  We will do this by keeping track of which
</font>        <font color='#009900'>// range we already added to final_ranges.
</font>        std::vector<font color='#5555FF'>&lt;</font>snl_range<font color='#5555FF'>&gt;</font> final_ranges;
        std::vector<font color='#5555FF'>&lt;</font><font color='#0000FF'><u>bool</u></font><font color='#5555FF'>&gt;</font> <font color='#BB00BB'>already_output</font><font face='Lucida Console'>(</font>ranges.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, <font color='#979000'>false</font><font face='Lucida Console'>)</font>;
        <font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'>&lt;</font> sets.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#0000FF'>auto</font> s <font color='#5555FF'>=</font> sets.<font color='#BB00BB'>find_set</font><font face='Lucida Console'>(</font>i<font face='Lucida Console'>)</font>;
            <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>already_output[s]<font face='Lucida Console'>)</font>
            <b>{</b>
                final_ranges.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>ranges[s]<font face='Lucida Console'>)</font>;
                already_output[s] <font color='#5555FF'>=</font> <font color='#979000'>true</font>;
            <b>}</b>
        <b>}</b>

        <font color='#009900'>// only use the greedy clusters if they found a clustering with fewer clusters.
</font>        <font color='#009900'>// Otherwise, the bottom up clustering probably produced a more sensible clustering.
</font>        <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>final_ranges.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>=</font> greedy_final_ranges.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>
            <font color='#0000FF'>return</font> final_ranges;
        <font color='#0000FF'>else</font>
            <font color='#0000FF'>return</font> greedy_final_ranges;
    <b>}</b>

<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>

<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_BOTTOM_uP_CLUSTER_Hh_
</font>

</pre></body></html>