(file) Return to ArrayImpl.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

Diff for /pegasus/src/Pegasus/Common/ArrayImpl.h between version 1.26 and 1.30.2.1

version 1.26, 2005/02/05 22:59:23 version 1.30.2.1, 2006/02/10 16:09:29
Line 1 
Line 1 
 //%2005////////////////////////////////////////////////////////////////////////  //%2006////////////////////////////////////////////////////////////////////////
 // //
 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems. // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
Line 8 
Line 8 
 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group. // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.; // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
 // EMC Corporation; VERITAS Software Corporation; The Open Group. // EMC Corporation; VERITAS Software Corporation; The Open Group.
   // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
   // EMC Corporation; Symantec Corporation; The Open Group.
 // //
 // Permission is hereby granted, free of charge, to any person obtaining a copy // Permission is hereby granted, free of charge, to any person obtaining a copy
 // of this software and associated documentation files (the "Software"), to // of this software and associated documentation files (the "Software"), to
Line 30 
Line 32 
 // Author: Mike Brasher (mbrasher@bmc.com) // Author: Mike Brasher (mbrasher@bmc.com)
 // //
 // Modified By: Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com) // Modified By: Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
   //              Mike Brasher, Inova Europe (mike-brasher@austin.rr.com)
 // //
 //%///////////////////////////////////////////////////////////////////////////// //%/////////////////////////////////////////////////////////////////////////////
  
Line 44 
Line 47 
 #include <Pegasus/Common/Memory.h> #include <Pegasus/Common/Memory.h>
 #include <Pegasus/Common/ArrayRep.h> #include <Pegasus/Common/ArrayRep.h>
 #include <Pegasus/Common/InternalException.h> #include <Pegasus/Common/InternalException.h>
   #include <Pegasus/Common/Linkage.h>
  
 PEGASUS_NAMESPACE_BEGIN PEGASUS_NAMESPACE_BEGIN
  
   PEGASUS_COMMON_LINKAGE void ArrayThrowIndexOutOfBoundsException();
   
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 Array<PEGASUS_ARRAY_T>::Array() Array<PEGASUS_ARRAY_T>::Array()
 { {
     _rep = ArrayRep<PEGASUS_ARRAY_T>::create(0);      _rep = &ArrayRepBase::_empty_rep;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
Line 60 
Line 66 
 #endif #endif
 Array<PEGASUS_ARRAY_T>::Array(const Array<PEGASUS_ARRAY_T>& x) Array<PEGASUS_ARRAY_T>::Array(const Array<PEGASUS_ARRAY_T>& x)
 { {
     _rep = static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(x._rep)->clone();      _rep = x._rep;
       ArrayRep<PEGASUS_ARRAY_T>::ref(Array_rep);
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
Line 68 
Line 75 
 #endif #endif
 Array<PEGASUS_ARRAY_T>::Array(Uint32 size) Array<PEGASUS_ARRAY_T>::Array(Uint32 size)
 { {
     _rep = ArrayRep<PEGASUS_ARRAY_T>::create(size);      _rep = ArrayRep<PEGASUS_ARRAY_T>::alloc(size);
  
     if (_rep == 0)      if (!_rep)
     {     {
         throw NullPointer();         throw NullPointer();
     }     }
  
     InitializeRaw(static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data(), size);      InitializeRaw(Array_data, size);
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
Line 83 
Line 90 
 #endif #endif
 Array<PEGASUS_ARRAY_T>::Array(Uint32 size, const PEGASUS_ARRAY_T& x) Array<PEGASUS_ARRAY_T>::Array(Uint32 size, const PEGASUS_ARRAY_T& x)
 { {
     _rep = ArrayRep<PEGASUS_ARRAY_T>::create(size);      _rep = ArrayRep<PEGASUS_ARRAY_T>::alloc(size);
  
     if (_rep == 0)      if (!_rep)
     {     {
         throw NullPointer();         throw NullPointer();
     }     }
  
     PEGASUS_ARRAY_T* data = static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data();      PEGASUS_ARRAY_T* data = Array_data;
   
       // Note: we could use template specialization (by adding functions to
       // Memory.h) so that this loop becomes a memset() for single byte raw
       // types, but this function is rarely called.
  
     while (size--)     while (size--)
         new(data++) PEGASUS_ARRAY_T(x);         new(data++) PEGASUS_ARRAY_T(x);
Line 101 
Line 112 
 #endif #endif
 Array<PEGASUS_ARRAY_T>::Array(const PEGASUS_ARRAY_T* items, Uint32 size) Array<PEGASUS_ARRAY_T>::Array(const PEGASUS_ARRAY_T* items, Uint32 size)
 { {
     _rep = ArrayRep<PEGASUS_ARRAY_T>::create(size);      _rep = ArrayRep<PEGASUS_ARRAY_T>::alloc(size);
  
     if (_rep == 0)      if (!_rep)
     {     {
         throw NullPointer();         throw NullPointer();
     }     }
  
     CopyToRaw(static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data(), items, size);      CopyToRaw(Array_data, items, size);
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
Line 116 
Line 127 
 #endif #endif
 Array<PEGASUS_ARRAY_T>::~Array() Array<PEGASUS_ARRAY_T>::~Array()
 { {
     ArrayRep<PEGASUS_ARRAY_T>::destroy(static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep));      ArrayRep<PEGASUS_ARRAY_T>::unref(Array_rep);
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
Line 125 
Line 136 
 Array<PEGASUS_ARRAY_T>& Array<PEGASUS_ARRAY_T>::operator=( Array<PEGASUS_ARRAY_T>& Array<PEGASUS_ARRAY_T>::operator=(
     const Array<PEGASUS_ARRAY_T>& x)     const Array<PEGASUS_ARRAY_T>& x)
 { {
     if (static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(x._rep) !=      if (x._rep != Array_rep)
         static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep))  
     {     {
         ArrayRep<PEGASUS_ARRAY_T>::destroy(static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep));          ArrayRep<PEGASUS_ARRAY_T>::unref(Array_rep);
         _rep = static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(x._rep)->clone();          _rep = x._rep;
           ArrayRep<PEGASUS_ARRAY_T>::ref(Array_rep);
     }     }
   
     return *this;     return *this;
 } }
  
Line 139 
Line 151 
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::clear() void Array<PEGASUS_ARRAY_T>::clear()
 { {
     ArrayRep<PEGASUS_ARRAY_T>::destroy(static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep));      if (Array_size)
     _rep = ArrayRep<PEGASUS_ARRAY_T>::create(0);      {
           if (Array_refs.get() == 1)
           {
               Destroy(Array_data, Array_size);
               Array_size = 0;
           }
           else
           {
               ArrayRep<PEGASUS_ARRAY_T>::unref(Array_rep);
               _rep = &ArrayRepBase::_empty_rep;
           }
       }
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
Line 148 
Line 171 
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::reserveCapacity(Uint32 capacity) void Array<PEGASUS_ARRAY_T>::reserveCapacity(Uint32 capacity)
 { {
     if (capacity > static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->capacity)      if (capacity > Array_capacity || Array_refs.get() != 1)
     {     {
         Uint32 size = this->size();  
         ArrayRep<PEGASUS_ARRAY_T>* rep =         ArrayRep<PEGASUS_ARRAY_T>* rep =
             ArrayRep<PEGASUS_ARRAY_T>::create(capacity);              ArrayRep<PEGASUS_ARRAY_T>::alloc(capacity);
  
         if (rep != 0)          if (!rep)
               return;
   
           rep->size = Array_size;
   
           if (Array_refs.get() == 1)
         {         {
             rep->size = size;              memcpy(rep->data(), Array_data, Array_size*sizeof(PEGASUS_ARRAY_T));
             CopyToRaw(              Array_size = 0;
                 rep->data(),  
                 static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data(),  
                 size);  
             ArrayRep<PEGASUS_ARRAY_T>::destroy(  
                 static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep));  
             _rep = rep;  
         }         }
           else
               CopyToRaw(rep->data(), Array_data, Array_size);
   
           ArrayRep<PEGASUS_ARRAY_T>::unref(Array_rep);
           _rep = rep;
     }     }
 } }
  
Line 173 
Line 199 
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::grow(Uint32 size, const PEGASUS_ARRAY_T& x) void Array<PEGASUS_ARRAY_T>::grow(Uint32 size, const PEGASUS_ARRAY_T& x)
 { {
     Uint32 oldSize = static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->size;      reserveCapacity(Array_size + size);
     reserveCapacity(oldSize + size);      PEGASUS_ARRAY_T* p = Array_data + Array_size;
   
     PEGASUS_ARRAY_T* p = static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data() + oldSize;  
     Uint32 n = size;     Uint32 n = size;
  
     while (n--)     while (n--)
         new(p++) PEGASUS_ARRAY_T(x);         new(p++) PEGASUS_ARRAY_T(x);
  
     static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->size += size;      Array_size += size;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
Line 190 
Line 214 
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::swap(Array<PEGASUS_ARRAY_T>& x) void Array<PEGASUS_ARRAY_T>::swap(Array<PEGASUS_ARRAY_T>& x)
 { {
     ArrayRep<PEGASUS_ARRAY_T>* tmp = static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep);      ArrayRep<PEGASUS_ARRAY_T>* tmp = Array_rep;
     _rep = static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(x._rep);      _rep = x._rep;
     x._rep = tmp;     x._rep = tmp;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 Uint32 Array<PEGASUS_ARRAY_T>::size() const  void Array<PEGASUS_ARRAY_T>::append(const PEGASUS_ARRAY_T& x)
 { {
     return static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->size;      Uint32 n = Array_size + 1;
   
       if (n > Array_capacity || Array_refs.get() != 1)
           reserveCapacity(n);
   
       new (Array_data + Array_size) PEGASUS_ARRAY_T(x);
       Array_size++;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 Uint32 Array<PEGASUS_ARRAY_T>::getCapacity() const  void Array<PEGASUS_ARRAY_T>::append(const PEGASUS_ARRAY_T* x, Uint32 size)
 { {
     return static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->capacity;      Uint32 n = Array_size + size;
       reserveCapacity(n);
       CopyToRaw(Array_data + Array_size, x, size);
       Array_size = n;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 const PEGASUS_ARRAY_T* Array<PEGASUS_ARRAY_T>::getData() const  void Array<PEGASUS_ARRAY_T>::appendArray(const Array<PEGASUS_ARRAY_T>& x)
 { {
     return static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data();      append(x.getData(), x.size());
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 PEGASUS_ARRAY_T& Array<PEGASUS_ARRAY_T>::operator[](Uint32 index)  void Array<PEGASUS_ARRAY_T>::prepend(const PEGASUS_ARRAY_T& x)
 { {
     if (index >= size())      prepend(&x, 1);
         throw IndexOutOfBoundsException();  
   
     return static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data()[index];  
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 const PEGASUS_ARRAY_T& Array<PEGASUS_ARRAY_T>::operator[](Uint32 index) const  void Array<PEGASUS_ARRAY_T>::prepend(const PEGASUS_ARRAY_T* x, Uint32 size)
 { {
     if (index >= size())      reserveCapacity(Array_size + size);
         throw IndexOutOfBoundsException();      memmove(
           Array_data + size,
     return static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data()[index];          Array_data,
           sizeof(PEGASUS_ARRAY_T) * Array_size);
       CopyToRaw(Array_data, x, size);
       Array_size += size;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::append(const PEGASUS_ARRAY_T& x)  void Array<PEGASUS_ARRAY_T>::insert(Uint32 index, const PEGASUS_ARRAY_T& x)
 { {
     reserveCapacity(size() + 1);      insert(index, &x, 1);
     new (_data() + size()) PEGASUS_ARRAY_T(x);  
     static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->size++;  
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::append(const PEGASUS_ARRAY_T* x, Uint32 size)  void Array<PEGASUS_ARRAY_T>::insert(
       Uint32 index, const PEGASUS_ARRAY_T* x, Uint32 size)
   {
       if (index > Array_size)
 { {
     reserveCapacity(this->size() + size);          throw IndexOutOfBoundsException();
     CopyToRaw(_data() + this->size(), x, size);  
     static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->size += size;  
 } }
  
 #ifndef PEGASUS_ARRAY_T      reserveCapacity(Array_size + size);
 template<class PEGASUS_ARRAY_T>  
 #endif      Uint32 n = Array_size - index;
 void Array<PEGASUS_ARRAY_T>::appendArray(const Array<PEGASUS_ARRAY_T>& x)  
       if (n)
 { {
     append(x.getData(), x.size());          memmove(
               Array_data + index + size,
               Array_data + index,
               sizeof(PEGASUS_ARRAY_T) * n);
       }
   
       CopyToRaw(Array_data + index, x, size);
       Array_size += size;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::prepend(const PEGASUS_ARRAY_T& x)  void Array<PEGASUS_ARRAY_T>::remove(Uint32 index)
 { {
     prepend(&x, 1);      remove(index, 1);
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::prepend(const PEGASUS_ARRAY_T* x, Uint32 size)  void Array<PEGASUS_ARRAY_T>::remove(Uint32 index, Uint32 size)
   {
       if (Array_refs.get() != 1)
           _rep = ArrayRep<PEGASUS_ARRAY_T>::copy_on_write(Array_rep);
   
       // Case 1: attempting to remove last element (this is an optimization
       // for when the array is used as a stack; see Stack class).
   
       if (index + 1 == Array_size)
       {
           Destroy(Array_data + index, 1);
           Array_size--;
           return;
       }
   
       // Case 2: not attempting to remove last element:
   
       if (index + size - 1 > Array_size)
       {
           throw IndexOutOfBoundsException();
       }
   
       Destroy(Array_data + index, size);
       Uint32 rem = Array_size - (index + size);
   
       if (rem)
 { {
     reserveCapacity(this->size() + size);          memmove(
     memmove(_data() + size, _data(), sizeof(PEGASUS_ARRAY_T) * this->size());              Array_data + index,
     CopyToRaw(_data(), x, size);              Array_data + index + size,
     static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->size += size;              sizeof(PEGASUS_ARRAY_T) * rem);
       }
   
       Array_size -= size;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::insert(Uint32 index, const PEGASUS_ARRAY_T& x)  Uint32 Array<PEGASUS_ARRAY_T>::size() const
 { {
     insert(index, &x, 1);      return Array_size;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::insert(Uint32 index, const PEGASUS_ARRAY_T* x, Uint32 size)  PEGASUS_ARRAY_T& Array<PEGASUS_ARRAY_T>::operator[](
       Uint32 index)
 { {
     if (index > this->size())  #ifndef PEGASUS_ARRAY_NO_THROW
         throw IndexOutOfBoundsException();      if (index >= Array_size)
           ArrayThrowIndexOutOfBoundsException();
   #endif
  
     reserveCapacity(this->size() + size);      if (Array_refs.get() != 1)
           _rep = ArrayRep<PEGASUS_ARRAY_T>::copy_on_write(Array_rep);
  
     Uint32 n = this->size() - index;      return Array_data[index];
   }
  
     if (n)  #ifndef PEGASUS_ARRAY_T
         memmove(_data() + index + size,  template<class PEGASUS_ARRAY_T>
                 _data() + index,  #endif
                 sizeof(PEGASUS_ARRAY_T) * n);  const PEGASUS_ARRAY_T& Array<PEGASUS_ARRAY_T>::operator[](
       Uint32 index) const
   {
   #ifndef PEGASUS_ARRAY_NO_THROW
       if (index >= Array_size)
           ArrayThrowIndexOutOfBoundsException();
   #endif
  
     CopyToRaw(_data() + index, x, size);      return Array_data[index];
     static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->size += size;  
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::remove(Uint32 index)  Uint32 Array<PEGASUS_ARRAY_T>::getCapacity() const
 { {
     remove(index, 1);      return Array_capacity;
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
 template<class PEGASUS_ARRAY_T> template<class PEGASUS_ARRAY_T>
 #endif #endif
 void Array<PEGASUS_ARRAY_T>::remove(Uint32 index, Uint32 size)  const PEGASUS_ARRAY_T* Array<PEGASUS_ARRAY_T>::getData() const
 { {
     if (index + size - 1 > this->size())      return Array_data;
         throw IndexOutOfBoundsException();  
   
     Destroy(_data() + index, size);  
   
     Uint32 rem = this->size() - (index + size);  
   
     if (rem)  
         memmove(_data() + index,  
                 _data() + index + size,  
                 sizeof(PEGASUS_ARRAY_T) * rem);  
   
     static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->size -= size;  
 } }
  
 #ifndef PEGASUS_ARRAY_T #ifndef PEGASUS_ARRAY_T
Line 350 
Line 416 
 #endif #endif
 PEGASUS_ARRAY_T* Array<PEGASUS_ARRAY_T>::_data() const PEGASUS_ARRAY_T* Array<PEGASUS_ARRAY_T>::_data() const
 { {
     return static_cast<ArrayRep<PEGASUS_ARRAY_T>*>(_rep)->data();      return Array_data;
 } }
  
 #endif //!defined(Pegasus_ArrayImpl_h) || !defined(PEGASUS_ARRAY_T) #endif //!defined(Pegasus_ArrayImpl_h) || !defined(PEGASUS_ARRAY_T)


Legend:
Removed from v.1.26  
changed lines
  Added in v.1.30.2.1

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2