# carray.h

    00001 //==========================================================================
    00002 //  CARRAY.H - part of
    00003 //                     OMNeT++/OMNEST
    00004 //            Discrete System Simulation in C++
    00005 //
    00006 //
    00007 //  Declaration of the following classes:
    00008 //    cBag      : expandable array to store small no-class items
    00009 //    cArray    : flexible array to store cObject objects
    00010 //
    00011 //==========================================================================
    00012
    00013 /*--------------------------------------------------------------*
    00014   Copyright (C) 1992-2005 Andras Varga
    00015
    00016   This file is distributed WITHOUT ANY WARRANTY. See the file
    00017   `license' for details on this and other legal matters.
    00018 *--------------------------------------------------------------*/
    00019
    00020 #ifndef __CARRAY_H
    00021 #define __CARRAY_H
    00022
    00023 #include "cobject.h"
    00024
    00025
    00042 class SIM_API cBag : public cObject
    00043 {
    00044   private:
    00045     char *vect;
    00046     int elemsize;
    00047     int size;
    00048     int delta;
    00049     int lastused;
    00050     int firstfree;
    00051
    00052   public:
    00055
    00059     cBag(const cBag& bag);
    00060
    00066     explicit cBag(const char *name=NULL, int esiz=4,int siz=0,int delt=5);
    00067
    00071     virtual ~cBag()  {clear();}
    00072
    00077     cBag& operator=(const cBag& bag);
    00079
    00082
    00087     virtual cPolymorphic *dup() const  {return new cBag(*this);}
    00088
    00093     virtual std::string info() const;
    00094
    00100     virtual void netPack(cCommBuffer *buffer);
    00101
    00107     virtual void netUnpack(cCommBuffer *buffer);
    00109
    00112
    00117     void setup(int esiz, int siz, int delt=5);
    00118
    00122     void clear();
    00123
    00129     int items() const {return lastused+1;}
    00130
    00136     int add(void *obj);
    00137
    00141     int addAt(int m, void *obj);
    00142
    00148     int find(void *obj) const;
    00149
    00154     void *get(int m);
    00155
    00160     const void *get(int m) const;
    00161
    00166     void *operator[](int m)
    00167           {return get(m);}
    00168
    00173     const void *operator[](int m) const
    00174       {return get(m);}
    00175
    00180     bool isUsed(int m) const;
    00181
    00187     void *remove(int m);
    00189 };
    00190
    00191 //==========================================================================
    00192
    00207 class SIM_API cArray : public cObject
    00208 {
    00209   public:
    00213     class Iterator
    00214     {
    00215       private:
    00216         cArray *array;
    00217         int k;
    00218
    00219       public:
    00225         Iterator(const cArray& a, bool athead=true)  {init(a, athead);}
    00226
    00230         void init(const cArray& a, bool athead=true);
    00231
    00236         cObject *operator()()  {return array->get(k);}
    00237
    00241         bool end() const   {return k<0 || k>=array->items();}
    00242
    00251         cObject *operator++(int);
    00252
    00261         cObject *operator--(int);
    00262     };
    00263
    00264   private:
    00265     bool tkownership;
    00266     cObject **vect;   // vector of objects
    00267     int size;         // size of vector
    00268     int delta;        // if needed, grows by delta
    00269     int firstfree;    // first free position in vect[]
    00270     int last;         // last used position
    00271
    00272   public:
    00275
    00282     cArray(const cArray& list);
    00283
    00288     explicit cArray(const char *name=NULL, int siz=0, int dt=10);
    00289
    00294     virtual ~cArray();
    00295
    00303     cArray& operator=(const cArray& list);
    00305
    00308
    00314     virtual cPolymorphic *dup() const  {return new cArray(*this);}
    00315
    00320     virtual std::string info() const;
    00321
    00326     virtual void forEachChild(cVisitor *v);
    00327
    00333     virtual void netPack(cCommBuffer *buffer);
    00334
    00340     virtual void netUnpack(cCommBuffer *buffer);
    00342
    00345
    00351     int items() const {return last+1;}
    00352
    00357     void clear();
    00358
    00364     int add(cObject *obj);
    00365
    00371     int addAt(int m,cObject *obj);
    00372
    00380     int set(cObject *obj);
    00381
    00387     int find(cObject *obj) const;
    00388
    00394     int find(const char *objname) const;
    00395
    00400     cObject *get(int m);
    00401
    00406     cObject *get(const char *objname);
    00407
    00412     const cObject *get(int m) const;
    00413
    00418     const cObject *get(const char *objname) const;
    00419
    00424     cObject *operator[](int m)
    00425       {return get(m);}
    00426
    00431     cObject *operator[](const char *objname)
    00432       {return get(objname);}
    00433
    00438     const cObject *operator[](int m) const
    00439       {return get(m);}
    00440
    00445     const cObject *operator[](const char *objname) const
    00446       {return get(objname);}
    00447
    00451     bool exist(int m) const
    00452       {return m>=0 && m<=last && vect[m]!=NULL;}
    00453
    00458     bool exist(const char *objname) const
    00459       {return find(objname)!=-1;}
    00460
    00466     cObject *remove(int m);
    00467
    00473     cObject *remove(const char *objname);
    00474
    00481     cObject *remove(cObject *obj);
    00483
    00491
    00497     void takeOwnership(bool tk) {tkownership=tk;}
    00498
    00504     bool takeOwnership() const   {return tkownership;}
    00506 };
    00507
    00508 #endif
    00509