diff options
author | schmidt <douglascraigschmidt@users.noreply.github.com> | 1997-04-30 15:39:46 +0000 |
---|---|---|
committer | schmidt <douglascraigschmidt@users.noreply.github.com> | 1997-04-30 15:39:46 +0000 |
commit | fe5d51f3b3bcb295493214e234c75fda3e20d3cb (patch) | |
tree | 632b44e1e98623cd1cfbf1ba7321f8d55496de06 /ace/Array.cpp | |
parent | aa9291fc9e6cc0d04386ee6e30478fbe38c491a3 (diff) | |
download | ATCD-fe5d51f3b3bcb295493214e234c75fda3e20d3cb.tar.gz |
*** empty log message ***
Diffstat (limited to 'ace/Array.cpp')
-rw-r--r-- | ace/Array.cpp | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/ace/Array.cpp b/ace/Array.cpp new file mode 100644 index 00000000000..238831e78c6 --- /dev/null +++ b/ace/Array.cpp @@ -0,0 +1,143 @@ +// Array.cpp +// $Id$ + +#if !defined (ARRAY_C) +#define ARRAY_C + +#define ACE_BUILD_DLL +#include "Array.h" + +#if !defined (__INLINE__) +#define INLINE +#include "Array.i" +#endif /* __INLINE__ */ + +// Dynamically initialize an array. + +template <class T> +ACE_Array<T>::ACE_Array (size_t size) + : cur_size_ (size), + max_size_ (size) +{ + ACE_NEW (this->array_, T[size]); +} + +template <class T> +ACE_Array<T>::ACE_Array (size_t size, + const T &default_value) + : cur_size_ (size), + max_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) + : cur_size_ (s.size ()), + max_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]; +} + +// Clean up the array (e.g., delete dynamically allocated memory). + +template <class T> +ACE_Array<T>::~ACE_Array (void) +{ + delete [] this->array_; +} + +// = Set/get methods. + +template <class T> T & +ACE_Array<T>::operator[] (size_t index) +{ + return this->array_[index]; +} + +// 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]; + 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 */ |