Associating Information with Geometric ObjectsMaps and Hashing ArraysPoints, lines, segments, rays, and circles have id-numbers and hence Hashing Arrays and Maps can be defined for them. Maps and Hashing Arrays associate information with representation objects, i.e., only identical objects share their information.ExampleThe following example shows how to use a map to associate a "color"
with two-dimensional points. We write #include <LEDA/geo/point.h> #include <LEDA/geo/float_kernel_names.h> #include <LEDA/core/map.h> using namespace leda; int main() { map<POINT,int> color; POINT p(0,0); color[p]=0; POINT q(0,0); color[q]=1; POINT r=p; std::cout << color[p] << color[q] << color[r] << std::endl; //outputs 010 return 0; } Dictionaries and Dictionary ArraysFor points we can also use Dictionaries and Dictionary Arrays. For the other geometric objects this requires the definition of a compare function. In Dictionaries and Dictionary Arrays equal objects share the same information.Example 1We replace in the above example the #include <LEDA/geo/point.h> #include <LEDA/geo/float_kernel_names.h> #include <LEDA/core/d_array.h> using namespace leda; int main() { d_array<POINT,int> color; POINT p(0,0); color[p]=0; POINT q(0,0); color[q]=1; POINT r=p; std::cout << color[p] << color[q] << color[r] << std::endl; //outputs 111 return 0; } Example 2Dictionary Arrays are useful for removing multiple occurrences of equal objects. This program removes all but the first occurrence of every point from L. If a Map is used instead of a Dictionary Array this will not work.#include <LEDA/geo/point.h> #include <LEDA/geo/float_kernel_names.h> #include <LEDA/core/d_array.h> #include <LEDA/core/list.h> using namespace leda; int main() { list<POINT> L; L.append(POINT(0,0)); L.append(POINT(2,3)); L.append(POINT(0,0)); L.append(POINT(1,5)); d_array<POINT,bool> first_occurrence(true); list_item it; forall_items(it,L) { if (!first_occurrence[L[it]]) L.del_item(it); else first_occurrence[L[it]]=false; } POINT p; forall(p,L) std::cout << p << std::endl; return 0; } |
See also:Handle Types, Identity, Equality Writing Kernel Independent Code |