git-svn-id: svn://db.shs.com.ru/pip@33 12ceb7fc-bf1f-11e4-8940-5bc7170c53b5

This commit is contained in:
2015-03-17 13:20:58 +00:00
parent f2e85a736d
commit 81f27034f9
3 changed files with 326 additions and 170 deletions

View File

@@ -36,7 +36,7 @@ class PIDeque {
public:
PIDeque(): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
//printf("new vector 1 %p (%s) ... !{\n", this, typeid(T).name());
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
//printf("(s=%d, d=%p) }!\n", int(pid_size), pid_data);
}
PIDeque(const PIDeque<T> & other): pid_data(0), pid_size(0), pid_rsize(0), pid_start(0) {
//printf("new vector 2 %p (%s) ... !{\n", this, typeid(T).name());
@@ -201,7 +201,13 @@ public:
}
return *this;
}
PIDeque<T> & reserve(size_t new_size) {if (new_size <= pid_rsize) return *this; size_t os = pid_size; alloc(new_size, true); pid_size = os; return *this;}
PIDeque<T> & reserve(size_t new_size) {
if (new_size <= pid_rsize) return *this;
size_t os = pid_size;
alloc(new_size, true);
pid_size = os;
return *this;
}
PIDeque<T> & insert(size_t index, const T & v = T()) {
bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
@@ -210,11 +216,10 @@ public:
alloc(pid_size + 1, true);
if (index < pid_size - 1) {
size_t os = pid_size - index - 1;
memmove(&(pid_data[pid_start + index + 1]), &(pid_data[pid_start + index]), os * sizeof(T));
memmove(&(pid_data[index + pid_start + 1]), &(pid_data[index + pid_start]), os * sizeof(T));
}
} else {
pid_start--;
alloc(pid_size + 1, false);
alloc(pid_size + 1, false, -1);
//piCout << "insert front" << pid_size << pid_rsize << pid_start << "!<";
if (index > 0)
memmove(&(pid_data[pid_start]), &(pid_data[pid_start + 1]), index * sizeof(T));
@@ -226,20 +231,22 @@ public:
PIDeque<T> & insert(size_t index, const PIDeque<T> & other) {
if (other.isEmpty()) return *this;
bool dir = pid_rsize <= 2 ? true : (index >= pid_rsize / 2 ? true : false);
//piCout << "insert" << dir << index << pid_size << pid_rsize << pid_start << "!<";
//piCout << this << "insert" << dir << index << pid_size << pid_rsize << pid_start << " <- " << other.size() << "!<";
if (dir) {
ssize_t os = pid_size - index;
alloc(pid_size + other.pid_size, true);
if (os > 0)
memmove(&(pid_data[pid_start + index + other.pid_size]), &(pid_data[pid_start + index]), os * sizeof(T));
memmove(&(pid_data[index + pid_start + other.pid_size]), &(pid_data[index + pid_start]), os * sizeof(T));
} else {
pid_start -= other.pid_size;
alloc(pid_size + other.pid_size, false);
//piCout << "insert front" << pid_size << pid_rsize << pid_start << "!<";
//if (pid_start < other.pid_size) pid_start = 0;
//piCout << this << " insert offseted start ba" << pid_start << pid_size << pid_rsize;
alloc(pid_size + other.pid_size, false, -other.pid_size);
//piCout << this << " insert offseted start aa" << pid_start << pid_size << pid_rsize;
//piCout << this << " insert front" << pid_size << pid_rsize << pid_start << "!<";
if (index > 0)
memmove(&(pid_data[pid_start]), &(pid_data[pid_start + other.pid_size]), index * sizeof(T));
}
//piCout << "insert" << pid_start << index << (pid_start + ssize_t(index)) << pid_size << ">!";
//piCout << this << "insert" << pid_start << index << (pid_start + ssize_t(index)) << pid_size << ">!";
newT(pid_data + pid_start + index, other.pid_data + other.pid_start, other.pid_size);
return *this;
}
@@ -251,10 +258,10 @@ public:
return *this;
}
size_t os = pid_size - index - count;
deleteT(&(pid_data[pid_start + index]), count);
deleteT(&(pid_data[index + pid_start]), count);
if (os <= index) {
//if (true) {
if (os > 0) memmove(&(pid_data[pid_start + index]), &(pid_data[pid_start + index + count]), os * sizeof(T));
if (os > 0) memmove(&(pid_data[index + pid_start]), &(pid_data[index + pid_start + count]), os * sizeof(T));
} else {
if (index > 0) memmove(&(pid_data[pid_start + count]), &(pid_data[pid_start]), index * sizeof(T));
pid_start += count;
@@ -327,11 +334,11 @@ private:
elementNew(dst + i, src[i]);
}
inline T * newRaw(size_t s) {
//cout << std::dec << " ![("<<this<<")newRaw " << s << " elements ... <\n" << endl;
//std::cout << std::dec << " ![("<<this<<")newRaw " << s << " elements ... <" << std::endl;
//uchar * ret = new uchar[s * sizeof(T)];
uchar * ret = (uchar*)(malloc(s * sizeof(T)));//new uchar[];
//zeroRaw((T*)ret, s);
//cout << std::hex << " > (new 0x" << (llong)ret << ") ok]!" << endl;
//std::cout << std::hex << " > (new 0x" << (llong)ret << ") ok]!" << std::endl;
return (T*)ret;
}
/*void reallocRawTemp(size_t s) {
@@ -339,7 +346,7 @@ private:
else pid_tdata = (T*)(realloc(pid_tdata, s * sizeof(T)));
}*/
inline void deleteT(T * d, size_t sz) {
//cout << " ~[("<<this<<")deleteT " << std::dec << sz << " elements " << std::hex << "0x" << (llong)d << " ... <\n" << endl;
//std::cout << " ~[("<<this<<")deleteT " << std::dec << sz << " elements " << " start " << pid_start << std::hex << " 0x" << (llong)d << " ... <" << std::endl;
if ((uchar*)d != 0) {
for (size_t i = 0; i < sz; ++i)
elementDelete(d[i]);
@@ -362,7 +369,7 @@ private:
inline void elementDelete(T & from) {from.~T();}
void dealloc() {deleteRaw(pid_data);}
inline void checkMove(bool direction) {
if (pid_size >= 4 && pid_size < pid_rsize / 4) {
if (pid_size >= 4 && pid_size < pid_rsize / 6) {
/*if (direction) {
if (pid_start >= 4 && pid_start > pid_size + pid_size && pid_start > pid_rsize / 2) {
piCout << (int)this << "checkMove" << direction << pid_start << (int)pid_data << pid_rsize << pid_size;
@@ -378,16 +385,18 @@ private:
pid_start = ssize_t(pid_rsize) - pid_size - pid_size;
}
}*/
//printf("(%p) check move st=%d sz=%d rs=%d\n", this, pid_start, pid_size, pid_rsize);
if (pid_start < pid_size + pid_size || pid_start > pid_rsize - pid_size - pid_size) {
size_t ns = (pid_rsize - pid_size) / 2;
ssize_t ns = (pid_rsize - pid_size) / 2;
if (pid_start != ns) {
//printf("(%p) move %d -> %d\n", this, pid_start, ns);
memmove(pid_data + ns, pid_data + pid_start, pid_size * sizeof(T));
pid_start = ns;
}
}
}
}
inline void alloc(size_t new_size, bool direction) { // direction == true -> alloc forward
inline void alloc(size_t new_size, bool direction, ssize_t start_offset = 0) { // direction == true -> alloc forward
if (direction) {
if (pid_start + new_size <= pid_rsize) {
pid_size = new_size;
@@ -397,42 +406,46 @@ private:
pid_size = new_size;
size_t as = asize(pid_start + new_size);
if (as != pid_rsize) {
//printf("(%p) realloc %d -> %d (%p)\n", this, pid_rsize, as, pid_data);
pid_data = (T*)(realloc(pid_data, as*sizeof(T)));
pid_rsize = as;
//printf("(%p) realloc done (%p)\n", this, pid_data);
}
} else {
size_t as = asize(piMax<ssize_t>(new_size, pid_rsize) - pid_start);
//piCout << "alloc" << new_size << pid_size << pid_rsize << as << pid_start;
if (pid_start >= 0 && as <= pid_rsize) {
pid_size = new_size;
checkMove(direction);
return;
}
size_t os = pid_size;
pid_size = new_size;
size_t as;
//piCout << "INS ba" << *this;
if (pid_start + start_offset < 0)
as = asize(pid_rsize - start_offset);
else as = pid_rsize;
//printf("%X alloc %d %d\n", this, pid_rsize, start_offset);
//printf("%X alloc %d %d %d %d %d %d\n", this, new_size, pid_size, pid_rsize, as, pid_start, start_offset);
if (as > pid_rsize) {
//piCout << "alloc new size" << as;
//printf("%X alloc new size %d\n", this, as);
//cout << std::hex << " ![("<<this<<")realloc " << pid_data << " data ... <\n" << endl;
T * td = newRaw(as);
//piCout << "pid_start" << pid_start << (pid_start + ssize_t(as) - os);
ssize_t ost = pid_start, ns = 0;
if (ost < 0) {ns -= ost; ost = 0;}
pid_start += ssize_t(as) - os;
if (os > 0 && pid_data != 0) {
memcpy(td + pid_start + ns, pid_data + ost, os * sizeof(T));
ssize_t ns = pid_start + as - pid_rsize;
//printf("%X pid_start ost=%d ors=%d nst=%d nrs=%d\n", this, pid_start, pid_rsize, ns, as);
if (pid_rsize > 0 && pid_data != 0) {
//printf("%X copy from %p + %d to %p + %d %d el\n", this, pid_data, pid_start, td, ns, pid_size);
memcpy(td + ns, pid_data + pid_start, pid_size * sizeof(T));
deleteRaw(pid_data);
}
pid_data = td;
pid_rsize = as;
pid_start = ns;
//piCout << "INS aa" << *this;
}
pid_start += start_offset;
pid_size = new_size;
checkMove(direction);
}
//checkMove(direction);
//piCout << "alloc new start" << pid_start;
//printf("%X alloc new start %d\n", this, pid_start);
}
T * pid_data;
volatile size_t pid_size, pid_rsize;
volatile size_t pid_start;
volatile ssize_t pid_start;
};
#define __PIDEQUE_SIMPLE_TYPE__(T) \

View File

@@ -1,2 +0,0 @@
// This file was generated by PIP CMake, don`t edit it!
#define __PIP_VERSION_STR__ "0.5.0_alpha"