FiberSpace.hpp

00001 #ifndef __FIBER_GRID_FIBER_HPP
00002 #define __FIBER_GRID_FIBER_HPP "Created 27.02.2001 21:42:27 by werner"
00003 
00004 #include "GridAPI.h"
00005 #include <field/Field.hpp>
00006 #include <memcore/TypeInfo.hpp>
00007 #include <set>
00008 #include <list>
00009 
00010 #include "BaseSpace.hpp"
00011 
00012 namespace Fiber
00013 {
00014         using MemCore::WeakPtr;
00015         using MemCore::NullPtr;
00016         using std::set;
00017         using std::list;
00018 
00023 template <class ID>
00024 class   GRID_API FiberSpace : public BaseSpaceDependency
00025 {
00026 protected:
00027         typedef map<ID, RefPtr<Field> > Fields_t;
00028         Fields_t Fields;
00029 
00035         RefPtr<Field>&create_entry(const ID&id)
00036         {
00037                 return Fields[ id ];
00038         }
00039 
00040 public: 
00042         FiberSpace()
00043         {} 
00044 
00049         Field&operator[](const ID&id)
00050         {
00051         RefPtr<Field>&F = Fields[ id ];
00052                 if (!F)
00053                         F = new Field();
00054                 return *F;
00055         } 
00056 
00060         RefPtr<Field> find(const ID&id) const
00061         {
00062         typename Fields_t::const_iterator f = Fields.find(id);
00063                 if (f==Fields.end() )
00064                         return NullPtr();
00065                 return f->second;
00066         } 
00067 
00072         RefPtr<Field> operator()(const ID&id) const
00073         {
00074                 return find(id);
00075         } 
00076 
00080         RefPtr<Field>   findMostFragmentedField() const
00081         {
00082                 if (Fields.size()<1)
00083                         return NullPtr();
00084 
00085         typename Fields_t::const_iterator it = Fields.begin();
00086         RefPtr<Field>   result = it->second;
00087 
00088                 if (!result) // no field stored? bad. we are screwed.
00089                         return NullPtr(); 
00090 
00091         size_t  MaxFragmentsSoFar = result -> nFragments();
00092                 for(; it != Fields.end(); it++)
00093                 {
00094                 RefPtr<Field> candidate = it->second; 
00095                         if (!candidate)
00096                                 continue; 
00097 
00098                 size_t  fragments =  candidate->nFragments(); 
00099                         if (fragments > MaxFragmentsSoFar)
00100                         {
00101                                 MaxFragmentsSoFar = fragments; 
00102                                 result = candidate;
00103                         }
00104                 } 
00105                 return result;
00106         } 
00107 
00111         int     getMemoryUsage(memsize_t&UsedMemory, memsize_t&WantedMemory) const
00112         {
00113         int     n = 0;  
00114                 for(typename Fields_t::const_iterator it = Fields.begin(); it != Fields.end(); it++)
00115                 {
00116                 RefPtr<Field> F = it->second; 
00117                         if (!F)
00118                                 continue; 
00119 
00120                         n += F->getMemoryUsage(UsedMemory, WantedMemory);
00121                 } 
00122                 return n;
00123         }
00124 
00125 };
00126 
00127 
00128 } /* namespace Fiber */ 
00129 
00130 #endif /* __FIBER_GRID_FIBER_HPP */
00131