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; 025 026 import java.io.Serializable; 027 import java.util.Arrays; 028 029 public class Coordinates implements Serializable { 030 private static final long serialVersionUID = 8361257560328772093L; 031 032 public static final int Y = Matrix.Y; 033 034 public static final int X = Matrix.X; 035 036 public static final int Z = Matrix.Z; 037 038 public static final int ROW = Matrix.ROW; 039 040 public static final int COLUMN = Matrix.COLUMN; 041 042 public static final int ALL = Matrix.ALL; 043 044 public static final long[] ZERO2D = new long[] { 0, 0 }; 045 046 public long[] co; 047 048 public Coordinates(long... dimensions) { 049 // cannot use Arrays.copyOf(): not supported in Java 5 050 this.co = new long[dimensions.length]; 051 System.arraycopy(dimensions, 0, this.co, 0, dimensions.length); 052 } 053 054 public Coordinates(Coordinates c) { 055 // cannot use Arrays.copyOf(): not supported in Java 5 056 this.co = new long[c.co.length]; 057 System.arraycopy(c.co, 0, this.co, 0, c.co.length); 058 } 059 060 public final int hashCode() { 061 return Arrays.hashCode(co); 062 } 063 064 public boolean equals(Coordinates c) { 065 return Arrays.equals(co, c.co); 066 } 067 068 public static long product(long[] c) { 069 long product = 1; 070 for (int i = c.length - 1; i != -1; i--) { 071 product *= c[i]; 072 } 073 return product; 074 } 075 076 public String toString() { 077 StringBuilder s = new StringBuilder(); 078 s.append("["); 079 for (int i = 0; i < co.length; i++) { 080 s.append(co[i]); 081 if (i < co.length - 1) { 082 s.append(", "); 083 } 084 } 085 s.append("]"); 086 return s.toString(); 087 } 088 089 public boolean equals(Object o) { 090 if (this == o) { 091 return true; 092 } 093 if (o instanceof Coordinates) { 094 return equals((Coordinates) o); 095 } else { 096 return false; 097 } 098 } 099 100 public void fillWithValue(long value) { 101 Arrays.fill(co, value); 102 } 103 104 public void clear() { 105 Arrays.fill(co, 0); 106 } 107 108 public static String toString(long... coordinates) { 109 return toString(',', coordinates); 110 } 111 112 public static String toString(char separator, long... coordinates) { 113 StringBuilder buf = new StringBuilder(); 114 for (int i = 0; i < coordinates.length; i++) { 115 buf.append(coordinates[i]); 116 if (i < coordinates.length - 1) { 117 buf.append(separator); 118 } 119 } 120 return buf.toString(); 121 } 122 123 public Coordinates clone() { 124 Coordinates c = new Coordinates(this); 125 return c; 126 } 127 128 public static final long[] plus(long[] c1, long[] c2) { 129 long[] co = copyOf(c1); 130 for (int i = co.length - 1; i != -1; i--) { 131 co[i] += c2[i]; 132 } 133 return co; 134 } 135 136 public static final long[] multiply(long[] c1, long[] c2) { 137 long[] co = copyOf(c1); 138 for (int i = co.length - 1; i != -1; i--) { 139 co[i] *= c2[i]; 140 } 141 return co; 142 } 143 144 public static final long[] modulo(long[] c1, long[] c2) { 145 long[] co = new long[c1.length]; 146 for (int i = co.length - 1; i != -1; i--) { 147 co[i] = c1[i] % c2[i]; 148 } 149 return co; 150 } 151 152 public static final long[] minus(long[] c1, long[] c2) { 153 long[] co = copyOf(c1); 154 for (int i = co.length - 1; i != -1; i--) { 155 co[i] -= c2[i]; 156 } 157 return co; 158 } 159 160 public static final long[] max(long[] c1, long[] c2) { 161 long[] co = copyOf(c1); 162 for (int i = co.length - 1; i != -1; i--) { 163 if (c2[i] > co[i]) { 164 co[i] = c2[i]; 165 } 166 } 167 return co; 168 } 169 170 public static final long[] min(long[] c1, long[] c2) { 171 long[] co = copyOf(c1); 172 for (int i = co.length - 1; i != -1; i--) { 173 if (c2[i] < co[i]) { 174 co[i] = c2[i]; 175 } 176 } 177 return co; 178 } 179 180 public static long[] parseString(String s) { 181 String[] fields = s.split("[,;\tx]"); 182 long[] dims = new long[fields.length]; 183 for (int i = fields.length - 1; i != -1; i--) { 184 dims[i] = Long.parseLong(fields[i]); 185 } 186 return dims; 187 } 188 189 public int getDimensionCount() { 190 return co.length; 191 } 192 193 public static final boolean equals(long[] c1, long[] c2) { 194 if (c1 == c2) { 195 return true; 196 } 197 if (c1.length != c2.length) { 198 return false; 199 } 200 for (int i = 0; i < c1.length; i++) { 201 if (c1[i] != c2[i]) { 202 return false; 203 } 204 } 205 return true; 206 } 207 208 public static long[] copyOf(long[] c) { 209 long[] ret = new long[c.length]; 210 System.arraycopy(c, 0, ret, 0, c.length); 211 return ret; 212 } 213 214 public static final long[] transpose(long[] c) { 215 long[] copy = copyOf(c); 216 copy[ROW] = c[COLUMN]; 217 copy[COLUMN] = c[ROW]; 218 return copy; 219 } 220 221 public static final long[] transpose(long[] c, int swap1, int swap2) { 222 long[] copy = copyOf(c); 223 copy[swap1] = c[swap2]; 224 copy[swap2] = c[swap1]; 225 return copy; 226 } 227 228 public static boolean isInsideOf(long[] coordinates, long[] position, long[] size) { 229 if (coordinates[ROW] < position[ROW]) { 230 return false; 231 } 232 if (coordinates[COLUMN] < position[COLUMN]) { 233 return false; 234 } 235 long[] secondCorner = Coordinates.plus(position, size); 236 if (coordinates[ROW] >= secondCorner[ROW]) { 237 return false; 238 } 239 if (coordinates[COLUMN] >= secondCorner[COLUMN]) { 240 return false; 241 } 242 return true; 243 } 244 245 public static boolean isSmallerThan(long[] coordinates, long[] size) { 246 for (int i = coordinates.length - 1; i != -1; i--) { 247 if (coordinates[i] >= size[i]) 248 return false; 249 } 250 return true; 251 } 252 253 public static boolean isSmallerOrEqual(long[] coordinates, long[] size) { 254 for (int i = coordinates.length - 1; i != -1; i--) { 255 if (coordinates[i] > size[i]) 256 return false; 257 } 258 return true; 259 } 260 261 public static long[] minusOne(long[] coordinates) { 262 long[] ret = new long[coordinates.length]; 263 for (int i = coordinates.length - 1; i != -1; i--) { 264 ret[i] = coordinates[i] - 1; 265 } 266 return ret; 267 } 268 269 public static boolean isZero(long[] coordinates) { 270 for (int i = coordinates.length - 1; i != -1; i--) { 271 if (coordinates[i] != 0) 272 return false; 273 } 274 return true; 275 } 276 277 }