stdutil: Formatting
This commit is contained in:
+156
-87
@@ -1,8 +1,8 @@
|
|||||||
|
|
||||||
#include "stdutil.hpp"
|
#include "stdutil.hpp"
|
||||||
|
|
||||||
#include <set>
|
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <set>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
@@ -11,9 +11,9 @@ using namespace std;
|
|||||||
#ifdef MEMDEBUG
|
#ifdef MEMDEBUG
|
||||||
|
|
||||||
struct Mem {
|
struct Mem {
|
||||||
Mem *next, *prev;
|
Mem * next, *prev;
|
||||||
const char *file;
|
const char* file;
|
||||||
int line, size, tag;
|
int line, size, tag;
|
||||||
};
|
};
|
||||||
|
|
||||||
static bool track;
|
static bool track;
|
||||||
@@ -21,27 +21,36 @@ static bool track;
|
|||||||
static Mem head, tail;
|
static Mem head, tail;
|
||||||
static Mem x_head, x_tail;
|
static Mem x_head, x_tail;
|
||||||
|
|
||||||
static void remove(Mem *m) {
|
static void remove(Mem* m)
|
||||||
|
{
|
||||||
m->next->prev = m->prev;
|
m->next->prev = m->prev;
|
||||||
m->prev->next = m->next;
|
m->prev->next = m->next;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void insert(Mem *m, Mem *next) {
|
static void insert(Mem* m, Mem* next)
|
||||||
m->next = next;
|
{
|
||||||
m->prev = next->prev;
|
m->next = next;
|
||||||
|
m->prev = next->prev;
|
||||||
next->prev->next = m;
|
next->prev->next = m;
|
||||||
next->prev = m;
|
next->prev = m;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void init() {
|
static void init()
|
||||||
if (head.next) return;
|
{
|
||||||
head.next = head.prev = &tail; head.tag = 'HEAD';
|
if (head.next)
|
||||||
tail.next = tail.prev = &head; tail.tag = 'TAIL';
|
return;
|
||||||
x_head.next = x_head.prev = &x_tail; x_head.tag = 'HEAD';
|
head.next = head.prev = &tail;
|
||||||
x_tail.next = x_tail.prev = &x_head; x_tail.tag = 'TAIL';
|
head.tag = 'HEAD';
|
||||||
|
tail.next = tail.prev = &head;
|
||||||
|
tail.tag = 'TAIL';
|
||||||
|
x_head.next = x_head.prev = &x_tail;
|
||||||
|
x_head.tag = 'HEAD';
|
||||||
|
x_tail.next = x_tail.prev = &x_head;
|
||||||
|
x_tail.tag = 'TAIL';
|
||||||
}
|
}
|
||||||
|
|
||||||
static void check(Mem *m) {
|
static void check(Mem* m)
|
||||||
|
{
|
||||||
if (m->tag != 'DNEW') {
|
if (m->tag != 'DNEW') {
|
||||||
MessageBox(GetDesktopWindow(), "mem_check: pre_tag!='DNEW'", "Memory error", MB_OK | MB_ICONWARNING);
|
MessageBox(GetDesktopWindow(), "mem_check: pre_tag!='DNEW'", "Memory error", MB_OK | MB_ICONWARNING);
|
||||||
if (m->tag == 'NDWE') {
|
if (m->tag == 'NDWE') {
|
||||||
@@ -51,7 +60,7 @@ static void check(Mem *m) {
|
|||||||
}
|
}
|
||||||
ExitProcess(0);
|
ExitProcess(0);
|
||||||
}
|
}
|
||||||
int *t = (int*)((char*)(m + 1) + m->size);
|
int* t = (int*)((char*)(m + 1) + m->size);
|
||||||
if (*t != 'dnew') {
|
if (*t != 'dnew') {
|
||||||
MessageBox(GetDesktopWindow(), "mem_check: post_tag!='dnew'", "Memory error", MB_OK | MB_ICONWARNING);
|
MessageBox(GetDesktopWindow(), "mem_check: post_tag!='dnew'", "Memory error", MB_OK | MB_ICONWARNING);
|
||||||
string t = "Probable memory overwrite - new file: " + string(m->file) + " line:" + itoa(m->line);
|
string t = "Probable memory overwrite - new file: " + string(m->file) + " line:" + itoa(m->line);
|
||||||
@@ -60,42 +69,55 @@ static void check(Mem *m) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void *op_new(size_t size, const char *file = "<unknown>", int line = 0) {
|
static void* op_new(size_t size, const char* file = "<unknown>", int line = 0)
|
||||||
|
{
|
||||||
init();
|
init();
|
||||||
Mem *m = (Mem*)malloc(sizeof(Mem) + size + sizeof(int));
|
Mem* m = (Mem*)malloc(sizeof(Mem) + size + sizeof(int));
|
||||||
memset(m + 1, 0xcc, size);
|
memset(m + 1, 0xcc, size);
|
||||||
m->file = file; m->line = line; m->size = size; m->tag = 'DNEW';
|
m->file = file;
|
||||||
int *t = (int*)((char*)(m + 1) + size); *t = 'dnew';
|
m->line = line;
|
||||||
if (track) insert(m, head.next);
|
m->size = size;
|
||||||
else insert(m, x_head.next);
|
m->tag = 'DNEW';
|
||||||
|
int* t = (int*)((char*)(m + 1) + size);
|
||||||
|
*t = 'dnew';
|
||||||
|
if (track)
|
||||||
|
insert(m, head.next);
|
||||||
|
else
|
||||||
|
insert(m, x_head.next);
|
||||||
return m + 1;
|
return m + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void op_delete(void *q) {
|
static void op_delete(void* q)
|
||||||
|
{
|
||||||
init();
|
init();
|
||||||
if (!q) return;
|
if (!q)
|
||||||
Mem *m = (Mem*)q - 1;
|
return;
|
||||||
|
Mem* m = (Mem*)q - 1;
|
||||||
check(m);
|
check(m);
|
||||||
remove(m);
|
remove(m);
|
||||||
m->tag = 'NDWE';
|
m->tag = 'NDWE';
|
||||||
*(int*)((char*)(m + 1) + m->size) = 'ndwe';
|
*(int*)((char*)(m + 1) + m->size) = 'ndwe';
|
||||||
free(m);
|
free(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
void trackmem(bool enable) {
|
void trackmem(bool enable)
|
||||||
|
{
|
||||||
init();
|
init();
|
||||||
if (track == enable) return;
|
if (track == enable)
|
||||||
|
return;
|
||||||
track = enable;
|
track = enable;
|
||||||
Mem *m;
|
Mem* m;
|
||||||
while ((m = head.next) != &tail) {
|
while ((m = head.next) != &tail) {
|
||||||
remove(m); insert(m, x_head.next);
|
remove(m);
|
||||||
|
insert(m, x_head.next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void checkmem(ostream &out) {
|
void checkmem(ostream& out)
|
||||||
|
{
|
||||||
init();
|
init();
|
||||||
Mem *m, *next;
|
Mem *m, *next;
|
||||||
int sum = 0, usum = 0, xsum = 0;
|
int sum = 0, usum = 0, xsum = 0;
|
||||||
for (m = head.next; m != &tail; m = next) {
|
for (m = head.next; m != &tail; m = next) {
|
||||||
check(m);
|
check(m);
|
||||||
next = m->next;
|
next = m->next;
|
||||||
@@ -116,14 +138,38 @@ void checkmem(ostream &out) {
|
|||||||
out << "Total mem in use:" << (sum + usum + xsum) << endl;
|
out << "Total mem in use:" << (sum + usum + xsum) << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
void * _cdecl operator new(size_t size) { return op_new(size); }
|
void* _cdecl operator new(size_t size)
|
||||||
void * _cdecl operator new[](size_t size) { return op_new(size); }
|
{
|
||||||
void * _cdecl operator new(size_t size, const char *file, int line) { return op_new(size, file, line); }
|
return op_new(size);
|
||||||
void * _cdecl operator new[](size_t size, const char *file, int line) { return op_new(size, file, line); }
|
}
|
||||||
void _cdecl operator delete(void *q) { op_delete(q); }
|
void* _cdecl operator new[](size_t size)
|
||||||
void _cdecl operator delete[](void *q) { op_delete(q); }
|
{
|
||||||
void _cdecl operator delete(void *q, const char *file, int line) { op_delete(q); }
|
return op_new(size);
|
||||||
void _cdecl operator delete[](void *q, const char *file, int line) { op_delete(q); }
|
}
|
||||||
|
void* _cdecl operator new(size_t size, const char* file, int line)
|
||||||
|
{
|
||||||
|
return op_new(size, file, line);
|
||||||
|
}
|
||||||
|
void* _cdecl operator new[](size_t size, const char* file, int line)
|
||||||
|
{
|
||||||
|
return op_new(size, file, line);
|
||||||
|
}
|
||||||
|
void _cdecl operator delete(void* q)
|
||||||
|
{
|
||||||
|
op_delete(q);
|
||||||
|
}
|
||||||
|
void _cdecl operator delete[](void* q)
|
||||||
|
{
|
||||||
|
op_delete(q);
|
||||||
|
}
|
||||||
|
void _cdecl operator delete(void* q, const char* file, int line)
|
||||||
|
{
|
||||||
|
op_delete(q);
|
||||||
|
}
|
||||||
|
void _cdecl operator delete[](void* q, const char* file, int line)
|
||||||
|
{
|
||||||
|
op_delete(q);
|
||||||
|
}
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
@@ -132,15 +178,18 @@ void _cdecl operator delete[](void *q, const char *file, int line) { op_delete(q
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
int atoi(const string &s) {
|
int atoi(const string& s)
|
||||||
|
{
|
||||||
return atoi(s.c_str());
|
return atoi(s.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
double atof(const string &s) {
|
double atof(const string& s)
|
||||||
|
{
|
||||||
return atof(s.c_str());
|
return atof(s.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
string itoa(int n) {
|
string itoa(int n)
|
||||||
|
{
|
||||||
char buff[32];
|
char buff[32];
|
||||||
_itoa(n, buff, 10);
|
_itoa(n, buff, 10);
|
||||||
return string(buff);
|
return string(buff);
|
||||||
@@ -149,7 +198,7 @@ string itoa(int n) {
|
|||||||
//static int _finite(double n) { // definition: exponent anything but 2047.
|
//static int _finite(double n) { // definition: exponent anything but 2047.
|
||||||
//
|
//
|
||||||
// int e; // 11 bit exponent
|
// int e; // 11 bit exponent
|
||||||
// const int eMax = 2047; // 0x7ff, all bits = 1
|
// const int eMax = 2047; // 0x7ff, all bits = 1
|
||||||
//
|
//
|
||||||
// int *pn = (int *)&n;
|
// int *pn = (int *)&n;
|
||||||
//
|
//
|
||||||
@@ -162,7 +211,7 @@ string itoa(int n) {
|
|||||||
//static int _isnan(double n) { // definition: exponent 2047, nonzero fraction.
|
//static int _isnan(double n) { // definition: exponent 2047, nonzero fraction.
|
||||||
//
|
//
|
||||||
// int e; // 11 bit exponent
|
// int e; // 11 bit exponent
|
||||||
// const int eMax = 2047; // 0x7ff, all bits = 1
|
// const int eMax = 2047; // 0x7ff, all bits = 1
|
||||||
//
|
//
|
||||||
// int *pn = (int *)&n;
|
// int *pn = (int *)&n;
|
||||||
//
|
//
|
||||||
@@ -182,25 +231,23 @@ string itoa(int n) {
|
|||||||
/////////////
|
/////////////
|
||||||
//By FLOYD!//
|
//By FLOYD!//
|
||||||
/////////////
|
/////////////
|
||||||
string ftoa(float n) {
|
string ftoa(float n)
|
||||||
|
{
|
||||||
static const int digits = 6;
|
static const int digits = 6;
|
||||||
|
|
||||||
int eNeg = -4, ePos = 8; // limits for e notation.
|
int eNeg = -4, ePos = 8; // limits for e notation.
|
||||||
|
|
||||||
char buffer[50]; // from MSDN example, 25 would probably suffice
|
char buffer[50]; // from MSDN example, 25 would probably suffice
|
||||||
string t;
|
string t;
|
||||||
int dec, sign;
|
int dec, sign;
|
||||||
|
|
||||||
if (_finite(n)) {
|
if (_finite(n)) {
|
||||||
|
|
||||||
// if ( digits < 1 ) digits = 1; // less than one digit is nonsense
|
// if ( digits < 1 ) digits = 1; // less than one digit is nonsense
|
||||||
// if ( digits > 8 ) digits = 8; // practical maximum for float
|
// if ( digits > 8 ) digits = 8; // practical maximum for float
|
||||||
|
|
||||||
t = _ecvt(n, digits, &dec, &sign);
|
t = _ecvt(n, digits, &dec, &sign);
|
||||||
|
|
||||||
if (dec <= eNeg + 1 || dec > ePos) {
|
if (dec <= eNeg + 1 || dec > ePos) {
|
||||||
|
|
||||||
_gcvt(n, digits, buffer);
|
_gcvt(n, digits, buffer);
|
||||||
t = buffer;
|
t = buffer;
|
||||||
return t;
|
return t;
|
||||||
@@ -210,34 +257,34 @@ string ftoa(float n) {
|
|||||||
// number with no e-notation or multiple trailing zeroes.
|
// number with no e-notation or multiple trailing zeroes.
|
||||||
|
|
||||||
if (dec <= 0) {
|
if (dec <= 0) {
|
||||||
|
t = "0." + string(-dec, '0') + t;
|
||||||
t = "0." + string(-dec, '0') + t;
|
dec = 1; // new location for decimal point
|
||||||
dec = 1; // new location for decimal point
|
|
||||||
|
|
||||||
} else if (dec < digits) {
|
} else if (dec < digits) {
|
||||||
|
|
||||||
t = t.substr(0, dec) + "." + t.substr(dec);
|
t = t.substr(0, dec) + "." + t.substr(dec);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
t = t + string(dec - digits, '0') + ".0";
|
t = t + string(dec - digits, '0') + ".0";
|
||||||
dec += dec - digits;
|
dec += dec - digits;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Finally, trim off excess zeroes.
|
// Finally, trim off excess zeroes.
|
||||||
|
|
||||||
int dp1 = dec + 1, p = t.length();
|
int dp1 = dec + 1, p = t.length();
|
||||||
while (--p > dp1 && t[p] == '0');
|
while (--p > dp1 && t[p] == '0')
|
||||||
|
;
|
||||||
t = string(t, 0, ++p);
|
t = string(t, 0, ++p);
|
||||||
|
|
||||||
return sign ? "-" + t : t;
|
return sign ? "-" + t : t;
|
||||||
|
|
||||||
} // end of finite case
|
} // end of finite case
|
||||||
|
|
||||||
if (_isnan(n)) return "NaN";
|
if (_isnan(n))
|
||||||
if (n > 0.0) return "Infinity";
|
return "NaN";
|
||||||
if (n < 0.0) return "-Infinity";
|
if (n > 0.0)
|
||||||
|
return "Infinity";
|
||||||
|
if (n < 0.0)
|
||||||
|
return "-Infinity";
|
||||||
|
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
@@ -279,80 +326,102 @@ string ftoa( float n ){
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
string tolower(const string &s) {
|
string tolower(const string& s)
|
||||||
|
{
|
||||||
string t = s;
|
string t = s;
|
||||||
for (unsigned int k = 0; k < t.size(); ++k) t[k] = tolower(t[k]);
|
for (unsigned int k = 0; k < t.size(); ++k)
|
||||||
|
t[k] = tolower(t[k]);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
string toupper(const string &s) {
|
string toupper(const string& s)
|
||||||
|
{
|
||||||
string t = s;
|
string t = s;
|
||||||
for (unsigned int k = 0; k < t.size(); ++k) t[k] = toupper(t[k]);
|
for (unsigned int k = 0; k < t.size(); ++k)
|
||||||
|
t[k] = toupper(t[k]);
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
|
|
||||||
string fullfilename(const string &t) {
|
string fullfilename(const string& t)
|
||||||
|
{
|
||||||
char buff[MAX_PATH + 1], *p;
|
char buff[MAX_PATH + 1], *p;
|
||||||
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
||||||
return string(buff);
|
return string(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
string filenamepath(const string &t) {
|
string filenamepath(const string& t)
|
||||||
|
{
|
||||||
char buff[MAX_PATH + 1], *p;
|
char buff[MAX_PATH + 1], *p;
|
||||||
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
||||||
if (!p) return "";
|
if (!p)
|
||||||
*p = 0; return string(buff);
|
return "";
|
||||||
|
*p = 0;
|
||||||
|
return string(buff);
|
||||||
}
|
}
|
||||||
|
|
||||||
string filenamefile(const string &t) {
|
string filenamefile(const string& t)
|
||||||
|
{
|
||||||
char buff[MAX_PATH + 1], *p;
|
char buff[MAX_PATH + 1], *p;
|
||||||
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
GetFullPathName(t.c_str(), MAX_PATH, buff, &p);
|
||||||
if (!p) return "";
|
if (!p)
|
||||||
|
return "";
|
||||||
return string(p);
|
return string(p);
|
||||||
}
|
}
|
||||||
|
|
||||||
const int MIN_SIZE = 256;
|
const int MIN_SIZE = 256;
|
||||||
|
|
||||||
qstreambuf::qstreambuf() {
|
qstreambuf::qstreambuf()
|
||||||
|
{
|
||||||
buf = new char[MIN_SIZE];
|
buf = new char[MIN_SIZE];
|
||||||
setg(buf, buf, buf);
|
setg(buf, buf, buf);
|
||||||
setp(buf, buf, buf + MIN_SIZE);
|
setp(buf, buf, buf + MIN_SIZE);
|
||||||
}
|
}
|
||||||
|
|
||||||
qstreambuf::~qstreambuf() {
|
qstreambuf::~qstreambuf()
|
||||||
|
{
|
||||||
delete buf;
|
delete buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
int qstreambuf::size() {
|
int qstreambuf::size()
|
||||||
|
{
|
||||||
return pptr() - gptr();
|
return pptr() - gptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
char *qstreambuf::data() {
|
char* qstreambuf::data()
|
||||||
|
{
|
||||||
return gptr();
|
return gptr();
|
||||||
}
|
}
|
||||||
|
|
||||||
qstreambuf::int_type qstreambuf::underflow() {
|
qstreambuf::int_type qstreambuf::underflow()
|
||||||
|
{
|
||||||
if (gptr() == egptr()) {
|
if (gptr() == egptr()) {
|
||||||
if (gptr() == pptr()) return traits_type::eof();
|
if (gptr() == pptr())
|
||||||
|
return traits_type::eof();
|
||||||
setg(gptr(), gptr(), pptr());
|
setg(gptr(), gptr(), pptr());
|
||||||
}
|
}
|
||||||
|
|
||||||
return traits_type::to_int_type(*gptr());
|
return traits_type::to_int_type(*gptr());
|
||||||
}
|
}
|
||||||
|
|
||||||
qstreambuf::int_type qstreambuf::overflow(qstreambuf::int_type c) {
|
qstreambuf::int_type qstreambuf::overflow(qstreambuf::int_type c)
|
||||||
if (c == traits_type::eof()) return c;
|
{
|
||||||
|
if (c == traits_type::eof())
|
||||||
|
return c;
|
||||||
|
|
||||||
if (pptr() == epptr()) {
|
if (pptr() == epptr()) {
|
||||||
int sz = size();
|
int sz = size();
|
||||||
int n_sz = sz * 2; if (n_sz < MIN_SIZE) n_sz = MIN_SIZE;
|
int n_sz = sz * 2;
|
||||||
char *n_buf = new char[n_sz];
|
if (n_sz < MIN_SIZE)
|
||||||
|
n_sz = MIN_SIZE;
|
||||||
|
char* n_buf = new char[n_sz];
|
||||||
memcpy(n_buf, gptr(), sz);
|
memcpy(n_buf, gptr(), sz);
|
||||||
delete buf; buf = n_buf;
|
delete buf;
|
||||||
|
buf = n_buf;
|
||||||
setg(buf, buf, buf + sz);
|
setg(buf, buf, buf + sz);
|
||||||
setp(buf + sz, buf + sz, buf + n_sz);
|
setp(buf + sz, buf + sz, buf + n_sz);
|
||||||
}
|
}
|
||||||
|
|
||||||
*pptr() = traits_type::to_char_type(c);
|
*pptr() = traits_type::to_char_type(c);
|
||||||
pbump(1); return traits_type::not_eof(c);
|
pbump(1);
|
||||||
|
return traits_type::not_eof(c);
|
||||||
}
|
}
|
||||||
|
|||||||
+103
-61
@@ -2,93 +2,135 @@
|
|||||||
#ifndef STDUTIL_H
|
#ifndef STDUTIL_H
|
||||||
#define STDUTIL_H
|
#define STDUTIL_H
|
||||||
|
|
||||||
#pragma warning(disable:4786)
|
#pragma warning(disable : 4786)
|
||||||
|
|
||||||
#include "config.hpp"
|
#include "config.hpp"
|
||||||
|
|
||||||
#include <string>
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
void trackmem( bool enable );
|
void trackmem(bool enable);
|
||||||
void checkmem( std::ostream &out );
|
void checkmem(std::ostream& out);
|
||||||
|
|
||||||
//some stuff that should be in std libs
|
//some stuff that should be in std libs
|
||||||
int atoi( const std::string &s );
|
int atoi(const std::string& s);
|
||||||
double atof( const std::string &s );
|
double atof(const std::string& s);
|
||||||
std::string itoa( int n );
|
std::string itoa(int n);
|
||||||
std::string ftoa( float n );
|
std::string ftoa(float n);
|
||||||
std::string tolower( const std::string &s );
|
std::string tolower(const std::string& s);
|
||||||
std::string toupper( const std::string &s );
|
std::string toupper(const std::string& s);
|
||||||
std::string fullfilename( const std::string &t );
|
std::string fullfilename(const std::string& t);
|
||||||
std::string filenamepath( const std::string &t );
|
std::string filenamepath(const std::string& t);
|
||||||
std::string filenamefile( const std::string &t );
|
std::string filenamefile(const std::string& t);
|
||||||
|
|
||||||
//lazy version of auto_ptr
|
//lazy version of auto_ptr
|
||||||
template<class T>
|
template<class T>
|
||||||
class a_ptr{
|
class a_ptr {
|
||||||
public:
|
public:
|
||||||
a_ptr(T *t=0):t(t){}
|
a_ptr(T* t = 0) : t(t) {}
|
||||||
~a_ptr(){delete t;}
|
~a_ptr()
|
||||||
a_ptr &operator=(T *t){this->t=t;return *this;}
|
{
|
||||||
T &operator*()const{return *t;}
|
delete t;
|
||||||
T *operator->()const{return t;}
|
}
|
||||||
operator T&()const{return *t;}
|
a_ptr& operator=(T* t)
|
||||||
operator T*()const{return t;}
|
{
|
||||||
T *release(){ T *tt=t;t=0;return tt; }
|
this->t = t;
|
||||||
private:
|
return *this;
|
||||||
T *t;
|
}
|
||||||
|
T& operator*() const
|
||||||
|
{
|
||||||
|
return *t;
|
||||||
|
}
|
||||||
|
T* operator->() const
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
operator T&() const
|
||||||
|
{
|
||||||
|
return *t;
|
||||||
|
}
|
||||||
|
operator T*() const
|
||||||
|
{
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
T* release()
|
||||||
|
{
|
||||||
|
T* tt = t;
|
||||||
|
t = 0;
|
||||||
|
return tt;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
T* t;
|
||||||
};
|
};
|
||||||
|
|
||||||
//Speed-up for SLOW sstream
|
//Speed-up for SLOW sstream
|
||||||
class qstreambuf : public std::streambuf{
|
class qstreambuf : public std::streambuf {
|
||||||
public:
|
public:
|
||||||
qstreambuf();
|
qstreambuf();
|
||||||
~qstreambuf();
|
~qstreambuf();
|
||||||
int size(); //bytes unread
|
int size(); //bytes unread
|
||||||
char *data(); //start of bytes unread
|
char* data(); //start of bytes unread
|
||||||
private:
|
private:
|
||||||
char *buf;
|
char* buf;
|
||||||
int_type underflow();
|
int_type underflow();
|
||||||
int_type overflow( int_type c );
|
int_type overflow(int_type c);
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class T>
|
template<class T>
|
||||||
class pool{
|
class pool {
|
||||||
T *free;
|
T* free;
|
||||||
enum{ N=512 };
|
enum { N = 512 };
|
||||||
public:
|
|
||||||
typedef size_t size_type;
|
public:
|
||||||
|
typedef size_t size_type;
|
||||||
typedef ptrdiff_t difference_type;
|
typedef ptrdiff_t difference_type;
|
||||||
typedef T *pointer;
|
typedef T* pointer;
|
||||||
typedef const T *const_pointer;
|
typedef const T* const_pointer;
|
||||||
typedef T &reference;
|
typedef T& reference;
|
||||||
typedef const T &const_reference;
|
typedef const T& const_reference;
|
||||||
typedef T value_type;
|
typedef T value_type;
|
||||||
pointer address( reference q )const{ return &q; }
|
pointer address(reference q) const
|
||||||
const_pointer address( const_reference q )const{ return &q; }
|
{
|
||||||
pool():free(0){}
|
return &q;
|
||||||
pointer allocate( size_type n,const void *){
|
}
|
||||||
clog<<"Allocating "<<n<<endl;
|
const_pointer address(const_reference q) const
|
||||||
if( n>1 ) return new T[n];
|
{
|
||||||
if( !free ){
|
return &q;
|
||||||
free=(T*)new char[sizeof(T)*N];
|
}
|
||||||
for( int k=0;k<N-1;++k ) *(T**)(free+k)=free+k+1;
|
pool() : free(0) {}
|
||||||
*(T**)(free+N-1)=0;
|
pointer allocate(size_type n, const void*)
|
||||||
|
{
|
||||||
|
clog << "Allocating " << n << endl;
|
||||||
|
if (n > 1)
|
||||||
|
return new T[n];
|
||||||
|
if (!free) {
|
||||||
|
free = (T*)new char[sizeof(T) * N];
|
||||||
|
for (int k = 0; k < N - 1; ++k)
|
||||||
|
*(T**)(free + k) = free + k + 1;
|
||||||
|
*(T**)(free + N - 1) = 0;
|
||||||
}
|
}
|
||||||
T *t=free;
|
T* t = free;
|
||||||
free=*(T**)t;
|
free = *(T**)t;
|
||||||
return t;
|
return t;
|
||||||
}
|
}
|
||||||
void deallocate( pointer q,size_type n ){
|
void deallocate(pointer q, size_type n)
|
||||||
clog<<"Deallocating "<<n<<endl;
|
{
|
||||||
while( n-->0 ){
|
clog << "Deallocating " << n << endl;
|
||||||
*(T**)q=free;
|
while (n-- > 0) {
|
||||||
*(T**)free=q;
|
*(T**)q = free;
|
||||||
|
*(T**)free = q;
|
||||||
++q;
|
++q;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void construct( pointer p,const T &q ){ new(p)T(q); }
|
void construct(pointer p, const T& q)
|
||||||
void destroy( pointer p ){ p->~T(); }
|
{
|
||||||
|
new (p) T(q);
|
||||||
|
}
|
||||||
|
void destroy(pointer p)
|
||||||
|
{
|
||||||
|
p->~T();
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user