comparison dwtx/core/internal/jobs/StringPool.d @ 122:9d0585bcb7aa

Add core.jobs package
author Frank Benoit <benoit@tionex.de>
date Tue, 12 Aug 2008 02:34:21 +0200
parents
children
comparison
equal deleted inserted replaced
121:c0304616ea23 122:9d0585bcb7aa
1 /*******************************************************************************
2 * Copyright (c) 2005 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 *******************************************************************************/
13 module dwtx.core.internal.jobs.StringPool;
14
15 import dwt.dwthelper.utils;
16 import dwtx.dwtxhelper.Collection;
17
18 /**
19 * A string pool is used for sharing strings in a way that eliminates duplicate
20 * equal strings. A string pool instance can be maintained over a long period
21 * of time, or used as a temporary structure during a string sharing pass over
22 * a data structure.
23 * <p>
24 * This class is not intended to be subclassed by clients.
25 * </p>
26 *
27 * Note: This class is copied from dwtx.core.resources
28 *
29 * @since 3.1
30 */
31 public final class StringPool {
32 private int savings;
33 private const HashMap map;
34
35 public this(){
36 map = new HashMap();
37 }
38
39 /**
40 * Adds a <code>String</code> to the pool. Returns a <code>String</code>
41 * that is equal to the argument but that is unique within this pool.
42 * @param string The string to add to the pool
43 * @return A string that is equal to the argument.
44 */
45 public String add(String string) {
46 if (string is null)
47 return string;
48 Object result = map.get(string);
49 if (result !is null) {
50 if (stringcast(result) !is string)
51 savings += 44 + 2 * string.length;
52 return stringcast( result );
53 }
54 map.put(string, string);
55 return string;
56 }
57
58 /**
59 * Returns an estimate of the size in bytes that was saved by sharing strings in
60 * the pool. In particular, this returns the size of all strings that were added to the
61 * pool after an equal string had already been added. This value can be used
62 * to estimate the effectiveness of a string sharing operation, in order to
63 * determine if or when it should be performed again.
64 *
65 * In some cases this does not precisely represent the number of bytes that
66 * were saved. For example, say the pool already contains string S1. Now
67 * string S2, which is equal to S1 but not identical, is added to the pool five
68 * times. This method will return the size of string S2 multiplied by the
69 * number of times it was added, even though the actual savings in this case
70 * is only the size of a single copy of S2.
71 */
72 public int getSavedStringCount() {
73 return savings;
74 }
75 }