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