1

add vector demo

This commit is contained in:
2022-07-21 02:44:06 +02:00
parent ae7294601d
commit 498990559c
2 changed files with 131 additions and 0 deletions

View File

@ -0,0 +1,111 @@
#include "user/demo/VectorDemo.h"
void print(bse::Vector<int> list) {
kout << "Printing List: ";
for (int i : list) {
kout << i << " ";
}
kout << endl;
}
void VectorDemo::run() {
bse::Vector<int> list;
kout.lock();
kout.clear();
kout << "Initial list size: " << dec << list.size() << endl;
kout << "Adding elements in order" << endl;
for (unsigned int i = 0; i < 5; ++i) {
list.push_back(i);
}
print(list);
kout << "Removing all elements from the front" << endl;
for (unsigned int i = 0; i < 5; ++i) {
list.erase(list.begin());
}
print(list);
// ============================================================
kout << "Adding elements in order with realloc" << endl;
for (unsigned int i = 0; i < 10; ++i) {
kout << "Add " << dec << i << endl;
list.push_back(i);
}
print(list);
kout << "Removing all elements from the back" << endl;
for (unsigned int i = 0; i < 10; ++i) {
list.erase(--list.end());
}
print(list);
// ============================================================
for (unsigned int i = 0; i < 5; ++i) {
list.push_back(i);
}
print(list);
kout << "Adding inside the list (at idx 0, 2, 5)" << endl;
list.insert(list.begin() + 0, 10);
list.insert(list.begin() + 2, 10);
list.insert(list.begin() + 5, 10);
print(list);
kout << "Removing inside the list (at idx 0, 2, 5)" << endl;
list.erase(list.begin() + 0);
list.erase(list.begin() + 2);
list.erase(list.begin() + 5);
print(list);
for (unsigned int i = 0; i < 5; ++i) {
list.erase(list.begin());
}
print(list);
// ============================================================
kout << "Mirror scheduling behavior" << endl;
// These are the threads
int active = 0; // Idle thread
list.push_back(1);
list.push_back(2);
list.push_back(3);
print(list);
kout << "Starting..." << endl;
for (unsigned int n = 0; n < 10000; ++n) {
list.push_back(active);
active = list[0];
list.erase(list.begin());
if (list.size() != 3 || active == -1) {
kout << "ERROR: Thread went missing" << endl;
break;
}
if (n < 5) {
print(list);
}
}
kout << "Finished." << endl;
print(list);
// ============================================================
kout << "Range based for support" << endl;
for (int i : list) {
kout << "List contains element: " << dec << i << endl;
}
for (bse::Vector<int>::Iterator it = list.begin(); it != list.end(); ++it) {
}
kout.unlock();
scheduler.exit();
}

View File

@ -0,0 +1,20 @@
#ifndef __VectorDemo_include__
#define __VectorDemo_include__
#include "kernel/Globals.h"
#include "kernel/threads/Thread.h"
#include "user/lib/Vector.h"
class VectorDemo : public Thread {
private:
VectorDemo(const VectorDemo& copy) = delete;
public:
VectorDemo() {
kout << "Initialized VectorDemo" << endl;
}
void run() override;
};
#endif