ONE - On-device Neural Engine
Loading...
Searching...
No Matches
nnfw::cker::TopContainer< T, Tidx > Class Template Reference

#include <TopKV2.h>

Public Member Functions

 TopContainer ()=delete
 
 TopContainer (uint32_t k, uint32_t row_size)
 
void start_collecting (const T *values)
 
void push (Tidx a)
 
const std::vector< Tidx > & sorted_result ()
 

Detailed Description

template<typename T, typename Tidx>
class nnfw::cker::TopContainer< T, Tidx >

Definition at line 26 of file TopKV2.h.

Constructor & Destructor Documentation

◆ TopContainer() [1/2]

template<typename T , typename Tidx >
nnfw::cker::TopContainer< T, Tidx >::TopContainer ( )
delete

◆ TopContainer() [2/2]

template<typename T , typename Tidx >
nnfw::cker::TopContainer< T, Tidx >::TopContainer ( uint32_t  k,
uint32_t  row_size 
)
inline

Definition at line 30 of file TopKV2.h.

30 : k_(k)
31 {
32 container_.reserve(std::min(k, row_size) + 1);
33 }

Member Function Documentation

◆ push()

template<typename T , typename Tidx >
void nnfw::cker::TopContainer< T, Tidx >::push ( Tidx  a)
inline

Definition at line 42 of file TopKV2.h.

43 {
44 auto comparator = [this](Tidx a, Tidx b) { return compare_fun(a, b); };
45 if (!is_heap_)
46 {
47 container_.push_back(a);
48 if (container_.size() == k_ + 1)
49 {
50 std::make_heap(container_.begin(), container_.end(), comparator);
51 std::pop_heap(container_.begin(), container_.end(), comparator);
52 container_.pop_back();
53 is_heap_ = true;
54 }
55 }
56 else if (comparator(a, container_.front()))
57 {
58 // Due to how we defined comparator / compare_fun, container_.front()
59 // contains the index of the smallest of the top-k elements seen so far.
60 //
61 // If control reaches this point, we know that the current index a
62 // corresponds to an element which is bigger than the smallest of the
63 // top-k elements seen so far. Hence, we have to update the indices of
64 // the top-k elements, by removing the index of the smallest top-k
65 // element, adding a, and making sure container_[0:k] is still a heap.
66 std::pop_heap(container_.begin(), container_.end(), comparator);
67 container_.back() = a;
68 std::push_heap(container_.begin(), container_.end(), comparator);
69 }
70 }

Referenced by nnfw::cker::TopKV2().

◆ sorted_result()

template<typename T , typename Tidx >
const std::vector< Tidx > & nnfw::cker::TopContainer< T, Tidx >::sorted_result ( )
inline

Definition at line 72 of file TopKV2.h.

73 {
74 auto comparator = [this](Tidx a, Tidx b) { return compare_fun(a, b); };
75 if (!is_heap_)
76 {
77 // Note: due to the way we defined compare_fun (see comments for that
78 // function) std::sort puts the indices from container_ in decreasing
79 // order of the corresponding elements.
80 std::sort(container_.begin(), container_.end(), comparator);
81 }
82 else
83 {
84 std::sort_heap(container_.begin(), container_.end(), comparator);
85 }
86 return container_;
87 }

Referenced by nnfw::cker::TopKV2().

◆ start_collecting()

template<typename T , typename Tidx >
void nnfw::cker::TopContainer< T, Tidx >::start_collecting ( const T *  values)
inline

Definition at line 35 of file TopKV2.h.

36 {
37 values_ = values;
38 container_.clear();
39 is_heap_ = false;
40 }

Referenced by nnfw::cker::TopKV2().


The documentation for this class was generated from the following file: