NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
LayerListIterators.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_LAYERLISTITERATORS_H
27 #define NVNEURAL_LAYERLISTITERATORS_H
28 
29 #include <nvneural/CoreTypes.h>
30 
31 #include <iterator>
32 
33 namespace nvneural {
34 
40 {
41 public:
43  using iterator_category = std::forward_iterator_tag;
45  using difference_type = std::ptrdiff_t;
47  using value_type = ILayer*;
49  using pointer = ILayer* const*;
51  using reference = ILayer* const&;
52 
56  LayerListIterator(const ILayerList* pLayerList, size_t index)
57  : m_pLayerList(pLayerList)
58  , m_index(index)
59  {
60  }
61 
63  LayerListIterator(RefPtr<ILayerList>& pLayerList, size_t index)
64  : m_pLayerList(pLayerList.get())
65  , m_index(index)
66  {
67  }
68 
70  ILayer* operator *() const noexcept
71  {
72  return m_pLayerList->getLayerByIndex(m_index);
73  }
74 
77  {
78  ++m_index;
79  return *this;
80  }
81 
84  {
85  LayerListIterator oldMe = *this;
86  ++*this;
87  return oldMe;
88  }
89 
92  bool operator !=(const LayerListIterator& other) const noexcept
93  {
94  const bool samePointer = m_pLayerList == other.m_pLayerList;
95  const bool sameIndex = m_index == other.m_index;
96  return !(samePointer && sameIndex);
97  }
98 
99 private:
100  const ILayerList* const m_pLayerList;
101  size_t m_index;
102 };
103 
108 inline LayerListIterator begin(const ILayerList* pLayerList) noexcept
109 {
110  return LayerListIterator(pLayerList, 0);
111 }
112 
114 inline LayerListIterator begin(const RefPtr<ILayerList>& pLayerList) noexcept
115 {
116  return LayerListIterator(pLayerList.get(), 0);
117 }
118 
123 inline LayerListIterator end(const ILayerList* pLayerList) noexcept
124 {
125  return LayerListIterator(pLayerList, pLayerList->layerCount());
126 }
127 
129 inline LayerListIterator end(const RefPtr<ILayerList>& pLayerList) noexcept
130 {
131  return LayerListIterator(pLayerList.get(), pLayerList->layerCount());
132 }
133 
134 } // namespace nvneural
135 
136 #endif // NVNEURAL_LAYERLISTITERATORS_H
Fundamental NvNeural data types are declared here.
LayerListIterator end(const ILayerList *pLayerList) noexcept
Returns a LayerListIterator pointing to the end of pLayerList (one past the final element).
Definition: LayerListIterators.h:123
LayerListIterator begin(const ILayerList *pLayerList) noexcept
Returns a LayerListIterator pointing to the beginning of pLayerList.
Definition: LayerListIterators.h:108
ILayer is the base class for neural network layers.
Definition: LayerTypes.h:59
ILayerList represents an immutable collection of ILayer pointers.
Definition: CoreTypes.h:1060
virtual ILayer * getLayerByIndex(size_t layerIndex) const noexcept=0
Returns the Nth ILayer pointer in the collection.
Iterator class to enable range-based for loops and similar constructs across any ILayerList.
Definition: LayerListIterators.h:40
std::ptrdiff_t difference_type
This is not a bidirectional iterator, but providing a difference_type allows std::iterator_traits to ...
Definition: LayerListIterators.h:45
ILayer *const & reference
Type of a reference to an iterated-to element.
Definition: LayerListIterators.h:51
bool operator!=(const LayerListIterator &other) const noexcept
Inequality operator.
Definition: LayerListIterators.h:92
ILayer * operator*() const noexcept
Dereferences the iterator, returning the underlying ILayer pointer.
Definition: LayerListIterators.h:70
ILayer *const * pointer
Type of a pointer to an iterated-to element.
Definition: LayerListIterators.h:49
LayerListIterator & operator++() noexcept
Preincrement (++iter); returns the updated iterator.
Definition: LayerListIterators.h:76
std::forward_iterator_tag iterator_category
Tells the C++ standard library this is a forward-only iterator.
Definition: LayerListIterators.h:43
LayerListIterator(const ILayerList *pLayerList, size_t index)
Creates a LayerListIterator pointing to a ILayerList raw pointer.
Definition: LayerListIterators.h:56
LayerListIterator(RefPtr< ILayerList > &pLayerList, size_t index)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition: LayerListIterators.h:63
Intrusive pointer using IRefObject's reference counting system.
Definition: RefPtr.h:46