mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 22:58:20 +00:00
Review fixups
This commit is contained in:
parent
499a3353b5
commit
690061b01c
3 changed files with 9 additions and 23 deletions
|
@ -23,4 +23,4 @@ class Attr;
|
||||||
typedef PList<Attr> attr_list;
|
typedef PList<Attr> attr_list;
|
||||||
|
|
||||||
class Timer;
|
class Timer;
|
||||||
typedef PList<Timer, LIST_UNORDERED> timer_list;
|
typedef PList<Timer, ListOrder::UNORDERED> timer_list;
|
||||||
|
|
|
@ -117,7 +117,7 @@ TEST_CASE("plists")
|
||||||
|
|
||||||
TEST_CASE("unordered list operation")
|
TEST_CASE("unordered list operation")
|
||||||
{
|
{
|
||||||
List<int, LIST_UNORDERED> list({1, 2, 3, 4});
|
List<int, ListOrder::UNORDERED> list({1, 2, 3, 4});
|
||||||
CHECK(list.size() == 4);
|
CHECK(list.size() == 4);
|
||||||
|
|
||||||
// An unordered list doesn't maintain the ordering of the elements when
|
// An unordered list doesn't maintain the ordering of the elements when
|
||||||
|
|
28
src/List.h
28
src/List.h
|
@ -29,9 +29,9 @@
|
||||||
// TODO: this can be removed in v3.1 when List::sort() is removed
|
// TODO: this can be removed in v3.1 when List::sort() is removed
|
||||||
typedef int (*list_cmp_func)(const void* v1, const void* v2);
|
typedef int (*list_cmp_func)(const void* v1, const void* v2);
|
||||||
|
|
||||||
enum list_order { LIST_ORDERED, LIST_UNORDERED };
|
enum class ListOrder : int { ORDERED, UNORDERED };
|
||||||
|
|
||||||
template<typename T, list_order Order = LIST_ORDERED>
|
template<typename T, ListOrder Order = ListOrder::ORDERED>
|
||||||
class List {
|
class List {
|
||||||
public:
|
public:
|
||||||
|
|
||||||
|
@ -216,7 +216,7 @@ public:
|
||||||
// For data where we don't care about ordering, we don't care about keeping
|
// For data where we don't care about ordering, we don't care about keeping
|
||||||
// the list in the same order when removing an element. Just swap the last
|
// the list in the same order when removing an element. Just swap the last
|
||||||
// element with the element being removed.
|
// element with the element being removed.
|
||||||
if ( Order == LIST_ORDERED )
|
if constexpr ( Order == ListOrder::ORDERED )
|
||||||
{
|
{
|
||||||
--num_entries;
|
--num_entries;
|
||||||
|
|
||||||
|
@ -252,16 +252,16 @@ public:
|
||||||
T replace(int ent_index, const T& new_ent) // replace entry #i with a new value
|
T replace(int ent_index, const T& new_ent) // replace entry #i with a new value
|
||||||
{
|
{
|
||||||
if ( ent_index < 0 )
|
if ( ent_index < 0 )
|
||||||
return get_default_value();
|
return T{};
|
||||||
|
|
||||||
T old_ent = get_default_value();
|
T old_ent{};
|
||||||
|
|
||||||
if ( ent_index > num_entries - 1 )
|
if ( ent_index > num_entries - 1 )
|
||||||
{ // replacement beyond the end of the list
|
{ // replacement beyond the end of the list
|
||||||
resize(ent_index + 1);
|
resize(ent_index + 1);
|
||||||
|
|
||||||
for ( int i = num_entries; i < max_entries; ++i )
|
for ( int i = num_entries; i < max_entries; ++i )
|
||||||
entries[i] = get_default_value();
|
entries[i] = T{};
|
||||||
num_entries = max_entries;
|
num_entries = max_entries;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -299,20 +299,6 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
T get_default_value()
|
|
||||||
{
|
|
||||||
if constexpr ( std::is_pointer_v<T> )
|
|
||||||
return nullptr;
|
|
||||||
else if constexpr ( std::is_integral_v<T> )
|
|
||||||
return 0;
|
|
||||||
else if constexpr ( std::is_floating_point_v<T> )
|
|
||||||
return 0.0;
|
|
||||||
else
|
|
||||||
// TODO: Should this return an error at compile time that we don't
|
|
||||||
// know what this type is?
|
|
||||||
return T();
|
|
||||||
}
|
|
||||||
|
|
||||||
// This could essentially be an std::vector if we wanted. Some
|
// This could essentially be an std::vector if we wanted. Some
|
||||||
// reasons to maybe not refactor to use std::vector ?
|
// reasons to maybe not refactor to use std::vector ?
|
||||||
//
|
//
|
||||||
|
@ -344,7 +330,7 @@ protected:
|
||||||
|
|
||||||
|
|
||||||
// Specialization of the List class to store pointers of a type.
|
// Specialization of the List class to store pointers of a type.
|
||||||
template<typename T, list_order Order = LIST_ORDERED>
|
template<typename T, ListOrder Order = ListOrder::ORDERED>
|
||||||
using PList = List<T*, Order>;
|
using PList = List<T*, Order>;
|
||||||
|
|
||||||
// Popular type of list: list of strings.
|
// Popular type of list: list of strings.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue