Skip to content

Commit

Permalink
Don't attempt to sort empty or singleton lists, also fix missed inter…
Browse files Browse the repository at this point in the history
…nal variable name changes
  • Loading branch information
nickavv committed Mar 31, 2021
1 parent d8e294f commit e46f288
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
32 changes: 31 additions & 1 deletion gm-stream/objects/obj_tests/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,36 @@ function testSort() {
return new TestResult(testName, true, "");
}

function testSortEmptyList() {
var testName = "Sort Empty List";
var initialArray = [];

var result = stream_of(initialArray)
.sort()
.collectJoining();

if (result != "") {
return new TestResult(testName, false, "expected result to be empty");
}

return new TestResult(testName, true, "");
}

function testSortMonoList() {
var testName = "Sort Mono List";
var initialArray = ["A"];

var result = stream_of(initialArray)
.sort()
.collectJoining();

if (result != "A") {
return new TestResult(testName, false, "expected result to be same as input");
}

return new TestResult(testName, true, "");
}

function testSortNumeric() {
var testName = "Sort Simple (Numeric)";
var initialArray = [3, 1, 20];
Expand Down Expand Up @@ -498,7 +528,7 @@ totalCount = 0;
failedCount = 0;
testSuite = [testListToStream, testArrayToStream, testCollectList, testCollectArray, testDistinct, testCount, testFilter, testMap, testAllMatchTrue, testAllMatchFalse,
testAnyMatchTrue, testAnyMatchFalse, testForEach, testCollectJoining, testCollectJoiningDelimiter, testCollectJoiningDelimiterPrefix,
testCollectJoiningDelimiterPrefixSuffix, testFindFirstEmpty, testFindFirst, testSort, testSortNumeric, testSortComparator, testNoneMatchTrue,
testCollectJoiningDelimiterPrefixSuffix, testFindFirstEmpty, testFindFirst, testSort, testSortEmptyList, testSortMonoList, testSortNumeric, testSortComparator, testNoneMatchTrue,
testNoneMatchFalse, testQueueToStream, testStackToStream, testFold, testReduce, testReduceEmpty];
show_debug_message("\nBeginning test suite\n");
for (var i = 0; i < array_length(testSuite); i++) {
Expand Down
30 changes: 16 additions & 14 deletions gm-stream/scripts/scr_gmstream/scr_gmstream.gml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,16 @@ function GmStream(_gms_data) constructor {
}

sort = function(comparatorFunction) {
if (is_undefined(comparatorFunction) && isSorted == false) {
ds_list_sort(gms_data, true);
isSorted = true;
} else {
// Don't check or set isSorted in this case as a new comparator function could re-sort us
var result = self.mergesort(gms_data, comparatorFunction);
ds_list_copy(gms_data, result);
ds_list_destroy(result);
if (ds_list_size(gms_data) > 1) {
if (is_undefined(comparatorFunction) && isSorted == false) {
ds_list_sort(gms_data, true);
isSorted = true;
} else {
// Don't check or set isSorted in this case as a new comparator function could re-sort us
var result = self.mergesort(gms_data, comparatorFunction);
ds_list_copy(gms_data, result);
ds_list_destroy(result);
}
}
return self;
}
Expand Down Expand Up @@ -144,8 +146,8 @@ function GmStream(_gms_data) constructor {

fold = function(initialValue, foldFunction) {
var result = initialValue;
for (var i = 0; i < ds_list_size(data); i++) {
result = foldFunction(result, data[| i]);
for (var i = 0; i < ds_list_size(gms_data); i++) {
result = foldFunction(result, gms_data[| i]);
}
self.clean_up();
return result;
Expand All @@ -164,13 +166,13 @@ function GmStream(_gms_data) constructor {
}

reduce = function(foldFunction) {
if (ds_list_size(data) == 0) {
if (ds_list_size(gms_data) == 0) {
self.clean_up();
throw "Cannot reduce an empty stream";
}
var result = data[| 0];
for (var i = 1; i < ds_list_size(data); i++) {
result = foldFunction(result, data[| i]);
var result = gms_data[| 0];
for (var i = 1; i < ds_list_size(gms_data); i++) {
result = foldFunction(result, gms_data[| i]);
}
self.clean_up();
return result;
Expand Down

0 comments on commit e46f288

Please sign in to comment.