Repository: godotengine/godot
File: core/variant/dictionary.cpp (C++)

Code before:
#include "core/variant/container_type_validate.h"
#include "core/variant/variant.h"
// required in this order by VariantInternal, do not remove this comment.
#include "core/object/class_db.h"
#include "core/object/object.h"
#include "core/variant/type_info.h"
#include "core/variant/variant_internal.h"

struct DictionaryPrivate {
	SafeRefCount refcount;
	Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values.
	HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map;
	ContainerTypeValidate typed_key;
	ContainerTypeValidate typed_value;
	Variant *typed_fallback = nullptr; // Allows a typed dictionary to return dummy values when attempting an invalid access.
};

Dictionary::ConstIterator Dictionary::begin() const {
	return _p->variant_map.begin();
}

Dictionary::ConstIterator Dictionary::end() const {
	return _p->variant_map.end();
}

LocalVector<Variant> Dictionary::get_key_list() const {
	LocalVector<Variant> keys;
	if (_p->variant_map.is_empty()) {
		return keys;
	}

	keys.reserve(_p->variant_map.size());
	for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
		keys.push_back(E.key);
	}
	return keys;
}

Variant Dictionary::get_key_at_index(int p_index) const {
	int index = 0;
	for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
		if (index == p_index) {
			return E.key;
		}
		index++;
	}

	return Variant();
}

Variant Dictionary::get_value_at_index(int p_index) const {

Review comment (suggestion):
I think this should be renamed to `keys_vector` (analogous to `keys()`), and moved next to it for visibility.

Code after:
#include "core/variant/container_type_validate.h"
#include "core/variant/variant.h"
// required in this order by VariantInternal, do not remove this comment.
#include "core/object/class_db.h"
#include "core/object/object.h"
#include "core/variant/type_info.h"
#include "core/variant/variant_internal.h"

struct DictionaryPrivate {
	SafeRefCount refcount;
	Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values.
	HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map;
	ContainerTypeValidate typed_key;
	ContainerTypeValidate typed_value;
	Variant *typed_fallback = nullptr; // Allows a typed dictionary to return dummy values when attempting an invalid access.
};

Dictionary::ConstIterator Dictionary::begin() const {
	return _p->variant_map.begin();
}

Dictionary::ConstIterator Dictionary::end() const {
	return _p->variant_map.end();
}

LocalVector<Variant> Dictionary::get_key_list() const {
	LocalVector<Variant> keys;

	keys.reserve(_p->variant_map.size());
	for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
		keys.push_back(E.key);
	}
	return keys;
}

Variant Dictionary::get_key_at_index(int p_index) const {
	int index = 0;
	for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
		if (index == p_index) {
			return E.key;
		}
		index++;
	}

	return Variant();
}

Variant Dictionary::get_value_at_index(int p_index) const {
	int index = 0;
	for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
		if (index == p_index) {