Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /******************************************************************
  2. *   21 days: a game for programmers
  3. *   Copyright (C) 2014 Maxim Grishin
  4. *
  5. *   This program is free software; you can redistribute it and/or
  6. *   modify it under the terms of the GNU General Public License
  7. *   as published by the Free Software Foundation; either version 2
  8. *   of the License, or (at your option) any later version.
  9. *
  10. *   This program is distributed in the hope that it will be useful,
  11. *   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. *   GNU General Public License for more details.
  14. *
  15. *   You should have received a copy of the GNU General Public License
  16. *   along with this program; if not, write to the Free Software
  17. *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  18. *   MA  02110-1301, USA.
  19. *
  20. *
  21. *   This file contains a part of file "/include/vector" from menuetlibc
  22. *   adapted to the developer's needs.
  23. *   Changes:
  24. *       1. "*__cdecl" replaced with "* __attribute__((cdecl))" in order
  25. *       to compile a C++ project.
  26. *       2. Added front() methods with the following code:
  27. *           T& front() {return data[0];}
  28. *       3. Code reformatted
  29. *
  30. ********************************************************************/
  31.  
  32. #ifndef KOS_VECTOR_H
  33. #define KOS_VECTOR_H
  34.  
  35. extern void * __attribute__((cdecl)) operator new(size_t);
  36. inline void * __attribute__((cdecl)) operator new(size_t, void *_P) {
  37.     return (_P);
  38.     }
  39.  
  40. template<class T> class vector21 {
  41.     unsigned length;
  42.     unsigned allocated;
  43.     T* data;
  44.   public:
  45.     typedef unsigned size_type;
  46.     typedef T* iterator;
  47.     vector21():length(0),allocated(0),data(NULL) {}
  48.     ~vector21() {for (unsigned i=length;i--;)data[i].~T();free(data);}
  49.     unsigned size() const {return length;}
  50.     void clear() {length=0;}
  51.     T& operator[](unsigned pos) {return data[pos];}
  52.     T* begin() {return data;}
  53.     T* end() {return data+length;}
  54.     void push_back(const T& x) {
  55.         if (length==allocated){
  56.             allocated+=16;
  57.             data=(T*)realloc(data,allocated*sizeof(T));
  58.             }
  59.         new (data+length++) T(x);
  60.         }
  61.     bool empty() const {return length==0;}
  62.     void pop_back() {data[--length].~T();}
  63.     T& back() {return data[length-1];}
  64.     T& front() {return data[0];}
  65.     iterator erase(iterator it) {
  66.         T* a=it;
  67.         while (++a != data+length) {
  68.             a[-1] = *a;
  69.             }
  70.         length--;
  71.         return it;
  72.         }
  73.     /*iterator*/T* insert(iterator where, const T& what = T()) {
  74.         int z=where-data;
  75.         if (length==allocated) {
  76.             allocated+=16;
  77.             data=(T*)realloc(data,allocated*sizeof(T));
  78.             }
  79.         T* a=data+length;
  80.         T* b=data+z;
  81.         length++;
  82.         while (a != b) {
  83.             *a = a[-1];
  84.             --a;
  85.             }
  86.         *a = what;
  87.         return /*iterator*/a;
  88.         }
  89.     };
  90.  
  91. #endif
  92.