LCOV - code coverage report
Current view: top level - util/cgm - ArrayBasedContainer.hpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 3 3 100.0 %
Date: 2020-06-30 00:58:45 Functions: 4 4 100.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : //- Class: ArrayBasedContainer
       2                 :            : //- Description: Base class for container classed implemented as arrays.
       3                 :            : //-              Contains 'void*' entities. This class allows copying
       4                 :            : //-              between container types.
       5                 :            : //- Assumptions: Containers derived from this are implemented as arrays.
       6                 :            : //-
       7                 :            : //- Owner: Paul Kinney
       8                 :            : //- Checked by: 
       9                 :            : //- Version: $Id: 
      10                 :            : 
      11                 :            : #ifndef ARRAYBASEDCONTAINER_HPP
      12                 :            : #define ARRAYBASEDCONTAINER_HPP
      13                 :            : 
      14                 :            : #include "CubitDefines.h"
      15                 :            : #include "MemoryManager.hpp"
      16                 :            : #include "CGMUtilConfigure.h"
      17                 :            : #include <cstring>
      18                 :            : 
      19                 :            : const int INCREMENT = 8; //- Default list size increment
      20                 :            : 
      21                 :            : class CUBIT_UTIL_EXPORT ArrayBasedContainer
      22                 :            : {
      23                 :            : 
      24                 :            : public:
      25                 :            :   
      26                 :            :     ArrayBasedContainer (int size);
      27                 :            :     //- Constructor: Create a list with initial size {size}. The list
      28                 :            :     //- will be grown by {size} each time it is filled. Memory for the
      29                 :            :     //- list is not allocated until the first element is inserted using
      30                 :            :     //- {insertLink}. 
      31                 :            :     //- If {size} is zero, the default increment ({INCREMENT}) will be used
      32                 :            :     //- From an efficiency standpoint, it is very important that the 
      33                 :            :     //- increment be set large enough to reduce the number of list 
      34                 :            :     //- growths, but small enough to not waste memory.
      35                 :            :     //- It is more efficient to sligthly overestimate the size than 
      36                 :            :     //- to underestimate the size.
      37                 :            : 
      38                 :            :     ArrayBasedContainer(const ArrayBasedContainer& copy_from);
      39                 :            :     //- Copy Constructor
      40                 :            : 
      41                 :            :     virtual ~ArrayBasedContainer();
      42                 :            :     //- Destructor: Free all resources used by this list.
      43                 :            : 
      44                 :            :     int  size() const;      //- Return the number of items stored in the list.
      45                 :            :     
      46                 :            :     void clean_out();
      47                 :            :     //- Delete all elements in the list, reset the pointer to zero.
      48                 :            :     //- Does not release memory already allocated for the list. This
      49                 :            :     //- call is more efficient creating a new list repeatadly within
      50                 :            :     //- a loop.
      51                 :            : 
      52                 :            :     void shrink(int k);
      53                 :            :     //- itemCount -= k;  Doesn't change array elements.
      54                 :            :        
      55                 :            :     ArrayBasedContainer& operator=(const ArrayBasedContainer&);
      56                 :            :     //- Create a copy of a list.
      57                 :            :     
      58                 :            :     ArrayBasedContainer& operator+=(const ArrayBasedContainer&);
      59                 :            :     //- Append one list to the end of the other list.
      60                 :            : 
      61                 :            :     ArrayBasedContainer& operator-=(const ArrayBasedContainer&);
      62                 :            :     //- Subtract one container from the other. Preserves order of first container.
      63                 :            : 
      64                 :            :     virtual void compress();
      65                 :            :     //- remove all the NULLs, preserve order
      66                 :            : 
      67                 :            :     static unsigned int current_allocated_memory();
      68                 :            :     static unsigned int maximum_allocated_memory();
      69                 :            : 
      70                 :            : protected:
      71                 :            : 
      72                 :            :   void **listArray;            //- array of items in the list
      73                 :            :   int  itemCount;              //- number of items in list
      74                 :            :   int  listLength;             //- size of {listArray}
      75                 :            :   Bit  poolAllocated : 1;
      76                 :            :   //- TRUE if allocated from special memory pool, FALSE if normal allocation
      77                 :            :   Bit  listIsSorted : 1;       //- Used in SDLList
      78                 :            :   Bit  listIncrement : 30;     //- amount by which to grow list
      79                 :            :   //- This is limited to 1.07e9
      80                 :            : 
      81                 :            :   void lengthen_list();
      82                 :            : 
      83                 :            : private:
      84                 :            :   static unsigned int currentAllocatedMemory;
      85                 :            :   static unsigned int maxAllocatedMemory;
      86                 :            : };
      87                 :            : 
      88                 :            : inline void ArrayBasedContainer::shrink(int k)
      89                 :            : {
      90                 :            :   if (k > 0) 
      91                 :            :   {
      92                 :            :     if (itemCount >= k)
      93                 :            :       itemCount -= k;
      94                 :            :     else
      95                 :            :       itemCount = 0;
      96                 :            :   }
      97                 :            : }
      98                 :            : 
      99                 :       2424 : inline int ArrayBasedContainer::size() const { return itemCount; }
     100                 :            : 
     101                 :            : inline void ArrayBasedContainer::clean_out ()
     102                 :            : {
     103                 :            :     itemCount = 0;
     104                 :            : }
     105                 :            : 
     106                 :            : 
     107                 :            : // This class is used internally by ArrayBasedContainer to maintain
     108                 :            : // a large pool of memory of size {INCREMENT} (The default size for
     109                 :            : // ArrayBasedContainers). If a list of size {INCREMENT} is constructed,
     110                 :            : // the memory is allocated from this pool rather than allocating the memory
     111                 :            : // from the system global new routine.
     112                 :            : class ArrayMemory
     113                 :            : {
     114                 :            :   public:
     115                 :         96 :     ArrayMemory() { memset(mem, 0, sizeof(mem)); }
     116                 :            : 
     117                 :        384 :     SetDynamicMemoryAllocation(memoryManager)
     118                 :            :     //- overloaded new and delete operators
     119                 :            : 
     120                 :            :   private:
     121                 :            : 
     122                 :            :     static MemoryManager  memoryManager;
     123                 :            : 
     124                 :            :     void*  mem[INCREMENT];
     125                 :            : };
     126                 :            : 
     127                 :            : #endif //- ARRAYBASEDCONTAINER_HPP
     128                 :            : 

Generated by: LCOV version 1.11