001 /* 002 * Copyright (C) 2008-2010 by Holger Arndt 003 * 004 * This file is part of the Universal Java Matrix Package (UJMP). 005 * See the NOTICE file distributed with this work for additional 006 * information regarding copyright ownership and licensing. 007 * 008 * UJMP is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU Lesser General Public License as 010 * published by the Free Software Foundation; either version 2 011 * of the License, or (at your option) any later version. 012 * 013 * UJMP is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public 019 * License along with UJMP; if not, write to the 020 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, 021 * Boston, MA 02110-1301 USA 022 */ 023 024 package org.ujmp.core.objectmatrix.stub; 025 026 import java.util.Map; 027 028 import org.ujmp.core.Coordinates; 029 import org.ujmp.core.Matrix; 030 import org.ujmp.core.exceptions.MatrixException; 031 import org.ujmp.core.interfaces.Wrapper; 032 import org.ujmp.core.util.CoordinateSetToLongWrapper; 033 import org.ujmp.core.util.MathUtil; 034 035 public abstract class AbstractMapToSparseMatrixWrapper extends AbstractSparseObjectMatrix implements 036 Wrapper<Map<Coordinates, Object>> { 037 private static final long serialVersionUID = -6292034262789053069L; 038 039 private final Object defaultValue = null; 040 041 private final Map<Coordinates, Object> values; 042 043 private int maximumNumberOfEntries = -1; 044 045 private final long[] size; 046 047 public AbstractMapToSparseMatrixWrapper(Map<Coordinates, Object> map, Matrix m) { 048 this.size = Coordinates.copyOf(m.getSize()); 049 this.values = map; 050 for (long[] c : m.allCoordinates()) { 051 setObject(m.getAsObject(c), c); 052 } 053 } 054 055 public AbstractMapToSparseMatrixWrapper(Map<Coordinates, Object> map, long... size) { 056 this.size = Coordinates.copyOf(size); 057 this.values = map; 058 } 059 060 public final Map<Coordinates, Object> getMap() { 061 return values; 062 } 063 064 public final long[] getSize() { 065 return size; 066 } 067 068 public final Map<Coordinates, Object> getWrappedObject() { 069 return getMap(); 070 } 071 072 public final void setWrappedObject(Map<Coordinates, Object> object) { 073 throw new MatrixException("not allowed"); 074 } 075 076 public final Object getObject(long... coordinates) throws MatrixException { 077 Object v = getMap().get(new Coordinates(coordinates)); 078 return v == null ? defaultValue : v; 079 } 080 081 public final boolean contains(long... coordinates) { 082 return getMap().containsKey(new Coordinates(coordinates)); 083 } 084 085 public final double getAsDouble(long... coordinates) throws MatrixException { 086 return MathUtil.getDouble(getObject(coordinates)); 087 } 088 089 public final void setAsDouble(double v, long... coordinates) throws MatrixException { 090 setObject(v, coordinates); 091 } 092 093 public final void setObject(Object o, long... coordinates) throws MatrixException { 094 while (maximumNumberOfEntries > 0 && getMap().size() > maximumNumberOfEntries) { 095 getMap().remove(getMap().keySet().iterator().next()); 096 } 097 if (Coordinates.isSmallerThan(coordinates, getSize())) { 098 getMap().put(new Coordinates(coordinates), o); 099 } 100 } 101 102 public final int getMaximumNumberOfEntries() { 103 return maximumNumberOfEntries; 104 } 105 106 public final long getValueCount() { 107 return getMap().size(); 108 } 109 110 public final Iterable<long[]> availableCoordinates() { 111 return new CoordinateSetToLongWrapper(getMap().keySet()); 112 } 113 114 public final void setMaximumNumberOfEntries(int maximumNumberOfEntries) { 115 this.maximumNumberOfEntries = maximumNumberOfEntries; 116 } 117 118 }