This file is indexed.

/usr/include/fst/heap.h is in libfst-dev 1.5.3+r3-2.

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
// See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// Implementation of a heap as in STL, but allows tracking positions
// in heap using a key. The key can be used to do an in-place update of
// values in the heap.

#ifndef FST_LIB_HEAP_H_
#define FST_LIB_HEAP_H_

#include <utility>
#include <vector>

#include <fst/compat.h>
namespace fst {

// A templated heap implementation that supports in-place update of values.
//
// The templated heap implementation is a little different from the
// STL priority_queue and the *_heap operations in STL. This heap
// supports indexing of values in the heap via an associated key.
//
// Each value is internally associated with a key which is returned
// to the calling functions on heap insert. This key can be used
// to later update the specific value in the heap.
//
// T: the element type of the hash, can be POD, Data or Ptr to Data
// Compare: comparison class for determining min-heapness.
template <class T, class Compare>
class Heap {
 public:
  using Value = T;
  enum { kNoKey = -1 };

  // Initializes with a specific comparator.
  Heap(Compare comp) : comp_(comp), size_(0) {}

  // Creates a heap with initial size of internal arrays of 0.
  Heap() : size_(0) {}

  // Inserts a value into the heap.
  int Insert(const Value& val) {
    if (size_ < values_.size()) {
      values_[size_] = val;
      pos_[key_[size_]] = size_;
    } else {
      values_.push_back(val);
      pos_.push_back(size_);
      key_.push_back(size_);
    }
    ++size_;
    return Insert(val, size_ - 1);
  }

  // Updates a value at position given by the key. The pos array is first
  // indexed by the key. The position gives the position in the heap array.
  // Once we have the position we can then use the standard heap operations
  // to calculate the parent and child positions.
  void Update(int key, const Value& val) {
    const int i = pos_[key];
    const bool is_better = comp_(val, values_[Parent(i)]);
    values_[i] = val;
    if (is_better) {
      Insert(val, i);
    } else {
      Heapify(i);
    }
  }

  // Returns the greatest (max=true) / least (max=false) value w.r.t.
  // from the heap.
  Value Pop() {
    Value top = values_.front();
    Swap(0, size_-1);
    size_--;
    Heapify(0);
    return top;
  }

  // Returns the greatest (max=true) / least (max=false) value w.r.t.
  // comp object from the heap.
  const Value& Top() const {
    return values_.front();
  }

  // Returns the element for the given key.
  const Value& Get(int key) const {
    return values_[pos_[key]];
  }

  // Checks if the heap is empty.
  bool Empty() const {
    return size_ == 0;
  }

  void Clear() {
    size_ = 0;
  }

  int Size() const {
    return size_;
  }

  void Reserve(int size) {
    values_.reserve(size);
    pos_.reserve(size);
    key_.reserve(size);
  }

 private:
  // The following private routines are used in a supportive role
  // for managing the heap and keeping the heap properties.

  // Computes left child of parent.
  static int Left(int i) {
    return 2 * (i + 1) - 1;  // 0 -> 1, 1 -> 3
  }

  // Computes right child of parent.
  static int Right(int i) {
    return 2 * (i + 1);  // 0 -> 2, 1 -> 4
  }

  // Given a child computes parent.
  static int Parent(int i) {
    return (i - 1) / 2;  // 0 -> 0, 1 -> 0, 2 -> 0,  3 -> 1,  4 -> 1, ...
  }

  // Swaps a child, parent. Use to move element up/down tree.
  // Note a little tricky here. When we swap we need to swap:
  //   the value
  //   the associated keys
  //   the position of the value in the heap
  void Swap(int j, int k) {
    const int tkey = key_[j];
    pos_[key_[j] = key_[k]] = j;
    pos_[key_[k] = tkey] = k;

    using std::swap;
    swap(values_[j], values_[k]);
  }

  // Heapifies subtree rooted at index i.
  void Heapify(int i) {
    const int l = Left(i);
    const int r = Right(i);
    int largest = (l < size_ && comp_(values_[l], values_[i])) ? l : i;
    if (r < size_ && comp_(values_[r], values_[largest]) )
      largest = r;

    if (largest != i) {
      Swap(i, largest);
      Heapify(largest);
    }
  }

  // Inserts (updates) element at subtree rooted at index i.
  int Insert(const Value& val, int i) {
    int p;
    while (i > 0 && !comp_(values_[p = Parent(i)], val)) {
      Swap(i, p);
      i = p;
    }
    return key_[i];
  }

 private:
  Compare comp_;

  std::vector<int> pos_;
  std::vector<int> key_;
  std::vector<Value> values_;
  int size_;
};

}  // namespace fst

#endif  // FST_LIB_HEAP_H_