-
Notifications
You must be signed in to change notification settings - Fork 790
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split nano::scheduler::bucket in to its own class and file
- Loading branch information
Showing
5 changed files
with
137 additions
and
44 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <nano/lib/blocks.hpp> | ||
#include <nano/node/scheduler/bucket.hpp> | ||
|
||
bool nano::scheduler::bucket::value_type::operator< (value_type const & other_a) const | ||
{ | ||
return time < other_a.time || (time == other_a.time && block->hash () < other_a.block->hash ()); | ||
} | ||
|
||
bool nano::scheduler::bucket::value_type::operator== (value_type const & other_a) const | ||
{ | ||
return time == other_a.time && block->hash () == other_a.block->hash (); | ||
} | ||
|
||
nano::scheduler::bucket::bucket (size_t maximum) : | ||
maximum{ maximum } | ||
{ | ||
debug_assert (maximum > 0); | ||
} | ||
|
||
nano::scheduler::bucket::~bucket () | ||
{ | ||
} | ||
|
||
bool nano::scheduler::bucket::available () const | ||
{ | ||
return !queue.empty (); | ||
} | ||
|
||
std::shared_ptr<nano::block> nano::scheduler::bucket::top () const | ||
{ | ||
debug_assert (!queue.empty ()); | ||
return queue.begin ()->block; | ||
} | ||
|
||
void nano::scheduler::bucket::pop () | ||
{ | ||
debug_assert (!queue.empty ()); | ||
queue.erase (queue.begin ()); | ||
} | ||
|
||
void nano::scheduler::bucket::push (uint64_t time, std::shared_ptr<nano::block> block) | ||
{ | ||
queue.insert ({ time, block }); | ||
if (queue.size () > maximum) | ||
{ | ||
debug_assert (!queue.empty ()); | ||
queue.erase (--queue.end ()); | ||
} | ||
} | ||
|
||
size_t nano::scheduler::bucket::size () const | ||
{ | ||
return queue.size (); | ||
} | ||
|
||
bool nano::scheduler::bucket::empty () const | ||
{ | ||
return queue.empty (); | ||
} | ||
|
||
void nano::scheduler::bucket::dump () const | ||
{ | ||
for (auto const & item : queue) | ||
{ | ||
std::cerr << item.time << ' ' << item.block->hash ().to_string () << '\n'; | ||
} | ||
} |
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,40 @@ | ||
#pragma once | ||
|
||
#include <cstddef> | ||
#include <cstdint> | ||
#include <memory> | ||
#include <set> | ||
|
||
namespace nano | ||
{ | ||
class block; | ||
} | ||
namespace nano::scheduler | ||
{ | ||
/** A class which holds an ordered set of blocks to be scheduled, ordered by their block arrival time | ||
*/ | ||
class bucket final | ||
{ | ||
class value_type | ||
{ | ||
public: | ||
uint64_t time; | ||
std::shared_ptr<nano::block> block; | ||
bool operator< (value_type const & other_a) const; | ||
bool operator== (value_type const & other_a) const; | ||
}; | ||
std::set<value_type> queue; | ||
size_t const maximum; | ||
|
||
public: | ||
bucket (size_t maximum); | ||
~bucket (); | ||
bool available () const; | ||
std::shared_ptr<nano::block> top () const; | ||
void pop (); | ||
void push (uint64_t time, std::shared_ptr<nano::block> block); | ||
size_t size () const; | ||
bool empty () const; | ||
void dump () const; | ||
}; | ||
} // namespace nano::scheduler |
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