Skip to content

Instantly share code, notes, and snippets.

@intsuc
Last active February 27, 2025 12:48
Show Gist options
  • Save intsuc/6bce0b99749306f637ced1d9e4cc9758 to your computer and use it in GitHub Desktop.
Save intsuc/6bce0b99749306f637ced1d9e4cc9758 to your computer and use it in GitHub Desktop.
[25w09b] Get the id of a given tag in constant time
# examples
data merge storage example: { \
1: 0b, \
2: 0s, \
3: 0, \
4: 0L, \
5: 0.0f, \
6: 0.0d, \
7: [B;], \
8: "", \
9: [], \
10: {}, \
11: [I;], \
12: [L;], \
7_: [B; 0b], \
8_: "0b", \
9_: [0b], \
10_: {0: 0b}, \
11_: [I; 0], \
12_: [L; 0L], \
}
execute store result storage example: id byte 1 run function get_id {accessor: "example: 1"}
execute if data storage example: {id: 1b} run say ✔ 1
execute unless data storage example: {id: 1b} run say ✘ 1
execute store result storage example: id byte 1 run function get_id {accessor: "example: 2"}
execute if data storage example: {id: 2b} run say ✔ 2
execute unless data storage example: {id: 2b} run say ✘ 2
execute store result storage example: id byte 1 run function get_id {accessor: "example: 3"}
execute if data storage example: {id: 3b} run say ✔ 3
execute unless data storage example: {id: 3b} run say ✘ 3
execute store result storage example: id byte 1 run function get_id {accessor: "example: 4"}
execute if data storage example: {id: 4b} run say ✔ 4
execute unless data storage example: {id: 4b} run say ✘ 4
execute store result storage example: id byte 1 run function get_id {accessor: "example: 5"}
execute if data storage example: {id: 5b} run say ✔ 5
execute unless data storage example: {id: 5b} run say ✘ 5
execute store result storage example: id byte 1 run function get_id {accessor: "example: 6"}
execute if data storage example: {id: 6b} run say ✔ 6
execute unless data storage example: {id: 6b} run say ✘ 6
execute store result storage example: id byte 1 run function get_id {accessor: "example: 7"}
execute if data storage example: {id: 7b} run say ✔ 7
execute unless data storage example: {id: 7b} run say ✘ 7
execute store result storage example: id byte 1 run function get_id {accessor: "example: 8"}
execute if data storage example: {id: 8b} run say ✔ 8
execute unless data storage example: {id: 8b} run say ✘ 8
execute store result storage example: id byte 1 run function get_id {accessor: "example: 9"}
execute if data storage example: {id: 9b} run say ✔ 9
execute unless data storage example: {id: 9b} run say ✘ 9
execute store result storage example: id byte 1 run function get_id {accessor: "example: 10"}
execute if data storage example: {id: 10b} run say ✔ 10
execute unless data storage example: {id: 10b} run say ✘ 10
execute store result storage example: id byte 1 run function get_id {accessor: "example: 11"}
execute if data storage example: {id: 11b} run say ✔ 11
execute unless data storage example: {id: 11b} run say ✘ 11
execute store result storage example: id byte 1 run function get_id {accessor: "example: 12"}
execute if data storage example: {id: 12b} run say ✔ 12
execute unless data storage example: {id: 12b} run say ✘ 12
execute store result storage example: id byte 1 run function get_id {accessor: "example: 7_"}
execute if data storage example: {id: 7b} run say ✔ 7_
execute unless data storage example: {id: 7b} run say ✘ 7_
execute store result storage example: id byte 1 run function get_id {accessor: "example: 8_"}
execute if data storage example: {id: 8b} run say ✔ 8_
execute unless data storage example: {id: 8b} run say ✘ 8_
execute store result storage example: id byte 1 run function get_id {accessor: "example: 9_"}
execute if data storage example: {id: 9b} run say ✔ 9_
execute unless data storage example: {id: 9b} run say ✘ 9_
execute store result storage example: id byte 1 run function get_id {accessor: "example: 10_"}
execute if data storage example: {id: 10b} run say ✔ 10_
execute unless data storage example: {id: 10b} run say ✘ 10_
execute store result storage example: id byte 1 run function get_id {accessor: "example: 11_"}
execute if data storage example: {id: 11b} run say ✔ 11_
execute unless data storage example: {id: 11b} run say ✘ 11_
execute store result storage example: id byte 1 run function get_id {accessor: "example: 12_"}
execute if data storage example: {id: 12b} run say ✔ 12_
execute unless data storage example: {id: 12b} run say ✘ 12_
execute unless data storage _ array_suffix_to_id run \
data modify storage _ array_suffix_to_id set value { \
b: 7, \
0: 11, \
1: 11, \
2: 11, \
3: 11, \
4: 11, \
5: 11, \
6: 11, \
7: 11, \
8: 11, \
9: 11, \
L: 12, \
}
$return run \
data get storage _ array_suffix_to_id.$(key)
execute unless data storage _ empty_collection_to_id run \
data modify storage _ empty_collection_to_id set value { \
"[B;]": 7, \
"[]": 9, \
"[I;]": 11, \
"[L;]": 12, \
}
$return run \
data get storage _ empty_collection_to_id."$(key)"
execute unless data storage _ suffix_to_id run \
data modify storage _ suffix_to_id set value { \
b: 1, \
s: 2, \
0: 3, \
1: 3, \
2: 3, \
3: 3, \
4: 3, \
5: 3, \
6: 3, \
7: 3, \
8: 3, \
9: 3, \
L: 4, \
f: 5, \
d: 6, \
}
$return run \
data get storage _ suffix_to_id.$(key)
# Check if the `$(accessor)` is a compound tag.
$execute if data storage $(accessor){} run \
return 10
# Check if the `$(accessor)` is a primitive tag;
# if you can stringify it successfully, it must be a primitive tag.
data remove storage _ key
$execute store success storage _ is_primitive byte 1 run \
data modify storage _ key set string storage $(accessor)
# Check if the `$(accessor)` is a numeric tag;
# if you can get it with a scale successfully, it must be a numeric tag.
# Just checking the suffix is not enough because it can be a string tag mimicking a numeric tag, e.g., "42b".
$execute store success storage _ is_numeric byte 1 run \
data get storage $(accessor) 0
# Get the suffix of the `$(accessor)`, if it is a numeric tag.
$execute if data storage _ {is_numeric: true} run \
data modify storage _ key set string storage $(accessor) -1
# If the `$(accessor)` is a numeric tag, look up the id by the suffix.
execute if data storage _ {is_numeric: true} run \
return run \
function get_id-suffix_to_id with storage _
# If the `$(accessor)` is a primitive tag but not a numeric tag, it must be a string tag.
execute if data storage _ {is_primitive: true} run \
return 8
# Check if the `$(accessor)` is an empty collection tag.
# If so, look up the id by the stringified empty collection.
$execute unless data storage $(accessor)[0] run \
data modify storage _ key set from storage $(accessor)
$execute unless data storage $(accessor)[0] run \
return run function get_id-empty_collection_to_id with storage _
# Check if the `$(accessor)` is a list tag;
# if you can append any tag except a byte/int/long tag to it, it must be a list tag.
$execute store success storage _ is_list byte 1 run \
data modify storage $(accessor) append value ""
$execute if data storage _ {is_list: true} run \
data remove storage $(accessor)[-1]
execute if data storage _ {is_list: true} run \
return 9
# Look up the id by the suffix of the first element of the `$(accessor)`.
$data modify storage _ key set string storage $(accessor)[0] -1
return run function get_id-array_suffix_to_id with storage _
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment