LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/dfx/common - vector_queue.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 76.7 % 73 56
Test Date: 2026-08-17 10:19:35 Functions: 60.4 % 48 29

            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 VECTOR_QUEUE_H
      11              : #define VECTOR_QUEUE_H
      12              : 
      13              : #include "queue.h"
      14              : #include <vector>
      15              : 
      16              : namespace Hccl {
      17              : constexpr uint32_t VECTOR_QUEUE_SIZE = 2048;
      18              : 
      19              : template <typename T>
      20              : class VectorQueue : public QueueWithSize<T> {
      21              : private:
      22              :     std::vector<T> elems_;
      23              : 
      24              : public:
      25              :     class Iterator : public Queue<T>::Iterator {
      26              :     private:
      27              :         const VectorQueue* queue_{nullptr};
      28              :         u32 index_ = 0;
      29              : 
      30              :     protected:
      31           24 :         void check() override
      32              :         {
      33           24 :             if ((this->index_) > queue_->size_) {
      34            0 :                 THROW<InternalException>(StringFormat("VectorQueue<T>::Iterator out of range"));
      35              :             }
      36           24 :         }
      37              : 
      38              :     public:
      39              :         using pointer = const T*;
      40              :         using reference = const T&;
      41              : 
      42           16 :         Iterator(const VectorQueue* queue, u32 index) : queue_(queue), index_(index) { check(); }
      43              : 
      44           20 :         ~Iterator() override = default;
      45              : 
      46           15 :         reference operator*() const override
      47              :         {
      48           15 :             if (this->index_ >= this->queue_->size_) {
      49            0 :                 THROW<InternalException>(StringFormat(
      50            0 :                     "VectorQueue<T>::Iterator dereference out of range, index[%u], size[%zu]", this->index_,
      51            0 :                     this->queue_->size_));
      52              :             }
      53           15 :             return (this->queue_->elems_[this->index_]);
      54              :         }
      55              : 
      56            0 :         pointer operator->() const override
      57              :         {
      58            0 :             if (this->index_ >= this->queue_->size_) {
      59            0 :                 THROW<InternalException>(StringFormat(
      60            0 :                     "VectorQueue<T>::Iterator dereference out of range, index[%u], size[%zu]", this->index_,
      61            0 :                     this->queue_->size_));
      62              :             }
      63            0 :             return &(this->queue_->elems_[this->index_]);
      64              :         }
      65              : 
      66            2 :         typename Queue<T>::Iterator& operator++() override
      67              :         {
      68            2 :             (this->index_)++;
      69            2 :             check();
      70            2 :             return *this;
      71              :         }
      72              : 
      73            2 :         typename Queue<T>::Iterator operator++(int) override
      74              :         {
      75            2 :             Iterator temp = *this;
      76            2 :             (this->index_)++;
      77            2 :             check();
      78            4 :             return temp;
      79            2 :         }
      80              : 
      81            2 :         typename Queue<T>::Iterator& operator--() override
      82              :         {
      83            2 :             (this->index_)--;
      84            2 :             check();
      85            2 :             return *this;
      86              :         }
      87              : 
      88            2 :         typename Queue<T>::Iterator operator--(int) override
      89              :         {
      90            2 :             Iterator temp = *this;
      91            2 :             (this->index_)--;
      92            2 :             check();
      93            4 :             return temp;
      94            2 :         }
      95              : 
      96            5 :         bool operator==(const typename Queue<T>::Iterator& other) const override
      97              :         {
      98            5 :             return this->index_ == static_cast<const Iterator&>(other).index_;
      99              :         }
     100              : 
     101            1 :         bool operator!=(const typename Queue<T>::Iterator& other) const override
     102              :         {
     103            1 :             return this->index_ != static_cast<const Iterator&>(other).index_;
     104              :         }
     105              :     };
     106              : 
     107           42 :     VectorQueue() : elems_(VECTOR_QUEUE_SIZE) {}
     108              : 
     109           58 :     ~VectorQueue() override { HCCL_INFO("[VectorQueue]Destroy"); }
     110              : 
     111         2074 :     void Append(T&& value) override
     112              :     {
     113         2074 :         if (UNLIKELY(this->size_ >= VECTOR_QUEUE_SIZE)) {
     114            0 :             THROW<InternalException>(StringFormat("VectorQueue<T>::Append size[%zu] is full", this->size_));
     115              :         }
     116         2074 :         elems_[this->size_] = std::move(value);
     117         2074 :         this->size_++;
     118         2074 :     }
     119              : 
     120            0 :     T& GetAndUpdate() override
     121              :     {
     122            0 :         if (UNLIKELY(this->size_ >= VECTOR_QUEUE_SIZE)) {
     123            0 :             THROW<InternalException>(StringFormat("VectorQueue<T>::GetAndUpdate size[%zu] is full", this->size_));
     124              :         }
     125            0 :         return elems_[this->size_++];
     126              :     }
     127              : 
     128            1 :     void Traverse(std::function<void(const T&)> action) override
     129              :     {
     130           11 :         for (size_t i = 0; i < this->size_; ++i) {
     131           10 :             action(elems_[i]);
     132              :         }
     133            1 :     }
     134              : 
     135            0 :     bool IsFull() const override { return this->size_ >= VECTOR_QUEUE_SIZE; }
     136              : 
     137            0 :     size_t Capacity() const override { return VECTOR_QUEUE_SIZE; }
     138              : 
     139            4 :     std::shared_ptr<typename Queue<T>::Iterator> Find(std::function<bool(const T&)> cond) override
     140              :     {
     141            6 :         for (size_t i = 0; i < this->size_; ++i) {
     142            4 :             if (cond(elems_[i])) {
     143            2 :                 return std::make_shared<Iterator>(this, static_cast<u32>(i));
     144              :             }
     145              :         }
     146            2 :         return std::make_shared<Iterator>(this, static_cast<u32>(this->size_));
     147              :     }
     148              : 
     149            4 :     std::shared_ptr<typename Queue<T>::Iterator> Begin() const override { return std::make_shared<Iterator>(this, 0); }
     150              : 
     151            2 :     std::shared_ptr<typename Queue<T>::Iterator> Tail() const override
     152              :     {
     153            2 :         if (this->IsEmpty()) {
     154            3 :             HCCL_WARNING("[VectorQueue][Tail] Queue is empty!");
     155            1 :             return std::make_shared<Iterator>(this, 0);
     156              :         }
     157            1 :         return std::make_shared<Iterator>(this, static_cast<u32>(this->size_ - 1));
     158              :     }
     159              : 
     160            6 :     std::shared_ptr<typename Queue<T>::Iterator> End() const override
     161              :     {
     162            6 :         return std::make_shared<Iterator>(this, static_cast<u32>(this->size_));
     163              :     }
     164              : };
     165              : 
     166              : } // namespace Hccl
     167              : #endif // VECTOR_QUEUE_H
        

Generated by: LCOV version 2.0-1