#include
#include
using namespace std;
template//空间配置器
class myalloctor
{
public:
typedef _Ty *pointer;
typedef const _Ty *const_pointer;
typedef _Ty& reference;
typedef const _Ty& const_reference;
typedef _Ty value_type;
//开辟内存
pointer _allocate(int _N, const void *)
{
return (pointer)operator new(_N*sizeof(_Ty)); //给定位置开辟空间返回开辟地点的指针//重载运算符new具体代码在上面
}
//构造对象
void construct(pointer _P, const _Ty& _V)//在指定的空间上构造对象
{
new (_P)_Ty(_V);
}
//析构对象
void destroy(pointer _P)//析构对象
{
_P->~_Ty();
}
//释放内存
void deallocate(void *_P, int)//释放内存
{
operator delete(_P);
}
};
template
class _reverse_iterator
{
public:
_reverse_iterator(Iterator it = Iterator()) :_it(it)
{
}
_reverse_iterator(const _reverse_iterator &src)
{
_it = src._it;
}
_reverse_iterator(Iterator &src)
{
_it = src;
}
_reverse_iterator operator+(int t2)
{
return _reverse_iterator(_it + t2);
}
_reverse_iterator operator-(int t2)
{
return _reverse_iterator(_it - t2);
}
_reverse_iterator& operator --(int)
{
_it++;
return *this;
}
_reverse_iterator& operator ++(int)
{
_it--;
return *this;
}
bool operator!=(const _reverse_iterator &src)
{
return _it != src._it;
}
bool operator!=(const _reverse_iterator &src)const
{
return _it != src._it;
}
T operator*()
{
return *_it;
}
int operator-(const _reverse_iterator &it)
{
return it - _it;
}
private:
Iterator _it;
};
template>
class Vector
{
public:
class iterator;
class const_iterator;
typedef T value_type;
typedef _reverse_iterator reverse_iterator;
typedef _reverse_iterator const_reverse_iterator;
explicit Vector(const A& _Al = A()) :_allocator(_Al), _first(0), _last(0), _end(0){}//初始化空间配置器 last 最后一个元素end容器的最底端 first
~Vector()
{
iterator it = _first;
for (; it != _end; it++)
{
_allocator.destroy(&*it);//空间配置析构;
}
_allocator.deallocate(&*_first, capacity());//delete空间配置器
}
void push_back(const value_type &val)
{
if (_last == _end)//刚开始的操作
{
if (_first == _end)
{
reserve(1);//first==end 初始化vector为一个大小然后以二倍增长
}
else
{
reserve(2 * size());
}
_allocator.construct(&*_last, val);//构造对象
_last++;
}
else
{
_allocator.construct(&*_last, val);//不满直接构造对象
_last++;
}
}
void pop_back()
{
if (empty())
{
return;
}
_allocator.destroy(&*_last);//不为空析构对象并将空间释放归还到内存池上
_last--;
}
bool empty()const
{
return _first == _last;
}
int size()
{
return _last - _first;
}
bool full()
{
return _last == _end;
}
iterator insert(iterator it1, const value_type &val)
{
if (full())
{
reserve(2 * size());
}
iterator it = _last;
while (it != it1)
{
*it = *(it - 1);
it--;
}
*it = val;
_last++;
return it;
}
iterator erase(iterator it)
{
iterator start = it;
for (; start < _last - 1; start++)
{
*start = *start + 1;
}
_allocator.destroy(&*_last);//析构掉该对象释放空间返回给内存池
_last--;
return it;
}
void reserve(int count)
{
if (capacity() < count)//返回总共的从vector最大的到元素所拥有的空间判断是否足够如果不够释放原来了vector的空间重新开辟并再上面构造新的对象;足够的话直接返回成功;
{
T *ptr = NULL;
int _size = size();
ptr = _allocator._allocate(count + size(), ptr);
T *s = ptr;
iterator it = _first;
for (; it != _last; it++, ptr++)
{
_allocator.construct(ptr, *it);
}
it = _first;
for (; it != _end; it++)
{
_allocator.destroy(&*it);
}
_allocator.deallocate(&*_first, capacity());
iterator tmp1(s);
_first = tmp1;
_last = tmp1 + _size;
_end = tmp1 + count;
}
}
void resize(int _newsize)
{
int sizes = _newsize;
if (_newsize > size())
{
reserve(_newsize);//进行空间调整
}
iterator it = _last;//_last=_first刚开始?有歧义这样邪恶
for (; it!= _end; it++)//进行初始化
{
_allocator.construct(&*it, 0);
}
_last = _end;
}
class const_iterator
{
public:
const_iterator(T *ptr = NULL) :_ptr(ptr)
{
}
const_iterator(const const_iterator &src)
{
_ptr = src._ptr;
}
const_iterator(const iterator &src)
{
_ptr = src._ptr;
}
const T operator*()const
{
return *_ptr;
}
value_type *operator->()const
{
return _ptr;
}
bool operator<(const const_iterator &src)const
{
if (_ptr()
{
return const_iterator::_ptr;
}
int operator-(const iterator &it) const
{
return const_iterator::_ptr - it._ptr;
}
const_iterator operator-(int t2)const
{
return const_iterator::_ptr - t2;
}
bool operator!=(const iterator &src)
{
return const_iterator::_ptr != src._ptr;
}
};
iterator begin()
{
return (iterator(_first));
}
iterator end()
{
return (iterator(_last));
}
const_iterator begin() const
{
return (const_iterator(_first));
}
const_iterator end() const
{
return (const_iterator(_last));
}
reverse_iterator rbegin()
{
return reverse_iterator(_last - 1);
}
reverse_iterator rend()
{
return reverse_iterator(_first - 1);
}
const_reverse_iterator rbegin()const
{
return const_iterator(_last - 1);
}
const_reverse_iterator rend()const
{
return const_reverse_iterator(const_iterator(_first - 1));
}
int capacity()
{
return _end - _first;
}
value_type operator[](int index)
{
return *(_first + index);
}
private:
A _allocator; //空间配置器
iterator _first;
iterator _last;
iterator _end;
};
int main()
{
Vector a;
a.push_back(1);
a.reserve(5);
a.push_back(2);
a.push_back(4);
cout << a[0];
cout << a[2];
cout << a.size()<::iterator b=a.begin();
for (; b != a.end(); b++)
{
cout << *b<<" ";
}
a.insert(b,1);
for (; b != a.end(); b++)
{
cout << *b << " ";
}
a.erase(b);
cout << a.size() << endl;
Vector::reverse_iterator it = a.rbegin();
cout << *it< c;
//Vector::const_reverse_iterator it2 = c.rbegin();
Vector::const_iterator it4= a.begin();
Vector::const_reverse_iterator it2 = c.rbegin();
for (; it2 != c.rend(); it2++)
{
cout << *it2 << " ";
}
return 0;
}
完整版的代码空间配置器内存池实现
///*具体只有内存分配代码 类似于c++的newhander 部分代码没有列出*/
//stl空间配置器的内存池模型
#pragma once
#include
#include
#include
using namespace std;
class _MallocAllocTemplate//定义一级空间配置器申请大内存直接调用这个配置器;对于
{
public:
static void* Allocate(size_t n);
static void Deallocate(void *p, size_t size);
static void *Reallocate(void *p, size_t old_size, size_t newsize);
};
void* _MallocAllocTemplate::Allocate(size_t n)
{
void *ret = malloc(n);
if (ret == NULL)
{
cout << "malloc fail"<< endl;
}
return ret;
}
void _MallocAllocTemplate::Deallocate(void *p,size_t size)
{
free(p);
}
void* _MallocAllocTemplate::Reallocate(void *p, size_t old_size, size_t newsize)
{
void *ret = realloc(p,newsize);
if (ret == NULL)
{
cout << "realloc fail" << endl;
}
return ret;
}
enum { __ALIGN = 8 }; //小区块的上调边界(8字节对齐)
enum { __MAX_BYTES = 128 }; // 小区块的最大值,上限
enum { __NUM_FREE_LIST = __MAX_BYTES / __ALIGN }; //free list个数为128/8=16 8字节对齐所以需要16个指正
template//空间配置器
class __DefaultAllocTemplate
{
private :
union Obj
{
Obj* freeListLink;
};//采用这样的结构可以让该指正的next和该指正作为一个使用减少了变量的产生;
static char* startFree;
static char* endFree;
static size_t heapSize;
static Obj* FreeList[__NUM_FREE_LIST];
static size_t RoundUp(size_t bytes)//对所申请的内存进行8字节对齐
{
return(bytes + __ALIGN - 1) & ~(__ALIGN - 1);
}
static size_t FreeListIndex(size_t bytes)//算出大小当前的下标
{
return ((bytes + __ALIGN - 1) / __ALIGN) - 1;
}
static void*ChunAlloc(size_t size, size_t& nobjs)
{
size_t bytesLeft = endFree - startFree; //内存池剩余空间
size_t totalBytes = size * nobjs;
char* ret = NULL;
if (bytesLeft >= totalBytes) // 内存池大小足够分配nobjs个对象大小
{
ret = startFree;
startFree += totalBytes;//需要分配的内存
return ret;
}
else if (bytesLeft >= size) // 内存池大小不够分配nobjs,但是至少分配一个
{
size_t nobjs = bytesLeft / size;//如果不够算出能可以开几个几个
totalBytes = size * nobjs;
ret = startFree;
startFree += totalBytes;
return ret; //返回开辟指正
}
else // 内存池一个都分配不了 对第一次进行内存池划分对start和end进行初始haul
{
//让内存池剩余的那么点挂在freelist上
if (bytesLeft > 0)
{
size_t index = FreeListIndex(bytesLeft);//算出可挂载该内存的下标中的freelist
((Obj*)startFree)->freeListLink = FreeList[index]; //直接让当前内存块指向该下表的空白内存
FreeList[index] = (Obj*)startFree;//然后让该内存放当前内存的地址;
}
size_t bytesToGet = 2 * totalBytes + RoundUp(heapSize>>4);//
startFree = (char*)malloc(bytesToGet);//直接malloc开
if (startFree == NULL)//开辟失败就找-
{
//申请失败,此时试着在自由链表中找
for (size_t i = size; i <= __MAX_BYTES; i += __ALIGN) //对于存储的每个所对应得下表进行访问查找
{
size_t index = FreeListIndex(i);//找出下表具体都是比自己打的
Obj** myFreeList = FreeList + index;//遍历每一条freelist去查找
Obj* p = *myFreeList;//p指向当前链表
if (FreeList[index] != NULL)//判断当前是否还可以插入
{
FreeList[index] = p->freeListLink;
startFree = (char*)p;//找到当前的点并且将startfree改为当前找到的地址startend改为丁当前的end在调用给他分配内存
endFree = startFree + i;
return ChunAlloc(size, nobjs);
}
}
endFree = NULL;
//试着调用一级空间配置器
startFree = (char*)_MallocAllocTemplate::Allocate(bytesToGet);
}
heapSize += bytesToGet;
endFree = startFree + bytesToGet;
return ChunAlloc(size, nobjs);//再去找
}
}
static void* Refill(size_t n)
{
size_t nobjs = 10;
char *chunk = (char*)ChunAlloc(n, nobjs);
if (nobjs == 1)
return chunk;
Obj *ret = (Obj*)chunk;
Obj* cur = (Obj*)chunk;
Obj* next = (Obj*)((char*)cur+n);
Obj* volatile *myFreeList = FreeList + FreeListIndex(n);//获取到当前的下标的的freelist链表
*myFreeList = cur;//将当前下标的下一块内存放到当前下标中
for (size_t i=1;ifreeListLink = next;
cur = next;
next = (Obj*)((char*)next + n);
}
cur->freeListLink = NULL;
return ret;
}
public:
static void* Allocate(size_t n)
{
if (n > 128)
{
return _MallocAllocTemplate::Allocate(n);
}
Obj* volatile* myFreeList = FreeList + FreeListIndex(n); //定位下标 用二级指针指向这块内存
Obj* ret = *myFreeList;
if (ret == NULL)
{
void* r = Refill(RoundUp(n));//没有可用free list 准备装填
return r;
}
*myFreeList = ret->freeListLink;//改变一级指正的位置让里面放下一个空闲空间
return ret;
}
static void Deallocate(void* p, size_t n)
{
if (n > (size_t)__MAX_BYTES)
{
_MallocAllocTemplate::Deallocate(p, n);
return;
}
Obj* volatile* myFreeList = FreeList + FreeListIndex(n);
Obj* q = (Obj*)p;
q->freeListLink = *myFreeList;
*myFreeList = q;
}
};
template
char* __DefaultAllocTemplate ::startFree = NULL;
template
char* __DefaultAllocTemplate::endFree = NULL;
template
size_t __DefaultAllocTemplate::heapSize = 0;
template
typename __DefaultAllocTemplate::Obj* __DefaultAllocTemplate::FreeList[__NUM_FREE_LIST];//告诉他是个类型参数
template>
class myalloctor
{
public:
typedef _Ty *pointer;
typedef const _Ty *const_pointer;
typedef _Ty& reference;
typedef const _Ty& const_reference;
typedef _Ty value_type;
void* operator new(size_t size)
{
return Alloc::Allocate(size);
}
void operator delete(void *p,size_t n)
{
if (p == NULL)return;
return Alloc::Deallocate(p,n);
}
pointer _allocate(int _N, const void *)
{
return (pointer)operator new(_N*sizeof(_Ty)); //给定位置开辟空间返回开辟地点的指针//重载运算符new具体代码在上面
}
//构造对象
void construct(pointer _P, const _Ty& _V)//在指定的空间上构造对象
{
new (_P)_Ty(_V);
}
//析构对象
void destroy(pointer _P)//析构对象
{
_P->~_Ty();
}
//释放内存
void deallocate(void *_P, size_t n)//释放内存
{
operator delete(_P,n);
}
};
template
class _reverse_iterator
{
public:
_reverse_iterator(Iterator it = Iterator()) :_it(it)
{
}
_reverse_iterator(const _reverse_iterator &src)
{
_it = src._it;
}
_reverse_iterator(Iterator &src)
{
_it = src;
}
_reverse_iterator operator+(int t2)
{
return _reverse_iterator(_it + t2);
}
_reverse_iterator operator-(int t2)
{
return _reverse_iterator(_it - t2);
}
_reverse_iterator& operator --(int)
{
_it++;
return *this;
}
_reverse_iterator& operator ++(int)
{
_it--;
return *this;
}
bool operator!=(const _reverse_iterator &src)
{
return _it != src._it;
}
bool operator!=(const _reverse_iterator &src)const
{
return _it != src._it;
}
T operator*()
{
return *_it;
}
int operator-(const _reverse_iterator &it)
{
return it - _it;
}
private:
Iterator _it;
};
template>
class Vector
{
public:
class iterator;
class const_iterator;
typedef T value_type;
typedef _reverse_iterator reverse_iterator;
typedef _reverse_iterator const_reverse_iterator;
explicit Vector(const A& _Al = A()) :_allocator(_Al), _first(0), _last(0), _end(0){}//初始化空间配置器 last 最后一个元素end容器的最底端 first
~Vector()
{
iterator it = _first;
for (; it != _end; it++)
{
_allocator.destroy(&*it);//空间配置析构;
}
_allocator.deallocate(&*_first, capacity());//delete空间配置器
}
void push_back(const value_type &val)
{
if (_last == _end)//刚开始的操作
{
if (_first == _end)
{
reserve(1);//first==end 初始化vector为一个大小然后以二倍增长
}
else
{
reserve(2 * size());
}
_allocator.construct(&*_last, val);//构造对象
_last++;
}
else
{
_allocator.construct(&*_last, val);//不满直接构造对象
_last++;
}
}
void pop_back()
{
if (empty())
{
return;
}
_allocator.destroy(&*_last);//不为空析构对象并将空间释放归还到内存池上
_last--;
}
bool empty()const
{
return _first == _last;
}
int size()
{
return _last - _first;
}
bool full()
{
return _last == _end;
}
iterator insert(iterator it1, const value_type &val)
{
if (full())
{
reserve(2 * size());
}
iterator it = _last;
while (it != it1)
{
*it = *(it - 1);
it--;
}
*it = val;
_last++;
return it;
}
iterator erase(iterator it)
{
iterator start = it;
for (; start < _last - 1; start++)
{
*start = *start + 1;
}
_allocator.destroy(&*_last);//析构掉该对象释放空间返回给内存池
_last--;
return it;
}
void reserve(int count)
{
if (capacity() < count)//返回总共的从vector最大的到元素所拥有的空间判断是否足够如果不够释放原来了vector的空间重新开辟并再上面构造新的对象;足够的话直接返回成功;
{
T *ptr = NULL;
int _size = size();
ptr = _allocator._allocate(count + size(), ptr);
T *s = ptr;
iterator it = _first;
for (; it != _last; it++, ptr++)
{
_allocator.construct(ptr, *it);
}
it = _first;
for (; it != _end; it++)
{
_allocator.destroy(&*it);
}
_allocator.deallocate(&*_first, capacity());
iterator tmp1(s);
_first = tmp1;
_last = tmp1 + _size;
_end = tmp1 + count;
}
}
void resize(int _newsize)
{
int sizes = _newsize;
if (_newsize > size())
{
reserve(_newsize);//进行空间调整
}
iterator it = _last;//_last=_first刚开始?有歧义这样邪恶
for (; it != _end; it++)//进行初始化
{
_allocator.construct(&*it, 0);
}
_last = _end;
}
class const_iterator
{
public:
const_iterator(T *ptr = NULL) :_ptr(ptr)
{
}
const_iterator(const const_iterator &src)
{
_ptr = src._ptr;
}
const_iterator(const iterator &src)
{
_ptr = src._ptr;
}
const T operator*()const
{
return *_ptr;
}
value_type *operator->()const
{
return _ptr;
}
bool operator<(const const_iterator &src)const
{
if (_ptr()
{
return const_iterator::_ptr;
}
int operator-(const iterator &it) const
{
return const_iterator::_ptr - it._ptr;
}
const_iterator operator-(int t2)const
{
return const_iterator::_ptr - t2;
}
bool operator!=(const iterator &src)
{
return const_iterator::_ptr != src._ptr;
}
};
iterator begin()
{
return (iterator(_first));
}
iterator end()
{
return (iterator(_last));
}
const_iterator begin() const
{
return (const_iterator(_first));
}
const_iterator end() const
{
return (const_iterator(_last));
}
reverse_iterator rbegin()
{
return reverse_iterator(_last - 1);
}
reverse_iterator rend()
{
return reverse_iterator(_first - 1);
}
const_reverse_iterator rbegin()const
{
return const_iterator(_last - 1);
}
const_reverse_iterator rend()const
{
return const_reverse_iterator(const_iterator(_first - 1));
}
int capacity()
{
return _end - _first;
}
value_type operator[](int index)
{
iterator p = _first;
return *(_first + index);
}
private:
A _allocator; //空间配置器
iterator _first;
iterator _last;
iterator _end;
};
int main()
{
Vector a;
a.push_back(1);
a.reserve(5);
a.push_back(2);
a.push_back(4);
//cout << a[0] << endl;
printf("%d\n",a[0]);
cout << a[2];
cout << a.size() << endl;
a.pop_back();
cout << a.size() << endl;
Vector::iterator b = a.begin();
for (; b != a.end(); b++)
{
cout << *b << " ";
}
a.insert(b, 1);
for (; b != a.end(); b++)
{
cout << *b << " ";
}
a.erase(b);
cout << a.size() << endl;
Vector::reverse_iterator it = a.rbegin();
cout << *it << endl;
cout << a.size();
for (; it != a.rend(); it++)
{
cout << *it << " ";
}
cout << endl;
const Vector c;
//Vector::const_reverse_iterator it2 = c.rbegin();
Vector::const_iterator it4 = a.begin();
Vector::const_reverse_iterator it2 = c.rbegin();
for (; it2 != c.rend(); it2++)
{
cout << *it2 << " ";
}
return 0;
}