CXXR (C++ R) API
DumbVector.hpp
Go to the documentation of this file.
1 /*CXXR $Id: DumbVector.hpp 1030 2011-01-04 14:00:23Z arr $
2  *CXXR
3  *CXXR This file is part of CXXR, a project to refactor the R interpreter
4  *CXXR into C++. It may consist in whole or in part of program code and
5  *CXXR documentation taken from the R project itself, incorporated into
6  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
7  *CXXR Licence.
8  *CXXR
9  *CXXR CXXR is Copyright (C) 2008-11 Andrew R. Runnalls, subject to such other
10  *CXXR copyrights and copyright restrictions as may be stated below.
11  *CXXR
12  *CXXR CXXR is not part of the R project, and bugs and other issues should
13  *CXXR not be reported via r-bugs or other R project channels; instead refer
14  *CXXR to the CXXR website.
15  *CXXR */
16 
17 /*
18  * R : A Computer Language for Statistical Data Analysis
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2.1 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, a copy is available at
32  * http://www.r-project.org/Licenses/
33  */
34 
40 #ifndef DUMBVECTOR_HPP
41 #define DUMBVECTOR_HPP 1
42 
43 #include <cstring>
44 #include "localization.h"
45 #include "R_ext/Error.h"
46 #include "CXXR/GCRoot.h"
47 #include "CXXR/VectorBase.h"
48 
49 namespace CXXR {
59  template <typename T, SEXPTYPE ST>
60  class DumbVector : public VectorBase {
61  public:
67  DumbVector(size_t sz)
68  : VectorBase(ST, sz), m_data(&m_singleton)
69  {
70  if (sz > 1)
71  allocData(sz);
72  }
73 
81  DumbVector(size_t sz, const T& initializer)
82  : VectorBase(ST, sz), m_data(&m_singleton),
83  m_singleton(initializer)
84  {
85  if (sz > 1) allocData(sz, true);
86  }
87 
92  DumbVector(const DumbVector<T, ST>& pattern);
93 
99  T& operator[](unsigned int index)
100  {
101  return m_data[index];
102  }
103 
109  const T& operator[](unsigned int index) const
110  {
111  return m_data[index];
112  }
113 
123  inline static const char* staticTypeName();
124 
125  // Virtual functions of RObject:
126  DumbVector<T, ST>* clone() const;
127  const char* typeName() const;
128  protected:
134  {
135  if (m_data != &m_singleton)
136  MemoryBank::deallocate(m_data, size()*sizeof(T));
137  }
138  private:
139  T* m_data; // pointer to the vector's data block.
140 
141  // If there is only one element, it is stored here, internally
142  // to the DumbVector object, rather than via a separate
143  // allocation from CXXR::MemoryBank. We put this last, so that it
144  // will be adjacent to any trailing redzone.
145  T m_singleton;
146 
147  // Not implemented yet. Declared to prevent
148  // compiler-generated versions:
149  DumbVector& operator=(const DumbVector&);
150 
151  // If there is more than one element, this function is used to
152  // allocate the required memory block from CXXR::MemoryBank :
153  void allocData(size_t sz, bool initialize = false);
154  };
155 
156  template <typename T, SEXPTYPE ST>
158  : VectorBase(pattern), m_data(&m_singleton),
159  m_singleton(pattern.m_singleton)
160  {
161  size_t sz = size();
162  if (sz > 1) {
163  allocData(sz);
164  memcpy(m_data, pattern.m_data, sizeof(T)*sz);
165  }
166  }
167 
168  template <typename T, SEXPTYPE ST>
169  void DumbVector<T, ST>::allocData(size_t sz, bool initialize)
170  {
171  size_t bytes = sz*sizeof(T);
172  // Check for integer overflow:
173  if (bytes/sizeof(T) != sz)
174  Rf_error(_("Request to create impossibly large vector."));
175  m_data = static_cast<T*>(MemoryBank::allocate(bytes));
176  if (initialize) {
177  for (unsigned int i = 0; i < sz; ++i)
178  m_data[i] = m_singleton;
179  }
180  }
181 
182  template <typename T, SEXPTYPE ST>
184  {
185  return expose(new DumbVector<T, ST>(*this));
186  }
187 
188  template <typename T, SEXPTYPE ST>
189  const char* DumbVector<T, ST>::typeName() const
190  {
192  }
193 } // namespace CXXR
194 
195 #endif // DUMBVECTOR_HPP