diff -r 000000000000 -r c455ebb9a5dc jitcraft/StringPool.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jitcraft/StringPool.h Mon Jun 25 02:57:28 2012 -0700 @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2012 David Anderson + * + * This file is part of JITCraft. + * + * JITCraft is free software: you can redistribute it and/or modify it under + * the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with + * JITCraft. If not, see http://www.gnu.org/licenses/. + */ +#ifndef _include_jitcraft_string_pool_h_ +#define _include_jitcraft_string_pool_h_ + +#include +#include +#include "PoolAllocator.h" + +namespace jc { + +typedef const char Name; + +// Guarantee that strings with the same contents will map to one pointer. The +// strings are pool allocated, and search is linear for simplicity. +class StringPool +{ + PoolAllocator &pool_; + std::vector strings_; + + public: + StringPool(PoolAllocator &pool) + : pool_(pool) + { + } + + Name *add(const char *string) { + for (size_t i = 0; i < strings_.size(); i++) { + if (strcmp(strings_[i], string) == 0) + return strings_[i]; + } + char *name = (char *)pool_.allocate(strlen(string) + 1); + strcpy(name, string); + strings_.push_back(name); + return name; + } +}; + +} + +#endif // _include_jitcraft_string_pool_h_