SVN / public / code / vorlesung-08 / Array.h

Revision 11340
Date
Committerhb1003
Download
// Copyright 2016, University of Freiburg,
// Chair of Algorithms and Data Structures.
// Author: Hannah Bast <bast@cs.uni-freiburg.de>.

#ifndef VORLESUNG_08_ARRAY_H_
#define VORLESUNG_08_ARRAY_H_

#include <gtest/gtest.h>

// A simple array that knows its size. Templated type: T.
template<class T>
class Array {
 public:
  // Create from given C-style array.
  Array(T* array, int n);

  // Destructor.
  ~Array();

  // Get the element with index i.
  T get(int i) const;

 private:
  // The elements of the array.
  T* _elements;

  // The number of elements.
  int _size;

  FRIEND_TEST(ArrayTest, ArrayInt);
  FRIEND_TEST(ArrayTest, ArrayFloat);
};


// A simple array that knows its size. Specialization for bool.
// For the sake of simplicity, accept at most 8 elements (bits).
template<>
class Array<bool> {
 public:
  // Create from given C-style array.
  Array(bool* array, int n);

  // Destructor not needed.
  // ~Array();

  // Get the element with index i.
  bool get(int i) const;

 private:
  // The elements of the array (at most 8).
  char _elements;

  // The number of elements.
  int _size;

  FRIEND_TEST(ArrayTest, ArrayBool);
};





// OLD NON-TEMPLATED CODE

/*

// A simple array that knows its size. Type: int.
class ArrayInt {
 public:
  // Create from given C-style array.
  ArrayInt(int* array, int n);

  // Destructor.
  ~ArrayInt();

  // Get the element with index i.
  int get(int i) const;

 private:
  // The elements of the array.
  int* _elements;

  // The number of elements.
  int _size;

  FRIEND_TEST(ArrayTest, ArrayInt);
};


// A simple array that knows its size. Type: float.
class ArrayFloat {
 public:
  // Create from given C-style array.
  ArrayFloat(float* array, int n);

  // Destructor.
  ~ArrayFloat();

  // Get the element with index i.
  float get(int i) const;

 private:
  // The elements of the array.
  float* _elements;

  // The number of elements.
  int _size;

  FRIEND_TEST(ArrayTest, ArrayFloat);
};

*/

#endif  // VORLESUNG_08_ARRAY_H_