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.

Handling List Type

Handling Stack And Queue Types

Handling Dictionary Type

Index

1Handling List Type

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.

1.1Create List



By writing "list<Type Name>", a list of multiple values of that type is represented (Figure 1-1).
  1. func main()
  2.   var items: list<int>
  3.   do items :: #list<int>
  4.   do items.add(3)
  5.   do items.add(5)
  6. end func
Figure 1-1: List
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.

1.2Get Number Of Elements In List



The number of elements in the list can be obtained with the element count operator "^" (Figure 1-2).
  1. func main()
  2.   var items: list<int> :: #list<int>
  3.   do items.add(3)
  4.   do items.add(5)
  5.   var num: int :: ^items {2}
  6. end func
Figure 1-2: Number Of Elements In List

1.3Traverse Elements Of List In Order



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).
  1. func main()
  2.   var items: list<int> :: #list<int>
  3.   do items.add(3)
  4.   do items.add(5)
  5.   do items.head() {Set the pointer to the beginning.}
  6.   while(!items.term()) {Repeat if the pointer is not over the end.}
  7.     var item: int :: items.get() {Get the element at the pointer position.}
  8.     do items.next() {Advance the pointer toward the end.}
  9.   end while
  10. end func
Figure 1-3: Traversing List
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.
  1. func main()
  2.   var items: list<int> :: #list<int>
  3.   do items.add(3)
  4.   do items.add(5)
  5.   do items.tail() {Set the pointer to the end.}
  6.   while(!items.term()) {Repeat if the pointer is not over the top.}
  7.     var item: int :: items.get() {Get the element at the pointer position.}
  8.     do items.prev() {Advance the pointer toward the top.}
  9.   end while
  10. end func
Figure 1-4: Reverse List Traversal

1.4Removing Element From List



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)。
  1. func main()
  2.   var items: list<int> :: #list<int>
  3.   do items.add(3)
  4.   do items.add(4)
  5.   do items.add(5)
  6.   do items.head()
  7.   while(!items.term())
  8.     var item: int :: items.get()
  9.     if(item = 4)
  10.       do items.del() {Delete the element at the pointer position and proceed towards the end.}
  11.     else
  12.       do items.next()
  13.     end if
  14.   end while
  15. end func
Figure 1-5: Removing Element From List
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).
  1. func main()
  2.   var items: list<int> :: #list<int>
  3.   do items.add(3)
  4.   do items.add(4)
  5.   do items.add(5)
  6.   do items.head()
  7.   do items.delNext()
  8. end func
Figure 1-6: Remove Next Element In List
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.

1.5Inserting Element At Pointer Position In List



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).
  1. func main()
  2.   var items: list<int> :: #list<int>
  3.   do items.add(3)
  4.   do items.add(5)
  5.   do items.head()
  6.   do items.next()
  7.   do items.ins(4)
  8. end func
Figure 1-7: Inserting Element In List
When you run this program, eventually the elements of the list will be 3, 4, and 5 in order from the top.

1.6Converting List To Array



To convert a list into an array, use the .toArray method (Figure 1-8).
  1. func main()
  2.   var items: list<int> :: #list<int>
  3.   do items.add(3)
  4.   do items.add(5)
  5.   var array: []int :: items.toArray() {[3, 5]}
  6. end func
Figure 1-8: Converting List To Array

2Handling Stack And Queue Types

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.

2.1Creating Stack And Queue



To represent a stack in Kuin, write "stack<Type Name>" and to represent a queue, write "queue<Type Name>" (Figure 2-1).
  1. func main()
  2.   var stackItems: stack<int>
  3.   var queueItems: queue<int>
  4.   do stackItems :: #stack<int>
  5.   do queueItems :: #queue<int>
  6.   do stackItems.add(3)
  7.   do queueItems.add(3)
  8. end func
Figure 2-1: Stack And Queue
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.

2.2Get Number Of Elements In Stack Or Queue



The number of elements in a stack or queue can be obtained with the element count operator "^" (Figure 2-2).
  1. func main()
  2.   var stackItems: stack<int> :: #stack<int>
  3.   var queueItems: queue<int> :: #queue<int>
  4.   do stackItems.add(3)
  5.   do queueItems.add(3)
  6.   var stackNum: int :: ^stackItems {1}
  7.   var queueNum: int :: ^queueItems {1}
  8. end func
Figure 2-2: Number Of Elements In Stack Or Queue
To determine if the stack or queue is empty, use the "^" operator to check if the result is zero.

2.3Getting And Removing Element From Stack Or Queue



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).
  1. func main()
  2.   var stackItems: stack<int> :: #stack<int>
  3.   var queueItems: queue<int> :: #queue<int>
  4.   do stackItems.add(1)
  5.   do stackItems.add(2)
  6.   do queueItems.add(1)
  7.   do queueItems.add(2)
  8.   var item: int
  9.   do item :: stackItems.get() {2}
  10.   do item :: stackItems.get() {1}
  11.   do item :: queueItems.get() {1}
  12.   do item :: queueItems.get() {2}
  13. end func
Figure 2-3: Getting Element Of Stack Or Queue

2.4Getting Element From Stack Or Queue Without Removing It



To get an element from the stack or queue without removing it, use the .peek method (Figure 2-4).
  1. func main()
  2.   var stackItems: stack<int> :: #stack<int>
  3.   var queueItems: queue<int> :: #queue<int>
  4.   do stackItems.add(1)
  5.   do stackItems.add(2)
  6.   do queueItems.add(1)
  7.   do queueItems.add(2)
  8.   var item: int
  9.   do item :: stackItems.peek() {2}
  10.   do item :: stackItems.peek() {2}
  11.   do item :: queueItems.peek() {1}
  12.   do item :: queueItems.peek() {1}
  13. end func
Figure 2-4: Getting Element From Stack Or Queue Without Removing It

3Handling Dictionary Type

A dictionary is a type of data structure that has multiple key-value pairs and allows fast lookup of values from keys.

3.1Creating Dictionary



To represent a dictionary in Kuin, write "dict<Key Type Name, Value Type Name>" (Figure 3-1).
  1. func main()
  2.   var items: dict<[]char, int>
  3.   do items :: #dict<[]char, int>
  4.   do items.add("one", 1)
  5.   do items.add("two", 2)
  6.   do items.add("three", 3)
  7. end func
Figure 3-1: Dictionary
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.

3.2Get Number Of Elements In Dictionary



The number of elements in the dictionary can be obtained with the element count operator "^" (Figure 3-2).
  1. func main()
  2.   var items: dict<[]char, int> :: #dict<[]char, int>
  3.   do items.add("one", 1)
  4.   do items.add("two", 2)
  5.   do items.add("three", 3)
  6.   var num: int :: ^items {3}
  7. end func
Figure 3-2: Number Of Elements In Dictionary

3.3Get Element From Dictionary



To get an element from the dictionary, use the .get method (Figure 3-3).
  1. func main()
  2.   var items: dict<[]char, int> :: #dict<[]char, int>
  3.   do items.add("one", 1)
  4.   do items.add("two", 2)
  5.   do items.add("three", 3)
  6.   var existed: bool
  7.   var item: int :: items.get("two", &existed) {item = 2, existed = true}
  8. end func
Figure 3-3: Getting Element Of Dictionary
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).

3.4Checking If Element Exists In Dictionary



To check if an element exists in the dictionary, use the .exist method (Figure 3-4).
  1. func main()
  2.   var items: dict<[]char, int> :: #dict<[]char, int>
  3.   do items.add("one", 1)
  4.   do items.add("two", 2)
  5.   do items.add("three", 3)
  6.   var existence: bool
  7.   do existence :: items.exist("two") {true}
  8.   do existence :: items.exist("five") {false}
  9. end func
Figure 3-4: Checking If Element Exists In Dictionary

3.5Processing All Elements Of Dictionary In Order



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).
  1. func main()
  2.   var items: dict<[]char, int> :: #dict<[]char, int>
  3.   do items.add("one", 1)
  4.   do items.add("two", 2)
  5.   do items.add("three", 3)
  6.  
  7.   var str: lib@Str :: #lib@Str {Class that wraps a string.}
  8.   do str.value :: "" {Set an empty string.}
  9.   var completed: bool :: items.forEach(callback, str) {true}
  10.  
  11.   func callback(key: []char, value: int, data: kuin@Class): bool
  12.     do(data $ lib@Str).value :~ "\{key}=\{value}." {Add a string for each element.}
  13.     ret true {Return true to continue, false to abort.}
  14.   end func
  15. end func
Figure 3-5: Dictionary Scanning
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.
1660381412enf