Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

performance improvements #53

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
18 changes: 17 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.hashids</groupId>
<artifactId>hashids</artifactId>
<version>1.0.3-SNAPSHOT</version>
<version>1.0.4-SNAPSHOT</version>
<packaging>jar</packaging>
<name>hashids</name>
<description>Implementing Hashids algorithm v1.0.0 version(http://hashids.org)</description>
Expand Down Expand Up @@ -190,6 +190,18 @@
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.9</version>
<scope>test</scope>
</dependency>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you add this dependency?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My mistake. I have run a benchmark to check the performance. Thanks for seeing it! :)

</dependencies>

<contributors>
Expand All @@ -207,5 +219,9 @@
<name>Matthias Vill</name>
<url>https://github.com/TheConstructor</url>
</contributor>
<contributor>
<name>Mihai Cazacu</name>
<url>https://github.com/cazacugmihai</url>
</contributor>
</contributors>
</project>
112 changes: 112 additions & 0 deletions src/main/java/org/hashids/CharUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package org.hashids;

import static java.lang.System.arraycopy;
import static java.util.Arrays.copyOf;

public final class CharUtils {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the public modifier.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? Is an utility class (like Math, Arrays, etc.).

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this name crashes with several CharUtils out there, this is a helper class for hashids, not broad use.


private CharUtils() {
throw new UnsupportedOperationException();
}

public static char[] concatenate(char[] arrA, char[] arrB, char[] arrC) {
final char[] result = new char[arrA.length + arrB.length + arrC.length];

arraycopy(arrA, 0, result, 0, arrA.length);
arraycopy(arrB, 0, result, arrA.length, arrB.length);
arraycopy(arrC, 0, result, arrA.length + arrB.length, arrC.length);

return result;
}

public static char[] concatenate(char[] arrA, char[] arrB, int bFrom, int bTo) {
final int bCopyLength = bTo - bFrom;
final char[] result = new char[arrA.length + bCopyLength];

arraycopy(arrA, 0, result, 0, arrA.length);
arraycopy(arrB, bFrom, result, arrA.length, bCopyLength);

return result;
}

public static int indexOf(char[] source, char c) {
int i = 0;

for (final char s : source) {
if (s == c) {
break;
}
i++;
}

return i;
}

public static char[] cleanup(char[] source, char[] allowedChars) {
if ((source == null) || (allowedChars == null)) {
return source;
}

final char[] result = new char[source.length];
int i = 0;

for (final char s : source) {
for (final char a : allowedChars) {
if (s == a) {
result[i++] = s;
break;
}
}
}

return copyOf(result, i);
}

public static char[] removeAll(char[] source, char[] charsToRemove) {
if ((source == null) || (charsToRemove == null)) {
return source;
}

final char[] result = new char[source.length];
int i = 0;
boolean found;

for (final char s : source) {
found = false;

for (final char c : charsToRemove) {
if (s == c) {
found = true;
break;
}
}

if (!found) {
result[i++] = s;
}
}

return copyOf(result, i);
}

public static boolean validate(char[] source, char[] allowedChars) {
boolean found;

for (final char s : source) {
found = false;

for (final char a : allowedChars) {
if (s == a) {
found = true;
break;
}
}

if (!found) {
return false;
}
}

return true;
}
}
Loading