diff options
author | Carsten Haitzler (Rasterman) <raster@rasterman.com> | 2018-11-20 17:15:57 +0000 |
---|---|---|
committer | Carsten Haitzler (Rasterman) <raster@rasterman.com> | 2018-11-21 13:37:16 +0000 |
commit | e68a952b8c039ca57f31b669efef84303c54922d (patch) | |
tree | 230805726c859d84ac1442b23d7ec96a2800f737 | |
parent | 61109729fada2c26bd713a1f105702c03da3d2b7 (diff) | |
download | efl-e68a952b8c039ca57f31b669efef84303c54922d.tar.gz |
eet - dictorinary - tidy up code to be easier to read
more compact so less scrolling - easier to read.
-rw-r--r-- | src/lib/eet/eet_dictionary.c | 208 |
1 files changed, 74 insertions, 134 deletions
diff --git a/src/lib/eet/eet_dictionary.c b/src/lib/eet/eet_dictionary.c index acff624f1c..46e8cbf723 100644 --- a/src/lib/eet/eet_dictionary.c +++ b/src/lib/eet/eet_dictionary.c @@ -14,16 +14,13 @@ Eet_Dictionary * eet_dictionary_add(void) { - Eet_Dictionary *new; + Eet_Dictionary *ed; - new = eet_dictionary_calloc(1); - if (!new) - return NULL; - - memset(new->hash, -1, sizeof (int) * 256); - eina_rwlock_new(&new->rwlock); - - return new; + ed = eet_dictionary_calloc(1); + if (!ed) return NULL; + memset(ed->hash, -1, sizeof(int) * 256); + eina_rwlock_new(&ed->rwlock); + return ed; } void @@ -32,19 +29,20 @@ eet_dictionary_free(Eet_Dictionary *ed) int i; if (!ed) return; - eina_rwlock_free(&ed->rwlock); - for (i = 0; i < ed->count; ++i) - if (ed->all_allocated[i >> 3] & (1 << (i & 0x7))) - eina_stringshare_del(ed->all[i].str); - + for (i = 0; i < ed->count; i++) + { + if (ed->all_allocated[i >> 3] & (1 << (i & 0x7))) + { + eina_stringshare_del(ed->all[i].str); + } + } free(ed->all); free(ed->all_hash); free(ed->all_allocated); if (ed->converts) eina_hash_free(ed->converts); - eet_dictionary_mp_free(ed); } @@ -55,27 +53,21 @@ _eet_dictionary_lookup(Eet_Dictionary *ed, int hash, int *previous) { - int prev = -1; - int current; + int prev = -1, current; current = ed->hash[hash]; - while (current != -1) { - if (ed->all[current].len == len) + if ((ed->all[current].str) && + ((ed->all[current].str == string) || + ((ed->all[current].len == len) && + (!strcmp(ed->all[current].str, string))))) { - if (ed->all[current].str && - ((ed->all[current].str == string) || - (!strcmp(ed->all[current].str, string)))) - { - break; - } + break; } - prev = current; current = ed->all[current].next; } - if (previous) *previous = prev; return current; } @@ -86,14 +78,9 @@ eet_dictionary_string_add(Eet_Dictionary *ed, { Eet_String *current; const char *str; - int hash; - int idx; - int pidx; - int len; - int cnt; + int hash, idx, pidx, len, cnt; - if (!ed) - return -1; + if (!ed) return -1; hash = _eet_hash_gen(string, 8); len = strlen(string) + 1; @@ -114,25 +101,24 @@ eet_dictionary_string_add(Eet_Dictionary *ed, eina_rwlock_take_write(&ed->rwlock); if (ed->total == ed->count) { - Eet_String *new; - unsigned char *new_hash; - unsigned char *new_allocated; + Eet_String *s; + unsigned char *new_hash, *new_allocated; int total; total = ed->total + 64; - new = realloc(ed->all, total * sizeof(Eet_String)); - if (!new) goto on_error; - ed->all = new; + s = realloc(ed->all, total * sizeof(Eet_String)); + if (!s) goto on_error; + ed->all = s; - new_hash = realloc(ed->all_hash, total * sizeof (unsigned char)); - if (!new_hash) goto on_error; - ed->all_hash = new_hash; + new_hash = realloc(ed->all_hash, total); + if (!new_hash) goto on_error; + ed->all_hash = new_hash; + + new_allocated = realloc(ed->all_allocated, ((total >> 3) + 1)); + if (!new_allocated) goto on_error; + ed->all_allocated = new_allocated; - new_allocated = realloc(ed->all_allocated, ((total >> 3) + 1) * sizeof (unsigned char)); - if (!new_allocated) goto on_error; - ed->all_allocated = new_allocated; - ed->total = total; } @@ -152,18 +138,14 @@ eet_dictionary_string_add(Eet_Dictionary *ed, else { current->next = idx; - - if (pidx != -1) - ed->all[pidx].next = ed->count; - else - ed->hash[hash] = ed->count; + if (pidx != -1) ed->all[pidx].next = ed->count; + else ed->hash[hash] = ed->count; } - cnt = ed->count++; eina_rwlock_release(&ed->rwlock); return cnt; - on_error: +on_error: eina_rwlock_release(&ed->rwlock); return -1; } @@ -175,24 +157,26 @@ eet_dictionary_string_get_size(const Eet_Dictionary *ed, int length = 0; if (!ed) goto done; - if (idx < 0) goto done; eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); - - if (idx < ed->count) - length = ed->all[idx].len; - + if (idx < ed->count) length = ed->all[idx].len; eina_rwlock_release((Eina_RWLock *)&ed->rwlock); - - done: +done: return length; } EAPI int eet_dictionary_count(const Eet_Dictionary *ed) { - return ed->count; + int cnt; + + if (!ed) return 0; + + eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); + cnt = ed->count; + eina_rwlock_release((Eina_RWLock *)&ed->rwlock); + return cnt; } int @@ -202,17 +186,12 @@ eet_dictionary_string_get_hash(const Eet_Dictionary *ed, int hash = -1; if (!ed) goto done; - if (idx < 0) goto done; eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); - - if (idx < ed->count) - hash = ed->all_hash[idx]; - + if (idx < ed->count) hash = ed->all_hash[idx]; eina_rwlock_release((Eina_RWLock *)&ed->rwlock); - - done: +done: return hash; } @@ -223,16 +202,14 @@ eet_dictionary_string_get_char(const Eet_Dictionary *ed, const char *s = NULL; if (!ed) goto done; - if (idx < 0) goto done; eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); - if (idx < ed->count) { #ifdef _WIN32 /* Windows file system could change the mmaped file when replacing a file. So we need to copy all string in memory to avoid bugs. */ - if (!(ed->all_allocated[idx >> 3] & (1 << (idx & 0x7)))) + if (!(ed->all_allocated[idx >> 3] & (1 << (idx & 0x7)))) { ed->all[idx].str = eina_stringshare_add(ed->all[idx].str); ed->all_allocated[idx >> 3] |= (1 << (idx & 0x7)); @@ -240,10 +217,8 @@ eet_dictionary_string_get_char(const Eet_Dictionary *ed, #endif /* ifdef _WIN32 */ s = ed->all[idx].str; } - eina_rwlock_release((Eina_RWLock *)&ed->rwlock); - - done: +done: return s; } @@ -257,10 +232,8 @@ _eet_dictionary_string_get_me_cache(const char *s, { *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0'); *exponent = (s[5] - '0'); - return EINA_TRUE; } - return EINA_FALSE; } @@ -269,19 +242,14 @@ _eet_dictionary_string_get_float_cache(const char *s, int len, float *result) { - int mantisse; - int exponent; + int mantisse, exponent; if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent)) { - if (s[4] == '+') - *result = (float)(mantisse << exponent); - else - *result = (float)mantisse / (float)(1 << exponent); - + if (s[4] == '+') *result = (float)(mantisse << exponent); + else *result = (float)mantisse / (float)(1 << exponent); return EINA_TRUE; } - return EINA_FALSE; } @@ -290,19 +258,14 @@ _eet_dictionary_string_get_double_cache(const char *s, int len, double *result) { - int mantisse; - int exponent; + int mantisse, exponent; if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent)) { - if (s[4] == '+') - *result = (double)(mantisse << exponent); - else - *result = (double)mantisse / (float)(1 << exponent); - + if (s[4] == '+') *result = (double)(mantisse << exponent); + else *result = (double)mantisse / (float)(1 << exponent); return EINA_TRUE; } - return EINA_FALSE; } @@ -314,21 +277,15 @@ _eet_dictionary_test(const Eet_Dictionary *ed, Eina_Bool limit = EINA_FALSE; if (!result) goto done; - if (!ed) goto done; - if (idx < 0) goto done; eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); - if (!(idx < ed->count)) goto unlock_done; - limit = EINA_TRUE; - - unlock_done: +unlock_done: eina_rwlock_release((Eina_RWLock *)&ed->rwlock); - - done: +done: return limit; } @@ -340,27 +297,22 @@ eet_dictionary_convert_get(const Eet_Dictionary *ed, Eet_Convert *result; eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); - *str = ed->all[idx].str; if (!ed->converts) { ((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free); - goto add_convert; } result = eina_hash_find(ed->converts, &idx); if (result) goto done; - add_convert: +add_convert: result = calloc(1, sizeof (Eet_Convert)); - eina_hash_add(ed->converts, &idx, result); - - done: +done: eina_rwlock_release((Eina_RWLock *)&ed->rwlock); - return result; } @@ -372,8 +324,7 @@ eet_dictionary_string_get_float(const Eet_Dictionary *ed, Eet_Convert *convert; const char *str; - if (!_eet_dictionary_test(ed, idx, result)) - return EINA_FALSE; + if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE; convert = eet_dictionary_convert_get(ed, idx, &str); if (!convert) return EINA_FALSE; @@ -393,14 +344,11 @@ eet_dictionary_string_get_float(const Eet_Dictionary *ed, eina_rwlock_release((Eina_RWLock *)&ed->rwlock); return EINA_FALSE; } - convert->f = ldexpf((float)mantisse, exponent); } eina_rwlock_release((Eina_RWLock *)&ed->rwlock); - convert->type |= EET_D_FLOAT; } - *result = convert->f; return EINA_TRUE; } @@ -413,8 +361,7 @@ eet_dictionary_string_get_double(const Eet_Dictionary *ed, Eet_Convert *convert; const char *str; - if (!_eet_dictionary_test(ed, idx, result)) - return EINA_FALSE; + if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE; convert = eet_dictionary_convert_get(ed, idx, &str); if (!convert) return EINA_FALSE; @@ -435,11 +382,9 @@ eet_dictionary_string_get_double(const Eet_Dictionary *ed, eina_rwlock_release((Eina_RWLock *)&ed->rwlock); return EINA_FALSE; } - convert->d = ldexp((double)mantisse, exponent); } eina_rwlock_release((Eina_RWLock *)&ed->rwlock); - convert->type |= EET_D_DOUBLE; } @@ -455,8 +400,7 @@ eet_dictionary_string_get_fp(const Eet_Dictionary *ed, Eet_Convert *convert; const char *str; - if (!_eet_dictionary_test(ed, idx, result)) - return EINA_FALSE; + if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE; convert = eet_dictionary_convert_get(ed, idx, &str); if (!convert) return EINA_FALSE; @@ -485,29 +429,25 @@ EAPI int eet_dictionary_string_check(Eet_Dictionary *ed, const char *string) { - int res = 0; - int i; + int res = 0, i; - if ((!ed) || (!string)) - return 0; + if ((!ed) || (!string)) return 0; eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); - - if ((ed->start <= string) && (string < ed->end)) - res = 1; + if ((ed->start <= string) && (string < ed->end)) res = 1; if (!res) { - for (i = 0; i < ed->count; ++i) - if ((ed->all_allocated[i >> 3] & (1 << (i & 0x7))) && ed->all[i].str == string) - { - res = 1; - break; - } + for (i = 0; i < ed->count; i++) + { + if ((ed->all_allocated[i >> 3] & (1 << (i & 0x7))) && ed->all[i].str == string) + { + res = 1; + break; + } + } } - eina_rwlock_release((Eina_RWLock *)&ed->rwlock); - return res; } |