⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hash.h

📁 Shorthand是一个强大的脚本语言
💻 H
字号:
#ifndef __shhash_h
#define __shhash_h
///////////////////////////////////////////////////////////////////////////////
// $Header: /shorthand/src/hash.h 2     2/14/03 4:20a Arm $
//-----------------------------------------------------------------------------
// Project: ShortHand interpreter
// Author: Andrei Remenchuk <andrei@remenchuk.com>
//-----------------------------------------------------------------------------
// sharray.cpp: ShortHand hash class - declarations
///////////////////////////////////////////////////////////////////////////////
#include "value.h"
#include "object.h"
#include "array.h"

class ShhHashCode
{
public:
    unsigned int m_hash;
    ShhHashCode() {}
    ShhHashCode(unsigned int code) : m_hash(code) {}
};

/**
 * Hash Element
 */
class ShhHashEntry : public ShhHashCode
{
protected:
    string   m_index;
    ShhValue m_value;
protected:
    ShhHashEntry(const string& index, const ShhValue& value);
    unsigned int hashCode() { return m_hash; }
    const string& getIndex() const { return m_index; }
    ShhValue& getValue() { return m_value; }

    friend class ShhHashBucket;
    friend class ShhHash;
    friend class ShhHashIterator;
};


class ShhHashBucket 
{
protected:
    unsigned int m_level;
    unsigned int m_key;
    unsigned int m_divider;
    
    int m_count; // number of items in m_elements
    int m_capacity; // capacity of m_elements
    int m_dirty; // "dirty" counter
    int m_sorted; // number of sorted items in the beginning of the array
    ShhHashEntry** m_elements;

    
protected:
    ShhHashBucket(unsigned int level, unsigned int key);

    void provideCapacity(int newCapacity);
    ShhHashEntry* findElement(const string& index);
    ShhHashEntry* createElement(const string& index, const ShhValue& value);
    
public:
    ~ShhHashBucket();

    friend class ShhHash;
    friend class ShhHashIterator;
};

class ShhHash;
class ShhHashIterator 
{
protected:
    ShhHash* m_hash;
    int m_bucket_index;
    int m_element_index;
public:
    bool next(ShhValue& index, ShhValue& value);
    friend class ShhHash;
public:
    ShhHashIterator();
};


/**
 * ShortHand Hash Array object
 */
class ShhHash : public ShhObject
{
protected:
    ShhValue m_null;
    ShhValue m_count_property;

    int m_count;

    array<ShhHashBucket> m_buckets;

protected:
    ShhHashBucket* findBucket(unsigned int hashCode);
    ShhHashBucket* createBucket(unsigned int hashCode);

public:
    ShhHash(ShhModule& module, const YYLTYPE& location);
    void constructor(ShhValueList* args);
    bool hasMethod(const char* name);
    ShhValue executeMethod(const char* name, ShhValueList* args);
    ShhValue& getProperty(const char* name);
    ~ShhHash();

    void createIterator(ShhHashIterator& iterator);

    ShhValue rvalue(const ShhValue& index);
    ShhValue& lvalue(const ShhValue& index);
    void set(const ShhValue& index, ShhValue value);
    int size() const;

    friend class ShhHashIterator;

};

#endif // __sharray_h

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -