jitcraft/StringPool.h
changeset 54 8e22792a3921
parent 0 c455ebb9a5dc
child 59 b7d61ddb3c8d
     1.1 --- a/jitcraft/StringPool.h	Sat Jul 07 17:11:31 2012 -0700
     1.2 +++ b/jitcraft/StringPool.h	Sat Jul 07 20:28:29 2012 -0700
     1.3 @@ -18,37 +18,66 @@
     1.4  #ifndef _include_jitcraft_string_pool_h_
     1.5  #define _include_jitcraft_string_pool_h_
     1.6  
     1.7 -#include <vector>
     1.8 -#include <string.h>
     1.9 -#include "PoolAllocator.h"
    1.10 +#include "AllocatorPolicies.h"
    1.11 +#include "HashTable.h"
    1.12 +#include "Strings.h"
    1.13  
    1.14  namespace jc {
    1.15  
    1.16 -typedef const char Name;
    1.17 +class Zone;
    1.18 +class String;
    1.19  
    1.20 -// Guarantee that strings with the same contents will map to one pointer. The
    1.21 -// strings are pool allocated, and search is linear for simplicity.
    1.22 +// Guarantee that strings with the same contents will map to one pointer. 
    1.23  class StringPool
    1.24  {
    1.25 -    PoolAllocator &pool_;
    1.26 -    std::vector<Name *> strings_;
    1.27 +    class CharsAndLength {
    1.28 +        const char *str_;
    1.29 +        size_t length_;
    1.30 +
    1.31 +      public:
    1.32 +        CharsAndLength(const char *str, size_t length)
    1.33 +          : str_(str),
    1.34 +            length_(length)
    1.35 +        {
    1.36 +        }
    1.37 +
    1.38 +        const char *str() const {
    1.39 +            return str_;
    1.40 +        }
    1.41 +        size_t length() const {
    1.42 +            return length_;
    1.43 +        }
    1.44 +    };
    1.45 +
    1.46 +    struct Policy
    1.47 +    {
    1.48 +        typedef String * Payload;
    1.49 +
    1.50 +        static uint32 hash(const CharsAndLength &key) {
    1.51 +            return HashCharSequence(key.str(), key.length());
    1.52 +        }
    1.53 +        
    1.54 +        static bool matches(const CharsAndLength &key, String *string) {
    1.55 +            if (string->length() != key.length())
    1.56 +                return false;
    1.57 +            return memcmp(string->chars(), key.str(), key.length() * sizeof(char)) == 0;
    1.58 +        }
    1.59 +    };
    1.60 +
    1.61 +    typedef HashTable<Policy, ZoneAllocatorPolicy> StringSet;
    1.62  
    1.63    public:
    1.64 -    StringPool(PoolAllocator &pool)
    1.65 -      : pool_(pool)
    1.66 -    {
    1.67 -    }
    1.68 +    StringPool(Zone *zone);
    1.69 +    ~StringPool();
    1.70 +    bool initialize();
    1.71  
    1.72 -    Name *add(const char *string) {
    1.73 -        for (size_t i = 0; i < strings_.size(); i++) {
    1.74 -            if (strcmp(strings_[i], string) == 0)
    1.75 -                return strings_[i];
    1.76 -        }
    1.77 -        char *name = (char *)pool_.allocate(strlen(string) + 1);
    1.78 -        strcpy(name, string);
    1.79 -        strings_.push_back(name);
    1.80 -        return name;
    1.81 -    }
    1.82 +  public:
    1.83 +    String *add(const char *str, size_t len);
    1.84 +    void sweep();
    1.85 +
    1.86 +  private:
    1.87 +    Zone *zone_;
    1.88 +    StringSet set_;
    1.89  };
    1.90  
    1.91  }