comparison src/org/tmatesoft/hg/internal/Pool.java @ 262:3dcd3dd90c77

Improve manifest parsing: decode bytes to chars once, minimize arraycopy on String instantiation, keep set of file revisions from previous manifest only
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 18 Aug 2011 03:46:36 +0200
parents e2115da4cf6a
children 6bb5e7ed051a
comparison
equal deleted inserted replaced
261:436bb5f65ce1 262:3dcd3dd90c77
16 */ 16 */
17 package org.tmatesoft.hg.internal; 17 package org.tmatesoft.hg.internal;
18 18
19 import java.util.HashMap; 19 import java.util.HashMap;
20 20
21 import org.tmatesoft.hg.util.SparseSet;
22
21 /** 23 /**
22 * Instance pooling. 24 * Instance pooling.
23 * 25 *
24 * @author Artem Tikhomirov 26 * @author Artem Tikhomirov
25 * @author TMate Software Ltd. 27 * @author TMate Software Ltd.
26 */ 28 */
27 public class Pool<T> { 29 public class Pool<T> {
28 private final HashMap<T,T> unify = new HashMap<T, T>(); 30 private final HashMap<T,T> unify;
31 // private final SparseSet<T> unify = new SparseSet<T>();
32
33 public Pool() {
34 unify = new HashMap<T, T>();
35 }
36
37 public Pool(int sizeHint) {
38 if (sizeHint <= 0) {
39 unify = new HashMap<T, T>();
40 } else {
41 unify = new HashMap<T, T>(sizeHint * 4 / 3, 0.75f);
42 }
43 }
29 44
30 public T unify(T t) { 45 public T unify(T t) {
31 T rv = unify.get(t); 46 T rv = unify.get(t);
32 if (rv == null) { 47 if (rv == null) {
33 // first time we see a new value 48 // first time we see a new value
35 rv = t; 50 rv = t;
36 } 51 }
37 return rv; 52 return rv;
38 } 53 }
39 54
55 public boolean contains(T t) {
56 return unify.containsKey(t);
57 }
58
59 public void record(T t) {
60 unify.put(t, t);
61 }
62
63 public void clear() {
64 unify.clear();
65 }
66
67 public int size() {
68 return unify.size();
69 }
70
40 @Override 71 @Override
41 public String toString() { 72 public String toString() {
42 StringBuilder sb = new StringBuilder(); 73 StringBuilder sb = new StringBuilder();
43 sb.append(Pool.class.getSimpleName()); 74 sb.append(Pool.class.getSimpleName());
44 sb.append('<'); 75 sb.append('<');
45 if (!unify.isEmpty()) { 76 // if (!unify.isEmpty()) {
46 sb.append(unify.keySet().iterator().next().getClass().getName()); 77 // sb.append(unify.keySet().iterator().next().getClass().getName());
47 } 78 // }
48 sb.append('>'); 79 sb.append('>');
49 sb.append(':'); 80 sb.append(':');
50 sb.append(unify.size()); 81 sb.append(unify.size());
51 return sb.toString(); 82 return sb.toString();
52 } 83 }