summaryrefslogtreecommitdiff
path: root/ace/Array.cpp
blob: dc46705cd3426c67c2f2c53bcd0c31429a75d504 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Array.cpp
// $Id$

#if !defined (ARRAY_C)
#define ARRAY_C

#define ACE_BUILD_DLL
#include "Array.h"

#if !defined (__ACE_INLINE__)
#include "Array.i"
#endif /* __ACE_INLINE__ */

// Dynamically initialize an array.

template <class T>
ACE_Array<T>::ACE_Array (size_t size)
  : max_size_ (size),
    cur_size_ (size)
{
  ACE_NEW (this->array_, T[size]);
}

template <class T>
ACE_Array<T>::ACE_Array (size_t size,
		 const T &default_value)
  : max_size_ (size),
    cur_size_ (size)
{
  ACE_NEW (this->array_, T[size]);

  for (size_t i = 0; i < size; i++)
    this->array_[i] = default_value;
}

// The copy constructor (performs initialization).

template <class T>
ACE_Array<T>::ACE_Array (const ACE_Array<T> &s)
   : max_size_ (s.size ()),
     cur_size_ (s.size ())
{
  ACE_NEW (this->array_, T[s.size ()]);

  for (size_t i = 0; i < this->size (); i++)
    this->array_[i] = s.array_[i];
}

// Assignment operator (performs assignment).

template <class T> void
ACE_Array<T>::operator= (const ACE_Array<T> &s)
{
  // Check for "self-assignment".

  if (this == &s)
    return;
  else if (this->max_size_ < s.size ())
    {
      delete [] this->array_;

      ACE_NEW (this->array_, T[s.size ()]);

      this->max_size_ = s.size ();
    }

  this->cur_size_ = s.size ();

  for (size_t i = 0; i < this->size (); i++)
    this->array_[i] = s.array_[i];
}

// Set an item in the array at location index.

template <class T> int
ACE_Array<T>::set (const T &new_item, size_t index)
{
  if (this->in_range (index))
    {
      this->array_[index] = new_item;
      return 0;
    }
  else
    return -1;
}

// Get an item in the array at location index.

template <class T> int
ACE_Array<T>::get (T &item, size_t index) const
{
   if (this->in_range (index))
     {
       item = this->array_[index];  // Copies the item.  If you don't
                                    // want to copy, use operator [] instead
                                    // (but then you'll be responsible for
                                    // range checking).
       return 0;
     }
   else
     return -1;
}

// Compare this array with <s> for equality.

template <class T> int
ACE_Array<T>::operator== (const ACE_Array<T> &s) const
{
  if (this == &s)
    return 1;
  else if (this->cur_size_ != s.cur_size_)
    return 0;

  for (size_t index = 0; index < s.cur_size_; index++)
    if (this->array_[index] != s.array_[index])
      return 0;

  return 1;
}

// Compare this array with <s> for inequality.

template <class T> int
ACE_Array<T>::operator!= (const ACE_Array<T> &s) const
{
  return !(*this == s);
}

#endif /* ARRAY_C */