lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



in reference with my previous email, I found a cleaner and more compact implementation to have the move-to-front effect when looking into the string cache

re-send with a correction, code was not checking last item in the bucket

TString *luaS_new(lua_State *L, const char *str) {
  unsigned int i = point2uint(str) % STRCACHE_N;  /* hash */
  int j;
  TString **p = G(L)->strcache[i];
  TString *ptemp;
  for(j = 1; j < STRCACHE_M; j++) {
    if (strcmp(str, getstr(p[0])) == 0) return p[0]; /* hit? */    
    ptemp = p[0]; p[0] = p[j]; p[j] = ptemp; /* swap */
  }
  if (strcmp(str, getstr(p[0])) == 0) return p[0]; /* check last element */
  /* new element is first in the list */
  p[0] = luaS_newlstr(L, str, strlen(str));
  return p[0];  

    Andrea

--
Andrea Vitali