summaryrefslogtreecommitdiffstats
path: root/rasodmg/stattiling.cc
blob: f9163b6024b5e136f980c2ec376afc626ee73876 (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
/*
* 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>.
/
/**
 * SOURCE: stattiling.cc
 *
 * MODULE: rasodmg
 * CLASS:  r_Stat_Tiling r_Access
 *
 * COMMENTS:
 *			None
 */

#ifdef __VISUALC__
// Diable warning for signed/unsigned mismatch.
#pragma warning( disable : 4018 )
#endif

#include "rasodmg/interesttiling.hh"
#include "rasodmg/alignedtiling.hh"
#include "rasodmg/stattiling.hh"
#include "raslib/rminit.hh"
#include "raslib/rmdebug.hh"

#include <assert.h>
#include <string.h>
#include <math.h>
#include <cstdlib>

// Uncoment the _VISUALIZE_2D_DECOMP_ line to generate ppm files the  
// visualization of the domain decomposition done by the algoritm
// #define _VISUALIZE_2D_DECOMP_

// Uncomment the following line to have debug information (printfs)
// #define _DEBUG_STATTILING_

#ifdef _VISUALIZE_2D_DECOMP_
#include "tools/visualtiling2d.hh"
#endif

const char*
r_Stat_Tiling::description = "dimensions, access patterns, border threshold, interesting threshold, tile size (in bytes) (ex: \"2;[0:9,0:9],3;[100:109,0:9],2;2;0.3;100\")";

const r_ULong
r_Stat_Tiling::DEF_BORDER_THR = 50;
const r_Double
r_Stat_Tiling::DEF_INTERESTING_THR  = 0.20;

r_Stat_Tiling::r_Stat_Tiling(const char* encoded) throw (r_Error)
	:	r_Dimension_Tiling(0, 0)
{
 if(!encoded)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << (encoded?encoded: "NULL") << ")" << endl;
 throw r_Error(TILINGPARAMETERNOTCORRECT);
 }

 r_Dimension tileD=0;
 std::vector<r_Access> vectAccessInfo;
 r_Access*    accessInfo=NULL;
 r_Minterval* accessInterv=NULL;
 r_ULong      accessTimes=0;
 r_Bytes tileS=0, lenToConvert=0, lenInToConvert=0;
 r_Area borderTH=0;
 r_Double interestTH=0.;
 const char *pStart=NULL, *pEnd=NULL, *pRes=NULL, *pTemp=NULL;
 char *pToConvert=NULL;
 const char *pInRes=NULL, *pInEnd=NULL;
 char *pInToConvert=NULL;
 
 pStart=encoded;
 pEnd=pStart+strlen(pStart);
 pTemp=pStart;

 pRes=strstr(pTemp,TCOLON);
 if(!pRes)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding tile dimension from \"" << pTemp << "\"." << endl;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
            
 //deal with dimension
 lenToConvert=pRes-pTemp;
 pToConvert=new char[lenToConvert+1];
 memcpy(pToConvert,pTemp, lenToConvert);
 pToConvert[lenToConvert]='\0';
 
 tileD=strtol(pToConvert, (char**)NULL, DefaultBase);
 if (!tileD)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding tile dimension from \"" << pToConvert << "\", is not a number." << endl;
  delete[] pToConvert;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 if (tileD < 0)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding tile dimension from \"" << pToConvert << "\", is negative." << endl;
  delete[] pToConvert;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 //skip COLON && free buffer
 delete[] pToConvert;
 if(pRes != (pEnd-1))
   pRes++;
 else
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access information from \"" << pStart << "\", end of stream." << endl;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 //deal with access informations
 pTemp=pRes;
 pRes=strstr(pTemp, TCOLON);
 if(!pRes)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access information from \"" << pTemp << "\"." << endl;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 while(pRes)
 {
  //is access info?
  if(*pTemp!=*LSQRBRA)
    break;
  
  //copy substring in buffer
  lenToConvert=pRes-pTemp;
  pToConvert=new char[lenToConvert+1];
  memcpy(pToConvert, pTemp, lenToConvert);
  pToConvert[lenToConvert]='\0';
  
  //deal with access Interval
  pInEnd=pToConvert+strlen(pToConvert);
  pInRes=strstr(pToConvert, RSQRBRA);
  if(!pInRes)
  {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access information from \"" << pToConvert << "\"." << endl;
   delete[] pToConvert;	
   throw r_Error(TILINGPARAMETERNOTCORRECT);
  }
  
  lenInToConvert=pInRes-pToConvert+1; //1 for ]
  pInToConvert=new char[lenInToConvert+1];
  memcpy(pInToConvert, pToConvert, lenInToConvert);
  pInToConvert[lenInToConvert]='\0';
  
  try
  {
   accessInterv=new r_Minterval(pInToConvert);
   delete [] pInToConvert;
  }
  catch(r_Error &err)
  {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access interval \"" << pInToConvert << "\" from \"" << pToConvert << "\"." << endl;
   RMInit::logOut << "Error " << err.get_errorno() << " : " << err.what() << endl;
   delete[] pInToConvert;	
   delete[] pToConvert;	
   throw r_Error(TILINGPARAMETERNOTCORRECT);
  }
  
  //deal with access Times
  pInRes=strstr(pInRes, TCOMMA);
  if(!pInRes)
  {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access times \"" << pInRes << "\" from acess information \"" << pToConvert << "\", not specified." << endl;
   delete[] pInToConvert;
   delete[] pToConvert;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
  }
  if(pInRes!=(pEnd-1))
   pInRes++;
  else
  {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access times \"" << pInRes << "\" from acess information \"" << pToConvert << "\", not specified." << endl;
   delete[] pInToConvert;
   delete[] pToConvert;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
  }
  accessTimes=strtol(pInRes, (char**)NULL, DefaultBase);
  if(!accessTimes)
  {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access times \"" << pInRes << "\" from acess information \"" << pToConvert << "\", not a number." << endl;
   delete[] pInToConvert;
   delete[] pToConvert;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
  }  
  if(accessTimes<0)
  {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access times \"" << pInRes << "\" from acess information \"" << pToConvert << "\", negative number." << endl;
   delete[] pInToConvert;
   delete[] pToConvert;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
  }    
    
  accessInfo=new r_Access(*accessInterv, accessTimes);
  vectAccessInfo.push_back(*accessInfo);
  delete accessInfo;
  delete accessInterv;
    
  //skip COLON && free buffer
  delete[] pToConvert;
  
  if(pRes != (pEnd-1))
    pRes++;
  else
  {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access informations, end of stream." << endl;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
  }
 
  //deal with next item
  pTemp=pRes;
  pRes=strstr(pTemp, TCOLON); 
 }
 
 if(vectAccessInfo.empty())
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding access informations, no access informations specified." << endl;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
  
 //deal with borderTH
 lenToConvert=pRes-pTemp;
 pToConvert=new char[lenToConvert+1];
 memcpy(pToConvert, pTemp, lenToConvert);
 pToConvert[lenToConvert]='\0';
 
 borderTH=strtol(pToConvert, (char**)NULL, DefaultBase);
 if (!borderTH)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding border threshold \"" << pToConvert << "\"." << endl;
   delete[] pToConvert;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 if (borderTH < 0)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding border threshold \"" << pToConvert << "\"." << endl;
  delete[] pToConvert;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 } 
 
 //skip COLON && free buffer
 delete[] pToConvert;
 if(pRes != (pEnd-1))
   pRes++;
 else
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding interesting threshold, end of stream." << endl;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 //deal with interestTH
 pTemp=pRes;
 pRes=strstr(pTemp,TCOLON);
 if(!pRes)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding interesting threshold." << endl;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 //copy substring into buffer
 lenToConvert=pRes-pTemp;
 pToConvert=new char[lenToConvert+1];
 memcpy(pToConvert, pTemp, lenToConvert);
 pToConvert[lenToConvert]='\0';
       
 interestTH=strtod(pToConvert, (char**)NULL);
 if (!interestTH)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding interesting threshold \"" << pToConvert << "\"." << endl; 
  delete[] pToConvert;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 if (interestTH < 0.)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding interesting threshold \"" << pToConvert << "\", negative number." << endl; 
   delete[] pToConvert;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 if (interestTH > 1.)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding interesting threshold \"" << pToConvert << "\", not in [0,1] interval." << endl; 
   delete[] pToConvert;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 //skip COLON && free buffer
 delete[] pToConvert;
 if(pRes != (pEnd-1))
   pRes++;
 else
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding tile size, end of stream." << endl; 
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 //deal with tilesize
 pTemp=pRes; 
 tileS=strtol(pTemp, (char**)NULL, DefaultBase);
 if (!tileS)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding tile size \"" << pToConvert << "\", not a number." << endl;
  throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 if (tileS < 0.)
 {
 RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << encoded << "): Error decoding tile size \"" << pToConvert << "\", negative number." << endl;
   throw r_Error(TILINGPARAMETERNOTCORRECT);
 }
 
 border_thr = borderTH;
 stat_info = vectAccessInfo;
 interesting_thr = interestTH;
 dimension = tileD;
 tile_size = tileS;
}


r_Stat_Tiling::r_Stat_Tiling(r_Dimension dim, const std::vector<r_Access>& stat_info2, r_Bytes ts, r_ULong border_threshold, r_Double interesting_threshold) throw (r_Error)
	:	r_Dimension_Tiling(dim, ts),
		border_thr(border_threshold), 
		stat_info(stat_info2),
		interesting_thr(interesting_threshold)
{
  RMDBGENTER(1, RMDebug::module_rasodmg, "r_Stat_Tiling", "Filtering accesses... ");
  // Filter accesses all areas have the same dimension if successfull else exception
  filter(stat_info);
  RMDBGMIDDLE(1, RMDebug::module_rasodmg, "r_Stat_Tiling", "done\n");
  std::vector<r_Access>::iterator areas_it = stat_info.begin();
  // Count total accesses
  r_ULong total_accesses = 0;


	
  for (;areas_it != stat_info.end(); areas_it++)
  {
    if ((*areas_it).get_pattern().dimension() != dim)
    {
     RMInit::logOut << "r_Stat_Tiling::r_Stat_Tiling(" << dim << ", " << &stat_info 
                    << ", " << ts << ", " << border_threshold << ", " << interesting_threshold 
                    << ") dimension (" << dim << ") does not match dimension of access patterns (" 
                    << (*areas_it).get_pattern().dimension() << ")" << endl;
     throw r_Edim_mismatch(dim, (*areas_it).get_pattern().dimension());
    }
    total_accesses += (*areas_it).get_times();
  }

  RMDBGMIDDLE(1, RMDebug::module_rasodmg, "r_Stat_Tiling", "Defining interest areas... ");

  // Mininum number of accesses for being interesting
  r_ULong critical_accesses = (r_ULong)(interesting_thr*total_accesses);

  iareas.clear();
  for (areas_it = stat_info.begin(); areas_it != stat_info.end(); areas_it++)
  {
    if ((*areas_it).get_times() >= critical_accesses) // Threshold exceeded or equal
    {
      iareas.push_back(areas_it->get_pattern());           // count this area in
    }
  }

  RMDBGEXIT(1, RMDebug::module_rasodmg, "r_Stat_Tiling", "Defining interest areas... done\n");
}

r_Stat_Tiling::~r_Stat_Tiling()
{
}

r_Tiling* r_Stat_Tiling::clone() const
{
  r_Tiling* copy = new r_Stat_Tiling(dimension, stat_info, tile_size, border_thr, interesting_thr);
  return copy;
}

void r_Stat_Tiling::print_status(std::ostream& os) const
{
  os << "r_Stat_Tiling[ ";
  r_Dimension_Tiling::print_status(os);
  os << " border threshold = " << border_thr << ", interesting threshold = " << interesting_thr << " ]";
}

const std::vector<r_Minterval>&
r_Stat_Tiling::get_interesting_areas() const
	{
	return iareas;
	}

r_Tiling_Scheme
r_Stat_Tiling::get_tiling_scheme() const
	{
	return r_StatisticalTiling;
	}

r_Area
r_Stat_Tiling::get_border_threshold() const
{
  return border_thr;
}

r_Double
r_Stat_Tiling::get_interesting_threshold() const
{
  return interesting_thr;
}

r_Access
r_Stat_Tiling::merge(const std::vector<r_Access>& patterns) const
{
  // Create an interator for list of patterns
  std::vector<r_Access>::const_iterator it = patterns.begin();

  // The result (initialy updated to the first element of patterns)
  r_Access result = (*it);
  it++;

  // For all patterns
  for (;it != patterns.end(); it++)
  {
    result.merge_with(*it);                          // Merge them
  }
  return result;                                     // Return the result
}

void r_Stat_Tiling::filter(std::vector<r_Access>& patterns) const throw (r_Error)
{
  // List to hold the result
  std::vector<r_Access> result;

  // List to hold the clusters
  std::vector<r_Access> cluster;

  // Iterators for pattern and cluster list
  std::vector<r_Access>::iterator pattern_it = patterns.begin();
  std::vector<r_Access>::iterator cluster_it;

  // For all elements in pattern table
  while (!patterns.empty())        
  {
    // Clean cluster
    cluster.clear();
    // Cluster with first element of pattern list
    cluster.push_back(patterns.back());
    patterns.pop_back();
    
    // For all elements in the cluster
    for (cluster_it = cluster.begin(); cluster_it != cluster.end(); cluster_it++)
    {
      // For all remaining patterns
      for (pattern_it = patterns.begin(); pattern_it != patterns.end(); pattern_it++)
      {
        // Pattern near an element from the cluster
	if ((*cluster_it).is_near(*pattern_it, border_thr))
	{
          // Add pattern to the cluster
	  cluster.push_back(*pattern_it);
          // Remove pattern from list
	  patterns.erase(pattern_it);
	}
      }
    }
    // Merge cluster and add to result
    result.push_back(merge(cluster));
  }
  // Filtered table
  patterns = result;
}

std::vector<r_Minterval>*  
r_Stat_Tiling::compute_tiles(const r_Minterval& domain, r_Bytes typelen) const throw (r_Error)
{
  r_Dimension num_dims = domain.dimension();                   // Dimensionality of dom
if (domain.dimension() != dimension)
	{
	RMInit::logOut << "r_Stat_Tiling::compute_tiles(" << domain << ", " << typelen << ") dimension (" << dimension << ") does not match dimension of object to tile (" << num_dims << ")" << endl;
	throw r_Edim_mismatch(dimension, num_dims);
	}
if (typelen > tile_size)
	{
	RMInit::logOut << "r_Stat_Tiling::compute_tiles(" << domain << ", " << typelen << ") tile size (" << tile_size << ") is smaller than type length (" << typelen << ")" << endl;
	throw r_Error(TILESIZETOOSMALL);
	}
#ifdef _VISUALIZE_2D_DECOMP_                           // User wants a visual 
  static int count;                                    // Number of decomps
  ++count;                                             // Update num decomps
                                                       // of the 2D decomp.
  Visual_Tiling_2D* vis;
  if (domain.dimension() == 2)
  {
    // Create an object for visualization
    char fname[80];
    sprintf(fname, "2D_decomp_stat_%d.ppm", count);
    vis = new Visual_Tiling_2D(domain, fname);
  }
#endif

  // *** Main algoritm ***

  std::vector<r_Minterval>* result;                          // Holds the result

  if (iareas.empty())                               // No interest areas
  {
    // Perform regular tiling

    RMDBGENTER(1, RMDebug::module_rasodmg, "r_Stat_Tiling", "Regular tiling...");

    result = r_Size_Tiling::compute_tiles(domain, typelen);

    RMDBGEXIT(1, RMDebug::module_rasodmg, "r_Stat_Tiling", "done\n");
  }
  else                                                 // We have interest areas
  {
    // Use interest areas for tiling the domain

    RMDBGENTER(1, RMDebug::module_rasodmg, "r_Stat_Tiling", "Statistic tiling...\n");
    r_Interest_Tiling* tiling=NULL;
    
    try
    { 
     tiling=new r_Interest_Tiling(dimension, iareas, get_tile_size(), r_Interest_Tiling::SUB_TILING);
     result = tiling->compute_tiles(domain, typelen);
     delete tiling;
    }
    catch(r_Error& err)
    {
     if(tiling)
       delete tiling;
     throw;
    }

    RMDBGEXIT(1, RMDebug::module_rasodmg, "r_Stat_Tiling", "done\n");
  }

  // *** End of the main algorithm

#ifdef _VISUALIZE_2D_DECOMP_
  if (domain.dimension() == 2)
  {
    vis->set_pen(0, 255, 255);

    std::vector<r_Minterval>::iterator it(*result);
    it.seek_begin();
    for (; it.not_done(); it++)
      (*vis) << (*it);

    vis->set_pen(255, 255, 0);
    
    std::vector<r_Minterval>& ia = (std::vector<r_Minterval>) iareas; // Cast away constness

    std::vector<r_Minterval>::iterator it2(ia);
    it2.seek_begin();
    for (; it2.not_done(); it2++)
      (*vis) << (*it2);

    delete vis;
  }
#endif

  // Return result

  return result;
}



//***************************************************************************

r_Access::r_Access() :
  times(0)
{
}

r_Access::r_Access(const r_Minterval& pattern, r_ULong accesses) :
  interval(pattern), times(accesses)
{
}

const r_Minterval& r_Access::get_pattern() const
{
  return interval;
}

void r_Access::set_pattern(const r_Minterval& pattern)
{
  interval = pattern;
}

r_ULong r_Access::get_times() const
{
  return times;
}

void r_Access::set_times(r_ULong accesses)
{
  times = accesses;
}

bool r_Access::is_near(const r_Access& other, r_ULong border_threshold) const throw (r_Error)
{
  const r_Minterval& a = this->interval;
  const r_Minterval& b = other.interval;

  r_Dimension num_dims = interval.dimension();
  if (num_dims != b.dimension())
	{
	RMInit::logOut << "r_Access::is_near(" << other << ", " << border_threshold << ") parameter 1 does not match my dimension (" << num_dims << ")" << endl;
	throw r_Edim_mismatch(num_dims, b.dimension());
	}
  bool the_same = true;

  // For all dimensions
  for (r_Dimension i = 0; i < num_dims; i++)
  {
    // Higher limit does not exceed border threshold
    if (labs(a[i].high() - b[i].high()) > border_threshold)
    {
      the_same = false;
      break;
    }

    // Lower limit does not exceed border threshold
    if (labs(a[i].low() - b[i].low()) > border_threshold)
    {
      the_same = false;
      break;
    }
  }

  return the_same;
}

void r_Access::merge_with(const r_Access& other)
{
  interval.closure_with(other.interval);
  times+= other.times;
}

void r_Access::print_status(std::ostream& os) const
{
  os << "{" << times <<"x: " << interval << "}";
}

std::ostream& operator<<(std::ostream& os, const r_Access& access)
{
  access.print_status(os);

  return os;
}

bool r_Access::operator==(const r_Access& other) const
{
  return ((this->interval == other.interval) && (this->times == other.times));
}

bool r_Access::operator!=(const r_Access& other) const
{
  return !(*this == other);
}