1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//
// EmojicodeDictionary.h
// Emojicode
//
// Created by Theo Weidmann on 19/04/15.
// Copyright (c) 2015 Theo Weidmann. All rights reserved.
//
#ifndef EmojicodeDictionary_h
#define EmojicodeDictionary_h
#include "String.hpp"
#include "RetainedObjectPointer.hpp"
namespace Emojicode {
/** Default initial capacity. MUST be a power of two, default: 8 */
#define DICTIONARY_DEFAULT_INITIAL_CAPACITY (1 << 3)
/** Factor for determining whether the dictionary should be resized. */
#define DICTIONARY_DEFAULT_LOAD_FACTOR (0.75f)
#define DICTIONARY_MAXIMUM_CAPACTIY (1 << 30)
#define DICTIONARY_MAXIMUM_CAPACTIY_THRESHOLD (1 << 16)
typedef uint64_t EmojicodeDictionaryHash;
/** The datastructure with the key-value pair */
struct EmojicodeDictionaryNode {
/** The user specified key. */
Object *key;
/** The user specified value. */
Box value;
/** The cached hash for the key. Calculated on item addition. */
EmojicodeDictionaryHash hash;
/** EmojicodeDictionary stores a bucket's elements in a linked list. */
Object *next;
};
/** Structure for the Emojicode standard Dictionary. The implementation is similar to Java's Hashmap. */
struct EmojicodeDictionary {
/** An array with pointers to linked lists. Initializes when the first item is inserted. */
Object *buckets;
/** Length of buckets array. */
size_t bucketsCounter;
/** The number of items stored in this dictionary. Used for determining whether to resize. */
size_t size;
/** When size * loadFactor >= bucketsCounter the dictionary doubles the amount of buckets */
float loadFactor;
/** Stores the next threshold for resizing. Is 0 until first resize when item is inserted. */
size_t nextThreshold;
};
/// Prepares the dictionary for a new value and returns a pointer to where the new value should be copied.
/// @warning Garbage collector invoking
Box* dictionaryPutVal(RetainedObjectPointer dictionaryObject, RetainedObjectPointer key, Thread *thread);
/// Removes the key and the associated value from the dictionary, if the key @c key is in the dictionary.
void dictionaryRemove(EmojicodeDictionary *dictionary, Object *key);
void dictionaryMark(Object *dict);
void initDictionaryBridge(Thread *thread);
void dictionaryInit(EmojicodeDictionary *dict);
void bridgeDictionarySet(Thread *thread);
void bridgeDictionaryGet(Thread *thread);
void bridgeDictionaryRemove(Thread *thread);
void bridgeDictionaryKeys(Thread *thread);
void bridgeDictionaryClear(Thread *thread);
void bridgeDictionaryContains(Thread *thread);
void bridgeDictionarySize(Thread *thread);
}
#endif /* EmojicodeDictionary_h */