Difference between revisions of "Dictionary class"

From Catglobe Wiki
Jump to: navigation, search
(Tag: visualeditor-switched)
Line 35: Line 35:
 
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>The key used to lookup the value.</nowiki>}}
 
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>The key used to lookup the value.</nowiki>}}
 
|Description=<nowiki>Get an item based on a key.</nowiki>}}
 
|Description=<nowiki>Get an item based on a key.</nowiki>}}
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>Inc</nowiki>|Parameters=
+
{{CGscriptMethods_Template|ReturnType=Empty|Name=<nowiki>Inc</nowiki>|Parameters=
 
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Increase the specific keys value</nowiki>}}
 
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Increase the specific keys value</nowiki>}}
 
|Description=<nowiki>Increase the value of a value for the given key by 1, creating the entry at 1 if doesnt exists.</nowiki>}}
 
|Description=<nowiki>Increase the value of a value for the given key by 1, creating the entry at 1 if doesnt exists.</nowiki>}}
 +
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>KeysWhere</nowiki>|Parameters=
 +
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>predicate</nowiki>|Description=<nowiki>A function that takes 2 parameters, the key and the value, and return true/false</nowiki>}}
 +
|Description=<nowiki>Filters keys based on a predicate.</nowiki>}}
 
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>Remove</nowiki>|Parameters=
 
{{CGscriptMethods_Template|ReturnType=object|Name=<nowiki>Remove</nowiki>|Parameters=
 
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Check with specific key</nowiki>}}
 
{{CGscriptParameters_Template|Type=string|Name=<nowiki>key</nowiki>|Description=<nowiki>Check with specific key</nowiki>}}
Line 57: Line 60:
 
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>Remove with specific key</nowiki>}}
 
{{CGscriptParameters_Template|Type=int|Name=<nowiki>key</nowiki>|Description=<nowiki>Remove with specific key</nowiki>}}
 
|Description=<nowiki>Gets the value associated with the specified key.</nowiki>}}
 
|Description=<nowiki>Gets the value associated with the specified key.</nowiki>}}
 +
{{CGscriptMethods_Template|ReturnType=array|Name=<nowiki>ValuesWhere</nowiki>|Parameters=
 +
{{CGscriptParameters_Template|Type=Function|Name=<nowiki>predicate</nowiki>|Description=<nowiki>A function that takes 2 parameters, the key and the value, and return true/false</nowiki>}}
 +
|Description=<nowiki>Filters values based on a predicate.</nowiki>}}
 
|Properties=
 
|Properties=
 
{{CGscriptProperties_Template|ReturnType=int|Name=<nowiki>Count</nowiki>|HasGetter=1|Description=<nowiki>Gets the number of key/value pairs contained in the dictionary.</nowiki>}}
 
{{CGscriptProperties_Template|ReturnType=int|Name=<nowiki>Count</nowiki>|HasGetter=1|Description=<nowiki>Gets the number of key/value pairs contained in the dictionary.</nowiki>}}

Revision as of 04:59, 17 April 2020

Dictionary



The dictionary object

Constructors

  • () - Create a new dictionary with non-argument.
  • (params AnyType) - Create new dictionary with the specified key-value pairs.
  • (array keys "keys arrayConstant", array values "values arrayConstant") - Create new dictionary with the specified keys and values.

Methods

  • bool Add(string key "Add with specific key", object value "Add with specific value") - Adds the specified key and value to the dictionary.
  • bool Add(int key "Add with specific key", object value "Add with specific value") - Adds the specified key and value to the dictionary.
  • Empty Add(Dictionary src "Source of keys and values to add") - Adds the all keys and values from src to current.
  • Empty Clear() - Remove all keys and values from the dictionary.
  • bool ContainsKey(string key "Check with specific key") - Determines whether the dictionary contains the specified key.
  • bool ContainsKey(int key "Check with specific key") - Determines whether the dictionary contains the specified key.
  • bool ContainsValue(object value "Add with specific value") - Determines whether the dictionary contains the specified value.
  • object this[] { get; }(string key "The key used to lookup the value.") - Get an item based on a key.
  • object this[] { get; }(int key "The key used to lookup the value.") - Get an item based on a key.
  • Empty Inc(string key "Increase the specific keys value") - Increase the value of a value for the given key by 1, creating the entry at 1 if doesnt exists.
  • array KeysWhere(Function predicate "A function that takes 2 parameters, the key and the value, and return true/false") - Filters keys based on a predicate.
  • object Remove(string key "Check with specific key") - Remove the value with the specified key from the dictionary.
  • object Remove(int key "Remove with specific key") - Remove the value with the specified key from the dictionary.
  • Empty this[] { set; }(string key "The key used to lookup the value.", object value "The value to set.") - Set an item based on a key and a value.
  • Empty this[] { set; }(int key "The key used to lookup the value.", object value "The value to set.") - Set an item based on a key and a value.
  • string ToString() - The string representation of the object.
  • object TryGetValue(string key "Remove with specific key") - Gets the value associated with the specified key.
  • object TryGetValue(int key "Remove with specific key") - Gets the value associated with the specified key.
  • array ValuesWhere(Function predicate "A function that takes 2 parameters, the key and the value, and return true/false") - Filters values based on a predicate.

Properties

  • int Count { get; } - Gets the number of key/value pairs contained in the dictionary.
  • array Keys { get; } - Gets a list of keys.
  • string ObjectTypeName { get; } - The name of the type of object.
  • array SortedKeys { get; } - Gets a list of keys sorted by current collation.
  • TypeInformation TypeInformation { get; } - Get information about this class.
  • array Values { get; } - Gets a list of values.


Examples

Dictionary d;
print(d); // {}


Dictionary d = new Dictionary();
print(d);  // {}


Dictionary d = {"key1": 1, "key2": 2};
print(d);   // {"key1": 1, "key2": 2}


Dictionary d = {};             // Not work, will cause error.


Dictionary d1 = new Dictionary("key_01","value01");
print(d1);                                                                      // {"key_01": sdasd}
print(d1.Add("key_02",2222));
print(d1.Add(3,"hello3333"));
print(d1.Add("key_03",{"happy",48,{"funny","sad",88}}));
print(d1);                               //          {"3": hello3333, "key_01": value01, "key_02": 2222, "key_03": {happy,48,{funny,sad,88}}}
print(d1.ContainsKey("key_01"));                                                // true
print(d1.ContainsKey(3));                                                       // true
print(d1.ContainsKey("noexist"));                                               // false
print(d1.ContainsValue("value01"));                                             // true
print(d1.ContainsValue(2222));                                                  // true
print(d1.ContainsValue({"happy",48,{"funny","sad",88}}));                       // true
print(d1.get_Item("key_01"));                                                   // value01
print(d1.get_Item(3));                                                          // hello3333
print(d1.set_Item("key01",1111));     
print(d1.set_Item(3,{3333,"string333"}));
print(d1.TryGetValue("key_01"));                                                // value01
print(d1.TryGetValue(3));                                                       // {3333,"string333"}
print(d1);                              //     {"3": {3333,string333}, "key_01": value01, "key_02": 2222, "key_03": {happy,48,{funny,sad,88}}, "key01": 1111}
print(d1.Count);                                                                // 5
print(d1.Keys);                                                                 // {key_01,key_02,3,key_03,key01}
print(d1.Values);                                                               // {value01,2222,{3333,string333},{happy,48,{funny,sad,88}},1111}
print(d1.Remove("key_01"));                                                     // value01
print(d1.Remove(3));                                                            // {3333,string333}
print(d1);                                                                      // {"key_02": 2222, "key_03": {happy,48,{funny,sad,88}}, "key01": 1111}


Dictionary d = {
  "key01": "value01",
  5: "value05"
}; 
print(d["key01"]);       // another way to get the values of keys
print(d[5]);             // another way to get the values of keys


//Example for using d.Inc("key") to increase value of the key up to 1, if the key not exist, create the key with value 1
Dictionary d = {"key1": 1, "key2": 2};
d.Inc("key2");// key2 existed, so increate value of key2 to 1 (2->3)
print(d);//{"key1": 1, "key2": 3}
d.Inc("key3");// key3 not exist, so add key3 to d with value 1
print(d);//"key1": 1, "key2": 3, "key3": 1}