Skip to content
Snippets Groups Projects
Commit 22a45a4b authored by 数学の武士's avatar 数学の武士
Browse files

Lmbda func impl for compare functions

parent d01961de
No related branches found
No related tags found
No related merge requests found
......@@ -12,18 +12,15 @@ enum class buf_choose_policy
namespace mf_utils
{
bool free_size_comparator(const buffer& obj, const size_t required_size);
bool size_comparator(const size_t required_size, const buffer& obj);
typename std::vector<buffer>::iterator get_lower_bound(
typename std::vector<buffer>::iterator first,
typename std::vector<buffer>::iterator last, const size_t value,
std::function<bool (const buffer&, const size_t) >& comp);
bool (*comp)(const buffer&, const size_t));
typename std::vector<buffer>::iterator get_upper_bound(
typename std::vector<buffer>::iterator first,
typename std::vector<buffer>::iterator last, const size_t value,
std::function<bool (const size_t, const buffer&) >& comp);
bool (*comp)(const size_t, const buffer&));
template<buf_choose_policy choose_type = buf_choose_policy::naive>
int get_buffer(const size_t required_size,
......
#include "mf-utils.h"
bool mf_utils::free_size_comparator(const buffer& obj, const size_t required_size)
{
if(obj.get_status() == false)
return false;
return obj.get_size() <= required_size;
}
bool mf_utils::size_comparator(const size_t required_size, const buffer& obj)
{
return required_size < obj.get_size();
}
typename std::vector<buffer>::iterator mf_utils::get_lower_bound(
typename std::vector<buffer>::iterator first,
typename std::vector<buffer>::iterator last, const size_t value,
std::function<bool (const buffer&, const size_t) >& comp)
bool (*comp)(const buffer&, const size_t))
{
typename std::vector<buffer>::iterator it;
typename std::iterator_traits<typename std::vector<buffer>::iterator>::difference_type count, step;
......@@ -43,7 +30,7 @@ typename std::vector<buffer>::iterator mf_utils::get_lower_bound(
typename std::vector<buffer>::iterator mf_utils::get_upper_bound(
typename std::vector<buffer>::iterator first,
typename std::vector<buffer>::iterator last, const size_t value,
std::function<bool (const size_t, const buffer&) >& comp)
bool (*comp)(const size_t, const buffer&))
{
typename std::vector<buffer>::iterator it;
typename std::iterator_traits<typename std::vector<buffer>::iterator>::difference_type count, step;
......@@ -119,7 +106,10 @@ int mf_utils::get_buffer<buf_choose_policy::sorted_vec>(const size_t required_si
}
typename std::vector<buffer>::iterator buf_it;
std::function<bool (const size_t, const buffer&) > comparator = mf_utils::size_comparator;
auto comparator = [](const size_t required_size, const buffer& obj) ->bool
{
return required_size < obj.get_size();
};
buf_it = mf_utils::get_upper_bound (buff_vec.begin(), buff_vec.end(), required_size, comparator);
buf_it = buff_vec.insert(buf_it, buffer(required_size, mem));
(*ptr) = buf_it->get_ptr();
......@@ -135,7 +125,14 @@ int mf_utils::get_buffer<buf_choose_policy::find_best_unsorted>(const size_t req
void ** ptr)
{
typename std::vector<buffer>::iterator available_buf_it;
std::function<bool (const buffer&, const size_t) > comparator = mf_utils::free_size_comparator;
auto comparator = [](const buffer& obj, const size_t required_size) ->bool
{
if(obj.get_status() == false)
return false;
return obj.get_size() <= required_size;
};
available_buf_it = mf_utils::get_lower_bound (buff_vec.begin(), buff_vec.end(), required_size, comparator);
if(available_buf_it != buff_vec.end())
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment