Browse Source

Add RtList appendAt and insertAt methods

tags/1.9.4
falkTX 11 years ago
parent
commit
d1dac603c8
3 changed files with 46 additions and 4 deletions
  1. +1
    -1
      source/libs/rtmempool/list.h
  2. +17
    -3
      source/tests/RtList.cpp
  3. +28
    -0
      source/utils/RtList.hpp

+ 1
- 1
source/libs/rtmempool/list.h View File

@@ -279,7 +279,7 @@ static inline void list_splice_init(struct list_head *list, struct list_head *he
}

/**
* list_splice_init - join two lists and reinitialise the emptied list.
* list_splice_tail_init - join two lists and reinitialise the emptied list.
* @list: the new list to add.
* @head: the place to add it in the first list.
*


+ 17
- 3
source/tests/RtList.cpp View File

@@ -83,7 +83,7 @@ struct PostRtEvents {

} postRtEvents;

void run4Tests()
void run5Tests()
{
unsigned short k = 0;
MyData allMyData[MAX_RT_EVENTS];
@@ -101,7 +101,7 @@ void run4Tests()
postRtEvents.mutex.unlock();

printf("Post-Rt Event Count: %i\n", k);
assert(k == 4);
assert(k == 5);

// data should be empty now
assert(postRtEvents.data.count() == 0);
@@ -124,6 +124,7 @@ int main()
MyData m2(2);
MyData m3(3);
MyData m4(4);
MyData m5(5);

// start
assert(postRtEvents.data.count() == 0);
@@ -141,6 +142,8 @@ int main()
postRtEvents.appendRT(m2);
postRtEvents.appendRT(m4);
postRtEvents.appendRT(m3);
assert(postRtEvents.data.count() == 1);
assert(postRtEvents.dataPendingRT.count() == 3);
postRtEvents.trySplice();
assert(postRtEvents.data.count() == 4);
assert(postRtEvents.dataPendingRT.count() == 0);
@@ -150,9 +153,20 @@ int main()
MyData& my = *it;

printf("FOR DATA!!!: %i %s\n", my.idStr, (const char*)my.str);

if (my.idStr == 1)
{
// +1 append at
postRtEvents.dataPendingRT.insertAt(m5, it);
assert(postRtEvents.data.count() == 4);
assert(postRtEvents.dataPendingRT.count() == 1);
postRtEvents.trySplice();
assert(postRtEvents.data.count() == 5);
assert(postRtEvents.dataPendingRT.count() == 0);
}
}

run4Tests();
run5Tests();

// reset
postRtEvents.clear();


+ 28
- 0
source/utils/RtList.hpp View File

@@ -80,6 +80,8 @@ protected:
k_list_head* const kQueue;
k_list_head* fEntry;
Data* fData;

friend class List;
};

List()
@@ -139,6 +141,19 @@ public:
return false;
}

bool appendAt(const T& value, const Itenerator& it)
{
if (Data* const data = _allocate())
{
std::memcpy(&data->value, &value, sizeof(T));
list_add_tail(&data->siblings, it.fEntry->next);
fCount++;
return true;
}

return false;
}

bool insert(const T& value)
{
if (Data* const data = _allocate())
@@ -152,6 +167,19 @@ public:
return false;
}

bool insertAt(const T& value, const Itenerator& it)
{
if (Data* const data = _allocate())
{
std::memcpy(&data->value, &value, sizeof(T));
list_add(&data->siblings, it.fEntry->prev);
fCount++;
return true;
}

return false;
}

T& getAt(const size_t index, const bool remove = false)
{
if (fCount == 0 || index >= fCount)


Loading…
Cancel
Save