Modernized GAlib  3.0.0 current
GAArray.h
1 /* ----------------------------------------------------------------------------
2  mbwall 22apr95
3  Copyright 1995 Massachusetts Institute of Technology
4 ---------------------------------------------------------------------------- */
5 
6 #pragma once
7 
8 #include <vector>
9 
10 
28 template <class T> class GAArray
29 {
30  public:
31  explicit GAArray(unsigned int s) : a(s)
32  {
33  }
34 
35  GAArray(const GAArray<T> &orig)
36  {
37  a.clear();
38  copy(orig);
39  }
40 
41  GAArray<T> &operator=(const GAArray<T> &orig)
42  {
43  copy(orig);
44  return *this;
45  }
46 
47  virtual ~GAArray() = default;
48 
49  GAArray<T> *clone() { return new GAArray<T>(*this); }
50  const T &operator[](unsigned int i) const { return a.at(i); }
51  T &operator[](unsigned int i) { return a.at(i); }
52 
53  void copy(const GAArray<T> &orig)
54  {
55  a = std::vector(orig.a);
56  }
57 
58  void copy(const GAArray<T> &orig, unsigned int dest, unsigned int src,
59  unsigned int length)
60  {
61  for (unsigned int i = 0; i < length; i++)
62  {
63  a.at(dest + i) = orig.a.at(src + i);
64  }
65  }
66 
67  void move(unsigned int dest, unsigned int src, unsigned int length)
68  {
69  for (unsigned int i = 0; i < length; i++)
70  {
71  a.at(dest + i) = a.at(src + i);
72  }
73  }
74 
75  void swap(unsigned int i, unsigned int j)
76  {
77  auto tmp = a.at(j);
78  a.at(j) = a.at(i);
79  a.at(i) = tmp;
80  }
81 
82  int size() const { return a.size(); }
83 
84  int size(unsigned int n)
85  {
86  if (n == a.size())
87  return a.size();
88 
89  a.resize(n);
90  return a.size();
91  }
92 
93  // TODO could be removed, but currently used in GA1DArrayGenome.hpp:995
94  bool equal(const GAArray<T> &b, unsigned int dest, unsigned int src,
95  unsigned int length) const
96  {
97  for (unsigned int i = 0; i < length; i++)
98  if (a[dest + i] != b.a[src + i])
99  return false;
100  return true;
101  }
102 
103  template <class U>
104  bool operator==(const GAArray<U>& rhs) const
105  {
106  if (std::equal(a.begin(), a.end(), rhs.a.begin()))
107  {
108  return true;
109  }
110  return false;
111  }
112 
113  template <class U>
114  bool operator!=(const GAArray<U>& rhs) const
115  {
116  return !operator==(rhs);
117  }
118 
119  protected:
121  std::vector<T> a;
122 };
Array Base Class.
Definition: GAArray.h:29
std::vector< T > a
the contents of the array
Definition: GAArray.h:121