Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef HCCLV2_ITERATOR_H
12 : #define HCCLV2_ITERATOR_H
13 :
14 : #include <iterator>
15 : #include <memory>
16 :
17 : namespace Hccl {
18 :
19 : template <typename T, typename Enable = void> struct IsSharedPtr final {
20 : static const bool value = false;
21 : };
22 :
23 : template <typename T>
24 : struct IsSharedPtr<T,
25 : typename std::enable_if<std::is_same<T, std::shared_ptr<typename T::element_type>>::value>::type> {
26 : static const bool value = true;
27 : };
28 :
29 : template <typename T, typename Enable = void> struct IsUniquePtr final {
30 : static const bool value = false;
31 : };
32 :
33 : template <typename T>
34 : struct IsUniquePtr<T,
35 : typename std::enable_if<std::is_same<T, std::unique_ptr<typename T::element_type>>::value>::type> {
36 : static const bool value = true;
37 : };
38 :
39 : template <typename T, typename Enable = void> struct IsSmartPtr final {
40 : static const bool value = false;
41 : };
42 :
43 : template <typename T>
44 : struct IsSmartPtr<T,
45 : typename std::enable_if<IsSharedPtr<T>::value || IsUniquePtr<T>::value
46 : || std::is_same<T, std::weak_ptr<typename T::element_type>>::value>::type> {
47 : static const bool value = true;
48 : };
49 :
50 : template <template <class U, typename _Alloc = std::allocator<U>> class Sequence, typename T, typename Enable = void>
51 : class BaseConstIterator {};
52 :
53 : template <template <class U, typename _Alloc = std::allocator<U>> class Sequence, typename T>
54 : class BaseConstIterator<Sequence, T, typename std::enable_if<IsSharedPtr<T>::value || IsUniquePtr<T>::value>::type> {
55 : public:
56 : using V = typename T::element_type;
57 :
58 : BaseConstIterator() : iter(nullptr), end(nullptr)
59 : {
60 : }
61 :
62 161 : explicit BaseConstIterator(const Sequence<T> &seq) : iter(seq.cbegin()), end(seq.cend())
63 : {
64 161 : }
65 :
66 100 : virtual const V &operator*()
67 : {
68 100 : return *(*iter);
69 : }
70 :
71 146 : virtual const V *operator->()
72 : {
73 146 : return (*iter).get();
74 : }
75 :
76 0 : virtual BaseConstIterator &Next()
77 : {
78 0 : iter++;
79 0 : return *this;
80 : }
81 :
82 93 : virtual BaseConstIterator &operator++()
83 : {
84 93 : iter++;
85 93 : return *this;
86 : }
87 :
88 256 : virtual bool HasNext()
89 : {
90 256 : return iter != end;
91 : }
92 :
93 166 : virtual ~BaseConstIterator() {};
94 :
95 : protected:
96 : typename Sequence<T>::const_iterator iter;
97 : typename Sequence<T>::const_iterator end;
98 : };
99 :
100 : template <template <class U, typename _Alloc = std::allocator<U>> class Sequence, typename T>
101 : class BaseConstIterator<Sequence, T, typename std::enable_if<!IsSmartPtr<T>::value>::type> {
102 : public:
103 : BaseConstIterator() : iter(nullptr), end(nullptr)
104 : {
105 : }
106 :
107 13 : explicit BaseConstIterator(const Sequence<T> &seq) : iter(seq.cbegin()), end(seq.cend())
108 : {
109 13 : }
110 :
111 17 : virtual const T &operator*()
112 : {
113 17 : return *iter;
114 : }
115 :
116 0 : virtual const T *operator->()
117 : {
118 0 : return &*iter;
119 : }
120 :
121 0 : virtual BaseConstIterator &Next()
122 : {
123 0 : iter++;
124 0 : return *this;
125 : }
126 :
127 10 : virtual BaseConstIterator &operator++()
128 : {
129 10 : iter++;
130 10 : return *this;
131 : }
132 :
133 19 : virtual bool HasNext()
134 : {
135 19 : return iter != end;
136 : }
137 :
138 : protected:
139 : typename Sequence<T>::const_iterator iter;
140 : typename Sequence<T>::const_iterator end;
141 : };
142 :
143 : } // namespace Hccl
144 :
145 : #endif // HCCLV2_ITERATOR_H
|