Any suggestions about which to use or not, other ways, etc, please shout!
Code: Select all
class ArrayTest
{
private:
core::array<stringc*> points;
public:
ArrayTest()
{
}
~ArrayTest()
{
clear();
}
void clear()
{
for(u32 i = 0; i < points.size(); i++)
{
//std::cout << "" << points[i].c_str() << std::endl;
delete points[i];
points[i] = NULL;
}
points.clear();
}
void dump()
{
if(points.size() > 0)
{
// ALT LOOP DUMP
stringc *it = points[0];
u32 i = 0;
do
{
it = points[i];
std::cout << "" << points[i]->c_str() << std::endl;
i++;
}
while(it != points.getLast());
}
}
void push_back(stringc *s)
{
points.push_back(s);
}
void insert(stringc *s, u32 pos=0)
{
if(pos < points.size())
{
// INSERT AT POS
points.insert(s, pos);
}
}
void insert_before(stringc *s, u32 pos=0)
{
if(pos < points.size())
{
// INSERT BEFORE POS
points.insert(s, pos);
}
else
{
// CATCH, APPEND
points.push_back(s);
}
}
void insert_after(stringc *s, u32 pos=0)
{
pos++;
if(pos < points.size()+1)
{
// INSERT AFTER POS
points.insert(s, pos);
}
else
{
// CATCH, APPEND
points.push_back(s);
}
}
void delete_at(u32 pos=0)
{
// DELETE AT POS
if(pos < points.size())
{
delete points[pos];
points[pos] = NULL;
points.erase(pos);
}
}
void delete_item(stringc *s)
{
// DELETE BY POINTER
for(u32 i = 0; i < points.size(); i++)
{
if(points[i] == s)
{
delete points[i];
points[i] = NULL;
points.erase(i);
break;
}
}
}
void insert_before(stringc *s, stringc *s_pos)
{
for(u32 i = 0; i < points.size(); i++)
{
if(points[i] == s_pos)
{
points.insert(s, i);
return;
}
}
// ELSE CATCH & APPEND LIKE FIRE
points.push_back(s);
}
void insert_after(stringc *s, stringc *s_pos)
{
for(u32 i = 0; i < points.size(); i++)
{
if(points[i] == s_pos)
{
i++;
if(i < points.size()+1)
{
points.insert(s, i);
return;
}
break;
}
}
// ELSE CATCH & APPEND LIKE FIRE
points.push_back(s);
}
stringc *at(u32 pos=0)
{
if(pos < points.size())
{
return points[pos];
}
return NULL;
}
stringc *front()
{
if(points.size() > 0)
{
return points[0];
}
return NULL;
}
stringc *back() // test: on empty array (without catch here)
{
if(points.size() > 0)
{
return points.getLast(); // TODO: what getLast return on fail? not NULL it seems...
}
return NULL;
}
};
void test_04()
{
// irr::core::array
ArrayTest *a = new ArrayTest();
a->push_back(new stringc("One"));
a->push_back(new stringc("Two"));
a->push_back(new stringc("Three"));
stringc *s = new stringc("Four");
a->insert(s, 0);
a->insert(new stringc("Five"), 1);
a->delete_at(3);
a->delete_item(s);
a->dump();
std::cout << "" << std::endl;
std::cout << "" << (*a->front()).c_str() << std::endl;
std::cout << "" << (*a->back()).c_str() << std::endl;
a->clear();
stringc *ss = a->back();
if(ss)
{
std::cout << "FOUND " << (*ss).c_str() << std::endl;
}
else
{
std::cout << "NOT FOUND " << std::endl;
}
std::cout << "" << std::endl;
std::cout << "INVALID NOW " << (*s).c_str() << std::endl;
std::cout << "" << std::endl;
a->push_back(new stringc("Apples"));
a->push_back(new stringc("Bananas"));
a->push_back(new stringc("Canned Pineapple"));
a->push_back(new stringc("Pears"));
a->push_back(new stringc("Oranges"));
/*
s = new stringc("Nectarine");
a->insert_after(s, 1); // stringc *s, u32 pos=0)
s = new stringc("Passion Fruit");
a->insert_after(s, 5); // stringc *s, u32 pos=0)
*/
s = new stringc("Grapes");
a->insert_after(s, 15); // stringc *s, u32 pos=0)
s = new stringc("Kiwi");
a->insert_after(s, (u32)0); // stringc *s, u32 pos=0)
/*
s = new stringc("Peach");
a->insert_before(s, 1); // stringc *s, u32 pos=0)
s = new stringc("Strawberry");
a->insert_before(s, 5); // stringc *s, u32 pos=0)
*/
/*
s = new stringc("Peach");
a->insert_before(s, 0); // stringc *s, u32 pos=0)
//s = new stringc("Strawberry");
//a->insert_before(s, 8); // stringc *s, u32 pos=0) // THIS IS OK
//s = new stringc("Strawberry");
//a->insert_before(s, 7); // stringc *s, u32 pos=0)
s = new stringc("Strawberry");
a->insert_before(s, 6); // stringc *s, u32 pos=0)
*/
a->dump();
std::cout << "" << std::endl;
std::cout << "" << std::endl;
std::cout << "" << std::endl;
stringc *sa = new stringc("Planks");
a->insert(sa, 0);
/*
ss = new stringc("Rocks"); // before kiwi
a->insert_before(ss, s); // stringc *s, stringc *s_pos
ss = new stringc("Pebbles"); // first
a->insert_before(ss, sa); // stringc *s, stringc *s_pos
sa = new stringc("Nails"); // add pointer to last element
a->push_back(sa);
ss = new stringc("Sunflowers"); //
a->insert_before(ss, sa); // stringc *s, stringc *s_pos
*/
ss = new stringc("Blocks"); // after kiwi
a->insert_after(ss, s); // stringc *s, stringc *s_pos
ss = new stringc("Blobs"); // after planks
a->insert_after(ss, sa); // stringc *s, stringc *s_pos
sa = new stringc("Nails"); // add pointer to last element
a->push_back(sa);
ss = new stringc("Buckets"); // after nails, last
a->insert_after(ss, sa); // stringc *s, stringc *s_pos
a->dump();
std::cout << "" << std::endl;
delete a;
a = NULL;
}