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>
20 : struct IsSharedPtr final {
21 : static const bool value = false;
22 : };
23 :
24 : template <typename T>
25 : struct IsSharedPtr<
26 : T, typename std::enable_if<std::is_same<T, std::shared_ptr<typename T::element_type>>::value>::type> {
27 : static const bool value = true;
28 : };
29 :
30 : template <typename T, typename Enable = void>
31 : struct IsUniquePtr final {
32 : static const bool value = false;
33 : };
34 :
35 : template <typename T>
36 : struct IsUniquePtr<
37 : T, typename std::enable_if<std::is_same<T, std::unique_ptr<typename T::element_type>>::value>::type> {
38 : static const bool value = true;
39 : };
40 :
41 : template <typename T, typename Enable = void>
42 : struct IsSmartPtr final {
43 : static const bool value = false;
44 : };
45 :
46 : template <typename T>
47 : struct IsSmartPtr<
48 : T, typename std::enable_if<
49 : IsSharedPtr<T>::value || IsUniquePtr<T>::value
50 : || std::is_same<T, std::weak_ptr<typename T::element_type>>::value>::type> {
51 : static const bool value = true;
52 : };
53 :
54 : template <template <class U, typename _Alloc = std::allocator<U>> class Sequence, typename T, typename Enable = void>
55 : class BaseConstIterator {};
56 :
57 : template <template <class U, typename _Alloc = std::allocator<U>> class Sequence, typename T>
58 : class BaseConstIterator<Sequence, T, typename std::enable_if<IsSharedPtr<T>::value || IsUniquePtr<T>::value>::type> {
59 : public:
60 : using V = typename T::element_type;
61 :
62 : BaseConstIterator() : iter(nullptr), end(nullptr) {}
63 :
64 136 : explicit BaseConstIterator(const Sequence<T>& seq) : iter(seq.cbegin()), end(seq.cend()) {}
65 :
66 100 : virtual const V& operator*() { return *(*iter); }
67 :
68 146 : virtual const V* operator->() { return (*iter).get(); }
69 :
70 0 : virtual BaseConstIterator& Next()
71 : {
72 0 : iter++;
73 0 : return *this;
74 : }
75 :
76 93 : virtual BaseConstIterator& operator++()
77 : {
78 93 : iter++;
79 93 : return *this;
80 : }
81 :
82 231 : virtual bool HasNext() { return iter != end; }
83 :
84 141 : virtual ~BaseConstIterator() {};
85 :
86 : protected:
87 : typename Sequence<T>::const_iterator iter;
88 : typename Sequence<T>::const_iterator end;
89 : };
90 :
91 : template <template <class U, typename _Alloc = std::allocator<U>> class Sequence, typename T>
92 : class BaseConstIterator<Sequence, T, typename std::enable_if<!IsSmartPtr<T>::value>::type> {
93 : public:
94 : BaseConstIterator() : iter(nullptr), end(nullptr) {}
95 :
96 13 : explicit BaseConstIterator(const Sequence<T>& seq) : iter(seq.cbegin()), end(seq.cend()) {}
97 :
98 17 : virtual const T& operator*() { return *iter; }
99 :
100 0 : virtual const T* operator->() { return &*iter; }
101 :
102 0 : virtual BaseConstIterator& Next()
103 : {
104 0 : iter++;
105 0 : return *this;
106 : }
107 :
108 10 : virtual BaseConstIterator& operator++()
109 : {
110 10 : iter++;
111 10 : return *this;
112 : }
113 :
114 19 : virtual bool HasNext() { return iter != end; }
115 :
116 : protected:
117 : typename Sequence<T>::const_iterator iter;
118 : typename Sequence<T>::const_iterator end;
119 : };
120 :
121 : } // namespace Hccl
122 :
123 : #endif // HCCLV2_ITERATOR_H
|