API Basic Dictionary ko - LIHACHTETAN/ClassicUO-BadNewbie-BasicIDE GitHub Wiki
Dictionary
ClassicUO โข Basic
Dictionary()๋ ํค โ ๊ฐ ์ปฌ๋ ์ ์ ๋ง๋ญ๋๋ค. ๋ฐํ ๊ฐ์ฒด์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํฉ๋๋ค. ์ซ์ ํค์ ๋ฌธ์์ด ํค, "ore"์ "Ore"๋ ์๋ก ๋ค๋ฆ ๋๋ค.
์ ํํ ๊ตฌ๋ฌธ
Dictionary() -> Object:Dictionary
Dictionary(source:Dictionary) -> Object:Dictionary
values.Add(key:String/Number, value:Any) -> Unit
values.Item(key:String/Number) -> Any
values[key] = value
values.Set(key:String/Number, value:Any) -> Unit
values.Get(key:String/Number, fallback:Any) -> Any
values.Count() -> Integer
values.ContainsKey(key:String/Number) -> Integer:1/0
values.Remove(key:String/Number) -> Integer:1/0
values.Clear() -> Unit
values.Keys() -> Array
values.Values() -> Array
entry.Key() -> String/Number
entry.Value() -> Any
๋งค๊ฐ๋ณ์
sourceโ source: ์๋ตํ๋ฉด ๋น ์ปฌ๋ ์ ์ ๋๋ค. List๋ ๋ฐฐ์ด ๋๋ List, Dictionary๋ Dictionary๋ฅผ ๋ฐ์ต๋๋ค. ์ธ๋ถ ์ปจํ ์ด๋๋ง ๋ณต์ฌํ๊ณ ์ค์ฒฉ ์ฐธ์กฐ๋ ๊ณต์ ํฉ๋๋ค.index / keyโ index / key: List ์์น๋ 0๋ถํฐ ์์ํ๋ ์ ์ ์ซ์์ด๋ฉฐ Insert๋ Count()๋ ํ์ฉํฉ๋๋ค. Dictionary ํค๋ ํ ์คํธ ๋๋ ์ ํ ์ซ์์ ๋๋ค. ์ซ์ 1๊ณผ 1.0์ ๊ฐ์ ํค์ด๊ณ ํ ์คํธ "1"์ ๋ค๋ฆ ๋๋ค.valueโ value: ์ด๊ธฐํ๋ Basic ๊ฐ์ผ๋ก ๋ฐฐ์ด์ด๋ ์ปฌ๋ ์ ๋ ๊ฐ๋ฅํฉ๋๋ค. Unit์ ์ ์ฅํ ์ ์์ต๋๋ค. ์ซ์ ๊ฐ, ๋์๋ฌธ์๋ฅผ ๊ตฌ๋ถํ๋ ๋ฌธ์์ด ๋๋ ๋ฐฐ์ด/๊ฐ์ฒด ์ฐธ์กฐ ๋์ผ์ฑ์ผ๋ก ๋น๊ตํฉ๋๋ค.fallbackโ fallback: ํค๊ฐ ์์ผ๋ฉด Get์ด ์ด ๋์ฒด๊ฐ์ ๋ฐํํ๊ณ ์ฝ์ ํ์ง ์์ต๋๋ค. fallback ์์ ํฌํจํ ๋ชจ๋ ์ธ์๋ ๋ฉ์๋ ํธ์ถ ์ ์ ๊ณ์ฐํฉ๋๋ค.
๋ฐํ๊ฐ
์์ฑ์๋ Object:Dictionary๋ฅผ ๋ฐํํฉ๋๋ค. Item/์ธ๋ฑ์ค/Get์ ๊ฐ, Count๋ ํค ์๋ฅผ ๋ฐํํฉ๋๋ค. ContainsKey/Remove๋ 1=TRUE ๋๋ 0=FALSE, Add/Set/Clear๋ Unit, Keys/Values๋ ์ Array๋ฅผ ๋ฐํํฉ๋๋ค. For Each๋ ํญ๋ชฉ ๊ฐ์ฒด๋ฅผ ๋ด๋ณด๋ด๋ฉฐ Key()๋ ํค, Value()๋ ๊ฐ์ ๋ฐํํฉ๋๋ค.
๋์
- Add๋ ๊ธฐ์กด ํค๋ฅผ ๋ฎ์ด์ฐ์ง ์๊ณ ๊ฑฐ๋ถํฉ๋๋ค. Set/์ธ๋ฑ์ค ๋์ ์ ์์ฑ ๋๋ ๊ต์ฒดํ๋ฉฐ Item/์ธ๋ฑ์ค ์ฝ๊ธฐ์๋ ๊ธฐ์กด ํค๊ฐ ํ์ํฉ๋๋ค. Get์ ๋์ฒด๊ฐ์ ์ฌ์ฉํฉ๋๋ค. Remove๋ ํค๊ฐ ์์ผ๋ฉด 0์ ๋ฐํํ๊ณ Clear๋ ๋น์๋๋ค.
- NaN, ๋ฌดํ๋, ๋ฐฐ์ด, ๊ฐ์ฒด, Unit์ ํค๊ฐ ๋ ์ ์์ต๋๋ค. ํค ์์๋ ๋ณด์ฅ๋์ง ์์ต๋๋ค. ๊ฐ ํญ๋ชฉ์ ์์ ์ ํค/๊ฐ ์์ ์ ์งํ๋ฉฐ ๋ค์ ๋ฐ๋ณต์ผ๋ก ๋์ด๊ฐ๋ ๋ฐ๋์ง ์์ต๋๋ค.
- Keys/Values๋ ์์ ์ค๋ ์ท์ ๋ง๋ญ๋๋ค. ์ญ์ /๊ต์ฒดํ๋ ค๋ฉด Keys()๋ฅผ ์ํํ์ธ์. ์ฌ์ ์ ์ง์ ์ํํ๋ฉด ํญ๋ชฉ ๊ฐ์ฒด๋ฅผ ์ป์ต๋๋ค.
- ์ง์ For Each๋ฅผ ์คํํ๋ ๋์ Set์ ํฌํจํ ๋ณ๊ฒฝ์ ํ๋ฉด ๋ค์ ๋จ๊ณ์์ ์ก์ ์ ์๋ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. Try/Finally๋ ์ ์์ ์ผ๋ก ์ข ๋ฃํฉ๋๋ค. ์คํจํ ์์ ์ ๊ธฐ์กด ๋ฐ์ดํฐ๋ฅผ ์ ์งํฉ๋๋ค.
- ๋ณ์นญ๊ณผ ByVal ์ธ์๋ ์ปฌ๋ ์ ์ ๊ณต์ ํฉ๋๋ค. ๋ณต์ฌ์ ์ค๋ ์ท์ ์ธ๋ถ ์ปจํ ์ด๋๋ง ๋ณต์ ํฉ๋๋ค. ์ธ๋ฑ์ค ByRef์ ๋ณตํฉ ๋์ ์ ์ปจํ ์ด๋/ํค๋ฅผ ํ ๋ฒ ๊ณ์ฐํ๋ฉฐ ๋ณ์ ๊ต์ฒด๊ฐ ์ ์ฅ๋ ๊ธฐ๋ก ์์น๋ฅผ ๋ฐ๊พธ์ง ์์ต๋๋ค.
- ๋ก์ปฌ ์คํฌ๋ฆฝํธ ๋ฐ์ดํฐ์ ๋๋ค. ๋ฉ์๋๋ ๊ฒ์ ์์ดํ ์ ์ฎ๊ธฐ๊ฑฐ๋ ๋คํธ์ํฌ๋ฅผ ์ฌ์ฉํ์ง ์์ต๋๋ค. ํ๋์ ์์๋ก ์ ์ฅ๋ ์์ดํ ๋ฌถ์์ ํ ์์น๋ฅผ ์ฐจ์งํฉ๋๋ค.
์์
1. ํค ํ์๊ณผ ๋์ฒด๊ฐ
# "ore"๋ 5์์ 8๋ก ๋ฐ๋๋๋ค. ์ซ์ ํค 1์ 2, ํ
์คํธ "1"์ 3์ ์ ์ฅํฉ๋๋ค. Get("wood",7)์ ์ฝ์
ํ์ง ์๊ณ 7์ ๋ฐํํฉ๋๋ค. Main์ 8*100+2*10+3+7, Integer 830์ ๋ฐํํฉ๋๋ค.
Option Explicit On
Sub Main()
Var values = Dictionary()
values.Add("ore", 5)
values.Set("ore", 8)
values[1] = 2
values["1"] = 3
Return values.Item("ore")*100 + values[1]*10 + values["1"] + values.Get("wood", 7)
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
"ore"๋ 5์์ 8๋ก ๋ฐ๋๋๋ค. ์ซ์ ํค 1์ 2, ํ ์คํธ "1"์ 3์ ์ ์ฅํฉ๋๋ค. Get("wood",7)์ ์ฝ์ ํ์ง ์๊ณ 7์ ๋ฐํํฉ๋๋ค. Main์ 8100+210+3+7, Integer 830์ ๋ฐํํฉ๋๋ค.
2. ํญ๋ชฉ, ์ค๋ ์ท, ์ญ์
# ๋ ๊ฐ์ ํฉ์ 5์
๋๋ค. Keys()๋ก ์ํ ์ค ์ญ์ ํ ์ ์์ต๋๋ค. copied๋ ore=2, snapshot์ ๋ ๊ฐ์ ์ ์งํฉ๋๋ค. Clear ํ Count()=0์
๋๋ค. Main์ 5*100+2*10+2+0, Integer 522๋ฅผ ๋ฐํํฉ๋๋ค.
Option Explicit On
Sub Main()
Var values = Dictionary()
values.Add("ore", 2)
values.Add("wood", 3)
Var copied = Dictionary(values)
Var snapshot = values.Values()
Var total = 0
For Each entry In values
If values.ContainsKey(entry.Key()) Then
total += entry.Value()
End If
Next
For Each key In values.Keys()
values.Remove(key)
Next
values.Clear()
Return total*100 + copied["ore"]*10 + GetArrayLength(snapshot) + values.Count()
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
๋ ๊ฐ์ ํฉ์ 5์ ๋๋ค. Keys()๋ก ์ํ ์ค ์ญ์ ํ ์ ์์ต๋๋ค. copied๋ ore=2, snapshot์ ๋ ๊ฐ์ ์ ์งํฉ๋๋ค. Clear ํ Count()=0์ ๋๋ค. Main์ 5100+210+2+0, Integer 522๋ฅผ ๋ฐํํฉ๋๋ค.
3. ์ค๋ณต ํค ์ฒ๋ฆฌ
# ์ฒซ Add๊ฐ ore=4๋ฅผ ์ ์ฅํฉ๋๋ค. ๋ ๋ฒ์งธ ๊ฐ 7์ ์ค๋ฅ๊ฐ ๋์ด Catch๊ฐ caught=1์ ์ค์ ํฉ๋๋ค. ore=4๋ ๋ณด์กด๋ฉ๋๋ค. Main์ 4*10+1, Integer 41์ ๋ฐํํฉ๋๋ค.
Option Explicit On
Sub Main()
Var values = Dictionary()
values.Add("ore", 4)
Var caught = 0
Try
values.Add("ore", 7)
Catch problem
caught = 1
End Try
Return values["ore"]*10 + caught
End Sub
๋งค๊ฐ๋ณ์ ๋ฐ ์คํ ์ค๋ช :
์ฒซ Add๊ฐ ore=4๋ฅผ ์ ์ฅํฉ๋๋ค. ๋ ๋ฒ์งธ ๊ฐ 7์ ์ค๋ฅ๊ฐ ๋์ด Catch๊ฐ caught=1์ ์ค์ ํฉ๋๋ค. ore=4๋ ๋ณด์กด๋ฉ๋๋ค. Main์ 4*10+1, Integer 41์ ๋ฐํํฉ๋๋ค.