qse/samples/cmn/arr02.cpp

149 lines
2.8 KiB
C++
Raw Normal View History

#include <stdio.h>
#include <string.h>
#include <qse/cmn/Array.hpp>
#include <qse/cmn/String.hpp>
2016-09-23 14:53:51 +00:00
/*
typedef QSE::Array<int,int> IntArray;
struct IntClass
{
2016-09-23 14:53:51 +00:00
IntClass (int x = 0): x (x) {}
int x;
};
2016-09-23 14:53:51 +00:00
struct IntClassComparator
{
int operator() (const IntClass& v, int y) const
{
2016-09-23 14:53:51 +00:00
IntTable::DefaultComparator comp;
return comp (v.x, y);
}
2016-09-23 14:53:51 +00:00
};
2016-09-23 14:53:51 +00:00
*/
2015-03-24 09:34:57 +00:00
2016-09-23 14:53:51 +00:00
#if 1
class PosStr: public QSE::MbString
{
public:
PosStr (const char* str = ""): QSE::MbString(str), pos((qse_size_t)-1) {};
PosStr (const PosStr& ps): QSE::MbString (ps), pos(ps.pos)
{
}
2016-09-23 14:53:51 +00:00
~PosStr()
{
}
2016-09-23 14:53:51 +00:00
qse_size_t pos;
};
#else
class PosStr
{
public:
PosStr (const char* str = "")
{
2016-09-23 14:53:51 +00:00
strcpy (buf, str);
}
2016-09-23 14:53:51 +00:00
const char* getBuffer() const { return this->buf; }
const char* data() const { return this->buf; }
char buf[512];
};
#endif
2016-09-23 14:53:51 +00:00
struct cstr_comparator
{
int operator() (const char* v1, const QSE::MbString& v2) const
{
return strcmp (v1, v2.getBuffer());
}
};
2016-09-23 14:53:51 +00:00
typedef QSE::Array<PosStr> StrArray;
2015-03-24 09:34:57 +00:00
int main ()
{
//StrArray h (100);
StrArray h (15);
2016-09-23 14:53:51 +00:00
char buf[20];
2016-09-23 14:53:51 +00:00
for (int i = 0; i < 20; i++)
{
2016-09-23 14:53:51 +00:00
sprintf (buf, "hello %d", i);
PosStr x (buf);
h.insert (0, x);
}
2016-09-23 14:53:51 +00:00
#if 0
printf ("%s\n", h.get(3).getBuffer());
h.remove (3, 5);
printf ("%s\n", h.get(3).getBuffer());
h.remove (3, 5);
printf ("%s\n", h.get(3).getBuffer());
#endif
printf ("--------------------\n");
for (qse_size_t i = 0; i < h.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h[i].getBuffer(), (unsigned long int)h.getIndex(h[i]));
}
printf ("--------------------\n");
2016-09-23 14:53:51 +00:00
StrArray h2 (h);
StrArray h3;
2016-09-23 14:53:51 +00:00
h3 = h2;
h.clear (true);
printf ("--------------------\n");
printf ("%d\n", (int)h.getSize());
printf ("--------------------\n");
for (qse_size_t i = 0; i < h2.getSize(); i++)
{
printf ("[%s] at [%lu]\n", h2[i].getBuffer(), (unsigned long int)h2.getIndex(h2[i]));
}
printf ("--------------------\n");
2016-09-23 14:53:51 +00:00
h3.insert (21, "this is a large string");
printf ("%d %d\n", (int)h2.getSize(), (int)h3.getSize());
printf ("--------------------\n");
h3.insert (21, "mystery!");
for (qse_size_t i = 0; i < h3.getSize(); i++)
{
2016-09-23 14:53:51 +00:00
printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i]));
}
2016-09-23 14:53:51 +00:00
printf ("--------------------\n");
h3.setCapacity (6);
h3.insert (6, "good?");
h3.rotate (StrArray::ROTATE_RIGHT, h3.getSize() / 2);
printf ("[%s] [%s]\n", h3.getValueAt(5).getBuffer(), h3.getValueAt(6).getBuffer());
printf ("%d\n", (int)h3.getSize());
printf ("--------------------\n");
h3.insert (1, "bad?");
for (qse_size_t i = 0; i < h3.getSize(); i++)
{
2016-09-23 14:53:51 +00:00
printf ("[%s] at [%lu]\n", h3[i].getBuffer(), (unsigned long int)h3.getIndex(h3[i]));
}
2016-09-23 14:53:51 +00:00
printf ("--------------------\n");
QSE::Array<int> a;
a.insert (0, 10);
a.insert (0, 20);
a.insert (0, 30);
const int& t = a[2u];
printf ("%lu\n", (unsigned long int)a.getIndex(t));
return 0;
}