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

📄 hashtable.c

📁 This is a java virtual machine implement in c
💻 C
📖 第 1 页 / 共 2 页
字号:
/*0231*/        unsigned long nextKey = table->bucketCount + bucket->next->key;
/*0232*/        if (nextKey >= 0x10000) { 
/*0233*/            fatalError(KVM_MSG_TOO_MANY_NAMETABLE_KEYS);
/*0234*/        }
/*0235*/        bucket->key = (unsigned short)nextKey;
/*0236*/    }
/*0237*/
/*0238*/    bucket->length = stringLength;
/*0239*/    *bucketPtr = bucket;
/*0240*/
/*0241*/    /* Increment the count, in case we need this information */
/*0242*/    table->count++;
/*0243*/
/*0244*/    /* Return the string */
/*0245*/    return bucket;
/*0246*/}
/*0247*/
/*0248*//*=========================================================================
/*0249./ * FUNCTION:      internString
/*0250./ * OVERVIEW:      Returns a unique Java String that corresponds to a
/*0251./ *                particular char* C string
/*0252./ * INTERFACE:
/*0253./ *   parameters:  string:  A C string
/*0254./ *   returns:     A unique Java String, such that if strcmp(x,y) == 0, then
/*0255./ *                internString(x) == internString(y).
/*0256./ *=======================================================================*/
/*0257*/
/*0258*/INTERNED_STRING_INSTANCE
/*0259*/internString(const char *utf8string, int length)
/*0260*/{ 
/*0261*/    HASHTABLE table = InternStringTable;
/*0262*/    unsigned int hash = stringHash(utf8string, length);
/*0263*/    unsigned int index = hash % table->bucketCount;
/*0264*/    unsigned int utfLength = utfStringLength(utf8string, length);
/*0265*/
/*0266*/    INTERNED_STRING_INSTANCE string, *stringPtr;
/*0267*/
/*0268*/    stringPtr = (INTERNED_STRING_INSTANCE *)&table->bucket[index];
/*0269*/    
/*0270*/    for (string = *stringPtr; string != NULL; string = string->next) { 
/*0271*/        if (string->length == utfLength) { 
/*0272*/            SHORTARRAY chars = string->array;
/*0273*/            int offset = string->offset;
/*0274*/            const char *p = utf8string;
/*0275*/            unsigned int i;
/*0276*/            for (i = 0; i < utfLength; i++) { 
/*0277*/                short unichar = utf2unicode(&p);
/*0278*/                if (unichar != chars->sdata[offset + i]) { 
/*0279*/                    /* We want to do "continue  <outerLoop>", but this is C */;
/*0280*/                    goto continueOuterLoop;
/*0281*/                }
/*0282*/            }
/*0283*/            if (EXCESSIVE_GARBAGE_COLLECTION && !ASYNCHRONOUS_NATIVE_FUNCTIONS){
/*0284*/                /* We might garbage collect, so we do  */
/*0285*/                garbageCollect(0);
/*0286*/            }
/*0287*/            return string;
/*0288*/        }
/*0289*/    continueOuterLoop:
/*0290*/        ;
/*0291*/    }
/*0292*/
/*0293*/    
/*0294*/    string = instantiateInternedString(utf8string, length);
/*0295*/    string->next = *stringPtr;
/*0296*/    *stringPtr = string;
/*0297*/    return string;
/*0298*/}
/*0299*/
/*0300*//*=========================================================================
/*0301./ * Conversion operations
/*0302./ *=======================================================================*/
/*0303*/
/*0304*//*=========================================================================
/*0305./ * FUNCTION:      utf2unicode
/*0306./ * OVERVIEW:      Converts UTF8 string to unicode char.
/*0307./ *
/*0308./ *   parameters:  utfstring_ptr: pointer to a UTF8 string. Set to point 
/*0309./ *                to the next UTF8 char upon return.
/*0310./ *   returns      unicode char
/*0311./ *=======================================================================*/
/*0312*/
/*0313*/short utf2unicode(const char **utfstring_ptr) {
/*0314*/    unsigned char *ptr = (unsigned char *)(*utfstring_ptr);
/*0315*/    unsigned char ch, ch2, ch3;
/*0316*/    int length = 1;     /* default length */
/*0317*/    short result = 0x80;    /* default bad result; */
/*0318*/
/*0319*/    switch ((ch = ptr[0]) >> 4) {
/*0320*/        default:
/*0321*/        result = ch;
/*0322*/        break;
/*0323*/
/*0324*/    case 0x8: case 0x9: case 0xA: case 0xB: case 0xF:
/*0325*/        /* Shouldn't happen. */
/*0326*/        break;
/*0327*/
/*0328*/    case 0xC: case 0xD: 
/*0329*/        /* 110xxxxx  10xxxxxx */
/*0330*/        if (((ch2 = ptr[1]) & 0xC0) == 0x80) {
/*0331*/            unsigned char high_five = ch & 0x1F;
/*0332*/            unsigned char low_six = ch2 & 0x3F;
/*0333*/            result = (high_five << 6) + low_six;
/*0334*/            length = 2;
/*0335*/        } 
/*0336*/        break;
/*0337*/
/*0338*/    case 0xE:
/*0339*/        /* 1110xxxx 10xxxxxx 10xxxxxx */
/*0340*/        if (((ch2 = ptr[1]) & 0xC0) == 0x80) {
/*0341*/            if (((ch3 = ptr[2]) & 0xC0) == 0x80) {
/*0342*/                unsigned char high_four = ch & 0x0f;
/*0343*/                unsigned char mid_six = ch2 & 0x3f;
/*0344*/                unsigned char low_six = ch3 & 0x3f;
/*0345*/                result = (((high_four << 6) + mid_six) << 6) + low_six;
/*0346*/                length = 3;
/*0347*/            } else {
/*0348*/                length = 2;
/*0349*/            }
/*0350*/        }
/*0351*/        break;
/*0352*/    } /* end of switch */
/*0353*/
/*0354*/    *utfstring_ptr = (char *)(ptr + length);
/*0355*/    return result;
/*0356*/}
/*0357*/
/*0358*/
/*0359*//*=========================================================================
/*0360./ * FUNCTION:      utfStringLength
/*0361./ * OVERVIEW:      Determine the number of 16-bit characters in a UTF-8 string.
/*0362./ *
/*0363./ *   parameters:  utfstring_ptr: pointer to a UTF8 string. 
/*0364./ *                length of string
/*0365./ *   returns      string length
/*0366./ *=======================================================================*/
/*0367*/
/*0368*/unsigned int 
/*0369*/utfStringLength(const char *utfstring, int length) 
/*0370*/{
/*0371*/    const char *ptr = utfstring;
/*0372*/    const char *end = utfstring + length;
/*0373*/    unsigned int count;
/*0374*/
/*0375*/    for (count = 0; ptr < end; count++) { 
/*0376*/        unsigned char ch = (unsigned char)ptr[0];
/*0377*/        if (ch < 0x80) { 
/*0378*/            /* 99% of the time */
/*0379*/            ptr++;
/*0380*/        } else { 
/*0381*/            switch(ch >> 4) { 
/*0382*/                default:
/*0383*/                    fatalError("Bad UTF string");
/*0384*/                    break;
/*0385*/                case 0xC: case 0xD: 
/*0386*/                    ptr += 2; break;
/*0387*/                case 0xE:
/*0388*/                    ptr += 3; break;
/*0389*/            }
/*0390*/        }
/*0391*/    }
/*0392*/    return count;
/*0393*/}
/*0394*/
/*0471*//*=========================================================================
/*0472./ * FUNCTION:      change_Name_to_Key, change_Key_to_Name
/*0473./ * OVERVIEW:      Converts between an array of bytes, and a unique 16-bit
/*0474./ *                number (the Key)
/*0475./ *
/*0476./ * INTERFACE:   change_Name_to_key
/*0477./ *   parameters:  string:       Pointer to an array of characters
/*0478./ *                stringLength: Length of array
/*0479./ *   returns      A unique 16-bit key
/*0480./ *
/*0481./ * INTERFACE:   change_key_to_Name
/*0482./ *   parameters:  key: A unique 16-bit key
/*0483./ *   lengthP:     If non-NULL, the length of the result is returned here.
/*0484./ *
/*0485./ *   returns      A pointer to the array of characters.
/*0486./ *=======================================================================*/
/*0487*/
/*0488*/NameKey
/*0489*/change_Name_to_Key(CONST_CHAR_HANDLE nameH, int offset, int length) { 
/*0490*/    UString UName = getUStringX(nameH, offset, length);
/*0491*/    return UName->key;
/*0492*/}
/*0493*/
/*0494*/char *
/*0495*/change_Key_to_Name(NameKey key, int *lengthP) { 
/*0496*/    HASHTABLE table = UTFStringTable;
/*0497*/    int index =  key % table->bucketCount;
/*0498*/    UTF_HASH_ENTRY *bucketPtr = (UTF_HASH_ENTRY *)&table->bucket[index];
/*0499*/    UTF_HASH_ENTRY bucket;
/*0500*/    /* Search the bucket for the corresponding string. */
/*0501*/    for (bucket = *bucketPtr; bucket != NULL; bucket = bucket->next) { 
/*0502*/        if (key == bucket->key) { 
/*0503*/            if (lengthP) { 
/*0504*/                *lengthP = bucket->length;
/*0505*/            }
/*0506*/            return UStringInfo(bucket);
/*0507*/        }
/*0508*/    }
/*0509*/    return NULL;
/*0510*/}
/*0511*/
/*0512*//*=========================================================================
/*0513./ * FUNCTION:      change_Name_to_CLASS, change_Key_to_CLASS
/*0514./ * OVERVIEW:      Internal function used by internString and internClass.
/*0515./ *                It maps a C string to a specific location capable of
/*0516./ *                holding a value.  This location is initially NULL.
/*0517./ * INTERFACE:
/*0518./ *   parameters:  table:  A hashtable whose buckets are MAPPING_HASH_ENTRY.
/*0519./ *                string:  A C string
/*0520./ *   returns:     A unique location, capable of holding a value, corresponding
/*0521./ *                to that string.
/*0522./ *=======================================================================*/
/*0523*/
/*0524*/CLASS
/*0525*/change_Name_to_CLASS(UString packageName, UString baseName) { 
/*0526*/    HASHTABLE table = ClassTable;
/*0527*/    unsigned int hash;
/*0528*/    unsigned int index;
/*0529*/    unsigned int lastKey = 0;
/*0530*/    CLASS *clazzPtr, clazz;

			//\\┮Τ

⌨️ 快捷键说明

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