$darkmode
DENOPTIM
GeneralUtils.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.utils;
20
21
22import java.text.DecimalFormat;
23import java.text.NumberFormat;
24import java.util.Collections;
25import java.util.Iterator;
26import java.util.List;
27import java.util.Locale;
28import java.util.Set;
29
30import denoptim.constants.DENOPTIMConstants;
31
32
37public class GeneralUtils
38{
39 private static Runtime RUNTIME = Runtime.getRuntime();
40 private final static String NL = DENOPTIMConstants.EOL;
41
42//------------------------------------------------------------------------------
43
51 public static String getPaddedString(int count, int number)
52 {
53 return String.format("%0" + count + "d", number);
54 }
55
56//------------------------------------------------------------------------------
57
61 public static void printMemoryDetails()
62 {
63 NumberFormat format = NumberFormat.getInstance();
64 StringBuilder sb = new StringBuilder();
65 long maxMemory = RUNTIME.maxMemory();
66 long allocatedMemory = RUNTIME.totalMemory();
67 long freeMemory = RUNTIME.freeMemory();
68 sb.append("Free memory: ");
69 sb.append(format.format(freeMemory / 1024));
70 sb.append(NL);
71 sb.append("Allocated memory: ");
72 sb.append(format.format(allocatedMemory / 1024));
73 sb.append(NL);
74 sb.append("Max memory: ");
75 sb.append(format.format(maxMemory / 1024));
76 sb.append(NL);
77 sb.append("Total free memory: ");
78 sb.append(format.format((freeMemory + (maxMemory - allocatedMemory))
79 / 1024));
80 sb.append(NL);
81
82 System.out.println(sb.toString());
83 }
84
85//------------------------------------------------------------------------------
86
96 public static int getIdxOfClosing(int id, String s)
97 {
98 String openingSing = "";
99 String closingSign = ")";
100 switch (id)
101 {
102 case (1):
103 openingSing = "(";
104 closingSign = ")";
105 break;
106 case (2):
107 openingSing = "]";
108 closingSign = "]";
109 break;
110 case (3):
111 openingSing = "}";
112 closingSign = "}";
113 break;
114 }
115
116 int result = -1;
117 int nOpen = 0;
118 for (int i=0; i<s.length(); i++)
119 {
120 String ss = s.substring(i);
121 if (ss.startsWith(closingSign))
122 {
123 if (nOpen == 0)
124 {
125 result = i;
126 break;
127 }
128 else
129 {
130 nOpen -= 1;
131 }
132 }
133 else if (ss.startsWith(openingSing))
134 {
135 nOpen++;
136 }
137 }
138
139 return result;
140 }
141
142//------------------------------------------------------------------------------
143
149 public static <T> void unionOfIntersectingSets(List<Set<T>> list)
150 {
151 Iterator<Set<T>> iterator = list.iterator();
152
153 int prevSize = 1;
154 int currentSize = 0;
155 while (prevSize > currentSize)
156 {
157 prevSize = list.size();
158 while (iterator.hasNext())
159 {
160 Set<T> setA = iterator.next();
161 Iterator<Set<T>> innerIterator = list.iterator();
162 while (innerIterator.hasNext())
163 {
164 Set<T> setB = innerIterator.next();
165 if (setA == setB)
166 continue;
167 if (!Collections.disjoint(setA,setB))
168 {
169 setB.addAll(setA);
170 iterator.remove();
171 break;
172 }
173 }
174 }
175 currentSize = list.size();
176 }
177 }
178
179//------------------------------------------------------------------------------
180
190 public static String getEnglishFormattedDecimal(String pattern,
191 int decimals, double value)
192 {
193 NumberFormat nf = NumberFormat.getNumberInstance(Locale.ENGLISH);
194 DecimalFormat df = (DecimalFormat) nf;
195 df.applyPattern(pattern);
196 df.setMinimumFractionDigits(decimals);
197 return df.format(value);
198 }
199
200//------------------------------------------------------------------------------
201
209 public static String getEnglishFormattedDecimal(String pattern, double value)
210 {
211 return getEnglishFormattedDecimal(pattern,4,value);
212 }
213
214//------------------------------------------------------------------------------
215
216}
General set of constants used in DENOPTIM.
static final String EOL
new line character
static String getEnglishFormattedDecimal(String pattern, int decimals, double value)
Formats a decimal number using the given pattern but with English format as for separators.
static String getEnglishFormattedDecimal(String pattern, double value)
Formats a decimal number using the given pattern but with English format as for separators.
static void printMemoryDetails()
Print an analysis of the current memory usage.
static final String NL
static String getPaddedString(int count, int number)
returns the padded string with zeroes placed to the left of 'number' up to reach the desired number o...
static int getIdxOfClosing(int id, String s)
Return the index of the closing parenthesis.
static< T > void unionOfIntersectingSets(List< Set< T > > list)
Takes the union of any two sets in this list that intersect.