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: 78.5 % 79 62
Test Date: 2026-08-04 10:52:23 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> class VectorQueue : public QueueWithSize<T> {
      20              : private:
      21              :     std::vector<T> elems_;
      22              : 
      23              : public:
      24              :     class Iterator : public Queue<T>::Iterator {
      25              :     private:
      26              :         const VectorQueue *queue_{nullptr};
      27              :         u32          index_ = 0;
      28              : 
      29              :     protected:
      30           24 :         void check() override
      31              :         {
      32           24 :             if ((this->index_) > queue_->size_) {
      33            0 :                 THROW<InternalException>(StringFormat("VectorQueue<T>::Iterator out of range"));
      34              :             }
      35           24 :         }
      36              : 
      37              :     public:
      38              :         using pointer   = const T *;
      39              :         using reference = const T &;
      40              : 
      41           16 :         Iterator(const VectorQueue *queue, u32 index) : queue_(queue), index_(index)
      42              :         {
      43           16 :             check();
      44           16 :         }
      45              : 
      46           20 :         ~Iterator() override = default;
      47              : 
      48           15 :         reference operator*() const override
      49              :         {
      50           15 :             if (this->index_ >= this->queue_->size_) {
      51            0 :                 THROW<InternalException>(StringFormat("VectorQueue<T>::Iterator dereference out of range, index[%u], size[%zu]",
      52            0 :                                                        this->index_, this->queue_->size_));
      53              :             }
      54           15 :             return (this->queue_->elems_[this->index_]);
      55              :         }
      56              : 
      57            0 :         pointer operator->() const override
      58              :         {
      59            0 :             if (this->index_ >= this->queue_->size_) {
      60            0 :                 THROW<InternalException>(StringFormat("VectorQueue<T>::Iterator dereference out of range, index[%u], size[%zu]",
      61            0 :                                                        this->index_, 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           28 :     VectorQueue() : elems_(VECTOR_QUEUE_SIZE)
     108              :     {
     109           14 :     }
     110              : 
     111           16 :     ~VectorQueue() override
     112              :     {
     113           42 :         HCCL_INFO("[VectorQueue]Destroy");
     114           30 :     }
     115              : 
     116         2074 :     void Append(T &&value) override
     117              :     {
     118         2074 :         if (UNLIKELY(this->size_ >= VECTOR_QUEUE_SIZE)) {
     119            0 :             THROW<InternalException>(StringFormat("VectorQueue<T>::Append size[%zu] is full", this->size_));
     120              :         }
     121         2074 :         elems_[this->size_] = std::move(value);
     122         2074 :         this->size_++;
     123         2074 :     }
     124              : 
     125            0 :     T& GetAndUpdate() override
     126              :     {
     127            0 :         if (UNLIKELY(this->size_ >= VECTOR_QUEUE_SIZE)) {
     128            0 :             THROW<InternalException>(StringFormat("VectorQueue<T>::GetAndUpdate size[%zu] is full", this->size_));
     129              :         }
     130            0 :         return elems_[this->size_++];
     131              :     }
     132              : 
     133            1 :     void Traverse(std::function<void(const T &)> action) override
     134              :     {
     135           11 :         for (size_t i = 0; i < this->size_; ++i) {
     136           10 :             action(elems_[i]);
     137              :         }
     138            1 :     }
     139              : 
     140            0 :     bool IsFull() const override
     141              :     {
     142            0 :         return this->size_ >= VECTOR_QUEUE_SIZE;
     143              :     }
     144              : 
     145            0 :     size_t Capacity() const override
     146              :     {
     147            0 :         return VECTOR_QUEUE_SIZE;
     148              :     }
     149              : 
     150            4 :     std::shared_ptr<typename Queue<T>::Iterator> Find(std::function<bool(const T &)> cond) override
     151              :     {
     152            6 :         for (size_t i = 0; i < this->size_; ++i) {
     153            4 :             if (cond(elems_[i])) {
     154            2 :                 return std::make_shared<Iterator>(this, static_cast<u32>(i));
     155              :             }
     156              :         }
     157            2 :         return std::make_shared<Iterator>(this, static_cast<u32>(this->size_));
     158              :     }
     159              : 
     160            4 :     std::shared_ptr<typename Queue<T>::Iterator> Begin() const override
     161              :     {
     162            4 :         return std::make_shared<Iterator>(this, 0);
     163              :     }
     164              : 
     165            2 :     std::shared_ptr<typename Queue<T>::Iterator> Tail() const override
     166              :     {
     167            2 :         if (this->IsEmpty()) {
     168            3 :             HCCL_WARNING("[VectorQueue][Tail] Queue is empty!");
     169            1 :             return std::make_shared<Iterator>(this, 0);
     170              :         }
     171            1 :         return std::make_shared<Iterator>(this, static_cast<u32>(this->size_ - 1));
     172              :     }
     173              : 
     174            6 :     std::shared_ptr<typename Queue<T>::Iterator> End() const override
     175              :     {
     176            6 :         return std::make_shared<Iterator>(this, static_cast<u32>(this->size_));
     177              :     }
     178              : };
     179              : 
     180              : } // namespace Hccl
     181              : #endif // VECTOR_QUEUE_H
        

Generated by: LCOV version 2.0-1