This file is indexed.

/usr/include/ola/stl/STLUtils.h is in libola-dev 0.10.3.nojsmin-2+deb9u1.

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
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * STLUtils.h
 * Helper functions for dealing with the STL.
 * Copyright (C) 2012 Simon Newton
 */

/**
 * @defgroup stl STL
 * @brief Various helper functions related to STL classes.
 *
 * @addtogroup stl
 * @{
 *
 * @file STLUtils.h
 * @brief Helper functions for STL classes.
 * @}
 */

#ifndef INCLUDE_OLA_STL_STLUTILS_H_
#define INCLUDE_OLA_STL_STLUTILS_H_

#include <assert.h>
#include <cstddef>
#include <map>
#include <set>
#include <vector>
#include <utility>

namespace ola {

/**
 * @addtogroup stl
 * @{
 */

/**
 * @brief Clear a stack.
 * @tparam T A stack.
 * @post The stack is empty.
 */
template<typename T>
void STLEmptyStack(T *stack) {
  while (!stack->empty()) {
    stack->pop();
  }
}

/**
 * @brief Clear a stack and delete all pointers..
 * @tparam T A stack of pointers.
 * @post The stack is empty.
 */
template<typename T>
void STLEmptyStackAndDelete(T *stack) {
  while (!stack->empty()) {
    delete stack->top();
    stack->pop();
  }
}

/**
 * @brief Delete the elements of a Sequence.
 * @param sequence the Sequence to delete the elements from.
 * @tparam T A Sequence.
 * @post The sequence is empty.
 *
 * All elements in the sequence will be deleted. The sequence will be cleared.
 *
 * @examplepara
 * ~~~~~~~~~~~~~~~~~~~~~
 * class Foo() {
 *  public:
 *    Foo() {
 *      // populate m_objects
 *    }
 *    ~Foo() {
 *      STLDeleteElements(&m_objects);
 *    }
 *  private:
 *    vector<Bar*> m_objects;
 * };
 * ~~~~~~~~~~~~~~~~~~~~~
 */
template<typename T>
void STLDeleteElements(T *sequence) {
  typename T::iterator iter = sequence->begin();
  for (; iter != sequence->end(); ++iter) {
    if (*iter) {
      delete *iter;
    }
  }
  sequence->clear();
}

/**
 * Delete all values in a pair associative container.
 * @param container
 * @tparam T A pair associative container.
 * @post The container is empty.
 *
 * All elements in the container will be deleted. The container will be cleared.
 *
 * @examplepara
 * ~~~~~~~~~~~~~~~~~~~~~
 * class Foo() {
 *  public:
 *    Foo() {
 *      // populate m_objects
 *    }
 *    ~Foo() {
 *      STLDeleteValues(&m_objects);
 *    }
 *  private:
 *    map<int, Bar*> m_objects;
 * };
 * ~~~~~~~~~~~~~~~~~~~~~
 * @sa STLDeleteElements.
 */
template<typename T>
void STLDeleteValues(T *container) {
  typename T::iterator iter = container->begin();
  for (; iter != container->end(); ++iter) {
    if (iter->second) {
      delete iter->second;
    }
  }
  container->clear();
}

/**
 * Returns true if the container contains the value.
 * @tparam T1 A container.
 * @tparam T2 The value to search for.
 * @param container the container to search in.
 * @param value the value to search for.
 */
template<typename T1, typename T2>
inline bool STLContains(const T1 &container, const T2 &value) {
  return container.find(value) != container.end();
}

/**
 * Extract the list of keys from a pair associative container.
 * @tparam T1 A container.
 * @param[in] container the container to extract the keys for.
 * @param[out] keys the vector to populate with the keys.
 */
template<typename T1>
void STLKeys(const T1 &container, std::vector<typename T1::key_type> *keys) {
  keys->reserve(keys->size() + container.size());
  typename T1::const_iterator iter = container.begin();
  for (; iter != container.end(); ++iter)
    keys->push_back(iter->first);
}

/**
 * @brief Extract a vector of values from a pair associative container.
 * @tparam T1 A container.
 * @tparam T2 The type of the values.
 * @param[in] container the container to extract the values for.
 * @param[out] values the vector to populate with the values.
 */
template<typename T1, typename T2>
void STLValues(const T1 &container, std::vector<T2> *values) {
  values->reserve(values->size() + container.size());
  typename T1::const_iterator iter = container.begin();
  for (; iter != container.end(); ++iter)
    values->push_back(iter->second);
}

/**
 * @brief Lookup a value by key in a associative container.
 * @tparam T1 A container.
 * @param container the container to search in.
 * @param key the key to search for.
 * @returns A pointer to the value, or NULL if the value isn't found.
 */
template<typename T1>
typename T1::mapped_type* STLFind(T1 *container,
                                  const typename T1::key_type &key) {
  typename T1::iterator iter = container->find(key);
  if (iter == container->end()) {
    return NULL;
  } else {
    return &iter->second;
  }
}

/**
 * @brief Lookup a value by key in a associative container.
 * @tparam T1 A container.
 * @param container the container to search in.
 * @param key the key to search for.
 * @returns A pointer to the value, or NULL if the value isn't found.
 */
template<typename T1>
typename T1::mapped_type const* STLFind(const T1 *container,
                                        const typename T1::key_type &key) {
  typename T1::const_iterator iter = container->find(key);
  if (iter == container->end()) {
    return NULL;
  } else {
    return &iter->second;
  }
}

/**
 * @brief Lookup a value by key in a associative container.
 * @tparam T1 A container.
 * @param container the container to search in.
 * @param key the key to search for.
 * @returns The value matching the key, or NULL if the value isn't found.
 *
 * This assumes that NULL can be co-erced to the mapped_type of the container.
 * It's most suitable for containers with pointers.
 */
template<typename T1>
typename T1::mapped_type STLFindOrNull(const T1 &container,
                                       const typename T1::key_type &key) {
  typename T1::const_iterator iter = container.find(key);
  if (iter == container.end()) {
    return NULL;
  } else {
    return iter->second;
  }
}


/**
 * @brief Replace a value in a pair associative container, inserting the key,
 * value if it doesn't already exist.
 * @tparam T1 A container.
 * @param container the container to replace the value in.
 * @param key the key to insert / replace.
 * @param value the value to insert / replace.
 * @returns true if the value was replaced, false if the value was inserted.
 *
 * @note Note if the value type is a pointer, and the container has ownership
 * of the pointer, replacing a value will leak memory. Use STLReplaceAndDelete
 * to avoid this.
 * @sa STLReplaceAndDelete.
 */
template<typename T1>
bool STLReplace(T1 *container, const typename T1::key_type &key,
                const typename T1::mapped_type &value) {
  std::pair<typename T1::iterator, bool> p = container->insert(
      typename T1::value_type(key, value));
  if (!p.second) {
    p.first->second = value;
    return true;
  }
  return false;
}

/**
 * @brief Replace a value in a pair associative container. If the key existed,
 * the old value is returned, otherwise NULL is returned.
 * @tparam T1 A container.
 * @param container the container to replace the value in.
 * @param key the key to insert / replace.
 * @param value the value to insert / replace.
 * @returns The value matching the key, or NULL if the value isn't found.
 *
 * @note This assumes that NULL can be co-erced to the mapped_type of the
 * container. It's most suitable for containers with pointers.
 * @sa STLReplaceAndDelete.
 */
template<typename T1>
typename T1::mapped_type STLReplacePtr(T1 *container,
                                       const typename T1::key_type &key,
                                       const typename T1::mapped_type &value) {
  std::pair<typename T1::iterator, bool> p = container->insert(
      typename T1::value_type(key, value));
  if (!p.second) {
    typename T1::mapped_type old_value = p.first->second;
    p.first->second = value;
    return old_value;
  }
  return NULL;
}


/**
 * @brief Similar to STLReplace but this will delete the value if the
 * replacement occurs.
 * @tparam T1 A container.
 * @param container the container to replace the value in.
 * @param key the key to insert / replace.
 * @param value the value to insert / replace.
 * @returns true if the value was replaced, false if the value was inserted.
 */
template<typename T1>
bool STLReplaceAndDelete(T1 *container, const typename T1::key_type &key,
                         const typename T1::mapped_type &value) {
  std::pair<typename T1::iterator, bool> p = container->insert(
      typename T1::value_type(key, value));
  if (!p.second) {
    delete p.first->second;
    p.first->second = value;
    return true;
  }
  return false;
}


/**
 * @brief Insert a value into a container only if this value doesn't already
 * exist.
 * @tparam T1 A container.
 * @param container the container to insert the value in.
 * @param key_value the pair<key, value> to insert.
 * @returns true if the key/value was inserted, false if the key already exists.
 */
template<typename T1>
bool STLInsertIfNotPresent(T1 *container,
                           const typename T1::value_type &key_value) {
  return container->insert(key_value).second;
}


/**
 * @brief Insert a value into a container only if this value doesn't already
 * exist.
 * @tparam T1 A container.
 * @param container the container to insert the value in.
 * @param key the key to insert.
 * @param value the value to insert.
 * @returns true if the key/value was inserted, false if the key already exists.
 * @sa STLInsertIfNotPresent.
 */
template<typename T1>
bool STLInsertIfNotPresent(T1 *container, const typename T1::key_type &key,
                           const typename T1::mapped_type &value) {
  return container->insert(typename T1::value_type(key, value)).second;
}


/**
 * @brief Insert an key : value into a pair associative container, or abort
 * the program if the key already exists.
 * @tparam T1 A container.
 * @param container the container to insert the value in.
 * @param key the key to insert.
 * @param value the value to insert.
 * @note This should only be used in unit-testing code.
 */
template<typename T1>
void STLInsertOrDie(T1 *container, const typename T1::key_type &key,
                    const typename T1::mapped_type &value) {
  assert(container->insert(typename T1::value_type(key, value)).second);
}


/**
 * @brief Remove a key / value from a container.
 * @tparam T1 A container.
 * @param container the container to remove the key from.
 * @param key the key to remove.
 * @returns true if the item was removed, false otherwise.
 */
template<typename T1>
bool STLRemove(T1 *container, const typename T1::key_type &key) {
  return container->erase(key);
}

/**
 * @brief Lookup and remove a key from a pair associative container.
 * @tparam T1 A container.
 * @param[in] container the container to remove the key from.
 * @param[in] key the key to remove.
 * @param[out] value A pointer which will be set to the removed value.
 * @returns true if the item was found and removed, false otherwise.
 *
 * Lookup a value by key in a pair associative container. If the value exists,
 * it's removed from the container, the value placed in value and true is
 * returned.
 */
template<typename T1>
bool STLLookupAndRemove(T1 *container,
                        const typename T1::key_type &key,
                        typename T1::mapped_type *value) {
  typename T1::iterator iter = container->find(key);
  if (iter == container->end()) {
    return false;
  } else {
    *value = iter->second;
    container->erase(iter);
    return true;
  }
}

/**
 * @brief Lookup or insert a NULL value into a pair associative container.
 * @tparam T1 A container.
 * @param[in] container the container to lookup from or insert into.
 * @param[in] key the key to lookup.
 * @returns An iterator pointing to the value.
 *
 * Lookup a value by key in a pair associative container or insert NULL if it
 * doesn't already exist.
 */
template<typename T1>
typename T1::iterator STLLookupOrInsertNull(T1 *container,
                                            const typename T1::key_type &key) {
  std::pair<typename T1::iterator, bool> p = container->insert(
      typename T1::value_type(key, NULL));
  return p.first;
}

template<typename T1>
void PairAssociativeAssignNew(T1 **location) {
  *location = new T1();
}

/**
 * @brief Lookup or insert a new object into a pair associative container.
 * @tparam T1 A container.
 * @param[in] container the container to lookup from or insert into.
 * @param[in] key the key to lookup.
 * @returns An iterator pointing to the value.
 */
template<typename T1>
typename T1::iterator STLLookupOrInsertNew(T1 *container,
                                           const typename T1::key_type &key) {
  std::pair<typename T1::iterator, bool> p = container->insert(
      typename T1::value_type(key, NULL));
  if (p.second) {
    PairAssociativeAssignNew(&p.first->second);
  }
  return p.first;
}

/**
 * @brief Remove a value from a pair associative container and delete it.
 * @tparam T1 A container.
 * @param[in] container the container to remove the key from.
 * @param[in] key the key to remove.
 * @returns true if the item was found and removed from the container, false
 *   otherwise.
 */
template<typename T1>
bool STLRemoveAndDelete(T1 *container, const typename T1::key_type &key) {
  typename T1::iterator iter = container->find(key);
  if (iter == container->end()) {
    return false;
  } else {
    delete iter->second;
    container->erase(iter);
    return true;
  }
}

/**
 * @brief Remove a value from a pair associative container and return the value.
 * @tparam T1 A container.
 * @param[in] container the container to remove the key from.
 * @param[in] key the key to remove.
 * @returns The value matching the key, or NULL if the value isn't found.
 *
 * @note This assumes that NULL can be co-erced to the mapped_type of the
 * container. It's most suitable for containers with pointers.
 */
template<typename T1>
typename T1::mapped_type STLLookupAndRemovePtr(
    T1 *container,
    const typename T1::key_type &key) {
  typename T1::iterator iter = container->find(key);
  if (iter == container->end()) {
    return NULL;
  } else {
    typename T1::mapped_type value = iter->second;
    container->erase(iter);
    return value;
  }
}

/**
 * Add elements of a sequence to an associative container.
 * @param output The associative container to add to.
 * @param input The sequence containing the elements to add.
 * @param value The value to use for each key in input.
 * @tparam T1 A pair associative container.
 * @tparam T2 A sequence.
 *
 * Any existing elements that conflict with the values in the sequence will be
 * replaced.
 */
template<typename T1, typename T2>
void STLMapFromKeys(T1 *output, const T2 input,
                    typename T1::mapped_type value) {
  typename T2::const_iterator iter = input.begin();
  for (; iter != input.end(); ++iter) {
    std::pair<typename T1::iterator, bool> p = output->insert(
        typename T1::value_type(*iter, value));
    if (!p.second) {
      p.first->second = value;
    }
  }
}
}  // namespace ola
#endif  // INCLUDE_OLA_STL_STLUTILS_H_
/**
 * @}
 */