File: RefVec.hpp

package info (click to toggle)
freemat 4.0-5
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 174,736 kB
  • ctags: 67,053
  • sloc: cpp: 351,060; ansic: 255,892; sh: 40,590; makefile: 4,323; perl: 4,058; asm: 3,313; pascal: 2,718; fortran: 1,722; ada: 1,681; ml: 1,360; cs: 879; csh: 795; python: 430; sed: 162; lisp: 160; awk: 5
file content (186 lines) | stat: -rw-r--r-- 4,369 bytes parent folder | download | duplicates (4)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (c) 2009 Samit Basu
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
#ifndef __RefVec_hpp__
#define __RefVec_hpp__

#include <vector>
#include <iostream>
#include "Exception.hpp"
using namespace std;

template <class T>
class RefVec;

template <class T>
class RefVecNode {
private:
  vector<T> data;
  int count;
  friend class RefVec<T>;
  RefVecNode() : count(0) {}
};

template <class T>
class RefVec {
  RefVecNode<T> *p;
  inline void Release() {
    if (p) {
      p->count--;
      if (p->count == -1) {
	cout << "ERROR\n";
      }	
      if (p->count <=0) 
	delete p;
    }
  }
  inline void Allocate() {
    p = new RefVecNode<T>;
    p->count++;
  }
  inline void Duplicate() {
    RefVecNode<T> *q = new RefVecNode<T>;
    q->data = p->data;
    q->count = 1;
    Release();
    p = q;
  }
public:
  inline RefVec() {
    p = NULL;
  }
  inline RefVec(int cnt) {
    Allocate();
    p->data = std::vector<T>(cnt);
  }
  inline RefVec(const RefVec& copy) {
    p = copy.p;
    if (p) p->count++;
  }
  inline ~RefVec() {
    Release();
  }
  inline void operator=(const RefVec& copy) {
    Release();
    p = copy.p;
    if (p) p->count++;
  }
  inline bool operator==(const RefVec& copy) {
    if ((!p) && (!copy.p)) return true;
    if ((!p) || (!copy.p)) return false;
    return (p->data == copy.p->data);
  }
  inline unsigned size() const {
    if (!p) return 0;
    return p->data.size();
  }
  inline int begin() {
    return 0;
  }
  inline void push_front(const T& x) {
    if (!p) Allocate();
    if (p->count > 1) Duplicate();
    p->data.insert(p->data.begin(),x);
  }
  inline void append(const RefVec<T>& x) {
    for (unsigned p=0;p<x.size();p++)
      push_back(x[p]);
  }
  inline void operator+=(const RefVec<T>& x) {
    append(x);
  }
  inline void push_back(const T& x) {
    if (!p) Allocate();
    if (p->count > 1) Duplicate();
    p->data.push_back(x);
  }
  inline void pop_back() {
    if (p) {
      if (p->count > 1) Duplicate();
      p->data.pop_back();
    }
  }
  inline void pop_front() {
    if (p) {
      if (p->count > 1) Duplicate();
      p->data.erase(p->data.begin());
    }
  }
  inline void clear() {
    if (p) {
      if (p->count > 1) Duplicate();
      p->data.clear();
    }
  }
  inline bool empty() const {
    if (!p) return true;
    return p->data.empty();
  }
  inline T& back() {
    if (!p)
      throw Exception("Illegal internal array access!");
    if (p->count > 1) Duplicate();
    return p->data.back();  
  }
  inline T& front() {
    if (!p)
      throw Exception("Illegal internal array access!");
    if (p->count > 1) Duplicate();
    return p->data.front();  
  }
  inline T& operator[] (unsigned n) {
    if (!p)
      throw Exception("Illegal internal array access!");
    if (p->count > 1) Duplicate();
    return p->data[n];
  }
  inline T& at(unsigned n) {
    if (!p)
      throw Exception("Illegal internal array access!");
    if (p->count > 1) Duplicate();
    return p->data.at(n);
  }
  inline void erase(unsigned n) {
    if (p) {
      if (p->count > 1) Duplicate();
      p->data.erase(p->data.begin()+n);
    }
  }
  inline const T& back() const {
    if (!p)
      throw Exception("Illegal internal array access!");
    return p->data.back();  
  }
  inline const T& front() const {
    if (!p)
      throw Exception("Illegal internal array access!");
    return p->data.front();  
  }
  inline const T& operator[] (unsigned n) const {
    if (!p)
      throw Exception("Illegal internal array access!");
    return p->data[n];
  }
  inline const T& at(unsigned n) const {
    if (!p)
      throw Exception("Illegal internal array access!");
    return p->data.at(n);
  }
};

#endif