This file is indexed.

/usr/lib/grass64/include/grass/iostream/pqheap.h is in grass-dev 6.4.3-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
/****************************************************************************
 * 
 *  MODULE:	iostream
 *
 *  COPYRIGHT (C) 2007 Laura Toma
 *   
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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.
 *
 *****************************************************************************/

#ifndef _PQHEAP_H
#define _PQHEAP_H

#include <assert.h>
#include <stdlib.h>

#define PQHEAP_MEM_DEBUG if(0)


//HEAPSTATUS can be defined at compile time



//this flag is currently off; we used it at some point for checking
//how many times is each element in the heap accessed or something
//like that
#ifdef HEAPSTATUS
static const int PAGESIZE = 1024;
#endif


// Helper functions for navigating through a binary heap.
/* for simplicity the heap structure is slightly modified as:
   0
   |
   1
   /\
  2  3
 /\  /\
4 5 6  7

*/

// The children of an element of the heap.
static inline unsigned int heap_lchild(unsigned int index) {
  return 2 * index;
}

static inline unsigned int heap_rchild(unsigned int index) {
  return 2 * index + 1;
}

// The parent of an element.
static inline unsigned int heap_parent(unsigned int index) {
  return index >> 1;
}


// return minimum of two integers
static unsigned int mymin(unsigned int a, unsigned int b) {
  return (a<=b)? a:b;
}



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

Priority queue templated on a single type 

assume T to be a class with getPriority() and getValue() implemented;

Supported operations: min, extract_min, insert in O(lg n)


***************************************************************
***************************************************************
***************************************************************/
template <class T>
class pqheap_t1 {
  // A pointer to an array of elements
  T* elements;
  
  // The number of elements currently in the queue.
  unsigned int cur_elts;
  
  // The maximum number the queue can hold.
  unsigned int max_elts;

private:
  void heapify(unsigned int root);

public:
  inline pqheap_t1(unsigned int size);
  
  //build heap from an array of elements; array a is REUSED, and NOT
  //COPIED, for efficiency; it'd better not be used after this
  //outside!!!
  inline pqheap_t1(T* a, unsigned int size);

  inline ~pqheap_t1(void); 

  //build a heap from an array of elements; 
  //if size > max_elts, insert first maxsize elements from array;
  //return nb of elements that did not fit;
  unsigned int fill(T* a, unsigned int size);

  // Is it full?
  inline bool full(void);

  //Is it empty?
  inline bool empty(void);
  inline bool is_empty() { return empty(); };

  // How many elements?
  inline unsigned int num_elts(void);
  
  // How many elements? sorry - i could never remember num_elts
  inline unsigned int size(void) const { return cur_elts; };
  
  // Min
  inline bool min(T& elt);
  T min();

  // Extract min and set elt = min
  inline bool extract_min(T& elt);

  //extract all elts with min key, add them and return their sum
  inline bool extract_all_min(T& elt);

  //delete min; same as extract_min, but ignore the value extracted
  inline bool delete_min();

  // Insert
  inline bool insert(const T& elt);

  //Delete the current minimum and insert the new item x; 
  //the minimum item is lost (i.e. not returned to user); 
  //needed to optimize merge 
  inline void delete_min_and_insert(const T &x);

  //this function is a dirty way to allow building faster the heap 
  //in case we build it from a sorted array; in that case we dont need 
  //to 'insert' and then 'heapify', but it is enough to 'set'
  void set(long i, T& elt);

  //print
  inline friend ostream& operator<<(ostream& s, const pqheap_t1<T> &pq) {
    s << "PQ: "; s.flush();
    for (unsigned int i=0; i< mymin(10, pq.cur_elts); i++) {
      s <<  "[" 
	//<< pq.elements[i].getPriority() << "," 
	//<< pq.elements[i].getValue() 
	<< pq.elements[i]
	<< "]";
    }
    return s;
  }
  //print
  void print();

  //print
  void print_range();


#ifdef HEAPSTATUS
  inline void heapstatus(int d);
  inline void heaptouch(unsigned int pos);
  unsigned int *numtouch;
#endif
};


//************************************************************/
template <class T>
inline 
pqheap_t1<T>::pqheap_t1(unsigned int size) {


  elements = new T [size];
  cout << "pqheap_t1: register memory\n"; 
  cout.flush();
  PQHEAP_MEM_DEBUG cout << "pqheap_t1::pq_heap_t1: allocate\n";
  //  PQHEAP_MEM_DEBUG MMmanager.print();

  
  if (!elements) {
	cerr << "could not allocate priority queue: insufficient memory..\n";
    exit(1);
  }
  assert(elements);
  
  max_elts = size;
  cur_elts = 0;
  
#ifdef HEAPSTATUS
  numtouch = new unsigned int[size/PAGESIZE];
  assert(numtouch);
  for(int i=0; i<size/PAGESIZE; i++) {
	numtouch[i] = 0;
  }
#endif
}


//************************************************************/
/* (this constructor is a bit nasty) Build heap from an array of
   elements; array a is reused, and not copied, for efficiency; it'd
   better not be used after this outside!!! */
template <class T>
inline 
pqheap_t1<T>::pqheap_t1(T* a, unsigned int size) {
  {
	static int flag = 0;
	if(!flag) {
	  cerr << "Using slow build in pqheap_t1" << endl;
	  flag = 1;
	}
  }

  elements = a;
  max_elts = size;
  cur_elts = size;

  if (max_elts) {
    for (int i = heap_parent(max_elts-1); i>=0; i--) {
      //cout << "heapify i=" << i<<"\n";
      heapify(i);
    }
  }
}

//************************************************************/
template <class T>
inline 
pqheap_t1<T>::~pqheap_t1() {
#ifdef HEAPSTATUS
  cout << endl << "pagesize = " << PAGESIZE << endl;
  cout << "max_elts = " << max_elts << endl;
  unsigned int n = max_elts / PAGESIZE;
  for(unsigned int i=0; i<n; i++) {
    cout << form("PQTEMP %d\t%d", i, numtouch[i]) << endl;
  }
  delete [] numtouch;
#endif
  
  delete [] elements;
  cur_elts = 0;
  max_elts = 0;
  return;
}
 

//************************************************************/
//build a heap from an array of elements; 
//if size > max_elts, insert first maxsize elements from array;
//return nb of elements that did not fit;
template <class T>
inline unsigned int
pqheap_t1<T>::fill(T* a, unsigned int size) {
  unsigned int i;
  assert(cur_elts == 0);
  for (i = 0; i<size; i++) {
    if (!insert(a[i])) { 
      break;
    }
  }
  if (i < size) {
    assert(i == max_elts);
    return size - i;
  } else {
    return 0;
  }
}



//************************************************************/
template <class T>
inline bool 
pqheap_t1<T>::full(void) {
  return cur_elts == max_elts;
}

//************************************************************/
template <class T>
inline bool 
pqheap_t1<T>::empty(void) {
  return cur_elts == 0;
}

//************************************************************/
template <class T>
inline unsigned int 
pqheap_t1<T>::num_elts(void) {
  return cur_elts;
}

//************************************************************/
template <class T>
inline bool 
pqheap_t1<T>::min(T& elt) {
  if (!cur_elts) {
    return false;
  }
  elt = elements[0];
  return true;
}


//************************************************************/
template <class T>
T
pqheap_t1<T>::min() {
  T elt;
  if(min(elt)) {
	return elt;
  } else {
	cerr << "unguarded min failed" << endl;
	assert(0);
	exit(1);
  }
  return elt;
}




//************************************************************/
//this function is a dirty hack to allow building faster the heap 
//in case we build it from a sorted array; in thiat case we dont need 
//to 'insert' and then 'heapify', but it is enough to 'set'
template <class T>
inline void
pqheap_t1<T>::set(long i, T& elt) {
  //must always set precisely the next element
  assert(i == cur_elts);
  elements[i] = elt;
  cur_elts++;
}


//************************************************************/
#ifdef HEAPSTATUS
template <class T>
inline void pqheap_t1<T>::heaptouch(unsigned int pos) {
  numtouch[pos/PAGESIZE]++;
  assert(numtouch[pos/PAGESIZE] > 0);
}
#endif

#ifdef HEAPSTATUS
template <class T>
inline void pqheap_t1<T>::heapstatus(int d) {
  static int count = 0;
  static int delta = 0;
  
  delta += d;
  count++;
  
  if((count % 10000) == 0) {
    cout << endl << form("PQHEAP %d\t%d", cur_elts, delta) << endl;
    count = 0;
    delta = 0;
  }
}    
#endif



//************************************************************/
template <class T>
inline bool 
pqheap_t1<T>::extract_min(T& elt) {
  if (!cur_elts) {
    return false;
  }
  elt = elements[0];
  elements[0] = elements[--cur_elts];
  heapify(0);
  
#ifdef HEAPSTATUS
  heaptouch(cur_elts);
  heaptouch(0);
  heapstatus(-1);
#endif
  
  return true;
}

//************************************************************/
//extract all elts with min key, add them and return their sum
template <class T>
inline bool 
pqheap_t1<T>::extract_all_min(T& elt) {
  
  T next_elt;
  bool done = false;
  
  //extract first elt
  if (!extract_min(elt)) {
    return false; 
  } else {
    while (!done) {
      //peek at the next min elt to see if matches
      if ((!min(next_elt)) || 
	  !(next_elt.getPriority() == elt.getPriority())) {
	done = true; 
      } else {
	extract_min(next_elt);
	elt = elt + next_elt;
      }
    }
  }
  return true;
}




//************************************************************/
template <class T>
inline bool 
pqheap_t1<T>::delete_min() {
  T dummy;
  return extract_min(dummy);
}


//************************************************************/
template <class T>
inline bool 
pqheap_t1<T>::insert(const T& elt) {
  unsigned int ii;
  
  if (full()) {
    return false;
  }
  
  for (ii = cur_elts++;
       ii && (elements[heap_parent(ii)].getPriority() > elt.getPriority());
       ii = heap_parent(ii)) {
    elements[ii] = elements[heap_parent(ii)];
  }
  elements[ii] = elt;
    
#ifdef HEAPSTATUS
  heaptouch(ii);
  heapstatus(+1);
#endif
  
  return true;
}                                       


//************************************************************/
template <class T>
inline void 
pqheap_t1<T>::heapify(unsigned int root) {
  unsigned int min_index = root;
  unsigned int lc = heap_lchild(root);
  unsigned int rc = heap_rchild(root);
  
#ifdef HEAPSTATUS
  // already did the root, so dont do it again
  if(lc < cur_elts) {
    heaptouch(lc);
  }
  if(rc < cur_elts) {
    heaptouch(rc);
  }
#endif
  if ((lc < cur_elts) && 
      ((elements[lc].getPriority()) < elements[min_index].getPriority())) {
    min_index = lc;
  }
  if ((rc < cur_elts) && 
      ((elements[rc].getPriority()) < elements[min_index].getPriority())) {
    min_index = rc;
  }
  
  if (min_index != root) {
    T tmp_q = elements[min_index];
    
    elements[min_index] = elements[root];
    elements[root] = tmp_q;
    
    heapify(min_index);
  }
}   


//************************************************************/
//Delete the current minimum and insert the new item; 
//the minimum item is lost (i.e. not returned to user); 
//needed to optimize merge 
template <class T>
inline void
pqheap_t1<T>::delete_min_and_insert(const T &x) {
  assert(cur_elts);
  elements[0] = x;
  heapify(0);
}




/************************************************************/
template <class T>
void pqheap_t1<T>::print() {
  cout << "[";
  for (unsigned int i=0; i<cur_elts; i++) {
    cout << elements[i].getPriority().field1() <<",";
  }
  cout << "]";
}


/************************************************************/
template <class T>
void pqheap_t1<T>::print_range() {
  cout << "[";
  T a, b;
  min(a);
  max(b);
  if (cur_elts) {
    cout << a.getPriority().field1() << ".."
	 << b.getPriority().field1();
  }
  cout << " (" << cur_elts << ")]";
}







#endif // _PQUEUE_HEAP_H