Make WvStreams compile with gcc 4.4.
[wvstreams.git] / utils / wvstringcache.cc
blob8888ee974c6a89d29414499a05bf0de702bcd4b0
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 2005 Net Integration Technologies, Inc.
5 * Definition for the WvStringCache class. See wvstringcache.h.
6 */
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()
16 refcount++;
17 if (!t)
19 t = new WvStringTable;
20 clean_threshold = 0;
25 WvStringCache::~WvStringCache()
27 refcount--;
28 if (!refcount)
30 delete t;
31 t = NULL;
32 clean_threshold = 0;
34 else
35 clean();
39 WvString WvStringCache::get(WvStringParm s)
41 // return s; // disable cache
42 WvString *ret = (*t)[s];
43 if (ret)
45 // printf("found(%s)\n", s.cstr());
46 return *ret;
48 else
50 // printf(" new(%s)\n", s.cstr());
51 ret = new WvString(s);
52 t->add(ret, true);
53 return *ret;
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)
63 return;
65 WvStringList l;
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(); )
88 t->remove(i.ptr());
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);