-
Notifications
You must be signed in to change notification settings - Fork 0
/
Nobloat.h
74 lines (70 loc) · 1.36 KB
/
Nobloat.h
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
#pragma once
#include<cassert>
#include <cstddef>
#include<cstring>
template<class T> class Stack{
T*data;
std::size_t count;
std::size_t capacity;
enum {INIT=5};
public:
Stack(){
count=0;
capacity=INIT;
data=new T[INIT];
}
void push(const T &t){
if(count==capacity){
std::size_t newCapacity=2*capacity;
T*newData=new T[newCapacity];
for(size_t i=0;i<count;i++) newData[i]=data[i];
delete []data;
data=newData;
capacity=newCapacity;
}
assert(count<capacity);
data[count++]=t;
}
void pop(){
assert(count>0) ;
--count;
}
T top()const {
assert(count>0);
return data[count-1];
}
std::size_t size() const {return count;}
};
template<> class Stack<void *> {
void **data;
std::size_t count;
std::size_t capacity;
enum{INIT=5};
public:
Stack(){
count=0;
capacity=INIT;
data=new void*[INIT];
}
void push( void * const& t){
if(count==capacity){
std::size_t newCapacity=2*capacity;
void**newData=new void*[newCapacity];
for(size_t i=0;i<count;i++) newData[i]=data[i];
delete []data;
data=newData;
capacity=newCapacity;
}
assert(count<capacity);
data[count++]=t;
}
void pop(){
assert(count>0) ;
--count;
}
void * top()const {
assert(count>0);
return data[count-1];
}
std::size_t size() const {return count;}
};