Remove last usage of vector
FossilOrigin-Name: 08c9d7b0fa8f592b68069fe2444e016d4a06025d04cae3f8271575cf0763a182
This commit is contained in:
parent
5b200abd87
commit
d25e07085c
7 changed files with 78 additions and 184 deletions
137
src/tools.h
137
src/tools.h
|
@ -19,19 +19,9 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#ifdef __GNUC__
|
||||
# include <new>
|
||||
#else
|
||||
# include <new.h>
|
||||
#endif
|
||||
|
||||
#import <ObjFW/ObjFW.h>
|
||||
|
||||
#ifdef NULL
|
||||
# undef NULL
|
||||
#endif
|
||||
#define NULL 0
|
||||
|
||||
typedef unsigned char uchar;
|
||||
typedef unsigned short ushort;
|
||||
typedef unsigned int uint;
|
||||
|
@ -52,137 +42,10 @@ typedef unsigned int uint;
|
|||
|
||||
#ifndef OF_WINDOWS
|
||||
# define __cdecl
|
||||
# define _vsnprintf vsnprintf
|
||||
#endif
|
||||
|
||||
#define fast_f2nat(val) ((int)(val))
|
||||
|
||||
extern void endianswap(void *, int, int);
|
||||
|
||||
template <class T> struct vector {
|
||||
T *buf;
|
||||
int alen;
|
||||
int ulen;
|
||||
|
||||
vector()
|
||||
{
|
||||
alen = 8;
|
||||
buf = (T *)OFAllocMemory(alen, sizeof(T));
|
||||
ulen = 0;
|
||||
}
|
||||
|
||||
~vector()
|
||||
{
|
||||
setsize(0);
|
||||
free(buf);
|
||||
}
|
||||
|
||||
vector(vector<T> &v);
|
||||
void operator=(vector<T> &v);
|
||||
|
||||
T &
|
||||
add(const T &x)
|
||||
{
|
||||
if (ulen == alen)
|
||||
realloc();
|
||||
new (&buf[ulen]) T(x);
|
||||
return buf[ulen++];
|
||||
}
|
||||
|
||||
T &
|
||||
add()
|
||||
{
|
||||
if (ulen == alen)
|
||||
realloc();
|
||||
new (&buf[ulen]) T;
|
||||
return buf[ulen++];
|
||||
}
|
||||
|
||||
T &
|
||||
pop()
|
||||
{
|
||||
return buf[--ulen];
|
||||
}
|
||||
|
||||
T &
|
||||
last()
|
||||
{
|
||||
return buf[ulen - 1];
|
||||
}
|
||||
|
||||
bool
|
||||
empty()
|
||||
{
|
||||
return ulen == 0;
|
||||
}
|
||||
|
||||
int
|
||||
length()
|
||||
{
|
||||
return ulen;
|
||||
}
|
||||
|
||||
T &
|
||||
operator[](int i)
|
||||
{
|
||||
assert(i >= 0 && i < ulen);
|
||||
return buf[i];
|
||||
}
|
||||
|
||||
void
|
||||
setsize(int i)
|
||||
{
|
||||
for (; ulen > i; ulen--)
|
||||
buf[ulen - 1].~T();
|
||||
}
|
||||
|
||||
T *
|
||||
getbuf()
|
||||
{
|
||||
return buf;
|
||||
}
|
||||
|
||||
void
|
||||
sort(void *cf)
|
||||
{
|
||||
qsort(buf, ulen, sizeof(T),
|
||||
(int(__cdecl *)(const void *, const void *))cf);
|
||||
}
|
||||
|
||||
void
|
||||
realloc()
|
||||
{
|
||||
buf = (T *)OFResizeMemory(buf, (alen *= 2), sizeof(T));
|
||||
}
|
||||
|
||||
T
|
||||
remove(int i)
|
||||
{
|
||||
T e = buf[i];
|
||||
for (int p = i + 1; p < ulen; p++)
|
||||
buf[p - 1] = buf[p];
|
||||
ulen--;
|
||||
return e;
|
||||
}
|
||||
|
||||
T &
|
||||
insert(int i, const T &e)
|
||||
{
|
||||
add(T());
|
||||
for (int p = ulen - 1; p > i; p--)
|
||||
buf[p] = buf[p - 1];
|
||||
buf[i] = e;
|
||||
return buf[i];
|
||||
}
|
||||
};
|
||||
|
||||
#define loopv(v) \
|
||||
if (false) { \
|
||||
} else \
|
||||
for (int i = 0; i < (v).length(); i++)
|
||||
#define loopvrev(v) \
|
||||
if (false) { \
|
||||
} else \
|
||||
for (int i = (v).length() - 1; i >= 0; i--)
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue