ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/branches/new_design/OOPSE-3.0/src/utils/TypeContainer.hpp
Revision: 1720
Committed: Sat Nov 6 05:21:55 2004 UTC (19 years, 9 months ago) by tim
File size: 5263 byte(s)
Log Message:
adding ForceField and TypeContainer classes

File Contents

# User Rev Content
1 tim 1720 /*
2     * Copyright (C) 2000-2004 Object Oriented Parallel Simulation Engine (OOPSE) project
3     *
4     * Contact: oopse@oopse.org
5     *
6     * This program is free software; you can redistribute it and/or
7     * modify it under the terms of the GNU Lesser General Public License
8     * as published by the Free Software Foundation; either version 2.1
9     * of the License, or (at your option) any later version.
10     * All we ask is that proper credit is given for our work, which includes
11     * - but is not limited to - adding the above copyright notice to the beginning
12     * of your source code files, and to any copyright notice that you may distribute
13     * with programs based on this work.
14     *
15     * This program is distributed in the hope that it will be useful,
16     * but WITHOUT ANY WARRANTY; without even the implied warranty of
17     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18     * GNU Lesser General Public License for more details.
19     *
20     * You should have received a copy of the GNU Lesser General Public License
21     * along with this program; if not, write to the Free Software
22     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23     *
24     */
25    
26     /**
27     * @file TypeContainer.hpp
28     * @author tlin
29     * @date 11/04/2004
30     * @time 13:51am
31     * @version 1.0
32     */
33    
34     #ifndef UTILS_TYPECONTAINER_HPP
35     #define UTILS_TYPECONTAINER_HPP
36    
37     #include <map>
38     #include <vector>
39     namespace oopse {
40    
41     /**
42     * @class TypeContainer TypeContainer.hpp "utils/TypeContainer.hpp"
43     */
44     template<class ElemType, int SIZE>
45     class TypeContainer {
46     public:
47    
48     typedef ElemType* ElemPtr;
49     typedef std::vector<std::string> KeyType;
50     typedef typename KeyType::iterator KeyTypeIterator;
51     typedef std::pair<int, ElemPtr> ValueType;
52     typedef std::map<KeyType, ValueType> MapType;
53     typedef std::map<KeyType, ValueType>::iterator MapTypeIterator;
54    
55     TypeContainer() index_(0) {}
56    
57     ~TypeContainer() {
58     MapTypeIterator i;
59     for (i = data_.begin(); i != data_.end(); ++i) {
60     delete (i->second).second;
61     }
62     data_.clear();
63     }
64    
65     bool add(KeyType& keys, ElemPtr elem) {
66     assert(keys.size() == SIZE);
67     return data_.insert(MapType::value_type(keys, make_pair(index_++,elem))).second;
68     }
69    
70     /** Exact Match */
71     ElemPtr find(KeyType& keys) {
72     assert(keys.size() == SIZE);
73     MapTypeIterator i;
74     ValueType foundType;
75    
76     i = data_.find(keys);
77     if (i != data_.end()) {
78     return (i->second).second
79     }
80    
81     KeyType reversedKeys = keys;
82     std::reverse(reversedKeys.begin(), reversedKeys.end());
83    
84     i = data_.find(keys);
85     if (i != data_.end()) {
86     return (i->second).second
87     } else {
88     return NULL;
89     }
90    
91     }
92    
93     /**
94     * @todo
95     */
96     ElemPtr find(KeyType& keys, const std::string& wildCard) {
97     assert(keys.size() == SIZE);
98    
99     std::vector<KeyTypeIterator> iterCont;
100     KeyType replacedKey;
101     MapTypeIterator i;
102     std::vector<ValueType> foundTypes;
103    
104     while (replaceWithWildCard(iterCont, keys, replacedKey, wildCard)) {
105     i = data_.find(replacedKey);
106     if (i != data_.end()) {
107     foundTypes.push_back(i->second);
108     }
109     }
110    
111     //reverse the order of keys
112     KeyType reversedKeys = keys;
113     std::reverse(reversedKeys.begin(), reversedKeys.end());
114    
115     //if the reversedKeys is the same as keys, just skip it
116     if (reversedKeys == keys) {
117    
118    
119     //empty the iterator container
120     iterCont.clear();
121    
122     while (replaceWithWildCard(iterCont, keys, replacedKey, wildCard)) {
123     i = data_.find(replacedKey);
124     if (i != data_.end()) {
125     foundTypes.push_back(i->second);
126     }
127     }
128    
129     //replaceWithWildCard can not generate this particular sequence, we have to
130     //do it by manually
131     KeyType allWildCards(SIZE, wildCard);
132     i = data_.find(replacedKey);
133     if (i != data_.end()) {
134     foundTypes.push_back(i->second);
135     }
136    
137     }
138    
139     std::vector<ValueType>::iterator j;
140     j = std::min_element(foundTypes.begin(), foundTypes.end());
141    
142     return j == foundTypes.end() ? NULL : (j->second).second;
143     }
144    
145     private:
146     int index_;
147     MapType data_;
148    
149     };
150    
151    
152     }//end namespace oopse
153    
154     #endif //UTILS_TYPECONTAINER_HPP