CXXR (C++ R) API
VectorBase.h
Go to the documentation of this file.
1 /*CXXR $Id: VectorBase.h 1351 2013-03-08 15:12:28Z 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-13 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  * Copyright (C) 1995, 1996 Robert Gentleman and Ross Ihaka
20  * Copyright (C) 1999-2006 The R Development Core Team.
21  * Andrew Runnalls (C) 2008
22  *
23  * This program is free software; you can redistribute it and/or modify
24  * it under the terms of the GNU General Public License as published by
25  * the Free Software Foundation; either version 2.1 of the License, or
26  * (at your option) any later version.
27  *
28  * This program is distributed in the hope that it will be useful,
29  * but WITHOUT ANY WARRANTY; without even the implied warranty of
30  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31  * GNU Lesser General Public License for more details.
32  *
33  * You should have received a copy of the GNU General Public License
34  * along with this program; if not, a copy is available at
35  * http://www.r-project.org/Licenses/
36  */
37 
42 #ifndef VECTORBASE_H
43 #define VECTORBASE_H
44 
45 #include "CXXR/RObject.h"
46 
47 /* type for length of vectors etc */
48 typedef int R_len_t; /* will be long later, LONG64 or ssize_t on Win64 */
49 
50 #define R_LEN_T_MAX INT_MAX
51 
52 #ifdef __cplusplus
53 
54 #include <boost/serialization/access.hpp>
55 #include <boost/serialization/base_object.hpp>
56 #include <boost/serialization/nvp.hpp>
57 
58 #include "CXXR/ElementTraits.hpp"
59 #include "CXXR/GCStackRoot.hpp"
60 #include "CXXR/SEXP_downcast.hpp"
61 
62 namespace CXXR {
63  class String;
64  template <typename, SEXPTYPE,
65  typename Initializer = RObject::DoNothing> class FixedVector;
66  typedef FixedVector<int, INTSXP> IntVector;
67  typedef FixedVector<RHandle<>, VECSXP> ListVector;
68  typedef FixedVector<RHandle<String>, STRSXP> StringVector;
69 
72  class VectorBase : public RObject {
73  public:
78  VectorBase(SEXPTYPE stype, std::size_t sz)
79  : RObject(stype), m_truelength(sz), m_size(sz)
80  {}
81 
86  VectorBase(const VectorBase& pattern)
87  : RObject(pattern), m_truelength(pattern.m_truelength),
88  m_size(pattern.m_size)
89  {}
90 
109  const ListVector* dimensionNames() const;
110 
126  const StringVector* dimensionNames(unsigned int d) const;
127 
138  const IntVector* dimensions() const;
139 
146  const StringVector* names() const;
147 
170  template <class V>
171  static V* resize(const V* pattern, std::size_t new_size);
172 
191  std::size_t new_size);
192 
215 
234  void setDimensionNames(unsigned int d, StringVector* names);
235 
254  void setDimensions(IntVector* dims);
255 
265  void setNames(StringVector* names);
266 
276  virtual void setSize(std::size_t new_size);
277 
282  std::size_t size() const
283  {
284  return m_size;
285  }
286 
291  static const char* staticTypeName()
292  {
293  return "(vector type)";
294  }
295 
296  // Virtual function of RObject, redeclared for covariant
297  // return type:
298  VectorBase* clone() const
299  {
300  return 0;
301  }
302 
303  // Make private in due course (or get rid altogether):
304  R_len_t m_truelength;
305  protected:
306  ~VectorBase() {}
307 
315  void adjustSize(std::size_t new_size)
316  {
317  m_size = new_size;
319  }
320 
321  private:
322  friend class boost::serialization::access;
323 
324  size_t m_size;
325 
326  // m_size will always be passed in by the constructor, and so
327  // is not serialised.
328  template<class Archive>
329  void serialize(Archive & ar, const unsigned int version)
330  {
331  ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(RObject);
332  ar & BOOST_SERIALIZATION_NVP(m_truelength);
333  }
334  };
335 
336  template <class V>
337  V* VectorBase::resize(const V* pattern, std::size_t new_size)
338  {
339  GCStackRoot<V> ans(CXXR_NEW(V(new_size)));
340  std::size_t copysz = std::min(pattern->size(), new_size);
341  typename V::const_iterator patb = pattern->begin();
342  typename V::iterator ansit
343  = std::copy(patb, patb + copysz, ans->begin());
344  std::fill(ansit, ans->end(), NA<typename V::value_type>());
345  ans->setAttributes(resizeAttributes(pattern->attributes(), new_size));
346  ans->setS4Object(pattern->isS4Object());
347  return ans;
348  }
349 } // namespace CXXR
350 
351 extern "C" {
352 
353 #endif /* __cplusplus */
354 
355  /* Accessor functions */
356 
357  /* Vector Access Functions */
358 
366 #ifndef __cplusplus
367  int LENGTH(SEXP x);
368 #else
369  inline int LENGTH(SEXP x)
370  {
371  using namespace CXXR;
372  if (!x) return 0;
373  VectorBase& vb = *SEXP_downcast<VectorBase*>(x);
374  return vb.size();
375  }
376 #endif
377 
388 #ifndef __cplusplus
389  int TRUELENGTH(SEXP x);
390 #else
391  inline int TRUELENGTH(SEXP x)
392  {
393  using namespace CXXR;
394  VectorBase& vb = *SEXP_downcast<VectorBase*>(x);
395  return vb.m_truelength;
396  }
397 #endif
398 
410  void SETLENGTH(SEXP x, int v);
411 
421 #ifndef __cplusplus
422  void SET_TRUELENGTH(SEXP x, int v);
423 #else
424  inline void SET_TRUELENGTH(SEXP x, int v)
425  {
426  using namespace CXXR;
427  VectorBase& vb = *SEXP_downcast<VectorBase*>(x);
428  vb.m_truelength = v;
429  }
430 #endif
431 
446  SEXP Rf_allocVector(SEXPTYPE stype, R_len_t length);
447 
457  Rboolean Rf_isVector(SEXP s);
458 
459 #ifdef __cplusplus
460 }
461 #endif
462 
463 #endif /* VECTORBASE_H */