00001 #include "NameDict.h"
00002
00003 #include <iostream>
00004
00005 using namespace X3DTK;
00006 using namespace std;
00007
00008 NameDict::NameDict()
00009 {
00010 }
00011
00012 NameDict::~NameDict()
00013 {
00014 _nameDict.clear();
00015 }
00016
00017 void NameDict::insert(const SFString &name, const SFNode &N)
00018 {
00019 _nameDict.insert(X3DDict::value_type(name, N));
00020 }
00021
00022 void NameDict::remove(SFNode N)
00023 {
00024 for (X3DDict::iterator it = _nameDict.begin(); it != _nameDict.end(); ++it)
00025 {
00026 if ((*it).second == N)
00027 {
00028 _nameDict.erase(it);
00029 return;
00030 }
00031 }
00032 }
00033
00034 void NameDict::makeDEFNonAmbiguous()
00035 {
00036 X3DDict nameDictCopy;
00037 nameDictCopy.swap(_nameDict);
00038
00039
00040 for (X3DDict::iterator it = nameDictCopy.begin(); it != nameDictCopy.end(); ++it)
00041 {
00042 SFString name = (*it).first;
00043 SFString nname = name;
00044
00045 unsigned int k = 1;
00046 while (_nameDict.find(nname) != _nameDict.end())
00047 {
00048 nname = name + "_" + SFString::number(k);
00049 ++k;
00050 }
00051 _nameDict.insert(X3DDict::value_type(nname, (*it).second));
00052 }
00053 }
00054
00055 SFNode NameDict::getNodeOfName(const SFString &name) const
00056 {
00057 X3DDict::const_iterator it = _nameDict.find(name);
00058 if (it != _nameDict.end())
00059 return (*it).second;
00060 return 0;
00061 }
00062
00063 SFString NameDict::getNameOfNode(const SFNode &N) const
00064 {
00065 for (X3DDict::const_iterator it = _nameDict.begin(); it != _nameDict.end(); ++it)
00066 {
00067 if ((*it).second == N)
00068 return (*it).first;
00069 }
00070
00071 return SFString("");
00072 }
00073
00074 void NameDict::printDict() const
00075 {
00076 cout << "DEF dictionary" << endl;
00077 for (X3DDict::const_iterator it = _nameDict.begin(); it != _nameDict.end(); ++it)
00078 cout << (*it).first << " -> " << (*it).second << endl;
00079 cout << "end dictionary" << endl;
00080 }