src/StringPool.h
author David Anderson <dvander@alliedmods.net>
Sun Nov 18 14:56:09 2012 -0800 (2012-11-18)
changeset 213 a9b8d24967d6
parent 195 c1ba166f3fc4
child 263 ba85a47ee414
permissions -rw-r--r--
Fix GCC retardedness.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of SourcePawn.
     6  *
     7  * SourcePawn is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #ifndef _include_jitcraft_string_pool_h_
    20 #define _include_jitcraft_string_pool_h_
    21 
    22 #include <string.h>
    23 #include "AllocatorPolicies.h"
    24 #include "HashTable.h"
    25 #include "Strings.h"
    26 
    27 namespace ke {
    28 
    29 class Zone;
    30 class String;
    31 
    32 // Guarantee that strings with the same contents will map to one pointer. 
    33 class StringPool
    34 {
    35     struct Policy
    36     {
    37         typedef String * Payload;
    38 
    39         static uint32 hash(const CharsAndLength &key) {
    40             return HashCharSequence(key.str(), key.length());
    41         }
    42         
    43         static bool matches(const CharsAndLength &key, String *string) {
    44             if (string->length() != key.length())
    45                 return false;
    46             return memcmp(string->chars(), key.str(), key.length() * sizeof(char)) == 0;
    47         }
    48     };
    49 
    50     typedef HashTable<Policy, ZoneAllocatorPolicy> StringSet;
    51 
    52   public:
    53     StringPool(Zone *zone);
    54     ~StringPool();
    55     bool initialize();
    56 
    57   public:
    58     String *add(const char *str, size_t len);
    59     void sweep();
    60 
    61   private:
    62     Zone *zone_;
    63     StringSet set_;
    64 };
    65 
    66 }
    67 
    68 #endif // _include_jitcraft_string_pool_h_