[Lemon-commits] [lemon_svn] klao: r466 - in hugo/trunk/src/work: . klao

Lemon SVN svn at lemon.cs.elte.hu
Mon Nov 6 20:39:36 CET 2006


Author: klao
Date: Sat Apr 17 03:57:48 2004
New Revision: 466

Added:
   hugo/trunk/src/work/klao/iter_map.h
   hugo/trunk/src/work/klao/iter_map_test.cc
Removed:
   hugo/trunk/src/work/klao/minlengthpaths.cc
   hugo/trunk/src/work/klao/minlengthpaths.h
Modified:
   hugo/trunk/src/work/klao/   (props changed)
   hugo/trunk/src/work/klao/Makefile
   hugo/trunk/src/work/makefile

Log:
A generic map with value type [0, N) where N is a small integer.
Can enumerate keys with a given value.


Modified: hugo/trunk/src/work/klao/Makefile
==============================================================================
--- hugo/trunk/src/work/klao/Makefile	(original)
+++ hugo/trunk/src/work/klao/Makefile	Sat Apr 17 03:57:48 2004
@@ -1,4 +1,4 @@
-BINARIES = path_test map_test minlengthpaths
+BINARIES = path_test map_test iter_map_test
 INCLUDEDIRS= -I. -I.. -I../../include -I../{marci,jacint,alpar,johanna,akos}
 include ../makefile
 

Added: hugo/trunk/src/work/klao/iter_map.h
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/klao/iter_map.h	Sat Apr 17 03:57:48 2004
@@ -0,0 +1,121 @@
+// -*- c++ -*- //
+
+#ifndef HUGO_ITER_MAP
+#define HUGO_ITER_MAP
+
+#include <vector>
+#include <algorithm>
+// for uint8_t
+#include <stdint.h>
+// for memset
+#include <cstring>
+
+
+namespace hugo {
+
+
+  /// \todo Decide whether we need all the range checkings!!!
+
+  template<typename KeyIntMap, uint8_t N>
+  class IterableMap {
+  public:
+
+    typedef typename KeyIntMap::KeyType KeyType;
+    typedef uint8_t ValueType;
+
+    typedef typename std::vector<KeyType>::const_iterator iterator;
+
+  protected:
+    KeyIntMap &base;
+    std::vector<KeyType> data;
+    size_t bounds[N];
+
+    uint8_t find(size_t a) const {
+      uint8_t n=0;
+      for(; n<N && bounds[n]<=a; ++n);
+      return n;
+    }
+
+    void half_swap(size_t &a, size_t b) {
+      if(a != b) {
+	base.set(data[b],a);
+	data[a] = data[b];
+	a = b;
+      }
+    }
+
+    size_t move(size_t a, uint8_t m, uint8_t n) {
+      if(m != n) {
+	size_t orig_a = a;
+	KeyType orig_key = data[a];
+	while(m > n) {
+	  --m;
+	  half_swap(a, bounds[m]++);
+	}
+	while(m < n) {
+	  half_swap(a, --bounds[m]);
+	  ++m;
+	}
+	if(a != orig_a) {
+	  base.set(orig_key, a);
+	  data[a]=orig_key;
+	}
+      }
+      return a;
+    }
+
+  public:
+    
+    IterableMap(KeyIntMap &_base) : base(_base) {
+      memset(bounds, 0, sizeof(bounds));
+      //    for(int i=0; i<N; ++i) { bounds[i]=0; }
+    }
+
+    uint8_t operator[](const KeyType& k) const {
+      return find(base[k]);
+    }
+
+    void set(const KeyType& k, uint8_t n) {
+      size_t a = base[k];
+      if(a < bounds[N-1]) {
+	base.set(k, move(a, find(a), n));
+      }
+    }
+
+    void insert(const KeyType& k, uint8_t n) {
+      if(n<N) {
+	data.push_back(k);
+	base.set(k, move(bounds[N-1]++, N-1, n));
+      }
+    }
+
+    iterator begin(uint8_t n) const {
+      if(n < N)
+	return data.begin() + (n ? bounds[n-1] : 0);
+      else
+	return data.end();
+    }
+
+    iterator end(uint8_t n) const {
+      if(n < N)
+	return data.begin() + bounds[n];
+      else
+	return data.end();
+    }
+
+    size_t size(uint8_t n) const {
+      if(n < N)
+	return bounds[n] - (n ? bounds[n-1] : 0);
+      else
+	return 0;
+    }
+    
+    size_t size() const {
+      // assert(bounds[N-1] == data.size());
+      return bounds[N-1];
+    }
+
+  };
+
+}
+#endif

Added: hugo/trunk/src/work/klao/iter_map_test.cc
==============================================================================
--- (empty file)
+++ hugo/trunk/src/work/klao/iter_map_test.cc	Sat Apr 17 03:57:48 2004
@@ -0,0 +1,117 @@
+#include <iter_map.h>
+#include <maps.h>
+
+#include <iostream>
+
+using namespace hugo;
+using namespace std;
+
+const int N = 3;
+
+typedef StdMap<int,int> BaseMap;
+typedef IterableMap<BaseMap, N> TestMap;
+
+
+void print(TestMap const& m) {
+  cout << "Size of the map: " << m.size() << endl;
+  for(int i=0; i<N; ++i) {
+    cout << "  Class " << i << ". (size=" << m.size(i) << "): " << flush;
+    cout << "    ";
+    for(TestMap::iterator j = m.begin(i); j!=m.end(i); ++j) {
+      cout << " " << *j;
+    }
+    cout << endl;
+  }
+}
+
+
+
+int main() {
+
+  BaseMap base(344);
+  TestMap test(base);
+
+
+  print(test);
+
+  cout << "Inserting 12 to class 2...\n";
+  test.insert(12,2);
+  print(test);
+
+
+  cout << "Inserting 22 to class 2...\n";
+  test.insert(22,2);
+  print(test);
+
+  cout << "Testing some map values:\n";
+  cout << " 12: " << int(test[12]) << endl;
+
+  cout << "Inserting 10 to class 0...\n";
+  test.insert(10,0);
+  print(test);
+
+  cout << "Testing some map values:\n";
+  cout << " 12: " << int(test[12]) << endl;
+
+  cout << "Inserting 11 to class 1...\n";
+  test.insert(11,1);
+  print(test);
+
+  cout << "Testing some map values:\n";
+  cout << " 12: " << int(test[12]) << endl;
+  cout << " 22: " << int(test[22]) << endl;
+  cout << " 10: " << int(test[10]) << endl;
+  cout << " 11: " << int(test[11]) << endl;
+  cout << " 42: " << int(test[42]) << endl;
+
+  cout << "Inserting 21 to class 1...\n";
+  test.insert(21,1);
+  print(test);
+
+  cout << "Inserting 20 to class 1...\n";
+  test.insert(20,0);
+  print(test);
+
+  cout << "Testing some map values:\n";
+  cout << " 12: " << int(test[12]) << endl;
+  cout << " 22: " << int(test[22]) << endl;
+  cout << " 10: " << int(test[10]) << endl;
+  cout << " 20: " << int(test[20]) << endl;
+  cout << " 11: " << int(test[11]) << endl;
+  cout << " 21: " << int(test[21]) << endl;
+  cout << " 42: " << int(test[42]) << endl;
+
+  cout << "Setting 20 to class 2...\n";
+  test.set(20,2);
+  print(test);
+  
+  cout << "Setting 10 to class 1...\n";
+  test.set(10,1);
+  print(test);
+  
+  cout << "Setting 11 to class 1...\n";
+  test.set(11,1);
+  print(test);
+  
+  cout << "Setting 12 to class 1...\n";
+  test.set(12,1);
+  print(test);
+  
+  cout << "Setting 21 to class 2...\n";
+  test.set(21,2);
+  print(test);
+  
+  cout << "Setting 22 to class 2...\n";
+  test.set(22,2);
+  print(test);
+  
+  cout << "Testing some map values:\n";
+  cout << " 12: " << int(test[12]) << endl;
+  cout << " 22: " << int(test[22]) << endl;
+  cout << " 10: " << int(test[10]) << endl;
+  cout << " 20: " << int(test[20]) << endl;
+  cout << " 11: " << int(test[11]) << endl;
+  cout << " 21: " << int(test[21]) << endl;
+  cout << " 42: " << int(test[42]) << endl;
+
+}

Modified: hugo/trunk/src/work/makefile
==============================================================================
--- hugo/trunk/src/work/makefile	(original)
+++ hugo/trunk/src/work/makefile	Sat Apr 17 03:57:48 2004
@@ -1,7 +1,7 @@
 INCLUDEDIRS ?= -I../include -I. -I./{marci,jacint,alpar,klao,akos}
 CXXFLAGS = -g -O -Wall $(INCLUDEDIRS) -ansi -pedantic
 
-BINARIES ?= bin_heap_demo iterator_bfs_demo
+BINARIES ?= bin_heap_demo
 
 # Hat, ez elismerem, hogy nagyon ronda, de mukodik minden altalam
 # ismert rendszeren :-)  (Misi)



More information about the Lemon-commits mailing list