001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.workinprogress.optimisation.params; 031 032import gnu.trove.iterator.TObjectDoubleIterator; 033import gnu.trove.map.hash.TObjectDoubleHashMap; 034import gnu.trove.procedure.TObjectDoubleProcedure; 035 036import java.util.Iterator; 037 038import org.openimaj.workinprogress.optimisation.params.KeyedParameters.ObjectDoubleEntry; 039 040public final class KeyedParameters<KEY> implements Parameters<KeyedParameters<KEY>>, Iterable<ObjectDoubleEntry<KEY>> { 041 public static class ObjectDoubleEntry<KEY> { 042 public KEY key; 043 public double value; 044 } 045 046 private TObjectDoubleHashMap<KEY> paramsMap = new TObjectDoubleHashMap<KEY>(); 047 048 /* 049 * (non-Javadoc) 050 * 051 * @see 052 * org.openimaj.workinprogress.params.Parameters#multiplyInplace(org.openimaj 053 * .workinprogress.params.KeyedParameters) 054 */ 055 @Override 056 public void multiplyInplace(final KeyedParameters<KEY> other) { 057 paramsMap.forEachEntry(new TObjectDoubleProcedure<KEY>() { 058 @Override 059 public boolean execute(KEY key, double value) { 060 if (other.contains(key)) { 061 paramsMap.put(key, other.get(key) * value); 062 } 063 return true; 064 } 065 }); 066 } 067 068 public void multiplyInplace(KEY key, double value) { 069 if (paramsMap.contains(key)) { 070 paramsMap.put(key, paramsMap.get(key) * value); 071 } 072 } 073 074 @Override 075 public void addInplace(final KeyedParameters<KEY> other) { 076 other.paramsMap.forEachEntry(new TObjectDoubleProcedure<KEY>() { 077 @Override 078 public boolean execute(KEY key, double value) { 079 paramsMap.adjustOrPutValue(key, value, value); 080 return true; 081 } 082 }); 083 } 084 085 public void addInplace(KEY key, double value) { 086 paramsMap.adjustOrPutValue(key, value, value); 087 } 088 089 public void set(KEY key, double value) { 090 paramsMap.put(key, value); 091 } 092 093 public double get(KEY key) { 094 return paramsMap.get(key); 095 } 096 097 public boolean contains(KEY key) { 098 return paramsMap.containsKey(key); 099 } 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}