Aug 13, 2022Kuina-chan
This is the reverse lookup dictionary 3 for exe execution environment of Kuin Programming Language, and is about how to handle list, stack, queue, and dictionary types.
A list is a type that is similar to an array and can handle multiple variables of the same type together, but it can add, delete, and insert elements faster than an array. It is an efficient type when the number of elements changes dynamically.
On the other hand, an array is faster than a list when accessing by element index, and arrays are more efficient in many cases where the number of elements does not change frequently.
By writing "list<Type Name>", a list of multiple values of that type is represented (Figure 1-1).
If you write "list<int>" as in the 2nd line, it will be a list of type int.
By writing "#list<Type Name>" as in the 3rd line, an instance of the list will be created.
You can add elements to the end of the list by using the .add method as shown in lines 4-5. In the case of this program, the values 3 and 5 are stored in order from the top.
The number of elements in the list can be obtained with the element count operator "^" (Figure 1-2).
The elements of the list are accessed using the pointers that the list has. To traverse the elements of a list in order, first set the pointer to the beginning, and then proceed one by one until you reach the end (Figure 1-3).
In this program, the while is executed twice in total, and the variable item is set to 3 the first time and 5 the second time.
To traverse the list in reverse order from the end to the beginning, write as in Figure 1-4.
To delete an element from the list, use the .del method. When the .del method is called, the element at the pointer position will be deleted and the pointer will point to the next element.(Figure 1-5)。
When you run this program, the list will contain elements 3, 4, and 5 from the top, and 4 will be removed, leaving only 3 and 5.
To delete the next element while keeping the pointer at its position, use the .delNext method (Figure 1-6).
When you run this program, 4 will be removed and the elements of the list will be 3 and 5 in order from the top.
To insert an element at the position of the pointer in the list, use the .ins method. The .ins method inserts an element in front of the pointer, keeping it at its position (Figure 1-7).
When you run this program, eventually the elements of the list will be 3, 4, and 5 in order from the top.
To convert a list into an array, use the .toArray method (Figure 1-8).
A stack is a type of data structure in which the later elements are fetched first, and a queue is a type of data structure in which elements are fetched in the order in which they are added.
To represent a stack in Kuin, write "stack<Type Name>" and to represent a queue, write "queue<Type Name>" (Figure 2-1).
If you write "stack<int>" or "queue<int>" as in the 2nd and 3rd lines, it will be an int type stack or queue.
By writing "#stack<Type Name>" or "#queue<Type Name>" as in the 4th and 5th lines, the stack or queue instance will be created.
You can add an element to the stack or queue by using the .add method as shown in lines 6-7.
The number of elements in a stack or queue can be obtained with the element count operator "^" (Figure 2-2).
To determine if the stack or queue is empty, use the "^" operator to check if the result is zero.
To get an element from the stack or queue, use the .get method. The obtained element will be removed from the stack or queue (Figure 2-3).
To get an element from the stack or queue without removing it, use the .peek method (Figure 2-4).
A dictionary is a type of data structure that has multiple key-value pairs and allows fast lookup of values from keys.
To represent a dictionary in Kuin, write "dict<Key Type Name, Value Type Name>" (Figure 3-1).
If you write "dict<char, int>" as in the 2nd line, the type is a dictionary with keys of type char and values of type int.
By writing "#dict<Key Type Name, Value Type Name>" as in the 3rd line, an instance of the dictionary will be created.
You can add elements to the dictionary by using the .add method as shown in line 4.
The number of elements in the dictionary can be obtained with the element count operator "^" (Figure 3-2).
To get an element from the dictionary, use the .get method (Figure 3-3).
If the key is not found in the dictionary, existed will be false and the return value will be the default value (0 for int type).
To check if an element exists in the dictionary, use the .exist method (Figure 3-4).
To process all the elements in the dictionary in order, use the .forEach method. When you pass a callback function, the function will be called for each element (Figure 3-5).
In line 9, the .forEach method is called, and the callback function is passed to the callback function as the first argument. The callback function is defined in lines 11 to 14.
The second argument of .forEach can be any data to be passed to the callback function. However, it has to be a class. In this case, I want to pass the string type, but since the string type is not a class, I use the lib@Str class that wraps the string type instead.
The order of the elements when the callback function is called is the order of the keys in ascending order. When '"one"', '"two"', and '"three"' are arranged in ascending (lexicographic) order, they become '"one"', '"three"', and '"two"', so the final value of str.value will be '"one=1.three=3.two=2."'.
If you set the return value of the callback function to false, you can end the .forEach process at that time. If you finish it in the middle, the return value of the .forEach method will also be false, and if it is processed to the end, the return value will be true.