SortedVector.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2005 Palmsource, Inc.
00003  * 
00004  * This software is licensed as described in the file LICENSE, which
00005  * you should have received as part of this distribution. The terms
00006  * are also available at http://www.openbinder.org/license.html.
00007  * 
00008  * This software consists of voluntary contributions made by many
00009  * individuals. For the exact contribution history, see the revision
00010  * history and logs, available at http://www.openbinder.org
00011  */
00012 
00013 #ifndef _SUPPORT_SORTEDVECTOR_H
00014 #define _SUPPORT_SORTEDVECTOR_H
00015 
00021 #include <support/Vector.h>
00022 
00023 #if _SUPPORTS_NAMESPACE
00024 namespace palmos {
00025 namespace support {
00026 #endif
00027 
00032 /*--------------------------------------------------------*/
00033 /*----- SAbstractSortedVector abstract base class -------*/
00034 
00036 class SAbstractSortedVector : public SAbstractVector
00037 {
00038 public:
00039     inline                  SAbstractSortedVector(size_t element_size);
00040     inline                  SAbstractSortedVector(const SAbstractSortedVector& o);
00041                             // WARNING: Your subclass must call MakeEmpty()
00042                             // in its own destructor!
00043     inline virtual          ~SAbstractSortedVector();
00044     
00045     inline  SAbstractSortedVector& operator=(const SAbstractSortedVector& o);
00046             
00047             // Note that we inherit the assignment operator, so you can
00048             // assign a plain SAbstractVector to an instance of this class.
00049     
00050             ssize_t         AddOrdered(const void* newElement, bool* added = NULL);
00051             
00052             ssize_t         OrderOf(const void* element) const;
00053             ssize_t         FloorOrderOf(const void* element) const;
00054             ssize_t         CeilOrderOf(const void* element) const;
00055             bool            GetOrderOf(const void* element, size_t* index) const;
00056             
00057             ssize_t         RemoveOrdered(const void* element);
00058 
00059     static  void            MoveBefore(SAbstractSortedVector* to, SAbstractSortedVector* from, size_t count);
00060     static  void            MoveAfter(SAbstractSortedVector* to, SAbstractSortedVector* from, size_t count);
00061             void            Swap(SAbstractSortedVector& o);
00062             
00063 protected:
00064     virtual int32_t         PerformCompare(const void* d1, const void* d2) const = 0;
00065     virtual bool            PerformLessThan(const void* d1, const void* d2) const = 0;
00066     
00067 private:
00068     virtual status_t        _ReservedUntypedOrderedVector1();
00069     virtual status_t        _ReservedUntypedOrderedVector2();
00070     virtual status_t        _ReservedUntypedOrderedVector3();
00071     virtual status_t        _ReservedUntypedOrderedVector4();
00072     virtual status_t        _ReservedUntypedOrderedVector5();
00073     virtual status_t        _ReservedUntypedOrderedVector6();
00074     
00075             int32_t         _reserved[2];
00076 };
00077 
00078 // Type optimizations.
00079 #if !defined(_MSC_VER)
00080 void BMoveBefore(SAbstractSortedVector* to, SAbstractSortedVector* from, size_t count);
00081 void BMoveAfter(SAbstractSortedVector* to, SAbstractSortedVector* from, size_t count);
00082 void BSwap(SAbstractSortedVector& v1, SAbstractSortedVector& v2);
00083 #endif
00084 
00085 /*--------------------------------------------------------*/
00086 /*----- SSortedVector concrete class --------------------*/
00087 
00089 
00092 template<class TYPE>
00093 class SSortedVector : private SAbstractSortedVector
00094 {
00095 public:
00096             typedef TYPE    value_type;
00097 
00098 public:
00099                             SSortedVector();
00100                             SSortedVector(const SSortedVector<TYPE>& o);
00101     virtual                 ~SSortedVector();
00102     
00103             SSortedVector<TYPE>&    operator=(const SSortedVector<TYPE>& o);
00104     
00105     /* Size stats */
00106     
00107             void            SetCapacity(size_t total_space);
00108             void            SetExtraCapacity(size_t extra_space);
00109             size_t          Capacity() const;
00110             
00111             size_t          CountItems() const;
00112     
00113     /* Data access */
00114 
00115             const TYPE&     operator[](size_t i) const;
00116             const TYPE&     ItemAt(size_t i) const;
00117     
00118             ssize_t         IndexOf(const TYPE& item) const;
00119             ssize_t         FloorIndexOf(const TYPE& item) const;
00120             ssize_t         CeilIndexOf(const TYPE& item) const;
00121             bool            GetIndexOf(const TYPE& item, size_t* index) const;
00122             
00123             bool            HasItem(const TYPE& item) const;
00124             
00125             const TYPE*     Array() const;
00126 
00127     /* Array modification */
00128     
00129             ssize_t         AddItem(const TYPE& item, bool* added = NULL);
00130             
00131             void            MakeEmpty();
00132             void            RemoveItemsAt(size_t index, size_t count = 1);
00133     
00134             ssize_t         RemoveItemFor(const TYPE& item);
00135             
00136     static  void            MoveBefore(SSortedVector<TYPE>& to, SSortedVector<TYPE>& from, size_t count);
00137     static  void            MoveAfter(SSortedVector<TYPE>& to, SSortedVector<TYPE>& from, size_t count);
00138             void            Swap(SSortedVector<TYPE>& o);
00139             
00140 protected:
00141     virtual void            PerformConstruct(void* base, size_t count) const;
00142     virtual void            PerformCopy(void* to, const void* from, size_t count) const;
00143     virtual void            PerformReplicate(void* to, const void* protoElement, size_t count) const;
00144     virtual void            PerformDestroy(void* base, size_t count) const;
00145     
00146     virtual void            PerformMoveBefore(void* to, void* from, size_t count) const;
00147     virtual void            PerformMoveAfter(void* to, void* from, size_t count) const;
00148     
00149     virtual void            PerformAssign(void* to, const void* from, size_t count) const;
00150     
00151     virtual int32_t         PerformCompare(const void* d1, const void* d2) const;
00152     virtual bool            PerformLessThan(const void* d1, const void* d2) const;
00153 
00154     virtual SValue          PerformAsValue(const void* from, size_t count) const;
00155     virtual status_t        PerformSetFromValue(void* to, const SValue& value, size_t count);
00156 };
00157 
00158 #if !defined(_MSC_VER)
00159 // Type optimizations.
00160 template<class TYPE>
00161 void BMoveBefore(SSortedVector<TYPE>* to, SSortedVector<TYPE>* from, size_t count = 1);
00162 template<class TYPE>
00163 void BMoveAfter(SSortedVector<TYPE>* to, SSortedVector<TYPE>* from, size_t count = 1);
00164 template<class TYPE>
00165 void BSwap(SSortedVector<TYPE>& v1, SSortedVector<TYPE>& v2);
00166 #endif // _MSC_VER
00167 
00170 /*-------------------------------------------------------------*/
00171 /*---- No user serviceable parts after this -------------------*/
00172 
00173 #if !defined(_MSC_VER)
00174 
00175 inline void BMoveBefore(SAbstractSortedVector* to, SAbstractSortedVector* from, size_t count)
00176 {
00177     SAbstractSortedVector::MoveBefore(to, from, count);
00178 }
00179 
00180 inline void BMoveAfter(SAbstractSortedVector* to, SAbstractSortedVector* from, size_t count)
00181 {
00182     SAbstractSortedVector::MoveAfter(to, from, count);
00183 }
00184 
00185 inline void BSwap(SAbstractSortedVector& v1, SAbstractSortedVector& v2)
00186 {
00187     v1.Swap(v2);
00188 }
00189 
00190 #endif // _MSC_VER
00191 
00192 /*-------------------------------------------------------------*/
00193 
00194 SAbstractSortedVector::SAbstractSortedVector(size_t element_size)
00195     :   SAbstractVector(element_size)
00196 {
00197 }
00198 
00199 SAbstractSortedVector::SAbstractSortedVector(const SAbstractSortedVector& o)
00200     :   SAbstractVector(o)
00201 {
00202 }
00203 
00204 SAbstractSortedVector::~SAbstractSortedVector()
00205 {
00206 }
00207 
00208 SAbstractSortedVector&  SAbstractSortedVector::operator=(const SAbstractSortedVector& o)
00209 {
00210     SAbstractVector::operator=(o);
00211     return *this;
00212 }
00213 
00214 /*-------------------------------------------------------------*/
00215 
00216 template<class TYPE> inline
00217 SSortedVector<TYPE>::SSortedVector()
00218     :   SAbstractSortedVector(sizeof(TYPE[2])/2)
00219 {
00220 }
00221 
00222 template<class TYPE> inline
00223 SSortedVector<TYPE>::SSortedVector(const SSortedVector<TYPE>& o)
00224     :   SAbstractSortedVector(o)
00225 {
00226 }
00227 
00228 template<class TYPE> inline
00229 SSortedVector<TYPE>::~SSortedVector()
00230 {
00231     MakeEmpty();
00232 }
00233 
00234 template<class TYPE> inline
00235 SSortedVector<TYPE>& SSortedVector<TYPE>::operator=(const SSortedVector<TYPE>& o)
00236 {
00237     SAbstractSortedVector::operator=(o);
00238     return *this;
00239 }
00240 
00241 template<class TYPE> inline
00242 void SSortedVector<TYPE>::SetCapacity(size_t total_space)
00243 {
00244     SAbstractVector::SetCapacity(total_space);
00245 }
00246 
00247 template<class TYPE> inline
00248 void SSortedVector<TYPE>::SetExtraCapacity(size_t extra_space)
00249 {
00250     SAbstractVector::SetExtraCapacity(extra_space);
00251 }
00252 
00253 template<class TYPE> inline
00254 size_t SSortedVector<TYPE>::Capacity() const
00255 {
00256     return SAbstractVector::Capacity();
00257 }
00258 
00259 template<class TYPE> inline
00260 size_t SSortedVector<TYPE>::CountItems() const
00261 {
00262     return SAbstractVector::CountItems();
00263 }
00264 
00265 template<class TYPE> inline
00266 const TYPE& SSortedVector<TYPE>::operator[](size_t i) const
00267 {
00268     // Don't use SAbstractVector::At() here, because we know
00269     // the item size so can do it slightly more efficiently.
00270     DbgOnlyFatalErrorIf(i >= CountItems() || Array() == NULL, "Vector index out of range");
00271     return *( static_cast<const TYPE*>(SAbstractVector::Array()) + i );
00272 }
00273 
00274 template<class TYPE> inline
00275 const TYPE& SSortedVector<TYPE>::ItemAt(size_t i) const
00276 {
00277     // Don't use SAbstractVector::At() here, because we know
00278     // the item size so can do it slightly more efficiently.
00279     DbgOnlyFatalErrorIf(i >= CountItems() || Array() == NULL, "Vector index out of range");
00280     return *( static_cast<const TYPE*>(SAbstractVector::Array()) + i );
00281 }
00282 
00283 template<class TYPE> inline
00284 ssize_t SSortedVector<TYPE>::AddItem(const TYPE& item, bool* added)
00285 {
00286     return AddOrdered(&item, added);
00287 }
00288 
00289 template<class TYPE> inline
00290 ssize_t SSortedVector<TYPE>::IndexOf(const TYPE& item) const
00291 {
00292     return OrderOf(&item);
00293 }
00294 
00295 template<class TYPE> inline
00296 ssize_t SSortedVector<TYPE>::FloorIndexOf(const TYPE& item) const
00297 {
00298     return FloorOrderOf(&item);
00299 }
00300 
00301 template<class TYPE> inline
00302 ssize_t SSortedVector<TYPE>::CeilIndexOf(const TYPE& item) const
00303 {
00304     return CeilOrderOf(&item);
00305 }
00306 
00307 template<class TYPE> inline
00308 bool SSortedVector<TYPE>::GetIndexOf(const TYPE& item, size_t* index) const
00309 {
00310     return GetOrderOf(&item, index);
00311 }
00312 
00313 template<class TYPE> inline
00314 bool SSortedVector<TYPE>::HasItem(const TYPE& item) const
00315 {
00316     size_t dummy;
00317     return GetOrderOf(&item, &dummy);
00318 }
00319 
00320 template<class TYPE> inline
00321 const TYPE * SSortedVector<TYPE>::Array() const
00322 {
00323     return static_cast<const TYPE*>(SAbstractVector::Array());
00324 }
00325 
00326 template<class TYPE> inline
00327 void SSortedVector<TYPE>::MakeEmpty()
00328 {
00329     SAbstractSortedVector::MakeEmpty();
00330 }
00331 
00332 template<class TYPE> inline
00333 void SSortedVector<TYPE>::RemoveItemsAt(size_t index, size_t count)
00334 {
00335     SAbstractSortedVector::RemoveItemsAt(index, count);
00336 }
00337 
00338 template<class TYPE> inline
00339 ssize_t SSortedVector<TYPE>::RemoveItemFor(const TYPE& item)
00340 {
00341     return RemoveOrdered(&item);
00342 }
00343 
00344 template<class TYPE> inline
00345 void SSortedVector<TYPE>::Swap(SSortedVector<TYPE>& o)
00346 {
00347     SAbstractSortedVector::Swap(o);
00348 }
00349 
00350 template<class TYPE>
00351 void SSortedVector<TYPE>::PerformConstruct(void* base, size_t count) const
00352 {
00353 #if (__CC_ARM)
00354     BConstruct((TYPE*)(base), count);
00355 #else
00356     BConstruct(static_cast<TYPE*>(base), count);
00357 #endif
00358 }
00359 
00360 template<class TYPE>
00361 void SSortedVector<TYPE>::PerformCopy(void* to, const void* from, size_t count) const
00362 {
00363 #if (__CC_ARM)
00364     BCopy((TYPE*)(to), (const TYPE*)(from), count);
00365 #else
00366     BCopy(static_cast<TYPE*>(to), static_cast<const TYPE*>(from), count);
00367 #endif
00368 }
00369 
00370 template<class TYPE>
00371 void SSortedVector<TYPE>::PerformReplicate(void* to, const void* protoElement, size_t count) const
00372 {
00373 #if (__CC_ARM)
00374     BReplicate((TYPE*)(to), (const TYPE*)(protoElement), count);
00375 #else
00376     BReplicate(static_cast<TYPE*>(to), static_cast<const TYPE*>(protoElement), count);
00377 #endif
00378 }
00379 
00380 template<class TYPE>
00381 void SSortedVector<TYPE>::PerformDestroy(void* base, size_t count) const
00382 {
00383 #if (__CC_ARM)
00384     BDestroy((TYPE*)(base), count);
00385 #else
00386     BDestroy(static_cast<TYPE*>(base), count);
00387 #endif
00388 }
00389 
00390 template<class TYPE>
00391 void SSortedVector<TYPE>::PerformMoveBefore(void* to, void* from, size_t count) const
00392 {
00393 #if (__CC_ARM)
00394     BMoveBefore((TYPE*)(to), (TYPE*)(from), count);
00395 #else
00396     BMoveBefore(static_cast<TYPE*>(to), static_cast<TYPE*>(from), count);
00397 #endif
00398 }
00399 
00400 template<class TYPE>
00401 void SSortedVector<TYPE>::PerformMoveAfter(void* to, void* from, size_t count) const
00402 {
00403 #if (__CC_ARM)
00404     BMoveAfter((TYPE*)(to), (TYPE*)(from), count);
00405 #else
00406     BMoveAfter(static_cast<TYPE*>(to), static_cast<TYPE*>(from), count);
00407 #endif
00408 }
00409 
00410 template<class TYPE>
00411 void SSortedVector<TYPE>::PerformAssign(void* to, const void* from, size_t count) const
00412 {
00413 #if (__CC_ARM)
00414     BAssign((TYPE*)(to), (const TYPE*)(from), count);
00415 #else
00416     BAssign(static_cast<TYPE*>(to), static_cast<const TYPE*>(from), count);
00417 #endif
00418 }
00419 
00420 template<class TYPE>
00421 int32_t SSortedVector<TYPE>::PerformCompare(const void* d1, const void* d2) const
00422 {
00423 #if (__CC_ARM)
00424     return BCompare(*(const TYPE*)(d1), *(const TYPE*)(d2));
00425 #else
00426     return BCompare(*static_cast<const TYPE*>(d1), *static_cast<const TYPE*>(d2));
00427 #endif
00428 }
00429 
00430 template<class TYPE>
00431 bool SSortedVector<TYPE>::PerformLessThan(const void* d1, const void* d2) const
00432 {
00433 #if (__CC_ARM)
00434     return BLessThan(*(const TYPE*)(d1), *(const TYPE*)(d2));
00435 #else
00436     return BLessThan(*static_cast<const TYPE*>(d1), *static_cast<const TYPE*>(d2));
00437 #endif
00438 }
00439 
00440 template<class TYPE>
00441 SValue SSortedVector<TYPE>::PerformAsValue(const void* from, size_t count) const
00442 {
00443 #if (__CC_ARM)
00444     return BArrayAsValue((const TYPE*)(from), count);
00445 #else
00446     return BArrayAsValue(static_cast<const TYPE*>(from), count);
00447 #endif
00448 }
00449 
00450 template<class TYPE>
00451 status_t SSortedVector<TYPE>::PerformSetFromValue(void* to, const SValue& value, size_t count)
00452 {
00453 #if (__CC_ARM)
00454     return BArrayConstruct((TYPE*)(to), value, count);
00455 #else
00456     return BArrayConstruct(static_cast<TYPE*>(to), value, count);
00457 #endif
00458 }
00459 
00460 /*-------------------------------------------------------------*/
00461 
00462 #if !defined(_MSC_VER)
00463 
00464 template<class TYPE> inline
00465 void BMoveBefore(SSortedVector<TYPE>* to, SSortedVector<TYPE>* from, size_t count)
00466 {
00467 #if (__CC_ARM)
00468     BMoveBefore((SAbstractSortedVector*)(to), (SAbstractSortedVector*)(from), count);
00469 #else
00470     BMoveBefore(static_cast<SAbstractSortedVector*>(to), static_cast<SAbstractSortedVector*>(from), count);
00471 #endif
00472 }
00473 
00474 template<class TYPE> inline
00475 void BMoveAfter(SSortedVector<TYPE>* to, SSortedVector<TYPE>* from, size_t count)
00476 {
00477 #if (__CC_ARM)
00478     BMoveAfter((SAbstractSortedVector*)(to), (SAbstractSortedVector*)(from), count);
00479 #else
00480     BMoveAfter(static_cast<SAbstractSortedVector*>(to), static_cast<SAbstractSortedVector*>(from), count);
00481 #endif
00482 }
00483 
00484 template<class TYPE> inline
00485 void BSwap(SSortedVector<TYPE>& v1, SSortedVector<TYPE>& v2)
00486 {
00487     v1.Swap(v2);
00488 }
00489 
00490 #endif // _MSC_VER
00491 
00492 #if _SUPPORTS_NAMESPACE
00493 } } // namespace palmos::support
00494 #endif
00495 
00496 #endif