Skip to content

Instantly share code, notes, and snippets.

@sonygod
Created November 26, 2019 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonygod/1699f69cd14d5065eabb00b39f8d6ef1 to your computer and use it in GitHub Desktop.
Save sonygod/1699f69cd14d5065eabb00b39f8d6ef1 to your computer and use it in GitHub Desktop.
bytes quick sort test
import haxe.MainLoop;
import haxe.io.Bytes;
import gen.GenKLSFJBytes.GenKLSFJSONBytes;
import utils.CustomTrace;
import utils.QuickSort;
import utils.PK10Bytes;
class KLSFGEN {
public static function main() {
CustomTrace.init();
var len:Int = 1000;
var a = [for (i in 0...len) Random.int(0, 255)];
var realLen:Int = cast len / 10;
var b = Bytes.alloc(len);
for (i in 0...len) {
b.set(i, a[i]);
}
var ap = new PK10Bytes(b);
var first = ap[0];
var se = ap[1];
trace('start ${len}个');
var q = new QuickSort(ap);
// QuickSort.quickSort(ap, 0, realLen - 1);
for (i in 0...10) {
MainLoop.addThread(function() {
q.work(i, function() {
for (j in 0...realLen) {
trace(ap[j].getSum());
}
});
});
}
trace("end");
// GenKLSFJSONBytes.genMulThread();
}
}
package utils;
import haxe.io.Bytes;
abstract PK10Bytes(Bytes) from Bytes to Bytes {
public function new(s:Bytes) {
this = s;
}
@:arrayAccess function get(k:Int):PK10Bytes {
var out = Bytes.alloc(10);
out.blit(0, this, k * 10, 10);
return cast out;
}
@:arrayAccess function set(k:Int, v:Bytes) {
return this.blit(k * 10, v, 0, 10);
}
@:op(A < B) static function lessThan(a:PK10Bytes, b:PK10Bytes):Bool {
return a.getSum() < b.getSum();
}
@:op(A <= B) static function lessThan2(a:PK10Bytes, b:PK10Bytes):Bool {
return a.getSum() <= b.getSum();
}
@:op(A > B) static function bigThan(a:PK10Bytes, b:PK10Bytes):Bool {
return a.getSum() > b.getSum();
}
@:op(A >= B) static function bigThan2(a:PK10Bytes, b:PK10Bytes):Bool {
return a.getSum() >= b.getSum();
}
@:op(A == B) static function equal(a:PK10Bytes, b:PK10Bytes):Bool {
return a.getSum() == b.getSum();
}
public function getSum():Int {
var sum = 0;
for (i in 0...10) {
sum += this.get(i);
}
return sum;
}
}
package utils;
import haxe.io.Bytes;
@:expose
class QuickSort {
var data:Bytes;
var arr:PK10Bytes;
var thread_num:Int;
var thread:Int;
var already:Int;
var maxNum:Int;
public function new(data:Bytes) {
this.data = data;
arr = new PK10Bytes(data);
thread = 10;
thread_num = cast data.length / (10 * thread);
already = 0;
maxNum = cast data.length / 10;
}
public function sort(begin:Int, end:Int) {
if (begin < end) {
var temp = arr[begin];
var i = begin;
var j = end;
while (i < j) {
while (i < j && arr[j] > temp)
j--;
arr[i] = arr[j];
while (i < j && arr[i] <= temp)
i++;
arr[j] = arr[i];
}
arr[i] = temp;
sort(begin, i - 1);
sort(i + 1, end);
} else
return;
}
/**
*
* @param index
*/
public function work(index:Int, callBack:() -> Void) {
sort(cast index * thread_num / 10, cast(index + 1) * thread_num / 10);
already++;
if (already == thread) {
meger();
callBack();
}
}
public function meger() {
// still not work.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment