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

Generated by: LCOV version 2.0-1