📄 genmath.java
字号:
/* -*- tab-width: 4 -*- * * Electric(tm) VLSI Design System * * File: GenMath.java * * Copyright (c) 2003 Sun Microsystems and Static Free Software * * Electric(tm) is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * Electric(tm) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Electric(tm); see the file COPYING. If not, write to * the Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, Mass 02111-1307, USA. */package com.sun.electric.database.geometry;import java.awt.Point;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.awt.geom.AffineTransform;import java.util.ArrayList;import java.util.List;import java.util.Map;import java.util.StringTokenizer;import java.io.Serializable;/** * General Math Functions. If you are working in Database Units, you * should be using DBMath instead. */public class GenMath{ /** * Minimal integer that is considered "small int". * Sum and difference of two "small ints" is always in [Integer.MIN_VALUE..Integer.MAX_VALUE] range. */ public static final int MIN_SMALL_COORD = Integer.MIN_VALUE/2; // 0xC0000000 /** * Maximal ineger that is considered "small int". * Sum and difference of two "small ints" is always in [Integer.MIN_VALUE..Integer.MAX_VALUE] range. */ public static final int MAX_SMALL_COORD = Integer.MAX_VALUE/2; // 0x3FFFFFFF static final double HALF = 0.5 - 0.5/(1L << 53); // 0x1.fffffffffffffp-2; /** * General method to obtain quadrant for a given box in a qTree based on the qTree center * @param centerX the X center of the qTree. * @param centerY the Y center of the qTree. * @param box the given box. * @return the quadrant number. */ public static int getQuadrants(double centerX, double centerY, Rectangle2D box) { int loc = 0; if (box.getMinY() < centerY) { // either 0 or 1 quadtrees if (box.getMinX() < centerX) loc |= 1 << 0; if (box.getMaxX() > centerX) loc |= 1 << 1; } if (box.getMaxY() > centerY) { // the other quadtrees if (box.getMinX() < centerX) loc |= 1 << 2; if (box.getMaxX() > centerX) loc |= 1 << 3; } return loc; } /** * Calculates the bounding box of a child depending on the location. Parameters are passed to avoid * extra calculation * @param x Parent x value * @param y Parent y value * @param w Child width (1/4 of parent if qtree) * @param h Child height (1/2 of parent if qtree) * @param centerX Parent center x value * @param centerY Parent center y value * @param loc Location in qtree */ public static Rectangle2D getQTreeBox(double x, double y, double w, double h, double centerX, double centerY, int loc) { if ((loc >> 0 & 1) == 1) { x = centerX; } if ((loc >> 1 & 1) == 1) { y = centerY; } return (new Rectangle2D.Double(x, y, w, h)); } /******************************************************************************************************** * *******************************************************************************************************/ /** * Method to transform an array of doubles into a string that can be stored in a preference. * The format of the string is "(v1 v2 v3 ...)" * @param s the values. * @return string representing the array. */ public static String transformArrayIntoString(double [] s) { StringBuffer sb = new StringBuffer(); for(int i=0; i<s.length; i++) { if (i == 0) sb.append('('); else sb.append(' '); sb.append(s[i]); } sb.append(')'); String dir = sb.toString(); return dir; } /** * Method to extract an array of doubles from a string. * The format of the string is "(v1 v2 v3 ...)" * @param vector the input vector in string form. * @return the array of values. */ public static double[] transformStringIntoArray(String vector) { StringTokenizer parse = new StringTokenizer(vector, "( )", false); List<Double> valuesFound = new ArrayList<Double>(); while (parse.hasMoreTokens()) { String value = parse.nextToken(); try { valuesFound.add(new Double(Double.parseDouble(value))); } catch (Exception e) { e.printStackTrace(); } } double [] values = new double[valuesFound.size()]; for(int i=0; i<valuesFound.size(); i++) values[i] = valuesFound.get(i).doubleValue(); return values; } /** * Class to define an Integer-like object that can be modified. */ public static class MutableInteger implements Serializable { private int value; /** * Constructor creates a MutableInteger object with an initial value. * @param value the initial value. */ public MutableInteger(int value) { this.value = value; } /** * Method to change the value of this MutableInteger. * @param value the new value. */ public void setValue(int value) { this.value = value; } /** * Method to add the value of this MutableInteger. * @param value the value to add. */ public void addValue(int value) { this.value += value; } /** * Method to increment this MutableInteger by 1. */ public void increment() { value++; } /** * Method to increment this MutableInteger by 1. */ public void decrement() { value--; } /** * Method to return the value of this MutableInteger. * @return the current value of this MutableInteger. */ public int intValue() { return value; } /** * Returns a printable version of this MutableInteger. * @return a printable version of this MutableInteger. */ public String toString() { return Integer.toString(value); } } /** * Class to define a Long-like object that can be modified. */ public static class MutableLong implements Serializable { private long value; /** * Constructor creates a MutableLong object with an initial value. * @param value the initial value. */ public MutableLong(long value) { this.value = value; } /** * Method to change the value of this MutableLong. * @param value the new value. */ public void setValue(long value) { this.value = value; } /** * Method to increment this MutableLong by 1. */ public void increment() { value++; } /** * Method to return the value of this MutableLong. * @return the current value of this MutableLong. */ public long longValue() { return value; } /** * Returns a printable version of this MutableLong. * @return a printable version of this MutableLong. */ public String toString() { return Long.toString(value); } } /** * Increments count to object in a bag. * If object was not in a bag, it will be added. * @param bag Map implementing Bag. * @param key object to add to bag. */ public static <T> void addToBag(Map<T,MutableInteger> bag, T key) { addToBag(bag, key, 1); } /** * Adds to bag another bag. * @param bag bag to update. * @param otherBag bag used for update. */ public static <T> void addToBag(Map<T,MutableInteger> bag, Map<T,MutableInteger> otherBag) { for (Map.Entry<T,MutableInteger> e : otherBag.entrySet()) { MutableInteger count = e.getValue(); addToBag(bag, e.getKey(), count.intValue()); } } /** * Adds to count of object in a bag. * If object was not in a bag, it will be added. * @param bag Map implementing Bag. * @param key object in a bag. * @param c count to add to bag. */ public static <T> void addToBag(Map<T,MutableInteger> bag, T key, int c) { MutableInteger count = bag.get(key); if (count == null) { count = new MutableInteger(0); bag.put(key, count); } count.setValue(count.intValue() + c); } /** * Method to return the a value at a location in a collection. * @param bag the collection (a Map). * @param key a key to an entry in the collection. * @return the value at that key. */ public static <T> int countInBag(Map<T,MutableInteger> bag, T key) { MutableInteger count = bag.get(key); return count != null ? count.intValue() : 0; } /** * Class to define an Double-like object that can be modified. */ public static class MutableDouble { private double value; /** * Constructor creates a MutableDouble object with an initial value. * @param value the initial value. */ public MutableDouble(double value) { this.value = value; } /** * Method to change the value of this MutableDouble. * @param value the new value. */ public void setValue(double value) { this.value = value; } /** * Method to return the value of this MutableDouble. * @return the current value of this MutableDouble. */ public double doubleValue() { return value; } /** * Returns a printable version of this MutableDouble. * @return a printable version of this MutableDouble. */ public String toString() { return Double.toString(value); } } /** * Class to define a Boolean object that can be modified. */ public static class MutableBoolean implements Serializable {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -