dsalglib  1.0.0
dsalglib is a ready to use data structures and algorithms library written in C++ . Object Oriented Template implementations are written.
array.h
Go to the documentation of this file.
1 //
2 // Created by moghya_s on 10/1/2016.
3 //
4 
5 #ifndef DSALGLIB_ARRAY_H
6 #define DSALGLIB_ARRAY_H
7 namespace dsa
8 {
9  template<class type>
10  class array
11  {
12  private:
13  static const long long int spare = 5;
14  long long int count,capacity;
15  type *objs;
16  public:
17  array(long long int size=0)
18  {
19  count = size;
20  capacity=size+spare;
21  objs = new type[capacity];
22  }
23 
24  array(long long int size,type param)
25  {
26  count = size;
27  capacity=size+spare;
28  objs = new type[capacity];
29  long long int i;
30  for(i=0;i<count;i++)
31  objs[i]=param;
32  }
33  void resize(long long int newsize)
34  {
35  if(newsize<capacity) return ;
36 
37  type *old = objs;
38  objs = new type[newsize];
39 
40  long long int temp;
41  for(temp=0;temp<count;temp++)
42  objs[temp] = old[temp];
43 
44  if(capacity!=0)
45  delete [] old;
46  capacity = newsize;
47  return;
48  }
49 
50  type &operator[](long long int index)
51  {
52  if(index>=0&&index<count)
53  {
54  return objs[index];
55  }
56  }
57 
59  {
60  clear();
61  long long int i,j;
62  j=from.size();
63  for(i=0;i<j;i++)
64  push_back(from[i]);
65  return;
66  }
67 
68 
69  void push_back(type param)
70  {
71  if(count==capacity)
72  resize(capacity+spare);
73  count++;
74  objs[count-1]=param;
75  return;
76  }
77 
78  void pop_back()
79  {
80  type temp = objs[count-1];
81  count--;
82  return ;
83  }
84 
85  void reverse(long long int start,long long int end)
86  {
87  if(start>=0&&start<count&&end>=0&&end<count)
88  {
89  while(start<end)
90  {
91  swapit(objs[start],objs[end]);
92  start++;
93  end--;
94  }
95  }
96  }
97 
98  long long int size()
99  {
100  return count;
101  }
102 
103  void traverse(void (fun)(type obj))
104  {
105  long long int i;
106  for(i=0;i<count;i++)
107  fun(objs[i]);
108  }
109 
110  bool isempty()
111  {
112  if(count==0) return true; return false;
113  }
114 
115  void clear()
116  {
117  type *old = objs;
118  if(capacity!=0)
119  delete [] old;
120  count = 0;
121  capacity = spare;
122  objs = new type[capacity];
123  }
124  };
125 }
126 
127 #endif //DSALGLIB_ARRAY_H
void swapit(T &x, T &y)
Definition: alginc.h:17
void resize(long long int newsize)
Definition: array.h:33
Definition: alginc.h:12
array(long long int size, type param)
Definition: array.h:24
array(long long int size=0)
Definition: array.h:17
void pop_back()
Definition: array.h:78
void operator=(array< type > from)
Definition: array.h:58
bool isempty()
Definition: array.h:110
type & operator[](long long int index)
Definition: array.h:50
void reverse(long long int start, long long int end)
Definition: array.h:85
void clear()
Definition: array.h:115
long long int size()
Definition: array.h:98
void traverse(void(fun)(type obj))
Definition: array.h:103
void push_back(type param)
Definition: array.h:69