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 : #ifndef QUEUE_H
11 : #define QUEUE_H
12 :
13 : #include <functional>
14 : #include <iterator>
15 : #include <vector>
16 : #include <memory>
17 : #include <task_info.h>
18 : #include "exception_util.h"
19 : #include "internal_exception.h"
20 : #include "log.h"
21 : namespace Hccl {
22 :
23 : template <typename T> class Queue {
24 : public:
25 : class Iterator {
26 : protected:
27 : typename std::vector<T>::const_iterator it_;
28 :
29 : protected:
30 0 : virtual void check()
31 : {
32 0 : return;
33 : }
34 :
35 : public:
36 : using pointer = const T *;
37 : using reference = const T &;
38 :
39 4253 : explicit Iterator(typename std::vector<T>::const_iterator it) : it_(it)
40 : {
41 4253 : }
42 :
43 16 : Iterator() {}
44 :
45 12473 : virtual ~Iterator() = default;
46 :
47 217 : virtual reference operator*() const
48 : {
49 217 : return *(this->it_);
50 : }
51 :
52 6 : virtual pointer operator->() const
53 : {
54 6 : return &*(this->it_);
55 : }
56 :
57 0 : virtual Iterator &operator++()
58 : {
59 0 : (this->it_)++;
60 0 : check();
61 0 : return *this;
62 : }
63 :
64 0 : virtual Iterator operator++(int)
65 : {
66 0 : Iterator temp(*this);
67 0 : ++it_;
68 0 : check();
69 0 : return temp;
70 0 : }
71 :
72 0 : virtual Iterator &operator--()
73 : {
74 0 : (this->it_)--;
75 0 : check();
76 0 : return *this;
77 : }
78 :
79 0 : virtual Iterator operator--(int)
80 : {
81 0 : Iterator temp(*this);
82 0 : --it_;
83 0 : check();
84 0 : return temp;
85 0 : }
86 :
87 13 : virtual bool operator==(const Iterator &other) const
88 : {
89 13 : return it_ == other.it_;
90 : }
91 :
92 4182 : virtual bool operator!=(const Iterator &other) const
93 : {
94 4182 : return it_ != other.it_;
95 : }
96 : };
97 :
98 52 : virtual ~Queue() = default;
99 :
100 : virtual void Append(T &&value) = 0;
101 : virtual T& GetAndUpdate() = 0; // 返回当前元素并更新index
102 : virtual void Traverse(std::function<void(const T &)> action) = 0;
103 : virtual size_t Size() const = 0;
104 : virtual bool IsEmpty() const = 0;
105 : virtual bool IsFull() const = 0;
106 : virtual size_t Capacity() const = 0;
107 : virtual std::shared_ptr<Iterator> Find(std::function<bool(const T &)> cond) = 0;
108 : virtual std::shared_ptr<Iterator> Begin() const = 0;
109 : virtual std::shared_ptr<Iterator> Tail() const = 0;
110 : virtual std::shared_ptr<Iterator> End() const = 0;
111 0 : virtual void PopFront()
112 : {
113 0 : THROW<InternalException>(StringFormat("Queue<T>::PopFront () is not supported"));
114 : }
115 : };
116 :
117 : template <typename T> class QueueWithSize : public Queue<T> {
118 : protected:
119 : size_t size_ = 0;
120 :
121 : public:
122 4 : size_t Size() const override
123 : {
124 4 : return size_;
125 : }
126 :
127 2169 : bool IsEmpty() const override
128 : {
129 2169 : return size_ == 0;
130 : }
131 : };
132 :
133 : } // namespace Hccl
134 : #endif // QUEUE_H
|