-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmystl_alloctor.hpp
76 lines (63 loc) · 1.57 KB
/
mystl_alloctor.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#ifndef MYSTL_ALLOCTOR_HPP
#define MYSTL_ALLOCTOR_HPP
// 这是一个简单的构造器
#include "mystl_utility.hpp"
namespace mystl
{
using uint = unsigned int;
template <typename T>
class alloctor
{
private:
/* data */
using value_type = T;
public:
value_type *allocate(uint size)
{
value_type *ptr = (value_type *)operator new(sizeof(value_type) * size);
return ptr;
}
void deallocate(value_type *ptr)
{
operator delete(static_cast<void *>(ptr));
}
};
//其他公共方法
template <class T1, class T2>
void construct(T1 *p, const T2 &t)
{
new (p) T2(t);
}
template <class T>
void destory(T *p)
{
p->~T();
}
template <class Iterator>
void destory(Iterator begin, Iterator end)
{
for (; begin != end; ++begin)
{
mystl::destory(&*begin);
}
}
template <class InputIterator, class ForwardIterator>
inline ForwardIterator uninitialized_copy(InputIterator begin, InputIterator end, ForwardIterator result)
{
for (; begin != end; ++begin, ++result)
{
mystl::construct(result, *begin);
}
return result;
}
template <class ForwardIterator, class Size, class T>
inline ForwardIterator uninitialized_fill_n(ForwardIterator begin, Size num, const T &t)
{
for (uint i = 0; i < num; ++i)
{
mystl::construct(begin + i, t);
}
return begin + num;
}
}
#endif