eet - dictorinary - tidy up code to be easier to read

more compact so less scrolling - easier to read.
This commit is contained in:
Carsten Haitzler 2018-11-20 17:15:57 +00:00
parent 61109729fa
commit e68a952b8c
1 changed files with 74 additions and 134 deletions

View File

@ -14,16 +14,13 @@
Eet_Dictionary * Eet_Dictionary *
eet_dictionary_add(void) eet_dictionary_add(void)
{ {
Eet_Dictionary *new; Eet_Dictionary *ed;
new = eet_dictionary_calloc(1); ed = eet_dictionary_calloc(1);
if (!new) if (!ed) return NULL;
return NULL; memset(ed->hash, -1, sizeof(int) * 256);
eina_rwlock_new(&ed->rwlock);
memset(new->hash, -1, sizeof (int) * 256); return ed;
eina_rwlock_new(&new->rwlock);
return new;
} }
void void
@ -32,19 +29,20 @@ eet_dictionary_free(Eet_Dictionary *ed)
int i; int i;
if (!ed) return; if (!ed) return;
eina_rwlock_free(&ed->rwlock); eina_rwlock_free(&ed->rwlock);
for (i = 0; i < ed->count; ++i) for (i = 0; i < ed->count; i++)
if (ed->all_allocated[i >> 3] & (1 << (i & 0x7))) {
eina_stringshare_del(ed->all[i].str); if (ed->all_allocated[i >> 3] & (1 << (i & 0x7)))
{
eina_stringshare_del(ed->all[i].str);
}
}
free(ed->all); free(ed->all);
free(ed->all_hash); free(ed->all_hash);
free(ed->all_allocated); free(ed->all_allocated);
if (ed->converts) eina_hash_free(ed->converts); if (ed->converts) eina_hash_free(ed->converts);
eet_dictionary_mp_free(ed); eet_dictionary_mp_free(ed);
} }
@ -55,27 +53,21 @@ _eet_dictionary_lookup(Eet_Dictionary *ed,
int hash, int hash,
int *previous) int *previous)
{ {
int prev = -1; int prev = -1, current;
int current;
current = ed->hash[hash]; current = ed->hash[hash];
while (current != -1) 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 && break;
((ed->all[current].str == string) ||
(!strcmp(ed->all[current].str, string))))
{
break;
}
} }
prev = current; prev = current;
current = ed->all[current].next; current = ed->all[current].next;
} }
if (previous) *previous = prev; if (previous) *previous = prev;
return current; return current;
} }
@ -86,14 +78,9 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
{ {
Eet_String *current; Eet_String *current;
const char *str; const char *str;
int hash; int hash, idx, pidx, len, cnt;
int idx;
int pidx;
int len;
int cnt;
if (!ed) if (!ed) return -1;
return -1;
hash = _eet_hash_gen(string, 8); hash = _eet_hash_gen(string, 8);
len = strlen(string) + 1; len = strlen(string) + 1;
@ -114,24 +101,23 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
eina_rwlock_take_write(&ed->rwlock); eina_rwlock_take_write(&ed->rwlock);
if (ed->total == ed->count) if (ed->total == ed->count)
{ {
Eet_String *new; Eet_String *s;
unsigned char *new_hash; unsigned char *new_hash, *new_allocated;
unsigned char *new_allocated;
int total; int total;
total = ed->total + 64; total = ed->total + 64;
new = realloc(ed->all, total * sizeof(Eet_String)); s = realloc(ed->all, total * sizeof(Eet_String));
if (!new) goto on_error; if (!s) goto on_error;
ed->all = new; ed->all = s;
new_hash = realloc(ed->all_hash, total * sizeof (unsigned char)); new_hash = realloc(ed->all_hash, total);
if (!new_hash) goto on_error; if (!new_hash) goto on_error;
ed->all_hash = new_hash; ed->all_hash = new_hash;
new_allocated = realloc(ed->all_allocated, ((total >> 3) + 1) * sizeof (unsigned char)); new_allocated = realloc(ed->all_allocated, ((total >> 3) + 1));
if (!new_allocated) goto on_error; if (!new_allocated) goto on_error;
ed->all_allocated = new_allocated; ed->all_allocated = new_allocated;
ed->total = total; ed->total = total;
} }
@ -152,18 +138,14 @@ eet_dictionary_string_add(Eet_Dictionary *ed,
else else
{ {
current->next = idx; current->next = idx;
if (pidx != -1) ed->all[pidx].next = ed->count;
if (pidx != -1) else ed->hash[hash] = ed->count;
ed->all[pidx].next = ed->count;
else
ed->hash[hash] = ed->count;
} }
cnt = ed->count++; cnt = ed->count++;
eina_rwlock_release(&ed->rwlock); eina_rwlock_release(&ed->rwlock);
return cnt; return cnt;
on_error: on_error:
eina_rwlock_release(&ed->rwlock); eina_rwlock_release(&ed->rwlock);
return -1; return -1;
} }
@ -175,24 +157,26 @@ eet_dictionary_string_get_size(const Eet_Dictionary *ed,
int length = 0; int length = 0;
if (!ed) goto done; if (!ed) goto done;
if (idx < 0) goto done; if (idx < 0) goto done;
eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 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); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
done:
done:
return length; return length;
} }
EAPI int EAPI int
eet_dictionary_count(const Eet_Dictionary *ed) 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 int
@ -202,17 +186,12 @@ eet_dictionary_string_get_hash(const Eet_Dictionary *ed,
int hash = -1; int hash = -1;
if (!ed) goto done; if (!ed) goto done;
if (idx < 0) goto done; if (idx < 0) goto done;
eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 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); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
done:
done:
return hash; return hash;
} }
@ -223,16 +202,14 @@ eet_dictionary_string_get_char(const Eet_Dictionary *ed,
const char *s = NULL; const char *s = NULL;
if (!ed) goto done; if (!ed) goto done;
if (idx < 0) goto done; if (idx < 0) goto done;
eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
if (idx < ed->count) if (idx < ed->count)
{ {
#ifdef _WIN32 #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. */ /* 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[idx].str = eina_stringshare_add(ed->all[idx].str);
ed->all_allocated[idx >> 3] |= (1 << (idx & 0x7)); ed->all_allocated[idx >> 3] |= (1 << (idx & 0x7));
@ -240,10 +217,8 @@ eet_dictionary_string_get_char(const Eet_Dictionary *ed,
#endif /* ifdef _WIN32 */ #endif /* ifdef _WIN32 */
s = ed->all[idx].str; s = ed->all[idx].str;
} }
eina_rwlock_release((Eina_RWLock *)&ed->rwlock); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
done:
done:
return s; 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'); *mantisse = (s[2] >= 'a') ? (s[2] - 'a' + 10) : (s[2] - '0');
*exponent = (s[5] - '0'); *exponent = (s[5] - '0');
return EINA_TRUE; return EINA_TRUE;
} }
return EINA_FALSE; return EINA_FALSE;
} }
@ -269,19 +242,14 @@ _eet_dictionary_string_get_float_cache(const char *s,
int len, int len,
float *result) float *result)
{ {
int mantisse; int mantisse, exponent;
int exponent;
if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent)) if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
{ {
if (s[4] == '+') if (s[4] == '+') *result = (float)(mantisse << exponent);
*result = (float)(mantisse << exponent); else *result = (float)mantisse / (float)(1 << exponent);
else
*result = (float)mantisse / (float)(1 << exponent);
return EINA_TRUE; return EINA_TRUE;
} }
return EINA_FALSE; return EINA_FALSE;
} }
@ -290,19 +258,14 @@ _eet_dictionary_string_get_double_cache(const char *s,
int len, int len,
double *result) double *result)
{ {
int mantisse; int mantisse, exponent;
int exponent;
if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent)) if (_eet_dictionary_string_get_me_cache(s, len, &mantisse, &exponent))
{ {
if (s[4] == '+') if (s[4] == '+') *result = (double)(mantisse << exponent);
*result = (double)(mantisse << exponent); else *result = (double)mantisse / (float)(1 << exponent);
else
*result = (double)mantisse / (float)(1 << exponent);
return EINA_TRUE; return EINA_TRUE;
} }
return EINA_FALSE; return EINA_FALSE;
} }
@ -314,21 +277,15 @@ _eet_dictionary_test(const Eet_Dictionary *ed,
Eina_Bool limit = EINA_FALSE; Eina_Bool limit = EINA_FALSE;
if (!result) goto done; if (!result) goto done;
if (!ed) goto done; if (!ed) goto done;
if (idx < 0) goto done; if (idx < 0) goto done;
eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
if (!(idx < ed->count)) goto unlock_done; if (!(idx < ed->count)) goto unlock_done;
limit = EINA_TRUE; limit = EINA_TRUE;
unlock_done:
unlock_done:
eina_rwlock_release((Eina_RWLock *)&ed->rwlock); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
done:
done:
return limit; return limit;
} }
@ -340,27 +297,22 @@ eet_dictionary_convert_get(const Eet_Dictionary *ed,
Eet_Convert *result; Eet_Convert *result;
eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock);
*str = ed->all[idx].str; *str = ed->all[idx].str;
if (!ed->converts) if (!ed->converts)
{ {
((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free); ((Eet_Dictionary *)ed)->converts = eina_hash_int32_new(free);
goto add_convert; goto add_convert;
} }
result = eina_hash_find(ed->converts, &idx); result = eina_hash_find(ed->converts, &idx);
if (result) goto done; if (result) goto done;
add_convert: add_convert:
result = calloc(1, sizeof (Eet_Convert)); result = calloc(1, sizeof (Eet_Convert));
eina_hash_add(ed->converts, &idx, result); eina_hash_add(ed->converts, &idx, result);
done:
done:
eina_rwlock_release((Eina_RWLock *)&ed->rwlock); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
return result; return result;
} }
@ -372,8 +324,7 @@ eet_dictionary_string_get_float(const Eet_Dictionary *ed,
Eet_Convert *convert; Eet_Convert *convert;
const char *str; const char *str;
if (!_eet_dictionary_test(ed, idx, result)) if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE;
return EINA_FALSE;
convert = eet_dictionary_convert_get(ed, idx, &str); convert = eet_dictionary_convert_get(ed, idx, &str);
if (!convert) return EINA_FALSE; 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); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
return EINA_FALSE; return EINA_FALSE;
} }
convert->f = ldexpf((float)mantisse, exponent); convert->f = ldexpf((float)mantisse, exponent);
} }
eina_rwlock_release((Eina_RWLock *)&ed->rwlock); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
convert->type |= EET_D_FLOAT; convert->type |= EET_D_FLOAT;
} }
*result = convert->f; *result = convert->f;
return EINA_TRUE; return EINA_TRUE;
} }
@ -413,8 +361,7 @@ eet_dictionary_string_get_double(const Eet_Dictionary *ed,
Eet_Convert *convert; Eet_Convert *convert;
const char *str; const char *str;
if (!_eet_dictionary_test(ed, idx, result)) if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE;
return EINA_FALSE;
convert = eet_dictionary_convert_get(ed, idx, &str); convert = eet_dictionary_convert_get(ed, idx, &str);
if (!convert) return EINA_FALSE; 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); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
return EINA_FALSE; return EINA_FALSE;
} }
convert->d = ldexp((double)mantisse, exponent); convert->d = ldexp((double)mantisse, exponent);
} }
eina_rwlock_release((Eina_RWLock *)&ed->rwlock); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
convert->type |= EET_D_DOUBLE; convert->type |= EET_D_DOUBLE;
} }
@ -455,8 +400,7 @@ eet_dictionary_string_get_fp(const Eet_Dictionary *ed,
Eet_Convert *convert; Eet_Convert *convert;
const char *str; const char *str;
if (!_eet_dictionary_test(ed, idx, result)) if (!_eet_dictionary_test(ed, idx, result)) return EINA_FALSE;
return EINA_FALSE;
convert = eet_dictionary_convert_get(ed, idx, &str); convert = eet_dictionary_convert_get(ed, idx, &str);
if (!convert) return EINA_FALSE; if (!convert) return EINA_FALSE;
@ -485,29 +429,25 @@ EAPI int
eet_dictionary_string_check(Eet_Dictionary *ed, eet_dictionary_string_check(Eet_Dictionary *ed,
const char *string) const char *string)
{ {
int res = 0; int res = 0, i;
int i;
if ((!ed) || (!string)) if ((!ed) || (!string)) return 0;
return 0;
eina_rwlock_take_read((Eina_RWLock *)&ed->rwlock); 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) if (!res)
{ {
for (i = 0; i < ed->count; ++i) for (i = 0; i < ed->count; i++)
if ((ed->all_allocated[i >> 3] & (1 << (i & 0x7))) && ed->all[i].str == string) {
{ if ((ed->all_allocated[i >> 3] & (1 << (i & 0x7))) && ed->all[i].str == string)
res = 1; {
break; res = 1;
} break;
}
}
} }
eina_rwlock_release((Eina_RWLock *)&ed->rwlock); eina_rwlock_release((Eina_RWLock *)&ed->rwlock);
return res; return res;
} }