View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.workinprogress.optimisation.params;
31  
32  import gnu.trove.iterator.TObjectDoubleIterator;
33  import gnu.trove.map.hash.TObjectDoubleHashMap;
34  import gnu.trove.procedure.TObjectDoubleProcedure;
35  
36  import java.util.Iterator;
37  
38  import org.openimaj.workinprogress.optimisation.params.KeyedParameters.ObjectDoubleEntry;
39  
40  public final class KeyedParameters<KEY> implements Parameters<KeyedParameters<KEY>>, Iterable<ObjectDoubleEntry<KEY>> {
41  	public static class ObjectDoubleEntry<KEY> {
42  		public KEY key;
43  		public double value;
44  	}
45  
46  	private TObjectDoubleHashMap<KEY> paramsMap = new TObjectDoubleHashMap<KEY>();
47  
48  	/*
49  	 * (non-Javadoc)
50  	 *
51  	 * @see
52  	 * org.openimaj.workinprogress.params.Parameters#multiplyInplace(org.openimaj
53  	 * .workinprogress.params.KeyedParameters)
54  	 */
55  	@Override
56  	public void multiplyInplace(final KeyedParameters<KEY> other) {
57  		paramsMap.forEachEntry(new TObjectDoubleProcedure<KEY>() {
58  			@Override
59  			public boolean execute(KEY key, double value) {
60  				if (other.contains(key)) {
61  					paramsMap.put(key, other.get(key) * value);
62  				}
63  				return true;
64  			}
65  		});
66  	}
67  
68  	public void multiplyInplace(KEY key, double value) {
69  		if (paramsMap.contains(key)) {
70  			paramsMap.put(key, paramsMap.get(key) * value);
71  		}
72  	}
73  
74  	@Override
75  	public void addInplace(final KeyedParameters<KEY> other) {
76  		other.paramsMap.forEachEntry(new TObjectDoubleProcedure<KEY>() {
77  			@Override
78  			public boolean execute(KEY key, double value) {
79  				paramsMap.adjustOrPutValue(key, value, value);
80  				return true;
81  			}
82  		});
83  	}
84  
85  	public void addInplace(KEY key, double value) {
86  		paramsMap.adjustOrPutValue(key, value, value);
87  	}
88  
89  	public void set(KEY key, double value) {
90  		paramsMap.put(key, value);
91  	}
92  
93  	public double get(KEY key) {
94  		return paramsMap.get(key);
95  	}
96  
97  	public boolean contains(KEY key) {
98  		return paramsMap.containsKey(key);
99  	}
100 
101 	@Override
102 	public void multiplyInplace(final double value) {
103 		paramsMap.forEachEntry(new TObjectDoubleProcedure<KEY>() {
104 			@Override
105 			public boolean execute(KEY key, double v) {
106 				paramsMap.put(key, value * v);
107 				return true;
108 			}
109 		});
110 	}
111 
112 	/*
113 	 * (non-Javadoc)
114 	 *
115 	 * @see org.openimaj.workinprogress.params.Parameters#addInplace(KEY)
116 	 */
117 	@Override
118 	public void addInplace(final double value) {
119 		paramsMap.forEachEntry(new TObjectDoubleProcedure<KEY>() {
120 			@Override
121 			public boolean execute(KEY key, double v) {
122 				paramsMap.put(key, value * v);
123 				return true;
124 			}
125 		});
126 	}
127 
128 	@Override
129 	public Iterator<ObjectDoubleEntry<KEY>> iterator() {
130 		return new Iterator<ObjectDoubleEntry<KEY>>() {
131 			TObjectDoubleIterator<KEY> iter = paramsMap.iterator();
132 			ObjectDoubleEntry<KEY> entry = new ObjectDoubleEntry<KEY>();
133 
134 			@Override
135 			public boolean hasNext() {
136 				return iter.hasNext();
137 			}
138 
139 			@Override
140 			public ObjectDoubleEntry<KEY> next() {
141 				iter.advance();
142 				entry.key = iter.key();
143 				entry.value = iter.value();
144 				return entry;
145 			}
146 
147 			@Override
148 			public void remove() {
149 				throw new UnsupportedOperationException("Not supported");
150 			}
151 		};
152 	}
153 }