1

add arraylist + demo

This commit is contained in:
2022-07-16 00:58:25 +02:00
parent ac4b1a05a8
commit c5d25736a3
4 changed files with 322 additions and 0 deletions

View File

@ -0,0 +1,87 @@
#include "user/demo/ArrayListDemo.h"
void ArrayListDemo::run() {
kout << "Initial list size: " << dec << this->list.size() << endl;
kout << "Adding elements in order" << endl;
for (unsigned int i = 0; i < 5; ++i) {
this->list.insert(i);
}
this->list.print();
kout << "Removing all elements from the front" << endl;
for (unsigned int i = 0; i < 5; ++i) {
this->list.remove_first();
}
this->list.print();
// ============================================================
kout << "Adding elements in order with realloc" << endl;
for (unsigned int i = 0; i < 10; ++i) {
kout << "Add " << dec << i << endl;
this->list.insert(i);
}
this->list.print();
kout << "Removing all elements from the back" << endl;
for (unsigned int i = 0; i < 10; ++i) {
this->list.remove_last();
}
this->list.print();
// ============================================================
for (unsigned int i = 0; i < 5; ++i) {
this->list.insert(i);
}
this->list.print();
kout << "Adding inside the list (at idx 0, 2, 5)" << endl;
this->list.insert_at(10, 0);
this->list.insert_at(10, 2);
this->list.insert_at(10, 5);
this->list.print();
kout << "Removing inside the list (at idx 0, 2, 5)" << endl;
this->list.remove_at(0);
this->list.remove_at(2);
this->list.remove_at(5);
this->list.print();
for (unsigned int i = 0; i < 5; ++i) {
this->list.remove_first();
}
this->list.print();
// ============================================================
kout << "Mirror scheduling behavior" << endl;
// These are the threads
int active = 0; // Idle thread
this->list.insert(1);
this->list.insert(2);
this->list.insert(3);
this->list.print();
kout << "Starting..." << endl;
for (unsigned int n = 0; n < 10000000; ++n) {
this->list.insert(active);
active = list.remove_first();
if (this->list.size() != 3) {
kout << "ERROR: Thread went missing" << endl;
break;
}
if (n < 5) {
this->list.print();
}
}
kout << "Finished." << endl;
this->list.print();
scheduler.exit();
}

View File

@ -0,0 +1,23 @@
#ifndef __ArrayListDemo_include__
#define __ArrayListDemo_include__
#include "kernel/Globals.h"
#include "kernel/threads/Thread.h"
#include "user/lib/ArrayList.h"
class ArrayListDemo : public Thread {
private:
ArrayListDemo(const ArrayListDemo& copy) = delete;
ArrayList<int> list;
public:
ArrayListDemo() {
this->list.init();
kout << "Initialized ArrayListDemo" << endl;
}
void run() override;
};
#endif