Code improvements.

This commit is contained in:
Ravin
2019-11-07 07:47:33 +05:30
committed by Ravin Perera
parent d16b43406b
commit 8d31075b7b
27 changed files with 155 additions and 171 deletions

View File

@@ -6,7 +6,7 @@ namespace util
// rollover_hashset class methods
rollover_hashset::rollover_hashset(uint32_t maxsize)
rollover_hashset::rollover_hashset(const uint32_t maxsize)
{
this->maxsize = maxsize == 0 ? 1 : maxsize;
}
@@ -15,13 +15,13 @@ rollover_hashset::rollover_hashset(uint32_t maxsize)
* Inserts the given hash to the list.
* @return True on succesful insertion. False if hash already exists.
*/
bool rollover_hashset::try_emplace(std::string hash)
bool rollover_hashset::try_emplace(const std::string hash)
{
auto itr = recent_hashes.find(hash);
const auto itr = recent_hashes.find(hash);
if (itr == recent_hashes.end()) // Not found
{
// Add the new message hash to the set.
auto [newitr, success] = recent_hashes.emplace(hash);
const auto [newitr, success] = recent_hashes.emplace(std::move(hash));
// Insert a pointer to the stored hash value to the back of the ordered list of hashes.
recent_hashes_list.push_back(&(*newitr));
@@ -48,7 +48,7 @@ bool rollover_hashset::try_emplace(std::string hash)
* @param bin_len Bytes length.
* @return Always returns 0.
*/
int bin2hex(std::string &encoded_string, const unsigned char *bin, size_t bin_len)
int bin2hex(std::string &encoded_string, const unsigned char *bin, const size_t bin_len)
{
// Allocate the target string.
encoded_string.resize(bin_len * 2);
@@ -70,7 +70,7 @@ int bin2hex(std::string &encoded_string, const unsigned char *bin, size_t bin_le
* @param decodedbuf_len Decoded buffer size.
* @param hex_str hex string to decode.
*/
int hex2bin(unsigned char *decodedbuf, size_t decodedbuf_len, std::string_view hex_str)
int hex2bin(unsigned char *decodedbuf, const size_t decodedbuf_len, std::string_view hex_str)
{
const char *hex_end;
size_t bin_len;
@@ -99,7 +99,7 @@ int64_t get_epoch_milliseconds()
/**
* Sleeps the current thread for specified no. of milliseconds.
*/
void sleep(uint64_t milliseconds)
void sleep(const uint64_t milliseconds)
{
std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
}