# SbPList.h

    00001 #ifndef COIN_LISTS_SBPLIST_H
    00002 #define COIN_LISTS_SBPLIST_H
    00003
    00004 /**************************************************************************\
    00005  *
    00006  *  This file is part of the Coin 3D visualization library.
    00007  *  Copyright (C) 1998-2003 by Systems in Motion.  All rights reserved.
    00008  *
    00009  *  This library is free software; you can redistribute it and/or
    00010  *  modify it under the terms of the GNU General Public License
    00011  *  ("GPL") version 2 as published by the Free Software Foundation.
    00012  *  See the file LICENSE.GPL at the root directory of this source
    00013  *  distribution for additional information about the GNU GPL.
    00014  *
    00015  *  For using Coin with software that can not be combined with the GNU
    00016  *  GPL, and for taking advantage of the additional benefits of our
    00017  *  support services, please contact Systems in Motion about acquiring
    00018  *  a Coin Professional Edition License.
    00019  *
    00020  *  See <URL:http://www.coin3d.org> for  more information.
    00021  *
    00022  *  Systems in Motion, Teknobyen, Abels Gate 5, 7030 Trondheim, NORWAY.
    00023  *  <URL:http://www.sim.no>.
    00024  *
    00025 \**************************************************************************/
    00026
    00027 #include <Inventor/SbBasic.h>
    00028 #include <assert.h>
    00029 #include <stddef.h> // NULL definition
    00030
    00031 class COIN_DLL_API SbPList {
    00032   enum { DEFAULTSIZE = 4 };
    00033
    00034 public:
    00035   SbPList(const int sizehint = DEFAULTSIZE);
    00036   SbPList(const SbPList & l);
    00037   ~SbPList();
    00038
    00039   void copy(const SbPList & l);
    00040   SbPList & operator=(const SbPList & l);
    00041   void fit(void);
    00042
    00043   void append(void * item);
    00044   int find(void * item) const;
    00045   void insert(void * item, const int insertbefore);
    00046   void removeItem(void * item);
    00047   void remove(const int index);
    00048   void removeFast(const int index);
    00049   int getLength(void) const;
    00050   void truncate(const int length, const int fit = 0);
    00051
    00052   void ** getArrayPtr(const int start = 0) const;
    00053   void *& operator[](const int index) const;
    00054
    00055   int operator==(const SbPList & l) const;
    00056   int operator!=(const SbPList & l) const;
    00057   void * get(const int index) const;
    00058   void set(const int index, void * item);
    00059
    00060 protected:
    00061
    00062   void expand(const int size);
    00063   int getArraySize(void) const;
    00064
    00065 private:
    00066   void expandlist(const int size) const;
    00067   void grow(const int size = -1);
    00068
    00069   int itembuffersize;
    00070   int numitems;
    00071   void ** itembuffer;
    00072   void * builtinbuffer[DEFAULTSIZE];
    00073 };
    00074
    00075 /* inlined methods ********************************************************/
    00076
    00077 inline void
    00078 SbPList::append(void * item)
    00079 {
    00080   if (this->numitems == this->itembuffersize) this->grow();
    00081   this->itembuffer[this->numitems++] = item;
    00082 }
    00083
    00084 inline void
    00085 SbPList::removeFast(const int index)
    00086 {
    00087 #ifdef COIN_EXTRA_DEBUG
    00088   assert(index >= 0 && index < this->numitems);
    00089 #endif // COIN_EXTRA_DEBUG
    00090   this->itembuffer[index] = this->itembuffer[--this->numitems];
    00091 }
    00092
    00093 inline int
    00094 SbPList::getLength(void) const
    00095 {
    00096   return this->numitems;
    00097 }
    00098
    00099 inline void
    00100 SbPList::truncate(const int length, const int fit )
    00101 {
    00102 #ifdef COIN_EXTRA_DEBUG
    00103   assert(length <= this->numitems);
    00104 #endif // COIN_EXTRA_DEBUG
    00105   this->numitems = length;
    00106   if (fit) this->fit();
    00107 }
    00108
    00109 inline void **
    00110 SbPList::getArrayPtr(const int start) const
    00111 {
    00112 #ifdef COIN_EXTRA_DEBUG
    00113   assert(index >= 0 && index < this->numitems);
    00114 #endif // COIN_EXTRA_DEBUG
    00115   return &this->itembuffer[start];
    00116 }
    00117
    00118 inline void *&
    00119 SbPList::operator[](const int index) const
    00120 {
    00121 #ifdef COIN_EXTRA_DEBUG
    00122   assert(index >= 0);
    00123 #endif // COIN_EXTRA_DEBUG
    00124   if (index >= this->getLength()) this->expandlist(index + 1);
    00125   return this->itembuffer[index];
    00126 }
    00127
    00128 inline int
    00129 SbPList::operator!=(const SbPList & l) const
    00130 {
    00131   return !(*this == l);
    00132 }
    00133
    00134 inline void *
    00135 SbPList::get(const int index) const
    00136 {
    00137   return this->itembuffer[index];
    00138 }
    00139
    00140 inline void
    00141 SbPList::set(const int index, void * item)
    00142 {
    00143   this->itembuffer[index] = item;
    00144 }
    00145
    00146 inline void
    00147 SbPList::expand(const int size)
    00148 {
    00149   this->grow(size);
    00150   this->numitems = size;
    00151 }
    00152
    00153 inline int
    00154 SbPList::getArraySize(void) const
    00155 {
    00156   return this->itembuffersize;
    00157 }
    00158
    00159
    00160 #endif // !COIN_LISTS_SBPLIST_H