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