Main Page | Namespace List | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | Examples

small_map.h

00001 /* Copyright 2004
00002 Stanford University
00003 
00004 This file is part of the DSR PDB Library.
00005 
00006 The DSR PDB Library is free software; you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation; either version 2.1 of the License, or (at your
00009 option) any later version.
00010 
00011 The DSR PDB Library is distributed in the hope that it will be useful, but
00012 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
00013 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00014 License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with the DSR PDB Library; see the file COPYING.LIB.  If not, write to
00018 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00019 MA 02111-1307, USA. */
00020 
00021 
00022 #ifndef DSR_PDB_SMALL_MAP_H
00023 #define DSR_PDB_SMALL_MAP_H
00024 
00025 #include <vector>
00026 #include <algorithm>
00027 
00028 namespace dsrpdb {
00029   template <class Key, class Data>
00030   class small_map {
00031   public:
00032     typedef std::pair<Key, Data> value_type;
00033     typedef Key key_type;
00034     typedef Data data_type;
00035     typedef std::vector<value_type> container;
00036     typedef typename container::iterator iterator;
00037     typedef typename container::const_iterator const_iterator;
00038 
00039 
00040     small_map(std::size_t sz=0){c_.reserve(sz);}
00041 
00042     iterator find(key_type k) {
00043       for (iterator it= c_.begin(); it != c_.end(); ++it){
00044         if (it->first==k) return it;
00045       }
00046       return end();
00047     }
00048     const_iterator find(key_type k) const {
00049       for (const_iterator it= c_.begin(); it != c_.end(); ++it){
00050         if (it->first==k) return it;
00051       }
00052       return end();
00053     }
00054 
00055     iterator begin() {
00056       return c_.begin();
00057     }
00058 
00059     iterator end() {
00060       return c_.end();
00061     }
00062 
00063     const_iterator begin() const {
00064       return c_.begin();
00065     }
00066 
00067     const_iterator end() const {
00068       return c_.end();
00069     }
00070 
00071     data_type& operator[](key_type k){
00072       iterator it= find(k);
00073       if (it != end()) return it->second;
00074       else {
00075         c_.push_back(value_type(k,data_type()));
00076         return c_.back().second;
00077       }
00078     }
00079 
00080     void insert(const value_type &v) {
00081       c_.push_back(v);
00082     }
00083 
00084     std::size_t size() const {
00085       return c_.size();
00086     }
00087 
00088   protected:
00089         container c_;
00090   };
00091 }
00092 #endif