aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/src/firebase/firestore/util/iterator_adaptors.h
blob: 042fd720f23d253c34208dc82ca0f478f266f08d (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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
/*
 * Copyright 2005, 2018 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Provides some iterator adaptors and views.

#ifndef FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_ITERATOR_ADAPTORS_H_
#define FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_ITERATOR_ADAPTORS_H_

#include <iterator>
#include <memory>
#include <type_traits>

#include "absl/base/port.h"
#include "absl/meta/type_traits.h"

namespace firebase {
namespace firestore {
namespace util {

namespace internal {

// value == true if Iter prohibits modification of its pointees.
template <typename Iter>
struct IsConstIter
    : std::is_const<typename std::remove_reference<
          typename std::iterator_traits<Iter>::reference>::type> {};

template <bool Cond, typename T>
struct AddConstIf : std::conditional<Cond, const T, T> {};

// SynthIterTraits propagates the constness of the 'BaseIter' iterator
// type to its own exported 'pointer' and 'reference' typedefs.
template <typename BaseIter, typename Val>
struct SynthIterTraits : std::iterator_traits<BaseIter> {
 private:
  static constexpr bool kIterConst = IsConstIter<BaseIter>::value;

 public:
  using value_type = typename std::remove_cv<Val>::type;
  using pointer = typename AddConstIf<kIterConst, Val>::type*;
  using reference = typename AddConstIf<kIterConst, Val>::type&;
};

// PointeeSynthIterTraits is similar to SynthIterTraits, but the 'Ptr'
// parameter is a pointer-like type, and value_type is the pointee.
template <typename BaseIter, typename Ptr>
struct PointeeSynthIterTraits : std::iterator_traits<BaseIter> {
 private:
  static constexpr bool kIterConst = IsConstIter<BaseIter>::value;

 public:
  using value_type = typename std::pointer_traits<Ptr>::element_type;
  using pointer = typename AddConstIf<kIterConst, value_type>::type*;
  using reference = typename AddConstIf<kIterConst, value_type>::type&;
};

// CRTP base class for generating iterator adaptors.
// 'Sub' is the derived type, and 'Policy' encodes
// all of the behavior for the adaptor.
// Policy requirements:
//   - type 'underlying_iterator': the underlying iterator type.
//   - type 'adapted_traits': the traits of the adaptor.
//   - static 'Extract(underlying_iterator)': convert iterator to reference.
//
template <typename Sub, typename Policy>
class IteratorAdaptorBase {
 private:
  // Everything needed from the Policy type is expressed in this section.
  using Iterator = typename Policy::underlying_iterator;
  using OutTraits = typename Policy::adapted_traits;
  static typename OutTraits::reference Extract(const Iterator& it) {
    return Policy::Extract(it);
  }

 public:
  using iterator_category = typename OutTraits::iterator_category;
  using value_type = typename OutTraits::value_type;
  using pointer = typename OutTraits::pointer;
  using reference = typename OutTraits::reference;
  using difference_type = typename OutTraits::difference_type;

  IteratorAdaptorBase() : it_() {
  }
  // NOLINTNEXTLINE(runtime/explicit)
  IteratorAdaptorBase(Iterator it) : it_(it) {
  }

  Sub& sub() {
    return static_cast<Sub&>(*this);
  }
  const Sub& sub() const {
    return static_cast<const Sub&>(*this);
  }

  const Iterator& base() const {
    return it_;
  }

  reference get() const {
    return Extract(base());
  }
  reference operator*() const {
    return get();
  }
  pointer operator->() const {
    return &get();
  }
  reference operator[](difference_type d) const {
    return *(sub() + d);
  }

  Sub& operator++() {
    ++it_;
    return sub();
  }
  Sub& operator--() {
    --it_;
    return sub();
  }
  Sub operator++(int /*unused*/) {
    return it_++;
  }
  Sub operator--(int /*unused*/) {
    return it_--;
  }

  Sub& operator+=(difference_type d) {
    it_ += d;
    return sub();
  }
  Sub& operator-=(difference_type d) {
    it_ -= d;
    return sub();
  }

  bool operator==(Sub b) const {
    return base() == b.base();
  }
  bool operator!=(Sub b) const {
    return base() != b.base();
  }
  // These shouldn't be necessary, as implicit conversion from 'Iterator'
  // should be enough to make such comparisons work.
  bool operator==(Iterator b) const {
    return *this == Sub(b);
  }
  bool operator!=(Iterator b) const {
    return *this != Sub(b);
  }

  friend Sub operator+(Sub it, difference_type d) {
    return it.base() + d;
  }
  friend Sub operator+(difference_type d, Sub it) {
    return it + d;
  }
  friend Sub operator-(Sub it, difference_type d) {
    return it.base() - d;
  }
  friend difference_type operator-(Sub a, Sub b) {
    return a.base() - b.base();
  }

  friend bool operator<(Sub a, Sub b) {
    return a.base() < b.base();
  }
  friend bool operator>(Sub a, Sub b) {
    return a.base() > b.base();
  }
  friend bool operator<=(Sub a, Sub b) {
    return a.base() <= b.base();
  }
  friend bool operator>=(Sub a, Sub b) {
    return a.base() >= b.base();
  }

 private:
  Iterator it_;
};

template <typename It>
struct FirstPolicy {
  using underlying_iterator = It;
  using adapted_traits =
      SynthIterTraits<underlying_iterator,
                      typename std::iterator_traits<
                          underlying_iterator>::value_type::first_type>;
  static typename adapted_traits::reference Extract(
      const underlying_iterator& it) {
    return it->first;
  }
};

template <typename It>
struct SecondPolicy {
  using underlying_iterator = It;
  using adapted_traits =
      SynthIterTraits<underlying_iterator,
                      typename std::iterator_traits<
                          underlying_iterator>::value_type::second_type>;
  static typename adapted_traits::reference Extract(
      const underlying_iterator& it) {
    return it->second;
  }
};

template <typename It>
struct SecondPtrPolicy {
  using underlying_iterator = It;
  using adapted_traits =
      PointeeSynthIterTraits<underlying_iterator,
                             typename std::iterator_traits<
                                 underlying_iterator>::value_type::second_type>;
  static typename adapted_traits::reference Extract(
      const underlying_iterator& it) {
    return *it->second;
  }
};

template <typename It>
struct PtrPolicy {
  using underlying_iterator = It;
  using adapted_traits = PointeeSynthIterTraits<
      underlying_iterator,
      typename std::iterator_traits<underlying_iterator>::value_type>;
  static typename adapted_traits::reference Extract(
      const underlying_iterator& it) {
    return **it;
  }
};

}  // namespace internal

// In both iterator adaptors, iterator_first<> and iterator_second<>,
// we build a new iterator based on a parameterized iterator type, "It".
// The value type, "Val" is determined by "It::value_type::first" or
// "It::value_type::second", respectively.

// iterator_first<> adapts an iterator to return the first value of a pair.
// It is equivalent to calling it->first on every value.
// Example:
//
// hash_map<string, int> values;
// values["foo"] = 1;
// values["bar"] = 2;
// for (iterator_first<hash_map<string, int>::iterator> x = values.begin();
//      x != values.end(); ++x) {
//   printf("%s", x->c_str());
// }
template <typename It>
struct iterator_first
    : internal::IteratorAdaptorBase<iterator_first<It>,
                                    internal::FirstPolicy<It>> {
  using Base = internal::IteratorAdaptorBase<iterator_first<It>,
                                             internal::FirstPolicy<It>>;
  iterator_first() {
  }
  iterator_first(It it)  // NOLINT(runtime/explicit)
      : Base(it) {
  }
  template <typename It2>
  iterator_first(iterator_first<It2> o)  // NOLINT(runtime/explicit)
      : Base(o.base()) {
  }
};

template <typename It>
iterator_first<It> make_iterator_first(It it) {
  return iterator_first<It>(it);
}

// iterator_second<> adapts an iterator to return the second value of a pair.
// It is equivalent to calling it->second on every value.
// Example:
//
// hash_map<string, int> values;
// values["foo"] = 1;
// values["bar"] = 2;
// for (iterator_second<hash_map<string, int>::iterator> x = values.begin();
//      x != values.end(); ++x) {
//   int v = *x;
//   printf("%d", v);
// }
template <typename It>
struct iterator_second
    : internal::IteratorAdaptorBase<iterator_second<It>,
                                    internal::SecondPolicy<It>> {
  using Base = internal::IteratorAdaptorBase<iterator_second<It>,
                                             internal::SecondPolicy<It>>;
  iterator_second() {
  }
  iterator_second(It it)  // NOLINT(runtime/explicit)
      : Base(it) {
  }
  template <typename It2>
  iterator_second(iterator_second<It2> o)  // NOLINT(runtime/explicit)
      : Base(o.base()) {
  }
};

template <typename It>
iterator_second<It> make_iterator_second(It it) {
  return iterator_second<It>(it);
}

// iterator_second_ptr<> adapts an iterator to return the dereferenced second
// value of a pair.
// It is equivalent to calling *it->second on every value.
// The same result can be achieved by composition
// iterator_ptr<iterator_second<> >
// Can be used with maps where values are regular pointers or pointers wrapped
// into linked_ptr. This iterator adaptor can be used by classes to give their
// clients access to some of their internal data without exposing too much of
// it.
//
// Example:
// class MyClass {
//  public:
//   MyClass(const string& s);
//   string DebugString() const;
// };
// typedef hash_map<string, linked_ptr<MyClass> > MyMap;
// typedef iterator_second_ptr<MyMap::iterator> MyMapValuesIterator;
// MyMap values;
// values["foo"].reset(new MyClass("foo"));
// values["bar"].reset(new MyClass("bar"));
// for (MyMapValuesIterator it = values.begin(); it != values.end(); ++it) {
//   printf("%s", it->DebugString().c_str());
// }
template <typename It>
struct iterator_second_ptr
    : internal::IteratorAdaptorBase<iterator_second_ptr<It>,
                                    internal::SecondPtrPolicy<It>> {
  using Base = internal::IteratorAdaptorBase<iterator_second_ptr<It>,
                                             internal::SecondPtrPolicy<It>>;
  iterator_second_ptr() {
  }
  iterator_second_ptr(It it)  // NOLINT(runtime/explicit)
      : Base(it) {
  }
  template <typename It2>
  iterator_second_ptr(iterator_second_ptr<It2> o)  // NOLINT(runtime/explicit)
      : Base(o.base()) {
  }
};

template <typename It>
iterator_second_ptr<It> make_iterator_second_ptr(It it) {
  return iterator_second_ptr<It>(it);
}

// iterator_ptr<> adapts an iterator to return the dereferenced value.
// With this adaptor you can write *it instead of **it, or it->something instead
// of (*it)->something.
// Can be used with vectors and lists where values are regular pointers
// or pointers wrapped into linked_ptr. This iterator adaptor can be used by
// classes to give their clients access to some of their internal data without
// exposing too much of it.
//
// Example:
// class MyClass {
//  public:
//   MyClass(const string& s);
//   string DebugString() const;
// };
// typedef vector<linked_ptr<MyClass> > MyVector;
// typedef iterator_ptr<MyVector::iterator> DereferencingIterator;
// MyVector values;
// values.push_back(make_linked_ptr(new MyClass("foo")));
// values.push_back(make_linked_ptr(new MyClass("bar")));
// for (DereferencingIterator it = values.begin(); it != values.end(); ++it) {
//   printf("%s", it->DebugString().c_str());
// }
//
// Without iterator_ptr you would have to do (*it)->DebugString()
template <typename It, typename Ptr /* ignored */ = void>
struct iterator_ptr : internal::IteratorAdaptorBase<iterator_ptr<It, Ptr>,
                                                    internal::PtrPolicy<It>> {
  using Base = internal::IteratorAdaptorBase<iterator_ptr<It, Ptr>,
                                             internal::PtrPolicy<It>>;
  iterator_ptr() {
  }
  iterator_ptr(It it)  // NOLINT(runtime/explicit)
      : Base(it) {
  }
  template <typename It2>
  iterator_ptr(iterator_ptr<It2> o)  // NOLINT(runtime/explicit)
      : Base(o.base()) {
  }
};

template <typename It>
iterator_ptr<It> make_iterator_ptr(It it) {
  return iterator_ptr<It>(it);
}

namespace internal {

// Template that uses SFINAE to inspect Container abilities:
// . Set has_size_type true, iff T::size_type is defined
// . Define size_type as T::size_type if defined, or size_t otherwise
template <typename C>
struct container_traits {
 private:
  // Test for availability of C::size_type.
  template <typename U, typename = void>
  struct test_size_type : std::false_type {};
  template <typename U>
  struct test_size_type<U, absl::void_t<typename U::size_type>>
      : std::true_type {};

  // Conditional provisioning of a size_type which defaults to size_t.
  template <bool Cond, typename U = void>
  struct size_type_def {
    using type = typename U::size_type;
  };
  template <typename U>
  struct size_type_def<false, U> {
    using type = size_t;
  };

 public:
  // Determine whether C::size_type is available.
  static const bool has_size_type = test_size_type<C>::value;

  // Provide size_type as either C::size_type if available, or as size_t.
  using size_type = typename size_type_def<has_size_type, C>::type;
};

template <typename C>
struct IterGenerator {
  using container_type = C;
  using iterator = typename C::iterator;
  using const_iterator = typename C::const_iterator;

  static iterator begin(container_type& c) {  // NOLINT(runtime/references)
    return c.begin();
  }
  static iterator end(container_type& c) {  // NOLINT(runtime/references)
    return c.end();
  }
  static const_iterator begin(const container_type& c) {
    return c.begin();
  }
  static const_iterator end(const container_type& c) {
    return c.end();
  }
};

template <typename SubIterGenerator>
struct ReversingIterGeneratorAdaptor {
  using container_type = typename SubIterGenerator::container_type;
  using iterator = std::reverse_iterator<typename SubIterGenerator::iterator>;
  using const_iterator =
      std::reverse_iterator<typename SubIterGenerator::const_iterator>;

  static iterator begin(container_type& c) {  // NOLINT(runtime/references)
    return iterator(SubIterGenerator::end(c));
  }
  static iterator end(container_type& c) {  // NOLINT(runtime/references)
    return iterator(SubIterGenerator::begin(c));
  }
  static const_iterator begin(const container_type& c) {
    return const_iterator(SubIterGenerator::end(c));
  }
  static const_iterator end(const container_type& c) {
    return const_iterator(SubIterGenerator::begin(c));
  }
};

// C:             the container type
// Iter:          the type of mutable iterator to generate
// ConstIter:     the type of constant iterator to generate
// IterGenerator: a policy type that returns native iterators from a C
template <typename C,
          typename Iter,
          typename ConstIter,
          typename IterGenerator = internal::IterGenerator<C>>
class iterator_view_helper {
 public:
  using container_type = C;
  using iterator = Iter;
  using const_iterator = ConstIter;
  using value_type = typename std::iterator_traits<iterator>::value_type;
  using size_type = typename internal::container_traits<C>::size_type;

  explicit iterator_view_helper(
      container_type& c)  // NOLINT(runtime/references)
      : c_(&c) {
  }

  iterator begin() {
    return iterator(IterGenerator::begin(container()));
  }
  iterator end() {
    return iterator(IterGenerator::end(container()));
  }
  const_iterator begin() const {
    return const_iterator(IterGenerator::begin(container()));
  }
  const_iterator end() const {
    return const_iterator(IterGenerator::end(container()));
  }
  const_iterator cbegin() const {
    return begin();
  }
  const_iterator cend() const {
    return end();
  }
  const container_type& container() const {
    return *c_;
  }
  container_type& container() {
    return *c_;
  }

  bool empty() const {
    return begin() == end();
  }
  size_type size() const {
    return c_->size();
  }

 private:
  container_type* c_;
};

template <typename C,
          typename ConstIter,
          typename IterGenerator = internal::IterGenerator<C>>
class const_iterator_view_helper {
 public:
  using container_type = C;
  using const_iterator = ConstIter;
  using value_type = typename std::iterator_traits<const_iterator>::value_type;
  using size_type = typename internal::container_traits<C>::size_type;

  explicit const_iterator_view_helper(const container_type& c) : c_(&c) {
  }

  // Allow implicit conversion from the corresponding iterator_view_helper.
  // Erring on the side of constness should be allowed. E.g.:
  //    MyMap m;
  //    key_view_type<MyMap>::type keys = key_view(m);  // ok
  //    key_view_type<const MyMap>::type const_keys = key_view(m);  // ok
  template <typename Iter>
  const_iterator_view_helper(const iterator_view_helper<container_type,
                                                        Iter,
                                                        const_iterator,
                                                        IterGenerator>& v)
      : c_(&v.container()) {
  }

  const_iterator begin() const {
    return const_iterator(IterGenerator::begin(container()));
  }
  const_iterator end() const {
    return const_iterator(IterGenerator::end(container()));
  }
  const_iterator cbegin() const {
    return begin();
  }
  const_iterator cend() const {
    return end();
  }
  const container_type& container() const {
    return *c_;
  }

  bool empty() const {
    return begin() == end();
  }
  size_type size() const {
    return c_->size();
  }

 private:
  const container_type* c_;
};

}  // namespace internal

// Note: The views like value_view, key_view should be in gtl namespace.
// Currently there are lot of callers that reference the methods in the global
// namespace.
//
// Traits to provide a typedef abstraction for the return value
// of the key_view() and value_view() functions, such that
// they can be declared as:
//
//    template <typename C> key_view_t<C> key_view(C& c);
//    template <typename C> value_view_t<C> value_view(C& c);
//
// This abstraction allows callers of these functions to use readable
// type names, and allows the maintainers of iterator_adaptors.h to
// change the return types if needed without updating callers.

template <typename C>
struct key_view_type {
  using type = internal::iterator_view_helper<
      C,
      iterator_first<typename C::iterator>,
      iterator_first<typename C::const_iterator>>;
};

template <typename C>
struct key_view_type<const C> {
  using type = internal::
      const_iterator_view_helper<C, iterator_first<typename C::const_iterator>>;
};

template <typename C>
struct value_view_type {
  using type = internal::iterator_view_helper<
      C,
      iterator_second<typename C::iterator>,
      iterator_second<typename C::const_iterator>>;
};

template <typename C>
struct value_view_type<const C> {
  using type = internal::const_iterator_view_helper<
      C,
      iterator_second<typename C::const_iterator>>;
};

// The key_view and value_view functions provide pretty ways to iterate either
// the keys or the values of a map using range based for loops.
//
// Example:
//    hash_map<int, string> my_map;
//    ...
//    for (string val : value_view(my_map)) {
//      ...
//    }
//
// Note: If you pass a temporary container to key_view or value_view, be careful
// that the temporary container outlives the wrapper view to avoid dangling
// references.
// This is fine:  PublishAll(value_view(Make());
// This is not:   for (const auto& v : value_view(Make())) Publish(v);

template <typename C>
typename key_view_type<C>::type key_view(
    C& map) {  // NOLINT(runtime/references)
  return typename key_view_type<C>::type(map);
}

template <typename C>
typename key_view_type<const C>::type key_view(const C& map) {
  return typename key_view_type<const C>::type(map);
}

template <typename C>
typename value_view_type<C>::type value_view(
    C& map) {  // NOLINT(runtime/references)
  return typename value_view_type<C>::type(map);
}

template <typename C>
typename value_view_type<const C>::type value_view(const C& map) {
  return typename value_view_type<const C>::type(map);
}

// Abstract container view that dereferences the pointer-like .second member
// of a container's std::pair elements, such as the elements of std::map<K,V*>
// or of std::vector<std::pair<K,V*>>.
//
// Example:
//   map<int, string*> elements;
//   for (const string& element : deref_second_view(elements)) {
//     ...
//   }
//
// Note: If you pass a temporary container to deref_second_view, be careful that
// the temporary container outlives the deref_second_view to avoid dangling
// references.
// This is fine:  PublishAll(deref_second_view(Make());
// This is not:   for (const auto& v : deref_second_view(Make())) {
//                  Publish(v);
//                }

template <typename C>
struct deref_second_view_type {
  using type = internal::iterator_view_helper<
      C,
      iterator_second_ptr<typename C::iterator>,
      iterator_second_ptr<typename C::const_iterator>>;
};

template <typename C>
struct deref_second_view_type<const C> {
  using type = internal::const_iterator_view_helper<
      C,
      iterator_second_ptr<typename C::const_iterator>>;
};

template <typename C>
typename deref_second_view_type<C>::type deref_second_view(
    C& map) {  // NOLINT(runtime/references)
  return typename deref_second_view_type<C>::type(map);
}

template <typename C>
typename deref_second_view_type<const C>::type deref_second_view(const C& map) {
  return typename deref_second_view_type<const C>::type(map);
}

// Abstract container view that dereferences pointer elements.
//
// Example:
//   vector<string*> elements;
//   for (const string& element : deref_view(elements)) {
//     ...
//   }
//
// Note: If you pass a temporary container to deref_view, be careful that the
// temporary container outlives the deref_view to avoid dangling references.
// This is fine:  PublishAll(deref_view(Make());
// This is not:   for (const auto& v : deref_view(Make())) { Publish(v); }

template <typename C>
struct deref_view_type {
  using type =
      internal::iterator_view_helper<C,
                                     iterator_ptr<typename C::iterator>,
                                     iterator_ptr<typename C::const_iterator>>;
};

template <typename C>
struct deref_view_type<const C> {
  using type = internal::
      const_iterator_view_helper<C, iterator_ptr<typename C::const_iterator>>;
};

template <typename C>
typename deref_view_type<C>::type deref_view(
    C& c) {  // NOLINT(runtime/references)
  return typename deref_view_type<C>::type(c);
}

template <typename C>
typename deref_view_type<const C>::type deref_view(const C& c) {
  return typename deref_view_type<const C>::type(c);
}

// Abstract container view that iterates backwards.
//
// Example:
//   vector<string> elements;
//   for (const string& element : reversed_view(elements)) {
//     ...
//   }
//
// Note: If you pass a temporary container to reversed_view_type, be careful
// that the temporary container outlives the reversed_view to avoid dangling
// references. This is fine:  PublishAll(reversed_view(Make());
// This is not:   for (const auto& v : reversed_view(Make())) { Publish(v); }

template <typename C>
struct reversed_view_type {
 private:
  using policy =
      internal::ReversingIterGeneratorAdaptor<internal::IterGenerator<C>>;

 public:
  using type = internal::iterator_view_helper<C,
                                              typename policy::iterator,
                                              typename policy::const_iterator,
                                              policy>;
};

template <typename C>
struct reversed_view_type<const C> {
 private:
  using policy =
      internal::ReversingIterGeneratorAdaptor<internal::IterGenerator<C>>;

 public:
  using type = internal::
      const_iterator_view_helper<C, typename policy::const_iterator, policy>;
};

template <typename C>
typename reversed_view_type<C>::type reversed_view(
    C& c) {  // NOLINT(runtime/references)
  return typename reversed_view_type<C>::type(c);
}

template <typename C>
typename reversed_view_type<const C>::type reversed_view(const C& c) {
  return typename reversed_view_type<const C>::type(c);
}

}  // namespace util
}  // namespace firestore
}  // namespace firebase

#endif  // FIRESTORE_CORE_SRC_FIREBASE_FIRESTORE_UTIL_ITERATOR_ADAPTORS_H_