Skip to content

Commit

Permalink
roc-streaminggh-751 ran formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
novertia committed Jul 17, 2024
1 parent 2bc56a1 commit 96e6f4a
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 234 deletions.
364 changes: 178 additions & 186 deletions src/internal_modules/roc_core/mov_quantile.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,205 +20,197 @@ namespace roc {
namespace core {

template <typename T> class MovQuantile {
public:
const size_t win_len_;
const size_t percentile_;
size_t old_heap_root_index_;
const size_t heap_root_;
size_t heap_size_;
size_t max_heap_index_;
size_t min_heap_index_;
size_t elem_index_;
bool win_filled_;
bool valid_;
Array<T> heap_;
Array<size_t> elem_index_heap_index_;
Array<size_t> heap_index_elem_index_;

MovQuantile(IArena& arena, const size_t win_len, const size_t percentile)
: win_len_(win_len)
, percentile_(percentile)
, old_heap_root_index_(0)
, heap_root_((percentile * (win_len - 1))/100)
, heap_size_(0)
, max_heap_index_(heap_root_)
, min_heap_index_(heap_root_)
, elem_index_(0)
, win_filled_(false)
, valid_(false)
, heap_(arena)
, elem_index_heap_index_(arena)
, heap_index_elem_index_(arena) {
if (win_len == 0) {
roc_panic("mov quantile: window length must be greater than 0");
}
if (percentile > 100){
roc_panic("mov quantile: percentile should be between 0 and 100");
}
if (!heap_.resize(win_len)) {
return;
}
if(!elem_index_heap_index_.resize(win_len)){
return;
}
if (!heap_index_elem_index_.resize(win_len)){
return;
}
valid_ = true;
public:
const size_t win_len_;
const size_t percentile_;
size_t old_heap_root_index_;
const size_t heap_root_;
size_t heap_size_;
size_t max_heap_index_;
size_t min_heap_index_;
size_t elem_index_;
bool win_filled_;
bool valid_;
Array<T> heap_;
Array<size_t> elem_index_heap_index_;
Array<size_t> heap_index_elem_index_;

MovQuantile(IArena& arena, const size_t win_len, const size_t percentile)
: win_len_(win_len)
, percentile_(percentile)
, old_heap_root_index_(0)
, heap_root_((percentile * (win_len - 1)) / 100)
, heap_size_(0)
, max_heap_index_(heap_root_)
, min_heap_index_(heap_root_)
, elem_index_(0)
, win_filled_(false)
, valid_(false)
, heap_(arena)
, elem_index_heap_index_(arena)
, heap_index_elem_index_(arena) {
if (win_len == 0) {
roc_panic("mov quantile: window length must be greater than 0");
}
bool is_valid(){
return valid_;
if (percentile > 100) {
roc_panic("mov quantile: percentile should be between 0 and 100");
}

void swap(size_t index_1, size_t index_2){
size_t elem_index_1 = heap_index_elem_index_[index_1];
size_t elem_index_2 = heap_index_elem_index_[index_2];

T temp = heap_[index_1];
heap_[index_1] = heap_[index_2];
heap_[index_2] = temp;

heap_index_elem_index_[index_1] = elem_index_2;
heap_index_elem_index_[index_2] = elem_index_1;

elem_index_heap_index_[elem_index_1] = index_2;
elem_index_heap_index_[elem_index_2] = index_1;
}

void min_heapify_up(size_t heap_index){
if (heap_index == heap_root_){
return;
}
size_t parent = (heap_index - heap_root_ - 1)/2 + heap_root_;
if(heap_[parent] > heap_[heap_index]){
swap(heap_index, parent);
min_heapify_up(parent);
}
}
if (!heap_.resize(win_len)) {
return;
}
if (!elem_index_heap_index_.resize(win_len)) {
return;
}
if (!heap_index_elem_index_.resize(win_len)) {
return;
}
valid_ = true;
}
bool is_valid() {
return valid_;
}

void swap(size_t index_1, size_t index_2) {
size_t elem_index_1 = heap_index_elem_index_[index_1];
size_t elem_index_2 = heap_index_elem_index_[index_2];

T temp = heap_[index_1];
heap_[index_1] = heap_[index_2];
heap_[index_2] = temp;

heap_index_elem_index_[index_1] = elem_index_2;
heap_index_elem_index_[index_2] = elem_index_1;

elem_index_heap_index_[elem_index_1] = index_2;
elem_index_heap_index_[elem_index_2] = index_1;
}

void min_heapify_up(size_t heap_index) {
if (heap_index == heap_root_) {
return;
}
size_t parent = (heap_index - heap_root_ - 1) / 2 + heap_root_;
if (heap_[parent] > heap_[heap_index]) {
swap(heap_index, parent);
min_heapify_up(parent);
}
}

void max_heapify_up(size_t heap_index){
//sift up
if (heap_index == heap_root_){
return;
}
size_t parent = heap_root_ - ((heap_root_ - heap_index - 1)/2);
if(heap_[parent] < heap_[heap_index]){
swap(heap_index, parent);
max_heapify_up(parent);
}
void max_heapify_up(size_t heap_index) {
// sift up
if (heap_index == heap_root_) {
return;
}
size_t parent = heap_root_ - ((heap_root_ - heap_index - 1) / 2);
if (heap_[parent] < heap_[heap_index]) {
swap(heap_index, parent);
max_heapify_up(parent);
}
}

void min_heapify_down(size_t heap_index){
size_t largest = heap_index;

size_t left = 2*(heap_index - heap_root_) + 1 + heap_root_;
if(left <= min_heap_index_ && heap_[left] < heap_[largest])
largest = left;
size_t right = 2*(heap_index - heap_root_) + 2 + heap_root_;
if(right <= min_heap_index_ && heap_[right] < heap_[largest])
largest = right;

if(largest != heap_index){
swap(heap_index,largest);
min_heapify_down(largest);
}
void min_heapify_down(size_t heap_index) {
size_t largest = heap_index;

size_t left = 2 * (heap_index - heap_root_) + 1 + heap_root_;
if (left <= min_heap_index_ && heap_[left] < heap_[largest])
largest = left;
size_t right = 2 * (heap_index - heap_root_) + 2 + heap_root_;
if (right <= min_heap_index_ && heap_[right] < heap_[largest])
largest = right;

if (largest != heap_index) {
swap(heap_index, largest);
min_heapify_down(largest);
}
}

void max_heapify_down(size_t heap_index) {
size_t largest = heap_index;

size_t left = 2 * (heap_root_ - heap_index) + 1;
if (left <= (heap_root_ - max_heap_index_)
&& heap_[heap_root_ - left] > heap_[largest])
largest = heap_root_ - left;
size_t right = 2 * (heap_root_ - heap_index) + 2;
if (right <= (heap_root_ - max_heap_index_)
&& heap_[heap_root_ - right] > heap_[largest])
largest = heap_root_ - right;

if (largest != heap_index) {
swap(heap_index, largest);
max_heapify_down(largest);
}
}

void max_heapify_down(size_t heap_index){
size_t largest = heap_index;

size_t left = 2*(heap_root_ - heap_index) + 1;
if(left <= (heap_root_ - max_heap_index_) && heap_[heap_root_ - left] > heap_[largest])
largest = heap_root_ - left;
size_t right = 2*(heap_root_ - heap_index) + 2;
if(right <= (heap_root_ - max_heap_index_) && heap_[heap_root_ - right] > heap_[largest])
largest = heap_root_ - right;

if(largest != heap_index){
swap(heap_index,largest);
max_heapify_down(largest);
}
}

void heapify(size_t heap_index){
if(heap_index < heap_root_){
size_t parent = heap_root_ - ((heap_root_ - heap_index - 1)/2);
if(heap_[parent] < heap_[heap_index]){
max_heapify_up(heap_index);
min_heapify_down(heap_root_);
}
else{
max_heapify_down(heap_index);
}
}
else if(heap_root_ == heap_index){
max_heapify_down(heap_index);
void heapify(size_t heap_index) {
if (heap_index < heap_root_) {
size_t parent = heap_root_ - ((heap_root_ - heap_index - 1) / 2);
if (heap_[parent] < heap_[heap_index]) {
max_heapify_up(heap_index);
min_heapify_down(heap_root_);
} else {
max_heapify_down(heap_index);
}
else{
size_t parent = (heap_index - heap_root_ - 1)/2 + heap_root_;
if(heap_[parent] > heap_[heap_index]){
min_heapify_up(heap_index);
max_heapify_down(heap_root_);
}
else{
min_heapify_down(heap_index);
}
} else if (heap_root_ == heap_index) {
max_heapify_down(heap_index);
min_heapify_down(heap_root_);
} else {
size_t parent = (heap_index - heap_root_ - 1) / 2 + heap_root_;
if (heap_[parent] > heap_[heap_index]) {
min_heapify_up(heap_index);
max_heapify_down(heap_root_);
} else {
min_heapify_down(heap_index);
}
}

void add(const T& x){
if (elem_index_ == win_len_)
win_filled_ = true;
heap_size_ = elem_index_ + 1;
elem_index_ = (elem_index_) % win_len_;
if(win_filled_){
heap_size_ = win_len_;
min_heap_index_ = win_len_ - 1;
max_heap_index_ = 0;
size_t heap_index = elem_index_heap_index_[elem_index_];
}
}

void add(const T& x) {
if (elem_index_ == win_len_)
win_filled_ = true;
heap_size_ = elem_index_ + 1;
elem_index_ = (elem_index_) % win_len_;
if (win_filled_) {
heap_size_ = win_len_;
min_heap_index_ = win_len_ - 1;
max_heap_index_ = 0;
size_t heap_index = elem_index_heap_index_[elem_index_];
heap_[heap_index] = x;
heapify(heap_index);
} else {
size_t k = (percentile_ * (heap_size_ - 1)) / 100;
size_t heap_index;
if (elem_index_ == 0) {
heap_index = heap_root_;
elem_index_heap_index_[elem_index_] = heap_index;
heap_[heap_index] = x;
heapify(heap_index);
}
else{
size_t k = (percentile_ * (heap_size_ - 1))/100;
size_t heap_index;
if (elem_index_ == 0){
heap_index = heap_root_;
elem_index_heap_index_[elem_index_] = heap_index;
heap_[heap_index] = x;
heap_index_elem_index_[heap_index] = elem_index_;
}
else {
if(old_heap_root_index_ == k){
min_heap_index_ += 1;
heap_index = min_heap_index_;

}
else{
max_heap_index_ -= 1;
heap_index = max_heap_index_;
}
elem_index_heap_index_[elem_index_] = heap_index;
heap_[heap_index] = x;
heap_index_elem_index_[heap_index] = elem_index_;
heapify(heap_index);
old_heap_root_index_ = k;
heap_index_elem_index_[heap_index] = elem_index_;
} else {
if (old_heap_root_index_ == k) {
min_heap_index_ += 1;
heap_index = min_heap_index_;

} else {
max_heap_index_ -= 1;
heap_index = max_heap_index_;
}

}

elem_index_ = elem_index_ + 1;

elem_index_heap_index_[elem_index_] = heap_index;
heap_[heap_index] = x;
heap_index_elem_index_[heap_index] = elem_index_;
heapify(heap_index);
old_heap_root_index_ = k;
}
}

T sliding_quantile(){
return heap_[heap_root_];
}
};

elem_index_ = elem_index_ + 1;
}

T sliding_quantile() {
return heap_[heap_root_];
}
};

} //namespace core
} //namespace roc
} // namespace core
} // namespace roc

#endif
#endif // ROC_CORE_MOV_QUANTILE_H_
Loading

0 comments on commit 96e6f4a

Please sign in to comment.