mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-29 07:43:35 +08:00
Map<>::insert() --> Value*, saves one lookup for TransferListener::handleFrame()
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdlib>
|
||||
#include <stdint.h>
|
||||
#include <uavcan/internal/linked_list.hpp>
|
||||
#include <uavcan/internal/impl_constants.hpp>
|
||||
@@ -173,7 +174,7 @@ public:
|
||||
}
|
||||
|
||||
/// If entry with the same key already exists, it will be replaced
|
||||
bool insert(const Key& key, const Value& value)
|
||||
Value* insert(const Key& key, const Value& value)
|
||||
{
|
||||
assert(!(key == Key()));
|
||||
remove(key);
|
||||
@@ -182,18 +183,18 @@ public:
|
||||
if (kv)
|
||||
{
|
||||
*kv = KVPair(key, value);
|
||||
return true;
|
||||
return &kv->value;
|
||||
}
|
||||
|
||||
void* const praw = allocator_->allocate(sizeof(KVGroup));
|
||||
if (praw == NULL)
|
||||
return false;
|
||||
return NULL;
|
||||
|
||||
KVGroup* const kvg = new (praw) KVGroup();
|
||||
assert(kvg);
|
||||
kvg->kvs[0] = KVPair(key, value);
|
||||
list_.insert(kvg);
|
||||
return true;
|
||||
return &kvg->kvs[0].value;
|
||||
}
|
||||
|
||||
void remove(const Key& key)
|
||||
|
||||
+10
-6
@@ -43,17 +43,17 @@ TEST(Map, Basic)
|
||||
ASSERT_EQ(0, pool.getNumUsedBlocks());
|
||||
|
||||
// Static insertion
|
||||
ASSERT_TRUE(map->insert("1", "a"));
|
||||
ASSERT_TRUE(map->insert("2", "b"));
|
||||
ASSERT_EQ("a", *map->insert("1", "a"));
|
||||
ASSERT_EQ("b", *map->insert("2", "b"));
|
||||
ASSERT_EQ(0, pool.getNumUsedBlocks());
|
||||
ASSERT_EQ(2, map->getNumStaticPairs());
|
||||
ASSERT_EQ(0, map->getNumDynamicPairs());
|
||||
|
||||
// Dynamic insertion
|
||||
ASSERT_TRUE(map->insert("3", "c"));
|
||||
ASSERT_EQ("c", *map->insert("3", "c"));
|
||||
ASSERT_EQ(1, pool.getNumUsedBlocks());
|
||||
|
||||
ASSERT_TRUE(map->insert("4", "d"));
|
||||
ASSERT_EQ("d", *map->insert("4", "d"));
|
||||
ASSERT_EQ(1, pool.getNumUsedBlocks()); // Assuming that at least 2 items fit one block
|
||||
ASSERT_EQ(2, map->getNumStaticPairs());
|
||||
ASSERT_EQ(2, map->getNumDynamicPairs());
|
||||
@@ -104,12 +104,16 @@ TEST(Map, Basic)
|
||||
{
|
||||
const std::string key = toString(i);
|
||||
const std::string value = toString(i);
|
||||
const bool res = map->insert(key, value); // Will override some from the above
|
||||
if (!res)
|
||||
std::string* res = map->insert(key, value); // Will override some from the above
|
||||
if (res == NULL)
|
||||
{
|
||||
ASSERT_LT(2, i);
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT_EQ(value, *res);
|
||||
}
|
||||
max_key_integer = i;
|
||||
}
|
||||
std::cout << "Max key/value: " << max_key_integer << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user