Skip to content

Commit

Permalink
Merge pull request #34 from iconstudio/develop
Browse files Browse the repository at this point in the history
Improvements on Maps
  • Loading branch information
iconstudio authored Sep 13, 2020
2 parents 874b23c + ece3a2c commit 48303ec
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 46 deletions.
12 changes: 6 additions & 6 deletions src/objects/oExample/Create_0.gml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ 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()) {
Expand All @@ -85,10 +90,5 @@ 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 Multimap")
// Transform into paired-container
transform(test_sum.first(), test_sum.last(), test_sum.first(), function (Value) {
return [irandom(2), Value]
})
print(test_sum)
2 changes: 1 addition & 1 deletion src/scripts/Iterator/Iterator.gml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ function iterator_advance(It, Distance) {
if is_real(It) {
return It + Distance
} else if is_iterator(It) {
return make_iterator(It).advance(Distance)
return It.duplicate().advance(Distance)
}
return undefined
}
Expand Down
61 changes: 35 additions & 26 deletions src/scripts/Map/Map.gml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
Usage:
To Iterate values:
for (var It = first(); It != last(); ++It)
myfunc(It.get()[0])
foreach(Container.first(), Container.last(), function(Value) {
myfunc(Value[1])
})
*/
#macro Dictionary Map
Expand Down Expand Up @@ -69,10 +70,10 @@ function Map(): Container() constructor {
}

///@function back()
function back() { return at(ds_map_find_last(raw)) }
function back() { return at(size() - 1) }

///@function front()
function front() { return at(ds_map_find_first(raw)) }
function front() { return at(0) }

///@function erase_at(key)
function erase_at(K) {
Expand All @@ -85,14 +86,27 @@ function Map(): Container() constructor {
///@function erase_one(iterator)
function erase_one(It) { return erase_at(It.get_index()) }

///@function key_change(key, value)
function key_change(K, Value) {
cash_push(K)
return ds_map_replace(raw, K, Value)
}

///@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 is_list(K)
function is_list(K) { return ds_map_is_list(raw, K) }

///@function is_map(K)
function is_map(K) { return ds_map_is_map(raw, K) }

///@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) }
Expand All @@ -103,12 +117,9 @@ function Map(): Container() constructor {
///@function clear()
function clear() { ds_map_clear(raw) }

///@function set_comparator(compare_function)
function set_comparator(Func) { cash_comparator = method(other, Func) }

///@function cash_push(key)
function cash_push(K) {
if !exists(K) {
if !contains(K) {
if 1 < cash.size() {
cash.push_back(K)
cash.sort_builtin(true)
Expand All @@ -118,21 +129,19 @@ function Map(): Container() constructor {
}
}

///@function key_change(key, value)
function key_change(K, Value) {
cash_push(K)
return ds_map_replace(raw, K, Value)
}

///@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 read(data_string)
function read(Str) { ds_map_read(raw, Str) }
function read(Str) {
var loaded = ds_map_create()
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)))
MIt = ds_map_find_next(loaded, MIt)
if is_undefined(MIt)
break
}
}
}

///@function write()
function write() { return ds_map_write(raw) }
Expand All @@ -145,7 +154,7 @@ function Map(): Container() constructor {
iterator_type = ForwardIterator
const_iterator_type = ConstIterator
cash = new List()
cash_comparator = compare_less
cash.clear() // To avoid the 0-populate-value problem.

if 0 < argument_count {
if argument_count == 1 {
Expand All @@ -171,7 +180,7 @@ function Map(): Container() constructor {
//ds_map_copy(raw, Item)
} else if is_struct(Item) {
if is_iterable(Item) {
// (*) Paired-Container and Map
// (*) Paired-Container
foreach(Item.first(), Item.last(), function(Value) {
insert(Value)
})
Expand Down
26 changes: 13 additions & 13 deletions src/scripts/Unordered_Map/Unordered_Map.gml
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,24 @@ function Unordered_Map(): Container() constructor {
///@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 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 is_list(K)
function is_list(K) { return ds_map_is_list(raw, K) }

///@function is_map(K)
function is_map(K) { return ds_map_is_map(raw, K) }

///@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) }
Expand All @@ -83,16 +93,6 @@ function Unordered_Map(): Container() constructor {
///@function clear()
function clear() { ds_map_clear(raw) }

///@function key_change(key, value)
function key_change(K, Value) { return ds_map_replace(raw, K, Value) }

///@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 read(data_string)
function read(Str) { ds_map_read(raw, Str) }

Expand All @@ -102,7 +102,7 @@ function Unordered_Map(): Container() constructor {
///@function destroy()
function destroy() { ds_map_destroy(raw); gc_collect() }

type = Map
type = Unordered_Map
raw = ds_map_create()
iterator_type = MapIterator
const_iterator_type = ConstMapIterator
Expand Down

0 comments on commit 48303ec

Please sign in to comment.