LCOV - code coverage report
Current view: top level - asar - autoarray.h (source / functions) Coverage Total Hit
Test: asar.info Lines: 93.4 % 76 71
Test Date: 2024-01-15 17:20:38 Functions: 81.2 % 96 78
Branches: 75.0 % 44 33

             Branch data     Line data    Source code
       1                 :             : #pragma once
       2                 :             : 
       3                 :             : #include "std-includes.h"
       4                 :             : 
       5                 :             : //Note: T must be a pointer type, or stuff will screw up. To make a pointer last longer than this object, assign nullptr to it and it won't free the old one.
       6                 :             : template<typename T> class autoptr {
       7                 :             :         T ptr;
       8                 :             : public:
       9                 :             :         operator T() const
      10                 :             :         {
      11                 :        3437 :                 return ptr;
      12                 :             :         }
      13                 :             : 
      14                 :             :         autoptr& operator=(T ptr_)
      15                 :             :         {
      16                 :         219 :                 ptr = ptr_;
      17                 :         201 :                 return *this;
      18                 :             :         }
      19                 :             : 
      20                 :         279 :         autoptr()
      21                 :             :         {
      22                 :         279 :                 ptr = nullptr;
      23                 :             :         }
      24                 :             : 
      25                 :       54754 :         autoptr(T ptr_)
      26                 :             :         {
      27                 :       51907 :                 ptr = ptr_;
      28                 :             :         }
      29                 :             : 
      30                 :             :         autoptr(const autoptr<T>& ptr_)
      31                 :             :         {
      32                 :             :                 ptr = ptr_.ptr;
      33                 :             :         }
      34                 :             : 
      35                 :       55087 :         ~autoptr()
      36                 :             :         {
      37         [ +  + ]:       55087 :                 if (ptr) free((void*)ptr);
      38                 :       55087 :         }
      39                 :             : };
      40                 :             : 
      41                 :             : template<typename T> class autoarray {
      42                 :             : public:
      43                 :             :         int count;
      44                 :             : 
      45                 :             : private:
      46                 :             :         T* ptr;
      47                 :             :         int bufferlen;
      48                 :             : 
      49                 :             :         T dummy;
      50                 :             :         static const int default_size = 128;
      51                 :             : 
      52                 :           0 :         const T& getconst(int id) const
      53                 :             :         {
      54         [ #  # ]:           0 :                 if (id < 0) return dummy;
      55         [ #  # ]:           0 :                 if (id >= count) return dummy;
      56                 :           0 :                 return ptr[id];
      57                 :             :         }
      58                 :             : 
      59                 :      962685 :         T& get(int id)
      60                 :             :         {
      61         [ -  + ]:      962685 :                 if (id < 0) return dummy;
      62         [ +  + ]:      962685 :                 if (id >= bufferlen - 4)
      63                 :             :                 {
      64                 :          19 :                         resize(id);
      65                 :             :                 }
      66         [ +  + ]:      962685 :                 if (id >= count)
      67                 :             :                 {
      68         [ +  + ]:      193009 :                         for (int i = count;i <= id;i++) new(ptr + i) T();
      69                 :       96479 :                         count = id + 1;
      70                 :             :                 }
      71                 :      962685 :                 return ptr[id];
      72                 :             :         }
      73                 :             : 
      74                 :          20 :         void resize(int size)
      75                 :             :         {
      76                 :          20 :                 int oldlen = count;
      77         [ +  + ]:          40 :                 while (bufferlen <= size + 4) bufferlen *= 2;
      78                 :          20 :                 T *old = ptr;
      79                 :          20 :                 ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen);
      80         [ +  + ]:        4692 :                 for(int i = 0; i < oldlen; i++){
      81                 :        4672 :                         new(ptr + i) T();
      82                 :        4672 :                         ptr[i] = static_cast<T &&>(old[i]);
      83                 :             :                 }
      84                 :          20 :                 free(old);
      85                 :          20 :                 memset(ptr + oldlen, 0, (size_t)(bufferlen - oldlen) * sizeof(T));
      86                 :          20 :         }
      87                 :             : 
      88                 :             : public:
      89                 :             : 
      90                 :        4688 :         void reset(int keep = 0)
      91                 :             :         {
      92         [ +  + ]:        4688 :                 if (keep >= count) return;
      93         [ +  + ]:         853 :                 for (int i = keep;i < count;i++) ptr[i].~T();
      94                 :         497 :                 memset(ptr + keep, 0, (size_t)(count - keep) * sizeof(T));
      95         [ +  - ]:         497 :                 if (keep < bufferlen / 2)
      96                 :             :                 {
      97   [ +  -  +  + ]:        1667 :                         while (keep < bufferlen / 2 && bufferlen>8) bufferlen /= 2;
      98                 :         497 :                         T *old = ptr;
      99                 :         497 :                         ptr = (T*)malloc(sizeof(T)*(size_t)bufferlen);
     100         [ +  + ]:         506 :                         for(int i = 0; i < keep; i++){
     101                 :           9 :                                 new(ptr + i) T();
     102                 :           9 :                                 ptr[i] = static_cast<T &&>(old[i]);
     103                 :             :                         }
     104                 :         497 :                         free(old);
     105                 :             : 
     106                 :             :                 }
     107                 :         497 :                 count = keep;
     108                 :             :         }
     109                 :             : 
     110                 :             :         T& operator[](int id)
     111                 :             :         {
     112                 :      551134 :                 return get(id);
     113                 :             :         }
     114                 :             : 
     115                 :             :         const T& operator[](int id) const
     116                 :             :         {
     117                 :           0 :                 return getconst(id);
     118                 :             :         }
     119                 :             : 
     120                 :             :         operator T*()
     121                 :             :         {
     122                 :             :                 return ptr;
     123                 :             :         }
     124                 :             : 
     125                 :             :         operator const T*() const
     126                 :             :         {
     127                 :             :                 return ptr;
     128                 :             :         }
     129                 :             : 
     130                 :       95178 :         T& append(const T& item)
     131                 :             :         {
     132                 :       95178 :                 return (get(count) = item);
     133                 :             :         }
     134                 :             : 
     135                 :             :         //insert is not safe for non pod types!!!
     136                 :             :         void insert(int pos)
     137                 :             :         {
     138                 :             :                 if (pos<0 || pos>count) return;
     139                 :             :                 if (count >= bufferlen - 4)
     140                 :             :                 {
     141                 :             :                         resize(count);
     142                 :             :                 }
     143                 :             :                 memmove(ptr + pos + 1, ptr + pos, sizeof(T)*(count - pos));
     144                 :             :                 memset(ptr + pos, 0, sizeof(T));
     145                 :             :                 new(ptr + pos) T();
     146                 :             :                 count++;
     147                 :             :         }
     148                 :             : 
     149                 :       79313 :         void insert(int pos, const T& item)
     150                 :             :         {
     151   [ +  -  +  - ]:       79313 :                 if (pos<0 || pos>count) return;
     152         [ +  + ]:       79313 :                 if (count >= bufferlen - 4)
     153                 :             :                 {
     154                 :           1 :                         resize(count);
     155                 :             :                 }
     156                 :       79313 :                 memmove(ptr + pos + 1, ptr + pos, sizeof(T)*(size_t)(count - pos));
     157                 :       79313 :                 memset(ptr + pos, 0, sizeof(T));
     158                 :       79313 :                 new(ptr + pos) T();
     159                 :       79313 :                 ptr[pos] = item;
     160                 :       79313 :                 count++;
     161                 :             :         }
     162                 :             : 
     163                 :      170823 :         void remove(int id)
     164                 :             :         {
     165   [ +  -  +  - ]:      170823 :                 if (id < 0 || id >= count) return;
     166                 :      170823 :                 count--;
     167                 :      170823 :                 ptr[id].~T();
     168         [ +  + ]:      282733 :                 for(int i = id; i < count; i++){
     169                 :      111910 :                         ptr[i] = static_cast<T &&>(ptr[i+1]);
     170                 :             :                 }
     171                 :             :         }
     172                 :             : 
     173                 :        3609 :         autoarray()
     174                 :        1604 :         {
     175                 :        3609 :                 ptr = (T*)malloc(sizeof(T) * default_size);
     176                 :        3609 :                 memset(ptr, 0, default_size*sizeof(T));
     177                 :        3609 :                 bufferlen = default_size;
     178                 :        3609 :                 count = 0;
     179                 :        3609 :         }
     180                 :             : 
     181                 :        3609 :         ~autoarray()
     182                 :             :         {
     183         [ +  + ]:        2947 :                 for (int i = 0;i < count;i++) ptr[i].~T();
     184                 :        3609 :                 free(ptr);
     185                 :        3609 :         }
     186                 :             : 
     187                 :             : #ifdef SERIALIZER
     188                 :             :         void serialize(serializer& s)
     189                 :             :         {
     190                 :             :                 if (s.serializing) s(count);
     191                 :             :                 else
     192                 :             :                 {
     193                 :             :                         int i;
     194                 :             :                         s(i);
     195                 :             :                         get(i - 1);
     196                 :             :                 }
     197                 :             :                 for (int i = 0;i < count;i++) s(ptr[i]);
     198                 :             :         }
     199                 :             : #endif
     200                 :             : #define SERIALIZER_BANNED
     201                 :             : };
        

Generated by: LCOV version 2.0-1