intmap.h

00001 /* MLPACK 0.2
00002  *
00003  * Copyright (c) 2008, 2009 Alexander Gray,
00004  *                          Garry Boyer,
00005  *                          Ryan Riegel,
00006  *                          Nikolaos Vasiloglou,
00007  *                          Dongryeol Lee,
00008  *                          Chip Mappus, 
00009  *                          Nishant Mehta,
00010  *                          Hua Ouyang,
00011  *                          Parikshit Ram,
00012  *                          Long Tran,
00013  *                          Wee Chin Wong
00014  *
00015  * Copyright (c) 2008, 2009 Georgia Institute of Technology
00016  *
00017  * This program is free software; you can redistribute it and/or
00018  * modify it under the terms of the GNU General Public License as
00019  * published by the Free Software Foundation; either version 2 of the
00020  * License, or (at your option) any later version.
00021  *
00022  * This program is distributed in the hope that it will be useful, but
00023  * WITHOUT ANY WARRANTY; without even the implied warranty of
00024  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00025  * General Public License for more details.
00026  *
00027  * You should have received a copy of the GNU General Public License
00028  * along with this program; if not, write to the Free Software
00029  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00030  * 02110-1301, USA.
00031  */
00038 #ifndef COL_INTMAP_H
00039 #define COL_INTMAP_H
00040 
00041 #include "fastlib/base/base.h"
00042 
00046 template<class TValue>
00047 class DenseIntMap {
00048  public:
00049   typedef TValue Value;
00050 
00051  private:
00052   Value *ptr_;
00053   index_t size_;
00054   Value default_value_;
00055 
00056   OT_DEF(DenseIntMap) {
00057     OT_MY_OBJECT(size_);
00058     OT_MY_OBJECT(default_value_);
00059     OT_MALLOC_ARRAY(ptr_, size_);
00060   }
00061 
00062  public:
00064   void Init() {
00065     ptr_ = NULL;
00066     size_ = 0;
00067   }
00068 
00070   Value& default_value() {
00071     return default_value_;
00072   }
00074   const Value& default_value() const {
00075     return default_value_;
00076   }
00077 
00084   index_t size() const {
00085     return size_;
00086   }
00087 
00093   Value& operator [] (index_t index) {
00094     DEBUG_BOUNDS(index, BIG_BAD_NUMBER);
00095     if (unlikely(index >= size_)) {
00096       index_t old_size = size_;
00097       size_ = std::max(size_ * 2, index + 1);
00098       ptr_ = mem::Realloc(ptr_, size_);
00099       for (index_t i = old_size; i < size_; i++) {
00100         new(ptr_+i)Value(default_value_);
00101       }
00102     }
00103     return ptr_[index];
00104   }
00108   const Value& operator [] (index_t index) const {
00109     return get(index);
00110   }
00114   const Value& get(index_t index) const {
00115     DEBUG_BOUNDS(index, BIG_BAD_NUMBER);
00116     if (likely(index < size_)) {
00117       return ptr_[index];
00118     } else {
00119       return default_value_;
00120     }
00121   }
00122 };
00123 
00124 #endif
Generated on Mon Jan 24 12:04:37 2011 for FASTlib by  doxygen 1.6.3