Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 6th, 2013  |  syntax: C++  |  size: 6.74 KB  |  views: 2,045  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.     Parsnip Parser Libary
  3.         http://parsnip-parser.sf.net
  4.         Copyright 2007 Alex Rubinsteyn
  5.         -----------------------------------------------------------------------
  6.         This file is part of Parsnip.
  7.  
  8.     Parsnip is free software: you can redistribute it and/or modify
  9.     it under the terms of the GNU General Public License as published by
  10.     the Free Software Foundation, either version 3 of the License, or
  11.     (at your option) any later version.
  12.  
  13.     Parsnip is distributed in the hope that it will be useful,
  14.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.     GNU General Public License for more details.
  17.  
  18.     You should have received a copy of the GNU General Public License
  19.     along with Parsnip.  If not, see <http://www.gnu.org/licenses/>.
  20. */
  21.  
  22.  
  23. /*
  24.         July 25th: renamed to Vector
  25.  
  26.         Modified by Alex Rubinsteyn (alex.rubinsteyn@gmail.com)
  27.         on May 16th, 2007 for use in the Parsnip parser
  28.         combinator library.
  29.                 - changed size_t and int to unsigned
  30.                 - removed assertions
  31.                 - dropped inclusion of std namespace
  32.                 - wrapped vec in Parsnip namespace
  33.                 - added operator+ for combining vecs
  34. */
  35.  
  36.  
  37. /*   _______              __
  38.     / ___/ /  ___  __ _  / /  ___
  39.    / /__/ _ \/ _ \/  V \/ _ \/ _ \
  40.    \___/_//_/\___/_/_/_/_.__/\___/
  41. */
  42. // CHOMBO Copyright (c) 2000-2004, The Regents of the University of
  43. // California, through Lawrence Berkeley National Laboratory (subject to
  44. // receipt of any required approvals from U.S. Dept. of Energy).  All
  45. // rights reserved.
  46. //
  47. // Redistribution and use in source and binary forms, with or without
  48. // modification, are permitted provided that the following conditions are met:
  49. //
  50. // (1) Redistributions of source code must retain the above copyright
  51. // notice, this list of conditions and the following disclaimer.
  52. // (2) Redistributions in binary form must reproduce the above copyright
  53. // notice, this list of conditions and the following disclaimer in the
  54. // documentation and/or other materials provided with the distribution.
  55. // (3) Neither the name of Lawrence Berkeley National Laboratory, U.S.
  56. // Dept. of Energy nor the names of its contributors may be used to endorse
  57. // or promote products derived from this software without specific prior
  58. // written permission.
  59. //
  60. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  61. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  62. // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  63. // PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  64. // OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  65. // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  66. // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  67. // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  68. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  69. // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  70. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  71. //
  72. // You are under no obligation whatsoever to provide any bug fixes,
  73. // patches, or upgrades to the features, functionality or performance of
  74. // the source code ("Enhancements") to anyone; however, if you choose to
  75. // make your Enhancements available either publicly, or directly to
  76. // Lawrence Berkeley National Laboratory, without imposing a separate
  77. // written license agreement for such Enhancements, then you hereby grant
  78. // the following license: a non-exclusive, royalty-free perpetual license
  79. // to install, use, modify, prepare derivative works, incorporate unsignedo
  80. // other computer software, distribute, and sublicense such Enhancements or
  81. // derivative works thereof, in binary and source code form.
  82. //
  83. // TRADEMARKS. Product and company names mentioned herein may be the
  84. // trademarks of their respective owners.  Any rights not expressly granted
  85. // herein are reserved.
  86. //
  87.  
  88. #ifndef CHOMBO_TUPLE_H
  89. #define CHOMBO_TUPLE_H
  90.  
  91. namespace Parsnip
  92. {
  93.  
  94. //
  95. /// Ordered Vectors for Types T
  96. /**
  97.  
  98.   This class represents ordered vecs of some user-specified concrete
  99.   type T for N > 0. The type T must have a default constructor.  If the
  100.   non-default constructor, copy constructor, or copy assignment operator
  101.   are used, T must also have a copy constructor.
  102. */
  103.  
  104. template <class T, unsigned N>
  105. class Vector
  106. {
  107. public:
  108.  
  109.     /**: The default constructor.  For user-defined types T, the
  110.                default constructor for T will be run on each of the N
  111.                objects in the Vector.  For builtin (intrinsic) types,
  112.                the values in the Vector will be garbage.
  113.     */
  114.     Vector ();
  115.  
  116.     /**: Constructs a Vector, initializing the elements in the Vector
  117.                with the corresponding elements in the vector v.  This assumes
  118.                that v contains at least N elements of type T -- an assumption
  119.                that is NOT checked.  For user-defined types, T must have a
  120.                well-defined and accessible copy constructor.
  121.     */
  122.   //explicit Vector (const T* v);
  123.     //
  124.     // The copy constructor.
  125.     //
  126.     Vector (const Vector& rhs);
  127.     //
  128.     // The copy assignment operator.
  129.     //
  130.     Vector& operator= (const Vector& rhs);
  131.  
  132.     /**: Returns a modifiable lvalue reference to the i'th
  133.                element in the Vector, counting from zero.    */
  134.     T& operator[] (unsigned i);
  135.  
  136.     /**: Returns a constant reference to the i'th element in the Vector,
  137.                counting from zero.  
  138.     */
  139.     const T& operator[] (unsigned i) const;
  140.  
  141.  
  142. protected:
  143.     //
  144.     // The underlying vector of T representing the Vector.
  145.     //
  146.     T vect[N];
  147. };
  148.  
  149. //
  150. // Inlines.
  151. //
  152.  
  153. template <class T, unsigned N>
  154. inline
  155. Vector<T,N>::Vector()
  156. {}
  157.  
  158. template <class T, unsigned N>
  159. inline
  160. T&
  161. Vector<T,N>::operator[] (unsigned i)
  162. {
  163.     return vect[i];
  164. }
  165.  
  166. template <class T, unsigned N>
  167. inline
  168. const T&
  169. Vector<T,N>::operator[] (unsigned i) const
  170. {
  171.     return vect[i];
  172. }
  173.  
  174.  
  175. template <class T, unsigned N>
  176. Vector<T,N>::Vector (const Vector<T,N>& rhs)
  177. {
  178.     for (unsigned i = 0; i < N; ++i)
  179.         vect[i] = rhs.vect[i];
  180. }
  181.  
  182. template <class T, unsigned N>
  183. Vector<T,N>&
  184. Vector<T,N>::operator= (const Vector<T,N>& rhs)
  185. {
  186.     for (unsigned i = 0; i < N; ++i)
  187.         vect[i] = rhs.vect[i];
  188.     return *this;
  189. }
  190.  
  191. /*
  192.         combines two vecs of size N1 and N2
  193.         into one vec of size N1 + N2
  194. */
  195. template <class T, unsigned N1, unsigned N2>
  196. Vector<T, N1+N2> operator+ (Vector<T, N1> vec1, Vector<T, N2> vec2)
  197. {
  198.         Vector<T, N1 + N2> result;
  199.         for (unsigned i = 0; i < N1; i++)
  200.                 result[i] = vec1[i];
  201.        
  202.         for (unsigned i = N1; i < N2; ++i)
  203.                 result[i] = vec2[i];
  204.        
  205.         return result;
  206. }
  207.  
  208.  
  209.  
  210. }
  211.  
  212. #endif
clone this paste RAW Paste Data