jitcraft/Vector.h
author David Anderson <dvander@alliedmods.net>
Tue Jul 24 23:53:34 2012 -0700 (2012-07-24)
changeset 86 32ad0523b309
parent 85 06a0015b6a5c
permissions -rw-r--r--
Renamed jitcraft/ to src/, fixed some /Wp64 warnings.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _INCLUDE_KEIMA_TPL_CPP_VECTOR_H_
    20 #define _INCLUDE_KEIMA_TPL_CPP_VECTOR_H_
    21 
    22 #include <new>
    23 #include <stdlib.h>
    24 #include "AllocatorPolicies.h"
    25 #include "Utility.h"
    26 
    27 namespace ke {
    28 
    29 template <typename T, typename AllocPolicy = ZoneAllocatorPolicy>
    30 class Vector : public AllocPolicy
    31 {
    32   public:
    33     Vector(AllocPolicy = AllocPolicy())
    34       : data(NULL),
    35         nitems(0),
    36         maxsize(0)
    37     { }
    38     ~Vector() {
    39         for (size_t i = 0; i < nitems; i++)
    40             data[i].~T();
    41         this->free(data);
    42     }
    43     bool append(const T& item) {
    44         if (!GrowIfNeeded(1))
    45             return false;
    46         new (&data[nitems]) T(item);
    47         nitems++;
    48         return true;
    49     }
    50     void pop() {
    51         assert(nitems);
    52         data[nitems - 1].~T();
    53         nitems--;
    54     }
    55     size_t length() const {
    56         return nitems;
    57     }
    58     T& at(size_t i) {
    59         assert(i < length());
    60         return data[i];
    61     }
    62     const T& at(size_t i) const {
    63         assert(i < length());
    64         return data[i];
    65     }
    66     T& operator [](size_t i) {
    67         return at(i);
    68     }
    69     const T& operator [](size_t i) const {
    70         return at(i);
    71     }
    72     void clear() {
    73         nitems = 0;
    74     }
    75 
    76     T *buffer() const {
    77         return data;
    78     }
    79 
    80   private:
    81     bool GrowIfNeeded(size_t needed)
    82     {
    83         if (!IsUintPtrAddSafe(nitems, needed)) {
    84             this->reportAllocationOverflow();
    85             return false;
    86         }
    87         if (maxsize == 0)
    88             maxsize = 8;
    89         while (nitems + needed > maxsize)
    90         {
    91             if (!IsUintPtrMultiplySafe(maxsize, 2)) {
    92                 this->reportAllocationOverflow();
    93                 return false;
    94             }
    95             maxsize *= 2;
    96         }
    97         T* newdata = (T*)this->malloc(sizeof(T) * maxsize);
    98         if (newdata == NULL)
    99             return false;
   100         for (size_t i = 0; i < nitems; i++)
   101         {
   102             new (&newdata[i]) T(data[i]);
   103             data[i].~T();
   104         }
   105         this->free(data);
   106         data = newdata;
   107         return true;
   108     }
   109 
   110   private:
   111     T* data;
   112     size_t nitems;
   113     size_t maxsize;
   114 };
   115 
   116 }
   117 
   118 #endif /* _INCLUDE_KEIMA_TPL_CPP_VECTOR_H_ */
   119