summaryrefslogtreecommitdiffstats
path: root/raslib/sinterval.hh
blob: f3edfabe4776f14405d88f69b4bb07e4992ef4fd (plain)
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * INCLUDE: sinterval.hh
 *
 * MODULE:  raslib
 * CLASS:   r_Sinterval
 *
 * COMMENTS:
 *
*/

#ifndef _D_SINTERVAL_
#define _D_SINTERVAL_

#ifdef __VISUALC__
// Disable warning about exception specification.
#pragma warning( disable : 4290 )
#endif

class r_Error;
class r_Eno_interval;

#include <iostream>

#include "raslib/point.hh"

//@ManMemo: Module: {\bf raslib}

/*@Doc:

 The class represents an interval with lower and upper bound.
 Operations on the interval are defined according to the
 ODMG-93 standard.
 The operations union, difference, and intersection are 
 defined according to the following table:
 
    | ...  fixed bound \\
    * ...  open bound
    
    
 \begin{verbatim}
    
 class   orientation       union    difference  intersection
 -----------------------------------------------------------
   1     |-a-| |-b-|       error    a           error        

   2     |-a-|             [a1,b2]  [a1,b1]     [b1,a2]
            |-b-|

   3     |--a--|           a        error       b 
          |-b-|

   4     |-b-|             [b1,a2]  [b2,a2]     [a1,b2]
            |-a-|

   5     |--b--|           b        error       a       
          |-a-|

   6     |-b-| |-a-|       error    a           error                 

   7     |-a-|-b-|         [a1,b2]  a           [a2,a2]            
          
   8     |-b-|-a-|         [b1,a2]  a           [b2,b2]

   9     |--a--|           a        [a1,b1]     b
           |-b-|

  10     |--a--|           a        [b2,a2]     b      
         |-b-|

  11     |-a-|             a        error       a
         |-b-|

  12     |--b--|           b        error       a
         |-a-|

  13     |--b--|           b        error       a  
           |-a-|

  -----------------------------------------------------

  14     |--a--*           a        error       b
          |-b-|

  15     |--a--*           a        [b2,a2]     b 
         |-b-|

  16     |-b-| |-a-*       error    a           error

  17     |-b-|-a-*         [b1,a2]  a           [b2,b2]

  18      |--a--*          [b1,a2]  [b2,a2]     [a1,b2]  
         |-b-|

  -----------------------------------------------------

  19     *--a--|           a        error       b
          |-b-|

  20     *--a--|           a        [a1,b1]     b 
           |-b-|

  21     *-a-| |-b-|       error    a           error

  22     *-a-|-b-|         [a1,b2]  a           [a2,a2]

  23     *--a--|           [a1,b2]  [a1,b1]     [b1,a2]  
            |-b-|
 
  -----------------------------------------------------
 
  24     |--b--*           b        error       a
          |-a-|
 
  25     |--b--*           b        error       a 
         |-a-|
 
  26     |-a-| |-b-*       error    a           error
 
  27     |-a-|-b-*         [a1,b2]  a           [a2,a2]
 
  28      |--b--*          [a1,b2]  [a1,b1]     [b1,a2]  
         |-a-|
 
  -----------------------------------------------------
 
  29     *--b--|           b        error       a
          |-a-|
 
  30     *--b--|           b        error       a
           |-a-|
 
  31     *-b-| |-a-|       error    a           error
 
  32     *-b-|-a-|         [b1,a2]  a           [b2,b2]
 
  33     *--b--|           [b1,a2]  [b2,a2]     [a1,b2]  
            |-a-|
 
  -----------------------------------------------------
 
  34     *-a-| |-b-*       error    a           error
 
  35     *-a-|-b-*         [a1,b2]  a           [a2,a2]
 
  36     *-a-|             [a1,b2]  [a1,b1]     [b1,a2]
            |-b-*
         
  -----------------------------------------------------
 
  37     *-b-| |-a-*       error    a           error
 
  38     *-b-|-a-*         [b1,a2]  a           [b2,b2]
 
  39     *-b-|             [b1,a2]  [a1,b1]     [a1,b2]
            |-a-*
         
  -----------------------------------------------------
 
  40     *-a-|             b        error       a
          *-b-|
 
  41     *-a-|             a        error       a
         *-b-|
 
  42     *-b-|             a        [b2,a2]     b
          *-a-|
 
  -----------------------------------------------------
 
  43     |-a-*             a        [a1,b1]     b
          |-b-*
 
  44     |-a-*             a        error       a
         |-b-*
 
  45     |-b-*             b        error       a
          |-a-*
 
  -----------------------------------------------------
  46     *-a-* |-b-|       a        error       b
 
  47     *-b-* |-a-|       b        error       a
 
  48     *-a-*             a        [b2,a2]     b
          *-b-|
 
  49     *-a-*             a        [a1,b1]     b
          |-b-*
 
  50     *-b-*             b        error       a
          *-a-|
 
  51     *-b-*             b        error       a
          |-a-*
 
  52     *-a-*             a        error       a
         *-b-*

 \end{verbatim}
 
 Attention: The difference operation has to be reconsidered in future 
 concerning a discrete interpretation of the intervals.
 	  
 The closure operation defines an interval which is the smallest
 interval containing the two operands.
 The method {\tt intersects_with()} returns 0 in the error cases of the
 intersection operation and 1 otherwise.
 
*/

class r_Sinterval
{
  public:
    /// default constructor creates an interval with open bounds
    r_Sinterval();
    
    /// constructor taking string representation (e.g. *:200 )
    r_Sinterval( char* ) throw(r_Eno_interval); 

    /// constructor for an interval with fixed bounds
    r_Sinterval( r_Range low, r_Range high ) throw( r_Eno_interval );
    
    //@Man: Constructors for intervals with at least one open bound.
    //@{
    ///
    r_Sinterval( char,        r_Range high );
    ///
    r_Sinterval( r_Range low, char         );
    ///
    r_Sinterval( char,        char         );
    ///
    //@}
	 
    /// equal operator 
    bool operator==( const r_Sinterval& ) const;
    
    /**
      Two intervals are equal if they have the same lower and upper bound.
    */
    
    /// non equal operator - negation of equal operator
    bool operator!=( const r_Sinterval& ) const;
    
    //@Man: Read/Write methods:
    //@{
    ///
    inline r_Range  low () const;
    ///
    inline r_Range  high() const;
    ///
    inline bool      is_low_fixed() const;
    ///
    inline bool      is_high_fixed() const;
     
    ///
    void set_low     ( r_Range low  ) throw( r_Eno_interval );
    ///
    void set_high    ( r_Range high ) throw( r_Eno_interval );
    ///
    inline void set_low ( char );                   
    ///
    inline void set_high( char );
    
    /// get the size of one dimensional interval as range.
    r_Range get_extent() const throw(r_Error);
    /*@Doc:
      Returns a range with high() - low() + 1 of this interval.
    */
    
    ///
    void set_interval( r_Range low, r_Range high ) throw( r_Eno_interval );
    ///
    void set_interval( char,        r_Range high );
    ///
    void set_interval( r_Range low, char         );
    ///
    void set_interval( char,        char         );
    ///
    //@}

    /// determines if the self interval intersects with the delivered one
    bool intersects_with( const r_Sinterval& ) const;
    
    //@Man: Methods/Operators for the union operation:
    //@{
    ///
    r_Sinterval& union_of           ( const r_Sinterval&, const r_Sinterval& )
 		     throw( r_Eno_interval );
    ///
    r_Sinterval& union_with         ( const r_Sinterval& )
		     throw( r_Eno_interval );
    ///
    r_Sinterval& operator+=         ( const r_Sinterval& ) 
		     throw( r_Eno_interval );
    ///
    r_Sinterval  create_union       ( const r_Sinterval& ) const
		     throw( r_Eno_interval );
    ///
    r_Sinterval  operator+          ( const r_Sinterval& ) const
		     throw( r_Eno_interval );   	
    ///
    //@}
    
    //@Man: Methods/Operators for the difference operation:
    //@{
    ///
    r_Sinterval& difference_of       ( const r_Sinterval&, const r_Sinterval& )
    		     throw( r_Eno_interval );
    ///
    r_Sinterval& difference_with     ( const r_Sinterval& )
                     throw( r_Eno_interval );
    ///
    r_Sinterval& operator-=          ( const r_Sinterval& )
                     throw( r_Eno_interval );        
    ///
    r_Sinterval  create_difference   ( const r_Sinterval& ) const
                     throw( r_Eno_interval );	
    ///
    r_Sinterval  operator-           ( const r_Sinterval& ) const 
                     throw( r_Eno_interval );
    ///
    //@}
    
    //@Man: Methods/Operators for the intersection operation:
    //@{
    ///
    r_Sinterval& intersection_of     ( const r_Sinterval&, const r_Sinterval& )
                     throw( r_Eno_interval );		    
    ///
    r_Sinterval& intersection_with   ( const r_Sinterval& )
                     throw( r_Eno_interval );
    ///
    r_Sinterval& operator*=          ( const r_Sinterval&)	              
                     throw( r_Eno_interval);
    ///
    r_Sinterval  create_intersection ( const r_Sinterval& ) const
                     throw( r_Eno_interval );
    ///
    r_Sinterval  operator*           ( const r_Sinterval& ) const
                     throw( r_Eno_interval );
    ///
    //@}    
   
    //@Man: Methods/Operators for the closure operation:
    //@{
    ///
    r_Sinterval& closure_of          ( const r_Sinterval&, const r_Sinterval& )
                     throw( r_Eno_interval );		    
    ///
    r_Sinterval& closure_with        ( const r_Sinterval& )
                     throw( r_Eno_interval );
    ///
    r_Sinterval  create_closure      ( const r_Sinterval& ) const
                     throw( r_Eno_interval );
    ///
    //@}
    
    /// writes the state of the object to the specified stream
    void print_status( std::ostream& s = std::cout ) const;
    
    /// gives back the string representation
    char* get_string_representation() const;
    /**
      The string representation delivered by this method is allocated using {\tt malloc()} and
      has to be free unsing {\tt free()} in the end. It can be used to construct a {\tt r_Sinterval}
      again with a special constructor provided. The string representation is build using 
      {\tt print_status()}.
    */

    //@Man: Methods for internal use only:
    //@{
    /// calculate the size of the storage space occupied		     
    r_Bytes get_storage_size( ) const;
    ///  
    //@}  
    
private:

    //@Man: Calculation methods for the operations:
    //@{
    ///
    r_Sinterval  calc_union       ( const r_Sinterval& a, const r_Sinterval& b ) const
                     throw( r_Eno_interval );
    ///
    r_Sinterval  calc_difference  ( const r_Sinterval& a, const r_Sinterval& b ) const
                     throw( r_Eno_interval );
    ///
    r_Sinterval  calc_intersection( const r_Sinterval& a, const r_Sinterval& b ) const
                     throw( r_Eno_interval );
    ///
    r_Sinterval  calc_closure     ( const r_Sinterval& a, const r_Sinterval& b ) const
                     throw( r_Eno_interval );
    ///
    //@}

    /// compute the class of the two operands
    int          classify( const r_Sinterval& a, const r_Sinterval& b ) const;

    //@Man: Attributes storing the bounds:
    //@{
    ///
    r_Range lower_bound;
    ///
    r_Range upper_bound;
    ///
    //@}
    
    //@Man: Attributes specifying wheter the lower/upper bound is fixed or not:
    //@{
    ///
    bool     low_fixed;
    ///
    bool     high_fixed;
    ///
    //@}
};




//@ManMemo: Module: {\bf raslib}
/**
  Output stream operator for objects of type {\tt const r_Sinterval}.
*/  
extern std::ostream& operator<<( std::ostream& s, const r_Sinterval& d );

#include "raslib/sinterval.icc"

#endif