Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,71 @@
|
|||
/* TrySort.java */
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Random;
|
||||
|
||||
public class TrySort {
|
||||
static boolean useC;
|
||||
static {
|
||||
try {
|
||||
System.loadLibrary("TrySort");
|
||||
useC = true;
|
||||
} catch(UnsatisfiedLinkError e) {
|
||||
useC = false;
|
||||
}
|
||||
}
|
||||
|
||||
static native void sortInC(int[] ary);
|
||||
|
||||
static class IntList extends java.util.AbstractList<Integer> {
|
||||
int[] ary;
|
||||
IntList(int[] ary) { this.ary = ary; }
|
||||
public Integer get(int i) { return ary[i]; }
|
||||
public Integer set(int i, Integer j) {
|
||||
Integer o = ary[i]; ary[i] = j; return o;
|
||||
}
|
||||
public int size() { return ary.length; }
|
||||
}
|
||||
|
||||
static class ReverseAbsCmp
|
||||
implements java.util.Comparator<Integer>
|
||||
{
|
||||
public int compare(Integer pa, Integer pb) {
|
||||
/* Order from highest to lowest absolute value. */
|
||||
int a = pa > 0 ? -pa : pa;
|
||||
int b = pb > 0 ? -pb : pb;
|
||||
return a < b ? -1 : a > b ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
static void sortInJava(int[] ary) {
|
||||
Collections.sort(new IntList(ary), new ReverseAbsCmp());
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
/* Create an array of random integers. */
|
||||
int[] ary = new int[1000000];
|
||||
Random rng = new Random();
|
||||
for (int i = 0; i < ary.length; i++)
|
||||
ary[i] = rng.nextInt();
|
||||
|
||||
/* Do the reverse sort. */
|
||||
if (useC) {
|
||||
System.out.print("Sorting in C... ");
|
||||
sortInC(ary);
|
||||
} else {
|
||||
System.out.print
|
||||
("Missing library for C! Sorting in Java... ");
|
||||
sortInJava(ary);
|
||||
}
|
||||
|
||||
for (int i = 0; i < ary.length - 1; i++) {
|
||||
int a = ary[i];
|
||||
int b = ary[i + 1];
|
||||
if ((a > 0 ? -a : a) > (b > 0 ? -b : b)) {
|
||||
System.out.println("*BUG IN SORT*");
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
System.out.println("ok");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
/* TrySort.c */
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "TrySort.h"
|
||||
|
||||
static void fail(JNIEnv *jenv, const char *error_name) {
|
||||
jclass error_class = (*jenv)->FindClass(jenv, error_name);
|
||||
(*jenv)->ThrowNew(jenv, error_class, NULL);
|
||||
}
|
||||
|
||||
static int reverse_abs_cmp(const void *pa, const void *pb) {
|
||||
jint a = *(jint *)pa;
|
||||
jint b = *(jint *)pb;
|
||||
a = a > 0 ? -a : a;
|
||||
b = b > 0 ? -b : b;
|
||||
return a < b ? -1 : a > b ? 1 : 0;
|
||||
}
|
||||
|
||||
void Java_TrySort_sortInC(JNIEnv *jenv, jclass obj, jintArray ary) {
|
||||
jint *elem, length;
|
||||
|
||||
if (ary == NULL) {
|
||||
fail(jenv, "java/lang/NullPointerException");
|
||||
return;
|
||||
}
|
||||
length = (*jenv)->GetArrayLength(jenv, ary);
|
||||
elem = (*jenv)->GetPrimitiveArrayCritical(jenv, ary, NULL);
|
||||
if (elem == NULL) {
|
||||
fail(jenv, "java/lang/OutOfMemoryError");
|
||||
return;
|
||||
}
|
||||
qsort(elem, length, sizeof(jint), reverse_abs_cmp);
|
||||
(*jenv)->ReleasePrimitiveArrayCritical(jenv, ary, elem, 0);
|
||||
}
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
# Makefile
|
||||
|
||||
# Edit the next lines to match your JDK.
|
||||
JAVA_HOME = /usr/local/jdk-1.8.0
|
||||
CPPFLAGS = -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/openbsd
|
||||
JAVAC = $(JAVA_HOME)/bin/javac
|
||||
JAVAH = $(JAVA_HOME)/bin/javah
|
||||
|
||||
CC = cc
|
||||
LDFLAGS = -shared -fPIC
|
||||
LIB = libTrySort.so
|
||||
|
||||
all: TrySort.class $(LIB)
|
||||
|
||||
$(LIB): TrySort.c TrySort.h
|
||||
$(CC) $(CPPFLAGS) $(LDFLAGS) -o $@ TrySort.c
|
||||
|
||||
.SUFFIXES: .class .java .h
|
||||
.class.h:
|
||||
rm -f $@
|
||||
$(JAVAH) -jni -o $@ $(<:.class=)
|
||||
.java.class:
|
||||
$(JAVAC) $<
|
||||
|
||||
clean:
|
||||
rm -f TrySort.class TrySort?IntList.class \
|
||||
TrySort?ReverseAbsCmp.class TrySort.h $(LIB)
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
import com.sun.jna.Library;
|
||||
import com.sun.jna.Native;
|
||||
|
||||
public class LoadLibJNA{
|
||||
private interface YourSharedLibraryName extends Library{
|
||||
//put shared library functions here with no definition
|
||||
public void sharedLibraryfunction();
|
||||
}
|
||||
|
||||
public static void main(String[] args){
|
||||
YourSharedLibraryName lib = (YourSharedLibraryName)Native.loadLibrary("sharedLibrary",//as in "sharedLibrary.dll"
|
||||
YourSharedLibraryName.class);
|
||||
lib.sharedLibraryFunction();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue