summaryrefslogtreecommitdiff
path: root/omegalib/omega_lib/include/basic/Map.c
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2016-09-17 19:27:15 +0000
committerTuowen Zhao <ztuowen@gmail.com>2016-09-17 19:27:15 +0000
commitad1cadf3512f3dd789151983e5c93af411f929db (patch)
treed6d25d562617d9bec7b13286cf413ed8f7567275 /omegalib/omega_lib/include/basic/Map.c
parentcfafd2ffcad803e7bb02b60c085eafd73f28f87a (diff)
downloadchill-ad1cadf3512f3dd789151983e5c93af411f929db.tar.gz
chill-ad1cadf3512f3dd789151983e5c93af411f929db.tar.bz2
chill-ad1cadf3512f3dd789151983e5c93af411f929db.zip
restructure
Diffstat (limited to 'omegalib/omega_lib/include/basic/Map.c')
-rw-r--r--omegalib/omega_lib/include/basic/Map.c63
1 files changed, 63 insertions, 0 deletions
diff --git a/omegalib/omega_lib/include/basic/Map.c b/omegalib/omega_lib/include/basic/Map.c
new file mode 100644
index 0000000..69cc3f7
--- /dev/null
+++ b/omegalib/omega_lib/include/basic/Map.c
@@ -0,0 +1,63 @@
+namespace omega {
+
+template<class K, class V> MapElement<K,V>:: MapElement(const MapElement<K,V>& M) {
+ if (M.tail) tail = new MapElement<K,V>(*M.tail);
+ else tail = 0;
+ k = M.k;
+ v = M.v;
+ }
+
+template<class K, class V> MapElement<K,V> &
+ MapElement<K,V>:: operator=(const MapElement<K,V>& M) {
+ if (this != &M) {
+ if (tail) delete tail;
+ if (M.tail) tail = new MapElement<K,V>(*M.tail);
+ else tail = 0;
+ k = M.k;
+ v = M.v;
+ }
+ return *this;
+ }
+
+
+
+
+#if ! defined linux
+template <class K, class V> Map <K,V>::Map(const V &default_value)
+#else
+template <class K, class V> Map <K,V>::Map(V default_value)
+#endif
+ : _default_value(default_value)
+ {
+ contents = 0;
+ }
+
+template <class K, class V> Map <K,V>::~Map()
+ {
+ delete contents;
+ }
+
+template <class K, class V> V Map<K,V>::operator()(K k) const {
+ MapElement <K,V> * P = contents;
+ while (P) {
+ if (P->k == k) return P->v;
+ P = P->tail;
+ };
+ return _default_value;
+ }
+
+template <class K, class V> V & Map<K,V>::operator[](K k) {
+ MapElement <K,V> * P = contents;
+ while (P) {
+ if (P->k == k) return P->v;
+ P = P->tail;
+ };
+ P = new MapElement <K,V>;
+ P->k = k;
+ P->v = _default_value;
+ P->tail = contents;
+ contents = P;
+ return P->v;
+ }
+
+} // namespace