Advertisement
Guest User

HGETileMapDatabases.h

a guest
Oct 5th, 2013
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.73 KB | None | 0 0
  1. #ifndef DB_H
  2. #define DB_H
  3.  
  4. #include <string>
  5. #include <algorithm>
  6.  
  7. //////////////////////////////////////////////////////////////////////////
  8. // Class pointer database
  9.  
  10. #define DATABASE_MAX 10240
  11.  
  12. template <class CLS>
  13. class CPDatabase {
  14. private:
  15.    
  16.     CLS *objlist[DATABASE_MAX];
  17.     int cobject;
  18.  
  19. public:
  20.    
  21.     CPDatabase() {
  22.         cobject = 0;
  23.        
  24.         for (int i=0; i<100; i++)
  25.             objlist[i] = NULL;
  26.     }
  27.  
  28.  
  29.     CLS *Add(CLS *Obj) {
  30.         objlist[cobject] = Obj;
  31.         cobject ++;
  32.         return Obj;
  33.     }
  34.  
  35.    
  36.     bool Remove(CLS *Obj) {
  37.         for (int i=0; i<cobject; i++) {
  38.             if (Obj == objlist[i]) {
  39.                 cobject --;
  40.  
  41.                 for (int j=i; j<cobject; j++)
  42.                     objlist[j] = objlist[j+1];
  43.  
  44.                 objlist[cobject] = NULL;
  45.                 return true;
  46.             }
  47.         }
  48.         return false;
  49.     }
  50.    
  51.  
  52.     CLS *Fetch(int idx) {
  53.         return objlist[idx];
  54.     }
  55.  
  56.     int Max() { return cobject; }
  57.     int size() { return cobject; }
  58.  
  59.     void clear() { cobject = 0; }
  60.  
  61.     CLS *operator [] (unsigned idx) { return objlist[idx]; }
  62.  
  63. };
  64.  
  65. //////////////////////////////////////////////////////////////////////////
  66. // struct database
  67.  
  68. #define SDATABASE_MAX 4096
  69.  
  70. template <class CLS>
  71. class CSDatabase {
  72. private:
  73.    
  74.     CLS objlist[SDATABASE_MAX];
  75.     int cobject;
  76.  
  77. public:
  78.    
  79.     CSDatabase() {
  80.         cobject = 0;
  81.     }
  82.  
  83.  
  84.     void Add(CLS Obj) {
  85.         objlist[cobject] = Obj;
  86.         cobject ++;
  87.     }
  88.  
  89.     bool Remove(int idx) {
  90.         for (int i=0; i<cobject; i++) {
  91.             if (idx == i) {
  92.                 cobject -= 1;
  93.  
  94.                 for (int j=i; j<cobject; j++)
  95.                     objlist[j] = objlist[j+1];
  96.  
  97.                 return true;
  98.             }
  99.         }
  100.         return false;
  101.     }
  102.  
  103.     CLS Fetch(int idx) {
  104.         return objlist[idx];
  105.     }
  106.  
  107.     CLS *FetchPointer(int idx) {
  108.         return &objlist[idx];
  109.     }
  110.  
  111.     int Max() { return cobject; }
  112.  
  113.     void Clear() { cobject = 0; }
  114.  
  115. };
  116.  
  117. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement