/home/coin/SVN-release/OS-2.2.0/Bcp/src/Member/BCP_vector_double.cpp

Go to the documentation of this file.
00001 // Copyright (C) 2000, International Business Machines
00002 // Corporation and others.  All Rights Reserved.
00003 
00004 #include "BCP_vector.hpp"
00005 
00006 //##############################################################################
00007 
00008 /* Here only the non-inlined methods are defined */
00009 
00010 //##############################################################################
00011 
00012 template<> void BCP_vec<double>::insert_aux(iterator position, const_reference x)
00013 {
00014         const size_t after_pos = finish - position;
00015         if (finish != end_of_storage) {
00016                 memmove(position+1, position, after_pos * sizeof(double));
00017                 *position = x;
00018                 ++finish;
00019         } else {
00020                 const size_t len = (2*size() + 0x100);
00021                 iterator tmp = allocate(len);
00022                 const size_t until_pos = position - start;
00023                 memmove(tmp, start, until_pos * sizeof(double));
00024                 iterator tmp_finish = tmp + until_pos;
00025                 *tmp_finish++ = x;
00026                 memmove(tmp_finish, position, after_pos * sizeof(double));
00027                 deallocate();
00028                 start = tmp;
00029                 finish = start + (until_pos + 1 + after_pos);
00030                 end_of_storage = tmp + len;
00031         }
00032 }
00033 
00034 //##############################################################################
00035 
00036 template<> BCP_vec<double>::BCP_vec(const size_t n, const_reference value) :
00037         start(0), finish(0), end_of_storage(0)
00038 {
00039         if (n > 0) {
00040                 start = finish = allocate(n);
00041                 end_of_storage = start + n;
00042                 while (finish != end_of_storage)
00043                         *finish++ = value;
00044         }
00045 }
00046 //------------------------------------------------------------------------------
00047 template<> BCP_vec<double>::BCP_vec(const_iterator first, const_iterator last) :
00048         start(0), finish(0), end_of_storage(0)
00049 {
00050         const size_t len = last - first;
00051         if (len > 0) {
00052                 start = allocate(len);
00053                 memcpy(start, first, len * sizeof(double));
00054                 end_of_storage = finish = start + len;
00055         }
00056 }
00057 //------------------------------------------------------------------------------
00058 template<> BCP_vec<double>::BCP_vec(const double* x, const size_t num) :
00059         start(0), finish(0), end_of_storage(0)
00060 {
00061         if (num > 0) {
00062                 start = allocate(num);
00063                 memcpy(start, x, num * sizeof(double));
00064                 end_of_storage = finish = start + num;
00065         }
00066 }
00067 
00068 //##############################################################################
00069 
00070 template<> void BCP_vec<double>::reserve(const size_t n)
00071 {
00072         if (capacity() < n) {
00073                 iterator tmp = allocate(n);
00074                 const size_t oldsize = size();
00075                 if (oldsize > 0)
00076                         memcpy(tmp, start, oldsize * sizeof(double));
00077                 deallocate();
00078                 start = tmp;
00079                 finish = start + oldsize;
00080                 end_of_storage = start + n;
00081         }
00082 }
00083 //------------------------------------------------------------------------------
00084 template<> BCP_vec<double>& BCP_vec<double>::operator=(const BCP_vec<double>& x)
00085 {
00086         if (&x != this) {
00087                 const size_t x_size = x.size();
00088                 if (x_size > capacity()) {
00089                         deallocate();
00090                         start = allocate(x_size);
00091                         end_of_storage = start + x_size;
00092                 }
00093                 if (x_size > 0)
00094                         memcpy(start, x.start, x_size * sizeof(double));
00095                 finish = start + x_size;
00096         }
00097         return *this;
00098 }
00099 
00100 //##############################################################################
00101 // these two members serve to copy out entries from a buffer.
00102 
00103 template<> void BCP_vec<double>::assign(const void* x, const size_t num)
00104 {
00105         if (num > capacity()){
00106                 deallocate();
00107                 start = allocate(num);
00108                 end_of_storage = start + num;
00109         }
00110         memcpy(start, x, num * sizeof(double));
00111         finish = start + num;
00112 }
00113 //------------------------------------------------------------------------------
00114 template<> void BCP_vec<double>::insert(double* position,
00115                                                                           const void* first, const size_t n)
00116 {
00117         if (n == 0) return;
00118         if ((size_t) (end_of_storage - finish) >= n) {
00119                 if (finish != position)
00120                         memmove(position + n, position, (finish - position) * sizeof(double));
00121                 memcpy(position, first, n * sizeof(double));
00122                 finish += n;
00123         } else {
00124                 const size_t new_size = (2*size() + n);
00125                 iterator tmp = allocate(new_size);
00126                 const size_t after_pos = finish - position;
00127                 const size_t until_pos = position - start;
00128                 memcpy(tmp, start, until_pos * sizeof(double));
00129                 memcpy(tmp + until_pos, first, n * sizeof(double));
00130                 memcpy(tmp + (until_pos + n), position, after_pos * sizeof(double));
00131                 deallocate();
00132                 start = tmp;
00133                 finish = start + (until_pos + n + after_pos);
00134                 end_of_storage = tmp + new_size;
00135         }
00136 }
00137 //------------------------------------------------------------------------------
00138 template<> void BCP_vec<double>::insert(iterator position,
00139                                                                           const_iterator first,
00140                                                                           const_iterator last)
00141 {
00142         if (first == last) return;
00143         const size_t n = last - first;
00144         if ((size_t) (end_of_storage - finish) >= n) {
00145                 memmove(position + n, position, (finish - position) * sizeof(double));
00146                 memcpy(position, first, n * sizeof(double));
00147                 finish += n;
00148         } else {
00149                 const size_t new_size = (2*size() + n);
00150                 iterator tmp = allocate(new_size);
00151                 const size_t after_pos = finish - position;
00152                 const size_t until_pos = position - start;
00153                 memcpy(tmp, start, until_pos * sizeof(double));
00154                 memcpy(tmp + until_pos, first, n * sizeof(double));
00155                 memcpy(tmp + (until_pos + n), position, after_pos * sizeof(double));
00156                 deallocate();
00157                 start = tmp;
00158                 finish = start + (until_pos + n + after_pos);
00159                 end_of_storage = tmp + new_size;
00160         }
00161 }
00162 //------------------------------------------------------------------------------
00163 template<> void BCP_vec<double>::insert(iterator position, const size_t n,
00164                                                                           const_reference x)
00165 {
00166         if (n == 0) return;
00167         if ((size_t) (end_of_storage - finish) >= n) {
00168                 memmove(position + n, position, (finish - position) * sizeof(double));
00169                 finish += n;
00170                 for (int i = n; i > 0; --i)
00171                         *position++ = x;
00172         } else {
00173                 const size_t new_size = (2*size() + n);
00174                 iterator tmp = allocate(new_size);
00175                 const size_t after_pos = finish - position;
00176                 const size_t until_pos = position - start;
00177                 memcpy(tmp, start, until_pos * sizeof(double));
00178                 memcpy(tmp + (until_pos + n), position, after_pos * sizeof(double));
00179                 deallocate();
00180                 start = tmp;
00181                 finish = start + (until_pos + n + after_pos);
00182                 end_of_storage = tmp + new_size;
00183                 position = tmp + until_pos;
00184                 for (int i = n; i > 0; --i)
00185                         *position++ = x;
00186         }
00187 }
00188 
00189 //##############################################################################

Generated on Thu Aug 5 03:02:53 2010 by  doxygen 1.4.7