Dictionary.cpp

Go to the documentation of this file.
00001 /*
00002 SagaEngine library
00003 Copyright (c) 2002-2006 Skalden Studio AS
00004 
00005 This software is provided 'as-is', without any express or implied 
00006 warranty. In no event will the authors be held liable for any 
00007 damages arising from the use of this software.
00008 
00009 Permission is granted to distribute the library under the terms of the 
00010 Q Public License version 1.0. Be sure to read and understand the license
00011 before using the library. It should be included here, or you may read it
00012 at http://www.trolltech.com/products/qt/licenses/licensing/qpl
00013 
00014 The original version of this library can be located at:
00015 http://www.sagaengine.com/
00016 
00017 Rune Myrland
00018 rune@skalden.com
00019 */
00020 
00021 
00022 #include "Dictionary.hpp"
00023 #include "util/error/Log.hpp"
00024 #include "../config/sim_config.hpp"
00025 #include <cstring>
00026 
00027 namespace se_core {
00028 
00029     Dictionary
00030     ::Dictionary()
00031             : entryCount_(0)
00032               , entries_(new const DictionaryEntry*[ MAX_ENTRIES ]) {
00033     }
00034 
00035 
00036     Dictionary
00037     ::~Dictionary() {
00038         delete[] entries_;
00039     }
00040 
00041 
00042     short Dictionary
00043     ::highestId(short type) const {
00044         int v = -1;
00045         for(int i = 0; i < entryCount_; ++i) {
00046             if(type == entries_[i]->type_ && v < entries_[i]->id_) {
00047                 v = entries_[i]->id_;
00048             }
00049         }
00050         return v;
00051     }
00052 
00053 
00054     void Dictionary
00055     ::add(const DictionaryEntry* entry) {
00056         if(checkName(entry->type_, entry->name_, entry->id_)) {
00057             // Duplicate
00058             return;
00059         }
00060         Assert(entryCount_ < MAX_ENTRIES);
00061         if(entry->type_ == DE_DICTIONARY_TYPE) {
00062             LogDetail("Registered dictionary with name " << entry->name_ << ", type " << entry->type_ << " and id " << entry->id_);
00063         }
00064         entries_[ entryCount_++ ] = entry;
00065         LogWarning(entryCount_ << " " << entry->name_);
00066     }
00067 
00068 
00069     void Dictionary
00070     ::remove(const DictionaryEntry* entry) {
00071         for(int i = 0; i < entryCount_; ++i) {
00072             if(entries_[i] == entry) {
00073                 entries_[i] = entries_[ --entryCount_ ];
00074             }
00075         }
00076     }
00077 
00078 
00079     short Dictionary
00080     ::id(short type, const char* name) {
00081         //LogDetail(entryCount_);
00082         for(int i = 0; i < entryCount_; ++i) {
00083             //LogDetail("Read dictionary entry: " << entries_[i]->type_ << ", " << entries_[i]->name_);
00084             //LogDetail(i << ", " << entries_[i]->type_ << ", " << entries_[i]->name_ << " == " << entries_[i]->id_);
00085             if(entries_[i]->type_ == type && strcmp(entries_[i]->name_, name) == 0) {
00086                 return entries_[i]->id_;
00087             }
00088         }
00089         LogFatal("Unknown dictionary entry of type" << type << ": " << name);
00090         return -1;
00091     }
00092 
00093 
00094     bool Dictionary
00095     ::checkName(short type, const char* name, short id) {
00096         for(int i = 0; i < entryCount_; ++i) {
00097             if(entries_[i]->type_ == type && strcmp(entries_[i]->name_, name) == 0) {
00098                 if(id == entries_[i]->id_) {
00099                     LogDetail("Dictionary duplicate: " << type << ", " << name);
00100                     return true;
00101                 }
00102                 else {
00103                     LogWarning("Same name 2 ids: " << type << ", " << name);
00104                 }
00105             }
00106         }
00107         return false;
00108     }
00109 
00110 
00111     const char* Dictionary
00112     ::name(short type, short id) {
00113         for(int i = 0; i < entryCount_; ++i) {
00114             if(entries_[i]->type_ == type && entries_[i]->id_ == id) {
00115                 return entries_[i]->name_;
00116             }
00117         }
00118 
00119         LogFatal("Unkown dictionary entry: " << type << ", " << id);
00120         return 0;
00121     }
00122 
00123 
00124     bool Dictionary
00125     ::hasId(short type, short id) const {
00126         for(int i = 0; i < entryCount_; ++i) {
00127             if(entries_[i]->type_ == type && entries_[i]->id_ == id) {
00128                 return true;
00129             }
00130         }
00131 
00132         return false;
00133     }
00134 
00135 
00136     short Dictionary
00137     ::hash(const char* name) {
00138         int h = 2166136261;
00139         for(const char* n = name; *n != 0; ++n) {
00140             h *= 16777619 * h;
00141             h ^= *n;
00142         }
00143         h ^= h >> 16;
00144         return (short)(h & 0xffff);
00145     }
00146 
00147 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:12 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo