2 * Worldvisions Weaver Software:
3 * Copyright (C) 2005 Net Integration Technologies, Inc.
5 * Definition for the WvStringCache class. See wvstringcache.h.
7 #include "wvstringcache.h"
8 #include "wvstringlist.h"
10 WvStringTable
*WvStringCache::t
;
11 int WvStringCache::refcount
;
12 size_t WvStringCache::clean_threshold
;
14 WvStringCache::WvStringCache()
19 t
= new WvStringTable
;
25 WvStringCache::~WvStringCache()
39 WvString
WvStringCache::get(WvStringParm s
)
41 // return s; // disable cache
42 WvString
*ret
= (*t
)[s
];
45 // printf("found(%s)\n", s.cstr());
50 // printf(" new(%s)\n", s.cstr());
51 ret
= new WvString(s
);
58 void WvStringCache::clean()
60 // do we actually need to clean yet? Skip it if we haven't added too
61 // many items since the last clean, since cleaning is pretty slow.
62 if (t
->count() < clean_threshold
)
67 // use a two-stage process so the iterator doesn't get messed up
68 // FIXME: this might actually be unnecessary with WvScatterHash, but
69 // someone should actually confirm that before taking this out.
71 WvStringTable::Iter
i(*t
);
72 for (i
.rewind(); i
.next(); )
74 if (i
->is_unique()) // last remaining instance
76 // printf("CLEANUP(%s)\n", i->cstr());
77 l
.append(i
.ptr(), false);
82 // printf("CLEANUP-1: %d elements at start (%d to remove)\n",
83 // (int)t->count(), (int)l.count());
86 WvStringList::Iter
i(l
);
87 for (i
.rewind(); i
.next(); )
91 clean_threshold
= t
->count() + t
->count()/10 + 1;
93 // printf("CLEANUP-2: %d elements left (thres=%d).\n",
94 // (int)t->count(), (int)clean_threshold);