-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
1,223 additions
and
809 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* Copyright (c) 2021 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#ifndef COMMON_UTILS_TOPKHEAP_H_ | ||
#define COMMON_UTILS_TOPKHEAP_H_ | ||
|
||
#include "common/base/Base.h" | ||
|
||
namespace nebula { | ||
|
||
template <class T> | ||
class TopKHeap final { | ||
public: | ||
TopKHeap(int heapSize, std::function<bool(T, T)> comparator) | ||
: heapSize_(heapSize), comparator_(std::move(comparator)) { | ||
v_.reserve(heapSize); | ||
} | ||
~TopKHeap() = default; | ||
|
||
void push(T data) { | ||
if (v_.size() < static_cast<size_t>(heapSize_)) { | ||
v_.push_back(data); | ||
adjustUp(v_.size() - 1); | ||
return; | ||
} | ||
if (comparator_(data, v_[0])) { | ||
v_[0] = data; | ||
adjustDown(0); | ||
} | ||
} | ||
|
||
std::vector<T> moveTopK() { return std::move(v_); } | ||
|
||
private: | ||
void adjustDown(size_t parent) { | ||
size_t child = parent * 2 + 1; | ||
size_t size = v_.size(); | ||
while (child < size) { | ||
if (child + 1 < size && comparator_(v_[child], v_[child + 1])) { | ||
child += 1; | ||
} | ||
if (!comparator_(v_[parent], v_[child])) { | ||
return; | ||
} | ||
std::swap(v_[parent], v_[child]); | ||
parent = child; | ||
child = parent * 2 + 1; | ||
} | ||
} | ||
void adjustUp(size_t child) { | ||
size_t parent = (child - 1) >> 1; | ||
while (0 != child) { | ||
if (!comparator_(v_[parent], v_[child])) { | ||
return; | ||
} | ||
std::swap(v_[parent], v_[child]); | ||
child = parent; | ||
parent = (child - 1) >> 1; | ||
} | ||
} | ||
|
||
private: | ||
int heapSize_; | ||
std::vector<T> v_; | ||
std::function<bool(T, T)> comparator_; | ||
}; | ||
} // namespace nebula | ||
#endif // COMMON_UTILS_TOPKHEAP_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* Copyright (c) 2020 vesoft inc. All rights reserved. | ||
* | ||
* This source code is licensed under Apache 2.0 License, | ||
* attached with Common Clause Condition 1.0, found in the LICENSES directory. | ||
*/ | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include "common/utils/TopKHeap.h" | ||
|
||
namespace nebula { | ||
|
||
std::vector<int> getVector(std::function<bool(int, int)> comparator) { | ||
TopKHeap<int> topKHeap(3, comparator); | ||
topKHeap.push(3); | ||
topKHeap.push(6); | ||
topKHeap.push(1); | ||
topKHeap.push(9); | ||
topKHeap.push(2); | ||
topKHeap.push(7); | ||
return topKHeap.moveTopK(); | ||
} | ||
|
||
TEST(TopKHeapTest, minHeapTest) { | ||
auto topK = getVector([](const int& lhs, const int& rhs) { return lhs < rhs; }); | ||
std::stringstream ss; | ||
for (auto& item : topK) { | ||
ss << item << " "; | ||
} | ||
ASSERT_EQ(ss.str(), "3 2 1 "); | ||
} | ||
|
||
TEST(TopKHeapTest, maxHeapTest) { | ||
auto topK = getVector([](const int& lhs, const int& rhs) { return lhs > rhs; }); | ||
std::stringstream ss; | ||
for (auto& item : topK) { | ||
ss << item << " "; | ||
} | ||
ASSERT_EQ(ss.str(), "6 7 9 "); | ||
} | ||
|
||
} // namespace nebula | ||
|
||
int main(int argc, char** argv) { | ||
testing::InitGoogleTest(&argc, argv); | ||
folly::init(&argc, &argv, true); | ||
google::SetStderrLogging(google::INFO); | ||
|
||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 0 additions & 80 deletions
80
src/graph/optimizer/rule/PushLimitDownEdgeIndexFullScanRule.cpp
This file was deleted.
Oops, something went wrong.
32 changes: 0 additions & 32 deletions
32
src/graph/optimizer/rule/PushLimitDownEdgeIndexFullScanRule.h
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.