diff --git a/src/objects/oExample/Create_0.gml b/src/objects/oExample/Create_0.gml index e5a76d7..2789d1d 100644 --- a/src/objects/oExample/Create_0.gml +++ b/src/objects/oExample/Create_0.gml @@ -1,3 +1,6 @@ +draw_set_color($ffffff) +draw_set_halign(1) +draw_set_valign(1) randomize() ///@function print(container) @@ -5,10 +8,8 @@ function print(Cont) { foreach(Cont.first(), Cont.last(), show_debug_message) } test1 = new List(4, 2, 8, 13, 11, 9) // 6 test2 = new Array(3, 3.5, 26, 7, 10, 15, 5, 4.5) // 8 - test_sum = new List() show_debug_message("\nThe First List") -test1.emplace_front(40) sort(test1.first(), test1.last()) print(test1) show_debug_message("Is sorted: " + string(is_sorted(test1.first(), test1.last()))) @@ -40,47 +41,12 @@ print(test_sum) show_debug_message("Is parted: " + string(is_parted)) show_debug_message("Parted on: " + string(part_point.get_index())) -show_debug_message("\nMap") -test_mapped_list = new Array("D", "E", "A", "T", "H", "K", "N", "I", "G", "H", "T") -// Transform into paired-container -transform(test_mapped_list.first(), test_mapped_list.last(), test_mapped_list.first(), function (Value) { - return [Value, irandom(3)] -}) -print(test_mapped_list) - -test_map = new Map(test_mapped_list) -show_debug_message("Cashes of Map: ") -print(test_map.cash) -show_debug_message("Contents of Map: ") -print(test_map) - -show_debug_message("\nUnordered_Map") -test_unomap = new Unordered_Map(test_mapped_list) -print(test_unomap) - -/* -show_debug_message("\nMultimap") -show_debug_message("\nTransform a List into a Multimap") -// Transform into paired-container -transform(test_sum.first(), test_sum.last(), test_sum.first(), function (Value) { - return [irandom(2), Value] -}) -test_multimap = new Multimap(test_sum) - -for (var KIt = test_multimap.first(); KIt != test_multimap.last(); KIt.go()) { - var TempKey = test_multimap.get_key(KIt) - var TempList = test_multimap.get(KIt) - show_debug_message("Values with key " + string(TempKey) + ": ") - print(TempList) -} - -/* -var nth = 10 +var nth = 8 show_debug_message("\nNth Sorting (" + string(nth) + ")") random_shuffle(test_sum.first(), test_sum.last()) var nth_interator = iterator_advance(test_sum.first(), nth) var nth_value = nth_interator.get() -var nth_result = nth_element(test_sum.first(), nth_interator, test_sum.last()) +nth_element(test_sum.first(), nth_interator, test_sum.last()) show_debug_message("Rearranged On: " + string(nth_interator.get_index())) show_debug_message("The Stand Value: " + string(nth_value)) @@ -90,5 +56,50 @@ show_debug_message("The " + string(nth) + "th Iterator: " + string(nth_result_it show_debug_message("The " + string(nth) + "th Value: " + string(nth_result_value)) print(test_sum) +show_debug_message("\nTransform a List into a paired List") +test_mapped_list = new List("D", "E", "A", "T", "H", "K", "N", "I", "G", "H", "T") +// Transform into paired-container +transform(test_mapped_list.first(), test_mapped_list.last(), test_mapped_list.first(), function (Value) { + return [Value, irandom(3)] +}) +test_mapped_list.push_back(["@", "1st"]) +test_mapped_list.push_back(["@", "2nd"]) +test_mapped_list.push_back(["@", "3rd"]) +test_mapped_list.push_back(["@", "4th"]) +test_mapped_list.push_back(["@", "5th"]) +test_mapped_list.push_back(["AA", "AA"]) +test_mapped_list.push_back(["AB", "AB"]) +test_mapped_list.push_back(["CT", "CT"]) +test_mapped_list.push_back(["CZ", "CZ"]) +test_mapped_list.push_back(["AC", "AC"]) // 17 +show_debug_message("Map") +test_map = new Map(test_mapped_list) +print(test_map) +show_debug_message("\nUnordered_Map") +test_unomap = new Unordered_Map(test_mapped_list) +print(test_unomap) + +show_debug_message("\nMultimap") +test_multimap = new Multimap(test_mapped_list) +test_multimap.insert(1, 10) +test_multimap.insert(2, 15) +test_multimap.insert(10, 50) +test_multimap.insert(11, 55) +test_multimap.insert(12, 60) +test_multimap.insert(0, new Wrapper("0-1st")) +test_multimap.insert(0, new Wrapper("0-2nd")) +test_multimap.insert(0, new Wrapper("0-3rd")) +test_multimap.insert(1, new Wrapper("1-1st")) +test_multimap.insert(1, new Wrapper("1-2nd")) +test_multimap.insert(1, new Wrapper("1-3rd")) + +var i, TempPair, Key, Value +for (i = 0; i < test_multimap.bucket_count(); ++i) { + TempPair = test_multimap.at(i) + Key = TempPair[0] + Value = TempPair[1] // List + show_debug_message("Values with key " + string(Key) + ": ") + print(Value) +} diff --git a/src/objects/oExample/Draw_64.gml b/src/objects/oExample/Draw_64.gml new file mode 100644 index 0000000..7fac0c2 --- /dev/null +++ b/src/objects/oExample/Draw_64.gml @@ -0,0 +1,2 @@ +/// @description Drawing a text +draw_text(320, 240, "See the output logs.") diff --git a/src/objects/oExample/KeyPress_113.gml b/src/objects/oExample/KeyPress_113.gml new file mode 100644 index 0000000..e52d00e --- /dev/null +++ b/src/objects/oExample/KeyPress_113.gml @@ -0,0 +1,2 @@ +/// @description Restart +room_restart() diff --git a/src/objects/oExample/oExample.yy b/src/objects/oExample/oExample.yy index 48dd993..064b79b 100644 --- a/src/objects/oExample/oExample.yy +++ b/src/objects/oExample/oExample.yy @@ -19,6 +19,8 @@ "physicsShapePoints": [], "eventList": [ {"isDnD":false,"eventNum":0,"eventType":0,"collisionObjectId":null,"parent":{"name":"oExample","path":"objects/oExample/oExample.yy",},"resourceVersion":"1.0","name":"","tags":[],"resourceType":"GMEvent",}, + {"isDnD":false,"eventNum":113,"eventType":9,"collisionObjectId":null,"parent":{"name":"oExample","path":"objects/oExample/oExample.yy",},"resourceVersion":"1.0","name":"","tags":[],"resourceType":"GMEvent",}, + {"isDnD":false,"eventNum":64,"eventType":8,"collisionObjectId":null,"parent":{"name":"oExample","path":"objects/oExample/oExample.yy",},"resourceVersion":"1.0","name":"","tags":[],"resourceType":"GMEvent",}, ], "properties": [], "overriddenProperties": [], diff --git a/src/scripts/Algorithm/Algorithm.gml b/src/scripts/Algorithm/Algorithm.gml index 62b91c3..21aab50 100644 --- a/src/scripts/Algorithm/Algorithm.gml +++ b/src/scripts/Algorithm/Algorithm.gml @@ -12,14 +12,14 @@ function assign(First, Last) { } ///@function erase(begin, [end]) -function erase(First, Last) { - First = make_iterator(First) +function erase() { + var First = make_iterator(argument[0]) if argument_count == 1 { erase_one(First) } else if argument_count == 2 { var Result = First.duplicate() - while First.not_equals(Last) { + while First.not_equals(argument[1]) { erase_one(First) First.go() Result.go() // last @@ -76,10 +76,10 @@ function foreach(First, Last, Pred) { } ///@function find(begin, end, value, [comparator=compare_equal]) -function find(First, Last, Value, Comparator) { +function find(First, Last, Value) { First = make_iterator(First) - var Compare = select_argument(Comparator, compare_equal) + var Compare = 3 < argument_count ? argument[3] : compare_equal while First.not_equals(Last) { if Compare(First.get(), Value) return First @@ -94,7 +94,7 @@ function find_if(First, Last, Pred) { while First.not_equals(Last) { var Value = First.get() - if !is_undefined(Value) and Pred(Value) + if Pred(Value) return First First.go() } @@ -181,7 +181,7 @@ function copy_if(First, Last, Output, Pred) { var Value = 0 while First.not_equals(Last) { Value = First.get() - if !is_undefined(Value) and Pred(Value) + if Pred(Value) Output.set(Value) First.go() Output.go() @@ -206,7 +206,7 @@ function replace_if(First, Last, Pred, NewVal) { while First.not_equals(Last) { var Value = First.get() - if !is_undefined(Value) and Pred(Value) + if Pred(Value) First.set(NewVal) First.go() } @@ -346,13 +346,13 @@ function transform_binary(First, Last, PairFirst, Output, Pred) { } ///@function min_element(begin, end, [comparator=compare_less]) -function min_element(First, Last, Comparator) { +function min_element(First, Last) { if First.equals(Last) return Last var Result = First.duplicate() var Cursor = First.next() - var Compare = select_argument(Comparator, compare_less) + var Compare = 2 < argument_count ? argument[2] : compare_less while Cursor.not_equals(Last) { if Compare(Cursor.get(), Result.get()) { delete Result @@ -366,13 +366,13 @@ function min_element(First, Last, Comparator) { } ///@function max_element(begin, end, [comparator=compare_less]) -function max_element(First, Last, Comparator) { +function max_element(First, Last) { if First.equals(Last) return Last var Result = First.duplicate() var Cursor = First.next() - var Compare = select_argument(Comparator, compare_less) + var Compare = 2 < argument_count ? argument[2] : compare_less while Cursor.not_equals(Last) { if Compare(Result.get(), Cursor.get()) { delete Result @@ -386,10 +386,10 @@ function max_element(First, Last, Comparator) { } ///@function lower_bound(begin, end, value, [comparator=compare_less]) -function lower_bound(First, Last, Value, Comparator) { // return the first and largest element which less than value. +function lower_bound(First, Last, Value) { // return the first and largest element which less than value. First = make_iterator(First) - var Compare = select_argument(Comparator, compare_less) + var Compare = 3 < argument_count ? argument[3] : compare_less var It, Step, count = First.distance(Last) while 0 < count { Step = count * 0.5 @@ -407,10 +407,10 @@ function lower_bound(First, Last, Value, Comparator) { // return the first and l } ///@function upper_bound(begin, end, value, [comparator=compare_less]) -function upper_bound(First, Last, Value, Comparator) { // return a greater element to the value. +function upper_bound(First, Last, Value) { // return a greater element to the value. First = make_iterator(First) - var Compare = select_argument(Comparator, compare_less) + var Compare = 3 < argument_count ? argument[3] : compare_less var It, Step, count = First.distance(Last) while 0 < count { Step = count * 0.5 @@ -428,10 +428,10 @@ function upper_bound(First, Last, Value, Comparator) { // return a greater eleme } ///@function binary_search(begin, end, value, [comparator=compare_less]) -function binary_search(First, Last, Value, Comparator) { +function binary_search(First, Last, Value) { First = make_iterator(First) - var Compare = select_argument(Comparator, compare_less) + var Compare = 3 < argument_count ? argument[3] : compare_less First = lower_bound(First, Last, Value, Compare) var FirstVal = First.get() return First.not_equals(Last) and !Compare(Value, FirstVal) @@ -439,13 +439,13 @@ function binary_search(First, Last, Value, Comparator) { ///@function sort(begin, end, [comparator]) ///@description quick sort -function sort(First, Last, Comparator) { +function sort(First, Last) { First = make_iterator(First) Last = make_iterator(Last) if Last.index <= 1 exit - var Compare = select_argument(Comparator, compare_less) + var Compare = 2 < argument_count ? argument[2] : compare_less var Pivot = First.duplicate(), Pivot_Next, Value = 0 for (var It = First.next(); It.index < First.index + Last.index; It.go()) { Value = It.get() @@ -471,10 +471,10 @@ function sort(First, Last, Comparator) { ///@function stable_sort(begin, end, [comparator=compare_less]) ///@description selection sort -function stable_sort(First, Last, Comparator) { +function stable_sort(First, Last) { First = make_iterator(First) - var selection, Compare = select_argument(Comparator, compare_less) + var selection, Compare = 2 < argument_count ? argument[2] : compare_less while First.not_equals(Last) { selection = min_element(First, Last, Compare) selection.swap(First) @@ -485,10 +485,10 @@ function stable_sort(First, Last, Comparator) { } ///@function insertion_sort(begin, end, [comparator=compare_less]) -function insertion_sort(First, Last, Comparator) { +function insertion_sort(First, Last) { First = make_iterator(First).go() - var Compare = select_argument(Comparator, compare_less) + var Compare = 2 < argument_count ? argument[2] : compare_less while First.not_equals(Last) { var Value = First.get() for(var It = First.previous(); 0 <= It.get_index() and Compare(Value, It.get()); It.back()) { @@ -497,18 +497,20 @@ function insertion_sort(First, Last, Comparator) { It.next().set(Value) First.go() + delete It } + gc_collect() } ///@function merge_sort(begin, end, [comparator=compare_less]) -function merge_sort(First, Last, Comparator) { +function merge_sort(First, Last) { First = make_iterator(First) Last = make_iterator(Last) var Dist = First.distance(Last) if Dist <= 1 exit - var Compare = select_argument(Comparator, compare_less) + var Compare = 2 < argument_count ? argument[2] : compare_less var Middle = iterator_advance(First, Dist * 0.5) merge_sort(First, Middle, Compare) merge_sort(Middle, Last, Compare) @@ -518,9 +520,8 @@ function merge_sort(First, Last, Comparator) { ///@function IMPLEMENTED FROM VS ///@description sort median of three elements to middle function sort_median(First, Middle, Last, Comparator) { - if Comparator(get(Middle), First.get()) { + if Comparator(Middle.get(), First.get()) Middle.swap(First) - } if Comparator(Last.get(), Middle.get()) { // swap middle and last, then test first again Last.swap(Middle) @@ -533,9 +534,12 @@ function sort_median(First, Middle, Last, Comparator) { ///@function IMPLEMENTED FROM VS ///@description sort median element to middle function predict_median(First, Middle, Last, Comparator) { - var _Count = iterator_distance(First, Last) - if 40 < _Count { // Tukey's ninther - var Step = (_Count + 1) >> 3 // +1 can't overflow because range was made inclusive in caller + First = make_iterator(First) + Middle = make_iterator(Middle) + + var Distance = iterator_distance(First, Last) + if 40 < Distance { // Tukey's ninther + var Step = (Distance + 1) >> 3 // +1 can't overflow because range was made inclusive in caller var _Two_step = Step << 1 // note: intentionally discards low-order bit sort_median(First, First + Step, First + _Two_step, Comparator) sort_median(Middle - Step, Middle, Middle + Step, Comparator) @@ -549,78 +553,91 @@ function predict_median(First, Middle, Last, Comparator) { ///@function IMPLEMENTED FROM VS ///@description partition [First, Last), using Comparator function partition_by_median_guess(First, Last, Comparator) { - var Middle = First + (iterator_distance(First, Last) >> 1) // shift for codegen (== * 0.5) - predict_median(First, Middle, Last - 1, Comparator) + First = make_iterator(First) + Last = make_iterator(Last) + + var Middle = iterator_advance(First, (iterator_distance(First, Last) >> 1)) // shift for codegen (== * 0.5) + predict_median(First, Middle, Last.previous(), Comparator) - var _Pfirst = Middle - var _Plast = _Pfirst + 1 - while First < _Pfirst && !Comparator(get(_Pfirst - 1), get(_Pfirst)) && !Comparator(get(_Pfirst), get(_Pfirst - 1)) { - --_Pfirst + var _Pfirst = Middle.duplicate() + var _Pbefore = _Pfirst.previous() + var _Plast = _Pfirst.next() + while First.get_index() < _Pfirst.get_index() && !Comparator(_Pbefore.get(), _Pfirst.get()) && !Comparator(_Pfirst.get(), _Pbefore.get()) { + _Pfirst.back() + _Pbefore.back() } + delete _Pbefore - while _Plast < Last && !Comparator(get(_Plast), get(_Pfirst)) && !Comparator(get(_Pfirst), get(_Plast)) { - ++_Plast + while _Plast.get_index() < Last.get_index() && !Comparator(_Plast.get(), _Pfirst.get()) && !Comparator(_Pfirst.get(), _Plast.get()) { + _Plast.go() } - var _Gfirst = _Plast - var _Glast = _Pfirst + var _Gfirst = _Plast.duplicate() + var _Glast = _Pfirst.duplicate() for (;;) { // partition - for (; _Gfirst < Last; ++_Gfirst) { - if Comparator(get(_Pfirst), get(_Gfirst)) { - } else if Comparator(get(_Gfirst), get(_Pfirst)) { + for (; _Gfirst < Last; _Gfirst.go()) { + if Comparator(_Pfirst.get(), _Gfirst.get()) { + } else if Comparator(_Gfirst.get(), _Pfirst.get()) { break - } else if _Plast != _Gfirst { + } else if _Plast.not_equals(_Gfirst) { swap(_Plast, _Gfirst) - _Plast++ + _Plast.go() } else { - _Plast++ + _Plast.go() } } - for (; First < _Glast; --_Glast) { - if Comparator(get(_Glast - 1), get(_Pfirst)) { - } else if Comparator(get(_Pfirst), get(_Glast - 1)) { - break - } else if --_Pfirst != _Glast - 1 { - swap(_Pfirst, _Glast - 1) + for (; First.get_index() < _Glast.get_index(); _Glast.back()) { + var _Gfore = _Glast.previous() + if Comparator(_Gfore.get(), _Pfirst.get()) { + } else if Comparator(_Pfirst.get(), _Gfore.get()) { + break + } else if _Pfirst.back().not_equals(_Gfore) { + swap(_Pfirst, _Gfore) } + delete _Gfore } - if _Glast == First and _Gfirst == Last { + if _Glast.equals(First) and _Gfirst.equals(Last) { return [_Pfirst, _Plast] } - if _Glast == First { // no room at bottom, rotate pivot upward - if _Plast != _Gfirst { - swap(_Pfirst, _Plast) + if _Glast.equals(First) { // no room at bottom, rotate pivot upward + if _Plast.not_equals(_Gfirst) { + swap(_Pfirst, _Plast) } - _Plast++ + _Plast.go() _Pfirst.swap(_Gfirst) - _PFirst.go() - _GFirst.go() - } else if _Gfirst == Last { // no room at top, rotate pivot downward - if --_Glast != --_Pfirst { - swap(_Glast, _Pfirst) + _Pfirst.go() + _Gfirst.go() + } else if _Gfirst.equals(Last) { // no room at top, rotate pivot downward + if _Glast.back().not_equals(_Pfirst.back()) { + swap(_Glast, _Pfirst) } - swap(_Pfirst, --_Plast) + swap(_Pfirst, _Plast.back()) } else { - swap(_Gfirst, --_Glast) - _GFirst.go() + swap(_Gfirst, _Glast.back()) + _Gfirst.go() } } + delete _Pfirst + delete _Plast + delete _Gfirst + delete _Glast + gc_collect() } ///@function nth_element(begin, nth, end, [comparator=compare_less]) -function nth_element(First, Nth, Last, Comparator) { +function nth_element(First, Nth, Last) { First = make_iterator(First) Nth = make_iterator(Nth) Last = make_iterator(Last) if First.equals(Last) or Nth.equals(Last) exit - var Compare = select_argument(Comparator, compare_less) + var Compare = 3 < argument_count ? argument[3] : compare_less while 32 < First.distance(Last) { // divide and conquer, ordering partition containing Nth var Middle = partition_by_median_guess(First, Last, Compare) @@ -637,28 +654,33 @@ function nth_element(First, Nth, Last, Comparator) { } ///@function is_sorted(begin, end, [comparator=compare_less]) -function is_sorted(First, Last, Comparator) { +function is_sorted(First, Last) { First = make_iterator(First) if First.equals(Last) return true - var Compare = select_argument(Comparator, compare_less) + var Compare = 2 < argument_count ? argument[2] : compare_less var Next = First.next() while Next.not_equals(Last) { - if Compare(Next.get(), First.get()) + if Compare(Next.get(), First.get()) { + delete Next + gc_collect() return false + } First.go() Next.go() } + delete Next + gc_collect() return true } ///@function unguarded_partition(begin, end, pivot, [comparator=compare_less]) -function unguarded_partition(First, Last, Pivot, Comparator) { +function unguarded_partition(First, Last, Pivot) { First = make_iterator(First) Last = make_iterator(Last) - var Compare = select_argument(Comparator, compare_less) + var Compare = 3 < argument_count ? argument[3] : compare_less while true { while Compare(First.get(), Pivot.get()) First.go() @@ -722,13 +744,13 @@ function is_partitioned(First, Last, Pred) { } ///@function merge(begin, end, other_begin, other_end, output, [comparator=compare_less]) -function merge(First, Last, OtherFirst, OtherLast, Output, Comparator) { +function merge(First, Last, OtherFirst, OtherLast, Output) { First = make_iterator(First) Last = make_iterator(Last) OtherFirst = make_iterator(OtherFirst) OtherLast = make_iterator(OtherLast) - var Compare = select_argument(Comparator, compare_less) + var Compare = 5 < argument_count ? argument[5] : compare_less while true { if First.equals(Last) return copy(OtherFirst, OtherLast, Output) @@ -752,12 +774,12 @@ function merge(First, Last, OtherFirst, OtherLast, Output, Comparator) { } ///@function inplace_merge(begin, middle, end, [comparator=compare_less]) -function inplace_merge(First, Middle, Last, Comparator) { +function inplace_merge(First, Middle, Last) { First = make_iterator(First) Middle = make_iterator(Middle) Last = make_iterator(Last) - var Compare = select_argument(Comparator, compare_less) + var Compare = 3 < argument_count ? argument[3] : compare_less var Temp = duplicate() Temp.merge(self, First, Middle, self, Middle, Last, First, Compare) copy(Temp.first(), Temp.last(), First) @@ -765,10 +787,10 @@ function inplace_merge(First, Middle, Last, Comparator) { } ///@function shuffle(begin, end, [engine=irandom_range]) -function shuffle(First, Last, Engine) { +function shuffle(First, Last) { First = make_iterator(First) - var Urng = select_argument(Engine, irandom_range) + var Urng = 2 < argument_count ? argument[2] : irandom_range var Dist = iterator_distance(First, Last) for (var i = Dist - 1; 0 < i; --i) { swap(iterator_advance(First, i), iterator_advance(First, Urng(0, i))) @@ -776,10 +798,10 @@ function shuffle(First, Last, Engine) { } ///@function random_shuffle(begin, end, [generator=irandom]) -function random_shuffle(First, Last, Generator) { +function random_shuffle(First, Last) { First = make_iterator(First) - var Gen = select_argument(Generator, irandom) + var Gen = 2 < argument_count ? argument[2] : irandom var Dist = iterator_distance(First, Last) for (var i = Dist - 1; 0 < i; --i) { swap(iterator_advance(First, i), iterator_advance(First, Gen(i + 1))) diff --git a/src/scripts/Array/Array.gml b/src/scripts/Array/Array.gml index 139e90b..c408bd0 100644 --- a/src/scripts/Array/Array.gml +++ b/src/scripts/Array/Array.gml @@ -42,8 +42,8 @@ function Array(): Container() constructor { ///@function front() function front() { return at(0) } - ///@function erase_at(index) - function erase_at(Index) { + ///@function erase_index(index) + function erase_index(Index) { var Value = at(Index) set(Index, undefined) return Value diff --git a/src/scripts/Container/Container.gml b/src/scripts/Container/Container.gml index 4819cfa..820bb62 100644 --- a/src/scripts/Container/Container.gml +++ b/src/scripts/Container/Container.gml @@ -2,67 +2,11 @@ function Container() { type = Container iterator_type = undefined const_iterator_type = undefined - value_type = undefined raw = undefined - ///@function make_element_from_tuple(tuple...) - function make_element_from_tuple() { - return new value_type(argument[0], argument[1] - , argument[2], argument[3], argument[4] - , argument[5], argument[6], argument[7] - , argument[9], argument[9], argument[10] - , argument[11], argument[12], argument[13] - , argument[14], argument[15]) // A limit of GameMaker - } - - ///@function make_element_from_array(array) - function make_element_from_array(data) { - var Data = array_create(16, undefined) - var Size = array_length(Data) - for (var i = 0; i < Size; ++i) - Data[i] = data[i] - return make_element_from_tuple(Data[0], Data[1] - , Data[2], Data[3], Data[4] - , Data[5], Data[6], Data[7] - , Data[9], Data[9], Data[10] - , Data[11], Data[12], Data[13] - , Data[14], Data[15]) - } - - ///@function construct(data...) - function construct() { - if is_undefined(value_type) { // literals - if argument_count == 0 - return 0 - else - return argument[0] - } else if argument_count == 1 { - if is_array(argument[0]) - return make_element_from_array(argument[0]) - else - return new value_type(argument[0]) - } else { - return make_element_from_tuple(argument[0], argument[1] - , argument[2], argument[3], argument[4] - , argument[5], argument[6], argument[7] - , argument[9], argument[9], argument[10] - , argument[11], argument[12], argument[13] - , argument[14], argument[15]) - } - } - function data() { return raw } function duplicate() { return new type(self) } - - ///@function set_value_type(type) - function set_value_type(Type) { - value_type = Type - return self - } - - ///@function get_value_type() - function get_value_type() { return value_type } } ///@function is_iterable(container) diff --git a/src/scripts/Functional/Functional.gml b/src/scripts/Functional/Functional.gml index 9942214..24952ef 100644 --- a/src/scripts/Functional/Functional.gml +++ b/src/scripts/Functional/Functional.gml @@ -52,8 +52,8 @@ function compare_string_less(a, b) { } ///@function select_argument(parameter, default) -function select_argument(arg, def) { - return integral(is_undefined(arg), def, arg) +function select_argument(Param, Default) { + return integral(is_undefined(Param), Default, Param) } ///@function choice(index, ...) diff --git a/src/scripts/Grid/Grid.gml b/src/scripts/Grid/Grid.gml index 5a5458c..f1acc0a 100644 --- a/src/scripts/Grid/Grid.gml +++ b/src/scripts/Grid/Grid.gml @@ -4,7 +4,6 @@ Initialize: new Grid(width, height) - set_value_type(type) Usage: Recommend using internal functions to control the Grid. @@ -76,19 +75,6 @@ function Grid(Width, Height): Container() constructor { return self } - ///@function insert(iterator, value) - function insert(It, Value) { - set(It.x, It.y, Value) - return self - } - - ///@function insert_on(index, value) - function insert_on(Index, Value) { - inner_iterator.set_index(Index) - set(inner_iterator.x, inner_iterator.y, Value) - return self - } - ///@function set_region(x1, y1, x2, y2, value) function set_region(X1, Y1, X2, Y2, Value) { ds_grid_set_region(raw, X1, Y1, X2, Y2, Value) diff --git a/src/scripts/Iterator/Iterator.gml b/src/scripts/Iterator/Iterator.gml index ab34a5f..41121ed 100644 --- a/src/scripts/Iterator/Iterator.gml +++ b/src/scripts/Iterator/Iterator.gml @@ -109,7 +109,8 @@ function ConstIterator(Cont, Index): Iterator(Cont, Index) constructor { function ForwardIterator(Cont, Index): Iterator(Cont, Index) constructor { type = ForwardIterator - function set(value) { container.set(pointer, value) } + ///@function set(value) + function set() { container.set(pointer, argument[0]) } ///@function swap(iterator) function swap(Other) { @@ -220,7 +221,7 @@ function ConstMapIterator(Cont, Index): ConstIterator(Cont, Index) constructor { ///@function MapIterator(container, index) function MapIterator(Cont, Index): ConstMapIterator(Cont, Index) constructor { - function set(value) { container.insert(make_pair(key, value)) } + function set(value) { container.insert(key, value) } } ///@function iterator_distance(iterator_1, iterator_2) diff --git a/src/scripts/List/List.gml b/src/scripts/List/List.gml index 56a5bab..82df5c9 100644 --- a/src/scripts/List/List.gml +++ b/src/scripts/List/List.gml @@ -30,20 +30,14 @@ function List(): Container() constructor { ///@function set(index, value) function set(Index, Value) { ds_list_set(raw, Index, Value) return self } - ///@function insert(index, value) - function insert(Index, Value) { ds_list_insert(raw, Index, Value) } + ///@function index_insert(index, value) + function index_insert(Index, Value) { ds_list_insert(raw, Index, Value) } ///@function push_back(value) function push_back(Value) { ds_list_add(raw, Value) } ///@function push_front(value) - function push_front(Value) { insert(0, Value) } - - ///@function emplace_back(tuple) - function emplace_back(Params) { push_back(construct(Params)) } - - ///@function emplace_front(tuple) - function emplace_front(Params) { push_front(construct(Params)) } + function push_front(Value) { index_insert(0, Value) } ///@function at(index) function at(Index) { return ds_list_find_value(raw, Index) } @@ -54,8 +48,8 @@ function List(): Container() constructor { ///@function front() function front() { return at(0) } - ///@function erase_at(index) - function erase_at(Index) { + ///@function erase_index(index) + function erase_index(Index) { var Value = at(Index) ds_list_delete(raw, Index) return Value @@ -70,10 +64,10 @@ function List(): Container() constructor { } ///@function pop_back() - function pop_back() { return erase_at(size() - 1) } + function pop_back() { return erase_index(size() - 1) } ///@function pop_front() - function pop_front() { return erase_at(0) } + function pop_front() { return erase_index(0) } ///@function mark_list(index) function mark_list(Index) { ds_list_mark_as_list(raw, Index) } @@ -115,6 +109,7 @@ function List(): Container() constructor { type = List iterator_type = RandomIterator const_iterator_type = ConstIterator + clear() // To avoid the 0-populate-value problem. // ** Assigning ** if 0 < argument_count { @@ -131,7 +126,7 @@ function List(): Container() constructor { assign(Item.first(), Item.last()) } else { // (*) Arg - push_back(0, Item) + push_back(Item) } } else { // (*) Iterator-Begin, Iterator-End diff --git a/src/scripts/Map/Map.gml b/src/scripts/Map/Map.gml index 715f669..817457f 100644 --- a/src/scripts/Map/Map.gml +++ b/src/scripts/Map/Map.gml @@ -2,6 +2,8 @@ Constructors: Map() Map(Arg) + Map(Maps) + Map(Unordered_Maps) Map(Paired-Container) Map(Builtin-Paired-Array) Map(Builtin-Paired-List) @@ -10,13 +12,13 @@ Initialize: new Map() - set_value_type(type) Usage: - To Iterate values: - foreach(Container.first(), Container.last(), function(Value) { - myfunc(Value[1]) - }) + To Iterate with pairs: + for (var It = Container.first(); It.not_equals(Container.last()); It.go()) { + var Pair = It.get() + myfunc(Pair[1]) + } */ #macro Dictionary Map @@ -33,13 +35,6 @@ function Map(): Container() constructor { ///@function clast() function clast() { return (new const_iterator_type(self, size())).pure() } - ///@function insert(pair) - function insert(Pair) { - cash_push(Pair[0]) - ds_map_set(raw, Pair[0], Pair[1]) - return self - } - ///@function set(index, value) function set(Index, Value) { var Key = cash.at(Index) @@ -48,15 +43,31 @@ function Map(): Container() constructor { return self } + ///@function insert(item) + function insert() { + var Key, Value + if argument_count == 2 { + Key = argument[0] + Value = argument[1] + } else { + var Pair = argument[0] + Key = Pair[0] + Value = Pair[1] + } + if !contains(Key) cash_push(Key) + ds_map_set(raw, Key, Value) + return self + } + ///@function set_list(key, builtin_list_id) function set_list(K, Value) { - cash_push(K) + if !contains(K) cash_push(K) ds_map_add_list(raw, K, Value) } ///@function set_map(key, builtin_map_id) function set_map(K, Value) { - cash_push(K) + if !contains(K) cash_push(K) ds_map_add_map(raw, K, Value) } @@ -75,16 +86,16 @@ function Map(): Container() constructor { ///@function front() function front() { return at(0) } - ///@function erase_at(key) - function erase_at(K) { - var Temp = at(K) + ///@function erase_index(key) + function erase_index(K) { + var Temp = seek(K) ds_map_delete(raw, K) remove(cash.first(), cash.last(), K) return Temp } ///@function erase_one(iterator) - function erase_one(It) { return erase_at(It.get_index()) } + function erase_one(It) { return erase_index(It.get_index()) } ///@function key_change(key, value) function key_change(K, Value) { @@ -99,10 +110,10 @@ function Map(): Container() constructor { ds_map_set(raw, Key2, Temp) } - ///@function is_list(K) + ///@function is_list(key) function is_list(K) { return ds_map_is_list(raw, K) } - ///@function is_map(K) + ///@function is_map(key) function is_map(K) { return ds_map_is_map(raw, K) } ///@function contains(key) @@ -119,23 +130,22 @@ function Map(): Container() constructor { ///@function cash_push(key) function cash_push(K) { - if !contains(K) { - if 1 < cash.size() { - cash.push_back(K) - cash.sort_builtin(true) - } else { - cash.push_back(K) - } + if 1 < cash.size() { + cash.push_back(K) + cash.sort_builtin(true) + } else { + cash.push_back(K) } } ///@function read(data_string) function read(Str) { var loaded = ds_map_create() + ds_map_read(loaded, Str) if 0 < ds_map_size(loaded) { var MIt = ds_map_find_first(loaded) while true { - insert(make_pair(MIt, ds_map_find_value(loaded, MIt))) + insert(MIt, ds_map_find_value(loaded, MIt)) MIt = ds_map_find_next(loaded, MIt) if is_undefined(MIt) break @@ -154,8 +164,7 @@ function Map(): Container() constructor { iterator_type = ForwardIterator const_iterator_type = ConstIterator cash = new List() - cash.clear() // To avoid the 0-populate-value problem. - + if 0 < argument_count { if argument_count == 1 { var Item = argument[0] @@ -171,15 +180,22 @@ function Map(): Container() constructor { if 0 < Size { var MIt = ds_map_find_first(Item) while true { - insert(make_pair(MIt, ds_map_find_value(Item, MIt))) + insert(MIt, ds_map_find_value(Item, MIt)) MIt = ds_map_find_next(Item, MIt) if is_undefined(MIt) break } } - //ds_map_copy(raw, Item) } else if is_struct(Item) { - if is_iterable(Item) { + var Type = instanceof(Item) + if Type == "Map" or Type == "Multimap" { + // (*) Maps + ds_map_copy(raw, Item.data()) + copy(cash.first(), cash.last(), Item.cash.first()) + } else if Type == "Unordered_Map" or Type == "Unordered_Multimap" { + // (*) Unordered_Maps + + } else if is_iterable(Item) { // (*) Paired-Container foreach(Item.first(), Item.last(), function(Value) { insert(Value) diff --git a/src/scripts/Multimap/Multimap.gml b/src/scripts/Multimap/Multimap.gml index a006969..3880d06 100644 --- a/src/scripts/Multimap/Multimap.gml +++ b/src/scripts/Multimap/Multimap.gml @@ -2,165 +2,163 @@ Constructors: Multimap() Multimap(Arg) - Multimap(Multimap) - Multimap(Iterable-PairedContainer) - Multimap(Map) - Multimap(Builtin-PairedArray) - Multimap(Builtin-PairedList) + Multimap(Maps) + Multimap(Unordered_Maps) + Multimap(Paired-Container) + Multimap(Builtin-Paired-Array) + Multimap(Builtin-Paired-List) Multimap(Builtin-Map) Multimap(Arg0, Arg1, ...) Initialize: - new Multimap - set_value_type(type) + new Multimap() Usage: - To Iterate on keys: - for (var It = first(); It != last(); ++It) - myfunc(get_key(It)) - - To Iterate on lists: - for (var It = first(); It != last(); ++It) - myfunc(get(It)) + To Iterate values on lists: + for (var It = Container.first(); It.not_equal(Container.last()); It.go()) { + var KList = It.get() + foreach(KList.first(), KList.first(), function(Value) { + myfunc(Value) + }) + } - To Iterate values with a key: - var BucketIndex = bucket(Key) - for (var It = first(BucketIndex); It != last(BucketIndex); ++It) - myfunc(get(BucketIndex, It)) + To Iterate values in a key: + var Bucket = Container.bucket("KEY") + for (var It = Container.first(Bucket); It.not_equal(Container.last(Bucket)); It.go()) + myfunc(It.get()) + + To Iterate values with bucket indexes: + var Pair, Key, Values + for (var i = 0; i < Container.bucket_count(); ++i) { + Pair = Container.at(i) // a Pair + Key = TempPair[0] + Values = TempPair[1] // a List + for (var It = Values.first(); It.not_equal(Values.last()); It.go()) + myfunc(It.get()) + } To Iterate values within methods: - var BucketIndex = bucket(Key) - myfunc(BucketIndex, first(BucketIndex), last(BucketIndex)) + var Bucket = Container.bucket("KEY") + myfunc(Container.first(Bucket), Container.last(Bucket)) */ function Multimap(): Container() constructor { - type = Multimap - raw = ds_map_create() - cash = new List() - cash.clear() // To avoid the 0-populate-value problem. + ///@function bucket(key) + function bucket(K) { return ds_list_find_index(cash.raw, K) } + + ///@function bucket_find(bucket_index) + function bucket_find(Index) { return seek(cash.at(Index)) } + + ///@function bucket_count() + function bucket_count() { return cash.size() } - ///@function first([bucket_iterator]) + ///@function bucket_create(key, [value]) + function bucket_create(K) { + var NewList = new List() + if 0 < argument_count + NewList.push_back(argument[1]) + ds_map_set(raw, K, NewList) + } + + ///@function first([bucket_index]) function first() { - if argument_count == 0 - return 0 + if 1 == argument_count + return bucket_find(argument[0]).first() else - return 0 // Logic + return (new iterator_type(self, 0)).pure() } - ///@function last([bucket_iterator]) + ///@function last([bucket_index]) function last() { - if argument_count == 0 { - return size() - } else if is_real(argument[0]) and 0 <= argument[0] and argument[0] < bucket_count() { - return bucket_size(argument[0]) - } - throw "An error occured when getting the end of a multimap!" + if 1 == argument_count + return bucket_find(argument[0]).last() + else + return (new iterator_type(self, size())).pure() } - ///@function set(values_pair) - function set(PairedVal) { - if is_array(PairedVal) - __set(PairedVal[0], PairedVal[1]) - else if is_struct(PairedVal) - __set(PairedVal.first, PairedVal.second) - else if argument_count == 2 - __set(argument[0], argument[1]) - return self + ///@function cfirst([bucket_index]) + function cfirst() { + if 1 == argument_count + return bucket_find(argument[0]).cfirst() + else + return (new const_iterator_type(self, 0)).pure() } - ///@function insert(values_pair) - function insert(PairedVal) { - if is_array(PairedVal) - __add(PairedVal[0], PairedVal[1]) - else if is_struct(PairedVal) - __add(PairedVal.first, PairedVal.second) - else if argument_count == 2 - __add(argument[0], argument[1]) - return self + ///@function clast([bucket_index]) + function clast() { + if 1 == argument_count + return bucket_find(argument[0]).clast() + else + return (new const_iterator_type(self, size())).pure() } - ///@function iter_set(bucket_iterator, values_pair) - function iter_set(BucketIndex, PairedVal) { - var It = -1, Value = 0 - if is_array(PairedVal) { - It = PairedVal[0] - Value = PairedVal[1] - } else if is_struct(PairedVal) { - It = PairedVal.first - Value = PairedVal.second - } else if argument_count == 3 { - It = argument[1] - Value = argument[2] - } - - bucket_set(BucketIndex, It, Value) + ///@function set(bucket_index, list) + function set(Index, Value) { + if !is_struct(Value) + throw "Cannot bind a non-struct item to Multimap!" + var Key = cash.at(Index) + if !is_undefined(Key) + ds_map_set(raw, Key, Value) return self } - ///@function get(bucket_iterator, [iterator]) - function get(BucketIndex) { - if 0 <= BucketIndex and BucketIndex < bucket_count() { - var Result = at(get_key(BucketIndex)) // list - if argument_count == 2 - Result = Result.get(argument[1]) // value - return Result + ///@function insert(item) + function insert() { + var Key, Value + if argument_count == 2 { + Key = argument[0] + Value = argument[1] + } else { + var Pair = argument[0] + Key = Pair[0] + Value = Pair[1] } - return undefined - } - - ///@function get_key(bucket_iterator) - function get_key(BucketIndex) { - if 0 <= BucketIndex and BucketIndex < bucket_count() // key - return cash.get(BucketIndex) - return undefined + if !contains(Key) { + cash_push(Key) + bucket_create(Key, Value) + } else { + var KList = seek(Key) + KList.push_back(Value) + stable_sort(KList.first(), KList.last(), value_comparator) + } + return self } - ///@function at(key) - function at(K) { return ds_map_find_value(raw, K) } + ///@function seek(key) + function seek(K) { return ds_map_find_value(raw, K) } - ///@function bucket_set(bucket_iterator, iterator, value) - function bucket_set(BucketIndex, It, Value) { - BucketIndex.get().set(It, Value) + ///@function at(bucket_index) + function at(Index) { + var K = cash.at(Index) + return make_pair(K, seek(K)) } - ///@function bucket(key) - function bucket(K) { - //Cannot return the iterator at end - return ds_list_find_index(cash.data(), K) // optimize - } - - ///@function bucket_count() - function bucket_count() { - return cash.size() - } + ///@function back() + function back() { return at(size() - 1) } - ///@function bucket_size(bucket_iterator) - function bucket_size(BucketIndex) { - return get(BucketIndex).size() - } + ///@function front() + function front() { return at(0) } - ///@function equal_range(key) - function equal_range(K) { - if !exists(K) { - return [0, 0] - } else { - var BucketIndex = bucket(K) - return [BucketIndex.first(), BucketIndex.last()] - } + ///@function erase_index(key) + function erase_index(K) { + var Temp = seek(K) + ds_map_delete(raw, K) + remove(cash.first(), cash.last(), K) + return Temp } - ///@function back(bucket_iterator) - function back(BucketIndex) { - return get(BucketIndex).back() - } + ///@function erase_one(iterator) + function erase_one(It) { return erase_index(It.get_index()) } - ///@function front(bucket_iterator) - function front(BucketIndex) { - return get(BucketIndex).front() + ///@function key_swap(key_1, key_2) + function key_swap(Key1, Key2) { + var Temp = seek(Key1) + ds_map_set(raw, Key1, seek(Key2)) + ds_map_set(raw, Key2, Temp) } - ///@function exists(key) - function exists(K) { return ds_map_exists(raw, K) } + ///@function contains(key) + function contains(K) { return ds_map_exists(raw, K) } ///@function size() function size() { return ds_map_size(raw) } @@ -171,357 +169,47 @@ function Multimap(): Container() constructor { ///@function clear() function clear() { ds_map_clear(raw) } - ///@function emplace(key, tuple) - function emplace(K, Params) { set(K, construct(Params)) } - - ///@function check_all(bucket_iterator, begin, end, predicate) - function check_all(BucketIndex, First, Last, Pred) { - Pred = method(other, Pred) - var MyList = get(BucketIndex) - return MyList.check_all(First, Last, pred) - } - - ///@function check_any(bucket_iterator, begin, end, predicate) - function check_any(BucketIndex, First, Last, Pred) { - Pred = method(other, Pred) - var MyList = get(BucketIndex) - return MyList.check_any(First, Last, pred) - } - - ///@function check_none(bucket_iterator, begin, end, predicate) - function check_none(BucketIndex, First, Last, Pred) { - Pred = method(other, Pred) - var MyList = get(BucketIndex) - return MyList.check_none(First, Last, pred) - } - - ///@function foreach(bucket_iterator, begin, end, predicate) - function foreach(BucketIndex, First, Last, Pred) { - Pred = method(other, Pred) - var MyList = get(BucketIndex) - return MyList.foreach(First, Last, pred) - } - - ///@function find(bucket_iterator, begin, end, value, [comparator]) - function find(BucketIndex, First, Last, Value, Comparator) { - var MyList = get(BucketIndex) - return MyList.find(First, Last, Value, Comparator) - } - - ///@function find_if(bucket_iterator, begin, end, predicate) - function find_if(BucketIndex, First, Last, Pred) { - Pred = method(other, Pred) - var MyList = get(BucketIndex) - return MyList.find_if(First, Last, pred) - } - - ///@function count(bucket_iterator, begin, end, value) - function count(BucketIndex, First, Last, Value) { - var MyList = get(BucketIndex) - return MyList.count(First, Last, Value) - } - - ///@function count_if(bucket_iterator, begin, end, predicate) - function count_if(BucketIndex, First, Last, Pred) { - Pred = method(other, Pred) - var MyList = get(BucketIndex) - return MyList.count_if(First, Last, pred) - } - - ///@function erase(bucket_iterator, begin, [end]) - function erase(BucketIndex) { - if argument_count == 1 - erase_key(get_key(BucketIndex)) - if argument_count == 2 - __erase_one(BucketIndex, argument[1]) - else if argument_count == 3 - __erase_range(BucketIndex, argument[1], argument[2]) - } - - ///@function erase_key(key) - function erase_key(K) { - var MyList = at(K) - MyList.destroy() - delete MyList - ds_map_delete(raw, K) - __try_decash(K) - } - - ///@function __erase_one(bucket_iterator, iterator) - function __erase_one(BucketIndex, It) { - var MyList = get(BucketIndex) - MyList.erase(It) - } - - ///@function __erase_range(bucket_iterator, begin, end) - function __erase_range(BucketIndex, First, Last) { - var MyList = get(BucketIndex) - for (; First.not_equals(Last); ++First) { - MyList.erase(First) - } - } - - ///@function swap(key_1, key_2) - function swap(KeyA, KeyB) { - var Temp = at(KeyA) - set(KeyA, at(KeyB)) - set(KeyB, Temp) - } - - ///@function key_swap(key, destination, destination_key) - function key_swap(K, Dst, DstK) { - var Temp = at(K) - set(K, Dst.at(DstK)) - Dst.set(DstK, Temp) - } - - ///@function bucket_swap(bucket_iterator, iterator_1, iterator_2) - function bucket_swap(BucketIndex, ItA, ItB) { - var Temp = get(BucketIndex, ItA) - bucket_set(BucketIndex, ItA, get(BucketIndex, ItB)) - bucket_set(BucketIndex, ItB, Temp) - } - - ///@function iter_swap(bucket_iterator_1, iterator_1, bucket_iterator_2, iterator_2) - function iter_swap(BucketIndex, It, OutIndex, Output) { - var Temp = get(BucketIndex, It) - iter_set(BucketIndex, It, get(OutIndex, Output)) - iter_set(OutIndex, OutIndex, Temp) - } - - ///@function swap_range(bucket_iterator_1, begin, end, bucket_iterator_2, bucket_iterator_2_output) - function swap_range(BucketIndex, First, Last, OutIndex, Output) { - while First.not_equals(Last) { - iter_swap(BucketIndex, First, OutIndex, Output) - - First++ - Output.go() - } - return Output - } - - ///@function bucket_iter_swap(bucket_iterator, iterator, destination, destination_bucket_iterator, destination_bucket_iterator_output) - function bucket_iter_swap(BucketIndex, It, Dst, OutIndex, Output) { - var Temp = BucketIndex.get(It) - bucket_set(BucketIndex, It, Dst.get(OutIndex, Output)) - Dst.bucket_set(OutIndex, Output, Temp) - } - - ///@function remove(bucket_iterator, begin, end, value) - function remove(BucketIndex, First, Last, Value) { - var MyList = get(BucketIndex) - return MyList.remove(First, Last, Value) - } - - ///@function remove_if(bucket_iterator, begin, end, predicate) - function remove_if(BucketIndex, First, Last, Pred) { - Pred = method(other, Pred) - var MyList = get(BucketIndex) - return MyList.remove_if(First, Last, pred) - } - - ///@function bucket_move(bucket_iterator, begin, end, output) - function bucket_move(BucketIndex, First, Last, Output) { - var MyList = get(BucketIndex) - return MyList.move(First, Last, Output) - } - - ///@function move(bucket_iterator_1, begin, end, bucket_iterator_2, bucket_iterator_2_output) - function move(BucketIndex, First, Last, OutIndex, Output) { - var MyList = get(BucketIndex) - var OtherList = get(OutIndex) - return MyList.move_to(First, Last, OtherList, Output) - } - - ///@function move_to(bucket_iterator, begin, end, destination, destination_bucket_iterator, destination_bucket_iterator_output) - function move_to(BucketIndex, First, Last, Dst, OutIndex, Output) { - var MyList = get(BucketIndex) - var DstList = Dst.get(OutIndex) - return MyList.move_to(First, Last, DstList, Output) - } - - ///@function fill(bucket_iterator, begin, end, value) - function fill(BucketIndex, First, Last, Value) { - var MyList = get(BucketIndex) - MyList.fill(First, Last, Value) - } - - ///@function rotate(bucket_iterator, begin, middle, end) - function rotate(BucketIndex, First, Middle, Last) { - var MyList = get(BucketIndex) - return MyList.rotate(First, Middle, Last) - } - - ///@function reverse(bucket_iterator, begin, end) - function reverse(BucketIndex, First, Last) { - var MyList = get(BucketIndex) - MyList.reverse(First, Last) - } - - ///@function transform(bucket_iterator, begin, end, output, predicate) - function transform(BucketIndex, First, Last, Output, Pred) { - Pred = method(other, Pred) - var MyList = get(BucketIndex) - return MyList.transform(First, Last, Output, pred) - } - - ///@function min_element(bucket_iterator, begin, end, [comparator]) - function min_element(BucketIndex, First, Last, Comparator) { - var MyList = get(BucketIndex) - return MyList.min_element(First, Last, Comparator) - } - - ///@function max_element(bucket_iterator, begin, end, [comparator]) - function max_element(BucketIndex, First, Last, Comparator) { - var MyList = get(BucketIndex) - return MyList.max_element(First, Last, Comparator) - } - - ///@function lower_bound(bucket_iterator, begin, end, value, [comparator]) - function lower_bound(BucketIndex, First, Last, Value, Comparator) { - var MyList = get(BucketIndex) - return MyList.lower_bound(First, Last, Value, Comparator) - } - - ///@function upper_bound(bucket_iterator, begin, end, value, [comparator]) - function upper_bound(BucketIndex, First, Last, Value, Comparator) { - var MyList = get(BucketIndex) - return MyList.upper_bound(First, Last, Value, Comparator) - } - - ///@function binary_search(bucket_iterator, begin, end, value, [comparator]) - function binary_search(BucketIndex, First, Last, Value, Comparator) { - var MyList = get(BucketIndex) - return MyList.binary_search(First, Last, Value, Comparator) - } - - ///@function sort(bucket_iterator, begin, end, [comparator]) - function sort(BucketIndex, First, Last, Comparator) { - var MyList = get(BucketIndex) - MyList.sort(First, Last, Comparator) - } - - ///@function stable_sort(bucket_iterator, begin, end, [comparator]) - function stable_sort(BucketIndex, First, Last, Comparator) { - var MyList = get(BucketIndex) - MyList.stable_sort(First, Last, Comparator) - } - - ///@function nth_element(bucket_iterator, begin, nth, end, [comparator]) - function nth_element(BucketIndex, First, Nth, Last, Comparator) { - var MyList = get(BucketIndex) - MyList.nth_element(First, Nth, Last, Comparator) - } - - ///@function is_sorted(bucket_iterator, begin, end, [comparator]) - function is_sorted(BucketIndex, First, Last, Comparator) { - var MyList = get(BucketIndex) - return MyList.is_sorted(First, Last, Comparator) - } - - ///@function unguarded_partition(bucket_iterator, begin, end, pivot, [comparator]) - function unguarded_partition(BucketIndex, First, Last, Pivot, Comparator) { - var MyList = get(BucketIndex) - return MyList.unguarded_partition(First, Last, Pivot, Comparator) - } + ///@function set_value_comp(compare_function) + function set_value_comp(Func) { value_comparator = method(other, Fun) return self } - ///@function partition(bucket_iterator, begin, end, predicate) - function partition(BucketIndex, First, Last, Pred) { - var MyList = get(BucketIndex) - return MyList.partition(First, Last, Pred) - } - - ///@function is_partitioned(bucket_iterator, begin, end, predicate) - function is_partitioned(BucketIndex, First, Last, Pred) { - var MyList = get(BucketIndex) - return MyList.is_partitioned(First, Last, Pred) - } - - function destroy() { - var Begin = first() - var End = last() - if 0 < End { - for (var It = Begin; It != End; ++It) { - var TempList = get(It) - TempList.destroy() - delete TempList - } - ds_map_clear(raw) - cash.destroy() - delete cash + ///@function cash_push(key) + function cash_push(K) { + if 1 < cash.size() { + cash.push_back(K) + cash.sort_builtin(true) + } else { + cash.push_back(K) } - ds_map_destroy(raw) - raw = undefined } ///@function read(data_string) - function read(Str) { - var TempMap = ds_map_create() - ds_map_read(TempMap, Str) - var Size = ds_map_size(TempMap) - if 0 < Size { - var MIt = ds_map_find_first(TempMap) - for (var i = 0; i < Size; ++i) { - __cash(MIt) - ds_map_set(raw, MIt, ds_map_find_value(TempMap, MIt)) - MIt = ds_map_find_next(TempMap, MIt) + function read(Str) { + var loaded = ds_map_create() + ds_map_read(loaded, Str) + if 0 < ds_map_size(loaded) { + var MIt = ds_map_find_first(loaded) + while true { + insert(MIt, ds_map_find_value(loaded, MIt)) + MIt = ds_map_find_next(loaded, MIt) + if is_undefined(MIt) + break } } } + ///@function write() function write() { return ds_map_write(raw) } - ///@function __create(key, value) - function __create(K, Value) { - __cash(K) - var NewList = new List() - NewList.push_back(Value) - ds_map_set(raw, K, NewList) - } - - ///@function __attach(key, value) - function __attach(K, Value) { - var TempList = at(K) - TempList.push_back(Value) - } - - ///@function __replace(key, value) - function __replace(K, Value) { - var TempList = at(K) - TempList.clear() - TempList.push_back(Value) - } - - ///@function __set(key, value) - function __set(K, Value) { - if !exists(K) - __create(K, Value) - else - __replace(K, Value) - } - - ///@function __add(key, value) - function __add(K, Value) { - if !exists(K) - __create(K, Value) - else - __attach(K, Value) - } - - ///@function __cash(key) - function __cash(K) { - cash.push_back(K) - } - - ///@function __try_decash(key) - function __try_decash(K) { - if 0 < cash.size() { - if cash.remove(cash.first(), cash.last(), K) != cash.last() - return true - } - return false - } + ///@function destroy() + function destroy() { ds_map_destroy(raw); cash.destroy(); delete cash; gc_collect() } + type = Map + raw = ds_map_create() + iterator_type = ForwardIterator + const_iterator_type = ConstIterator + value_comparator = compare_less + cash = new List() + if 0 < argument_count { if argument_count == 1 { var Item = argument[0] @@ -537,7 +225,7 @@ function Multimap(): Container() constructor { if 0 < Size { var MIt = ds_map_find_first(Item) while true { - set(MIt, ds_map_find_value(Item, MIt)) + insert(MIt, ds_map_find_value(Item, MIt)) MIt = ds_map_find_next(Item, MIt) if is_undefined(MIt) break @@ -546,21 +234,13 @@ function Multimap(): Container() constructor { //ds_map_copy(raw, Item) } else if is_struct(Item) { var Type = instanceof(Item) - if Type == "Multimap" { - // (*) Multimap - if 0 < Item.size() { - for (var KIt = Item.first(); KIt != Item.last(); ++KIt) { - var TempKey = Item.get_key(KIt) - var TempList = Item.get(KIt) - for (var It = TempList.first(); It != TempList.last(); ++It) { - insert(TempKey, TempList.get(It)) - } - } - } - } else if Type == "Map" { - // (*) Map + if Type == "Map" or Type == "Unordered_Map" { + // (*) Maps ds_map_copy(raw, Item.data()) - ds_list_copy(cash, Item.cash) + copy(cash.first(), cash.last(), Item.cash.first()) + } else if Type == "Unordered_Map" or Type == "Unordered_Multimap" { + // (*) Unordered_Maps + } else if is_iterable(Item) { // (*) Paired-Container foreach(Item.first(), Item.last(), function(Value) { diff --git a/src/scripts/Priority_Deque/Priority_Deque.gml b/src/scripts/Priority_Deque/Priority_Deque.gml index dfa2548..5796fa8 100644 --- a/src/scripts/Priority_Deque/Priority_Deque.gml +++ b/src/scripts/Priority_Deque/Priority_Deque.gml @@ -2,23 +2,23 @@ Constructors: Priority_Deque() Priority_Deque(Arg) + Priority_Deque(Maps) Priority_Deque(Priority_Deque) - Priority_Deque(Paired-Container) - Priority_Deque(Map) Priority_Deque(Priority_Queue) - Priority_Deque(Builtin-Paired-Array) - Priority_Deque(Builtin-Paired-List) + Priority_Deque(Container) + Priority_Deque(Builtin-Array) + Priority_Deque(Builtin-List) Priority_Deque(Builtin-Map) Priority_Deque(Builtin-Priority) Priority_Deque(Arg0, Arg1, ...) Initialize: - new Priority_Deque(); + new Priority_Deque() Usage: AI_Target_Filter = new Priority_Deque() AI_Target_Filter.set_procedure(function(Target) { - return bool(Target.hp / Target.hp_max <= 0.4) + return (Target.hp / Target.hp_max) * 20 }) */ @@ -29,11 +29,19 @@ function Priority_Deque(): Container() constructor { ///@function push(value) function push(Value) { ds_priority_add(raw, Value, procedure(Value)) } - ///@function insert(pair) - function insert(Pair) { ds_priority_add(raw, Pair[1], Pair[0]) } - - ///@function emplace(tuple) - function emplace(Params) { insert(construct(Params)) } + ///@function insert(item) + function insert() { + var Pri, Value + if argument_count == 2 { + Pri = argument[0] + Value = argument[1] + } else { + var Pair = argument[0] + Pri = Pair[0] + Value = Pair[1] + } + ds_priority_add(raw, Value, Pri) + } ///@function pop() function pop() { ds_priority_delete_max(raw) } @@ -82,58 +90,61 @@ function Priority_Deque(): Container() constructor { type = Priority_Deque raw = ds_priority_create() - procedure = function(Pair) { return Pair[0] } + procedure = function(Pair) { return real(Pair[0]) } if 0 < argument_count { if argument_count == 1 { var Item = argument[0] - if is_struct(Item) { - if is_iterable(Item) { - // (*) Iterable-PairedContainer - for (var It = Item.first(); It != Item.last(); ++It) { - push(Item.get(It)) - } - } else if instanceof(Item) == "Map" { - // (*) Map - var BucketNumber = Item.bucket_count() - for (var i = 0; i < BucketNumber; ++i) { - var Key = Item.get_key(i) - var Value = Item.at(Key) - push(Key, Value) - } - } else if instanceof(Item) == "Priority_Deque" { - // (*) Priority Deque - ds_priority_copy(raw, Item.data()) - } else if instanceof(Item) == "Priority_Queue" { - // (*) Priority Queue - - } - } else if is_array(Item) { - // (*) Built-in PairedArray - for (var i = 0; i < array_length(Item); ++i) { - push(Item[i]) - } + if is_array(Item) { + // (*) Built-in Array + for (var i = 0; i < array_length(Item); ++i) push(Item[i]) } else if !is_nan(Item) and ds_exists(Item, ds_type_list) { - // (*) Built-in PairedList - for (var i = 0; i < ds_list_size(Item); ++i) { - push(Item[| i]) - } + // (*) Built-in List + for (var i = 0; i < ds_list_size(Item); ++i) push(Item[| i]) } else if !is_nan(Item) and ds_exists(Item, ds_type_map) { // (*) Built-in Map - + var Size = ds_map_size(Item) + if 0 < Size { + var MIt = ds_map_find_first(Item) + while true { + set(MIt, ds_map_find_value(Item, MIt)) + MIt = ds_map_find_next(Item, MIt) + if is_undefined(MIt) + break + } + } } else if !is_nan(Item) and ds_exists(Item, ds_type_priority) { // (*) Built-in Priority ds_priority_copy(raw, Item) + } else if is_struct(Item) { + var Type = instanceof(Item) + if Type == "Map" or Type == "Unordered_Map" { + // (*) Maps + foreach(Item.first(), Item.last(), function(Value) { + insert(Value[1]) + }) + } else if Type == "Priority_Deque" { + // (*) Priority Deque + ds_priority_copy(raw, Item.data()) + } else if Type == "Priority_Queue" { + // (*) Priority Queue + foreach(Item.cfirst(), Item.clast(), function(Value) { + push(Value) + }) + } else if is_iterable(Item) { + // (*) Container + foreach(Item.first(), Item.last(), function(Value) { + push(Value) + }) + } } else { // (*) Arg push(Item) } } else { // (*) Arg0, Arg1, ... - for (var i = 0; i < argument_count; ++i) { - push(argument[i]) - } + for (var i = 0; i < argument_count; ++i) push(argument[i]) } } } diff --git a/src/scripts/Priority_Queue/Priority_Queue.gml b/src/scripts/Priority_Queue/Priority_Queue.gml index 87c21fe..d70cf59 100644 --- a/src/scripts/Priority_Queue/Priority_Queue.gml +++ b/src/scripts/Priority_Queue/Priority_Queue.gml @@ -33,10 +33,10 @@ function Priority_Queue(Comparator, Is_Stable): Container() constructor { } ///@function cfirst() - function cfirst() { return (new const_iterator_type(self, 0)).pure() } + function cfirst() { return raw.cfirst() } ///@function clast() - function clast() { return (new const_iterator_type(self, size())).pure() } + function clast() { return raw.clast() } ///@function push(value) function push(Value) { @@ -53,18 +53,12 @@ function Priority_Queue(Comparator, Is_Stable): Container() constructor { } } - ///@function insert(pair) - function insert(Pair) { push(raw, Pair[1], Pair[0]) } - ///@function pop() function pop() { raw.pop_front() } ///@function pop_front() function pop_front() { return raw.pop_front() } - ///@function emplace(tuple) - function emplace(Params) { push(construct(Params)) } - ///@function top() function top() { return raw.front() } diff --git a/src/scripts/Queue/Queue.gml b/src/scripts/Queue/Queue.gml index 155ea1c..c174b42 100644 --- a/src/scripts/Queue/Queue.gml +++ b/src/scripts/Queue/Queue.gml @@ -11,7 +11,6 @@ Initialize: new Queue - set_value_type(type) Usage: @@ -29,9 +28,6 @@ function Queue(): Container() constructor { ///@function pop_front() function pop_front() { return ds_queue_dequeue(raw) } - ///@function emplace(tuple) - function emplace(Params) { push(construct(Params)) } - ///@function back() function back() { return ds_queue_tail(raw)} diff --git a/src/scripts/Stack/Stack.gml b/src/scripts/Stack/Stack.gml index 63984ae..7d83d33 100644 --- a/src/scripts/Stack/Stack.gml +++ b/src/scripts/Stack/Stack.gml @@ -11,7 +11,6 @@ Initialize: new Stack - set_value_type(type) Usage: @@ -29,9 +28,6 @@ function Stack(): Container() constructor { ///@function pop_back() function pop_back() { return ds_stack_pop(raw) } - ///@function emplace(tuple) - function emplace(Params) { push(construct(Params)) } - ///@function top() function top() { return ds_stack_top(raw) } diff --git a/src/scripts/Unordered_Map/Unordered_Map.gml b/src/scripts/Unordered_Map/Unordered_Map.gml index 91001d8..f20f5e0 100644 --- a/src/scripts/Unordered_Map/Unordered_Map.gml +++ b/src/scripts/Unordered_Map/Unordered_Map.gml @@ -2,6 +2,8 @@ Constructors: Unordered_Map() Unordered_Map(Arg) + Unordered_Map(Maps) + Unordered_Map(Unordered_Maps) Unordered_Map(Paired-Container) Unordered_Map(Builtin-Paired-Array) Unordered_Map(Builtin-Paired-List) @@ -10,12 +12,13 @@ Initialize: new Unordered_Map() - set_value_type(type) Usage: - To Iterate with keys: - for (var It = first(); It != last(); ++It) - myfunc(It.get()) + To Iterate with pairs: + for (var It = Container.first(); It.not_equals(Container.last()); It.go()) { + var Pair = It.get() + myfunc(Pair[1]) + } */ function Unordered_Map(): Container() constructor { @@ -31,11 +34,23 @@ function Unordered_Map(): Container() constructor { ///@function clast() function clast() { return (new const_iterator_type(self, size())).pure() } - ///@function insert(pair) - function insert(Pair) { ds_map_set(raw, Pair[0], Pair[1]) return self } - ///@function set(key, value) - function set(K, Value) { ds_map_set(raw, K, Value) return self } + function set(K, Value) { ds_map_set(raw, K, Value) return self } + + ///@function insert(item) + function insert() { + var Key, Value + if argument_count == 2 { + Key = argument[0] + Value = argument[1] + } else { + var Pair = argument[0] + Key = Pair[0] + Value = Pair[1] + } + ds_map_set(raw, Pair[0], Pair[1]) + return self + } ///@function set_list(key, builtin_list_id) function set_list(K, Value) { ds_map_add_list(raw, K, Value) } @@ -55,18 +70,15 @@ function Unordered_Map(): Container() constructor { ///@function front() function front() { return at(ds_map_find_first(raw)) } - ///@function erase_at(key) - function erase_at(K) { - var Temp = at(K) + ///@function erase_index(key) + function erase_index(K) { + var Temp = seek(K) ds_map_delete(raw, K) return Temp } ///@function erase_one(iterator) - function erase_one(It) { return erase_at(It.get_key()) } - - ///@function key_change(key, value) - function key_change(K, Value) { return ds_map_replace(raw, K, Value) } + function erase_one(It) { return erase_index(It.get_key()) } ///@function key_swap(key_1, key_2) function key_swap(Key1, Key2) { @@ -75,10 +87,10 @@ function Unordered_Map(): Container() constructor { ds_map_set(raw, Key2, Temp) } - ///@function is_list(K) + ///@function is_list(key) function is_list(K) { return ds_map_is_list(raw, K) } - ///@function is_map(K) + ///@function is_map(key) function is_map(K) { return ds_map_is_map(raw, K) } ///@function contains(key) @@ -94,7 +106,19 @@ function Unordered_Map(): Container() constructor { function clear() { ds_map_clear(raw) } ///@function read(data_string) - function read(Str) { ds_map_read(raw, Str) } + function read(Str) { + var loaded = ds_map_create() + ds_map_read(loaded, Str) + if 0 < ds_map_size(loaded) { + var MIt = ds_map_find_first(loaded) + while true { + insert(MIt, ds_map_find_value(loaded, MIt)) + MIt = ds_map_find_next(loaded, MIt) + if is_undefined(MIt) + break + } + } + } ///@function write() function write() { return ds_map_write(raw) } @@ -122,15 +146,23 @@ function Unordered_Map(): Container() constructor { if 0 < Size { var MIt = ds_map_find_first(Item) while true { - set(MIt, ds_map_find_value(Item, MIt)) + insert(MIt, ds_map_find_value(Item, MIt)) MIt = ds_map_find_next(Item, MIt) if is_undefined(MIt) break } } } else if is_struct(Item) { - if is_iterable(Item) { - // (*) Paired-Container and Map + var Type = instanceof(Item) + if Type == "Map" or Type == "Multimap" { + // (*) Maps + ds_map_copy(raw, Item.data()) + copy(cash.first(), cash.last(), Item.cash.first()) + } else if Type == "Unordered_Map" or Type == "Unordered_Multimap" { + // (*) Unordered_Maps + + } else if is_iterable(Item) { + // (*) Paired-Container foreach(Item.first(), Item.last(), function(Value) { insert(Value) })