aboutsummaryrefslogtreecommitdiff
path: root/gdbsupport/enumerate.h
blob: 504d789f0ac36dc755b0919011050a64bb00ac7f (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
/* An enumerate range adapter for GDB, the GNU debugger.
   Copyright (C) 2026 Free Software Foundation, Inc.

   This file is part of GDB.

   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 3 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.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#ifndef GDBSUPPORT_ENUMERATE_H
#define GDBSUPPORT_ENUMERATE_H

#include <cstddef>
#include <iterator>
#include <tuple>
#include <utility>

namespace gdb::ranges::views
{

/* An iterator that wraps another iterator and yields tuples containing
   both the index and the value.  */

template<typename Iterator>
class enumerate_iterator
{
  using base_iterator = Iterator;
public:
  using value_type
    = std::tuple<std::size_t,
		 typename std::iterator_traits<base_iterator>::reference>;

  explicit enumerate_iterator (Iterator it)
    : m_it (std::move (it))
  {}

  value_type operator* () const
  { return { m_index, *m_it }; }

  enumerate_iterator &operator++ ()
  {
    ++m_it;
    ++m_index;
    return *this;
  }

  bool operator== (const enumerate_iterator &other) const
  { return m_it == other.m_it; }

  bool operator!= (const enumerate_iterator &other) const
  { return m_it != other.m_it; }

private:
  Iterator m_it;
  std::size_t m_index = 0;
};

/* A range adapter that allows iteration on both index and value.  */

template<typename Range>
class enumerate_range
{
  using base_iterator = decltype (std::begin (std::declval<Range &> ()));

public:
  using iterator = enumerate_iterator<base_iterator>;

  explicit enumerate_range (Range &&range)
    : m_range (std::forward<Range> (range))
  {}

  iterator begin ()
  { return iterator (std::begin (m_range)); }

  iterator end ()
  { return iterator (std::end (m_range)); }

private:
  Range m_range;
};

/* Return an enumerate_range for RANGE, allowing iteration with both
   index and value.

   Example usage:

     std::vector<int> vec = {10, 20, 30};
     for (auto [i, val] : gdb::ranges::views::enumerate (vec))
       printf ("%zu: %d\n", i, val);

   This prints:

     0: 10
     1: 20
     2: 30

   The value is a reference to the element in the container, so
   modifications are possible:

     for (auto [i, val] : gdb::ranges::views::enumerate (vec))
       val *= 2;  */

template<typename Range>
enumerate_range<Range>
enumerate (Range &&range)
{
  return enumerate_range<Range> (std::forward<Range> (range));
}

} /* namespace gdb::ranges::views */

#endif /* GDBSUPPORT_ENUMERATE_H */