/** value will always be non-negative. */ public void Put(int key, int value) { int hashKey = key % MAX_LENGTH; var keyValueNode = container[hashKey]; var newNode = new KeyValueNode(key, value); if (keyValueNode == null) { container[hashKey] = newNode; return; } if (keyValueNode.Key == key) { keyValueNode.Value = value; return; } while (keyValueNode.Next != null) { if (keyValueNode.Key == key) { keyValueNode.Value = value; return; } keyValueNode = keyValueNode.Next; } keyValueNode.Next = newNode; newNode.Pre = keyValueNode; }
public INode GetKeyValueNode(KeyValuePair <string, object> keyValuePair) { INode value = null; if (keyValuePair.Value is Dictionary <string, object> ) { // has key, value var listKeyvalueNodeParser = new ListKeyValueNodeParser(); var listKeyvalueNode = listKeyvalueNodeParser.GetKeyValueNodes(keyValuePair.Value as Dictionary <string, object>, 0); value = listKeyvalueNode; } //else if (keyValuePair.Value is ArrayList) //{ // var keyArrayNodeParser = new KeyArrayNodeParser(); // var arrayNode = keyArrayNodeParser.GetArrayListNode(keyValuePair.Key, keyValuePair.Value as ArrayList); // value = arrayNode; //} else if (keyValuePair.Value is object) // needs to be last { var ValueNodeParser = new ValueNodeParser(); var valueNode = ValueNodeParser.GetValueNode(keyValuePair.Value as object); value = valueNode; } KeyValueNode rootnode = new KeyValueNode(); rootnode.Key = keyValuePair.Key; rootnode.Value = value; return(rootnode); }
public void Set(string key, T value) { lock (_lockObj) { if (_objects.TryGetValue(key, out KeyValueNode <T> node)) { node.Value = value; DeleteNode(node); AddToHead(node); } else { node = new KeyValueNode <T>(key, value); _objects[key] = node; if (_count < _capacity) { _count++; AddToHead(node); } else { _objects.Remove(_tail.Previous.Key); DeleteNode(_tail.Previous); AddToHead(node); } } } }
private void AddToHead(KeyValueNode <T> node) { node.Next = _head.Next; node.Next.Previous = node; node.Previous = _head; _head.Next = node; }
public void Put(string key, string value) { if (key == null) { return; } int index = Hash(key); KeyValueNode node = GetInChain(key, table[index]); if (node == null) { table[index] = new KeyValueNode(key, value, table[index]); Size++; if (Size > table.Length * LoadFactor) { Rehash(); } } else { node.Value = value; } }
/// <summary> /// /// </summary> /// <param name="rootNode"></param> /// <returns></returns> public Dictionary <string, object> ParseToDictionary(KeyValueNode rootNode) { var listKeyValueNodeParser = new ListKeyValueNodeParser(); var jsonAsDictionary = listKeyValueNodeParser.GetDictionary(rootNode.Value as ListKeyValueNode); return(jsonAsDictionary); }
/// <summary> /// private constructor to force use of factory methods /// </summary> /// <param name="node">the good node</param> /// <param name="goodType">the good type</param> private Good(KeyValueNode node, GoodTypeEnum goodType) : base(node.Key) { GoodType = goodType; Cost = node["cost"].Value.AsDecimal(); var children = node.Children; if (children.ContainsKey("available_from_start")) { AvailableFromStart = node["available_from_start"].Value.AsBool(); } if (children.ContainsKey("overseas_penalty")) { OverseasPenalty = node["overseas_penalty"].Value.AsBool(); } if (children.ContainsKey("tradeable")) { Tradeable = node["tradeable"].Value.AsBool(); } if (children.ContainsKey("money")) { Money = node["money"].Value.AsBool(); } var colors = node["color"]; foreach (var child in colors) { Color.Add(child.Key.AsInt()); } }
public KeyValuePair <string, object> GetKeyValuePair(KeyValueNode node) { object value = null; if (node.Value is ListKeyValueNode) { // has key, value var listKeyvalueNodeParser = new ListKeyValueNodeParser(); Dictionary <string, object> dictionary = listKeyvalueNodeParser.GetDictionary(node.Value as ListKeyValueNode); value = dictionary; } //else if (node.Value is KeyArrayNode) //{ // var keyArrayNodeParser = new KeyArrayNodeParser(); // KeyValuePair<string, ArrayList> keyValueArrayPair = keyArrayNodeParser.GetKeyArrayListPair(node.Value as KeyArrayNode); // value = keyValueArrayPair; //} else if (node.Value is ValueNode) { var ValueNodeParser = new ValueNodeParser(); var valueNode = ValueNodeParser.GetDictionaryValue(node.Value as ValueNode); value = valueNode; } var keyArrayListPair = new KeyValuePair <string, object>(node.Key, value); return(keyArrayListPair); }
private Building(GameState state, KeyValueNode root) : base(root.Key) { BuildingType = BuildingTypeMap[root["type"].Value]; Cost = root["cost"]?.Value?.AsInt() ?? 0; Time = root["time"].Value.AsInt(); Visability = root["visability"]?.Value?.AsBool() ?? false; OnMap = root["onmap"]?.Value?.AsBool() ?? false; MaxLevel = root["MaxLevel"]?.Value?.AsInt() ?? Int32.MaxValue; Province = root["province"]?.Value?.AsBool() ?? false; Infrastructure = root["infrastructure"]?.Value?.AsDecimal() ?? 0; MovementCost = root["movement_cost"]?.Value?.AsDecimal() ?? 0; PopBuildFactory = root["pop_build_factory"]?.Value?.AsBool() ?? false; SpawnRailwayTrack = root["spawn_railway_track"]?.Value?.AsBool() ?? false; NavalCapacity = root["naval_capacity"]?.Value?.AsInt() ?? 0; Capital = root["capital"]?.Value?.AsBool() ?? false; ColonialPoints = root["colonial_points"]?.Select(x => x.Key.AsInt())?.ToList(); ColonialRange = root["colonial_range"]?.Value?.AsInt() ?? 0; OnePerState = root["one_per_state"]?.Value?.AsBool() ?? false; LocalShipBuildTime = root["local_ship_build"]?.Value?.AsDecimal() ?? 0; FortLevel = root["fort_level"]?.Value?.AsInt() ?? 0; CompletionSize = root["completion_size"]?.Value?.AsDecimal() ?? 0; AdvancedFactory = root["advanced_factory"]?.Value?.AsBool() ?? false; DefaultEnabled = root["default_enabled"]?.Value?.AsBool() ?? false; ProductionType = root["production_type"]?.Value; OnCompletion = root["on_completion"]?.Value; SetGoods(state, root); }
/// <summary> /// returns true if the given country has more than x% of the infamy limit /// </summary> /// <param name="scope"></param> /// <param name="root"></param> /// <returns></returns> private bool Badboy(Scope scope, KeyValueNode root) { var value = root.Value.AsDecimal(); var scopeValue = scope.Country.DiplomaticInfo.Infamy / scope.State.GlobalMetadata.InfamyLimit; return(scopeValue >= value); }
public void ConfigurationNodeParsesCorrectForIncorrectValue() { var json = @"key1"; var retrievedNode = new KeyValueNode("myconfig", json); Assert.ThrowsAny <Exception>(() => retrievedNode.ParseConfig(new JsonConfigurationParser())); }
internal sealed override PersistentHashMap <K, V> Set(uint h, int s, KeyValueNode <K, V> n) { var localIdx = (int)LocalHash(h, s); var nvs = CopyArray(Nodes); nvs[localIdx] = Nodes[localIdx].Set(h, s + TrieShift, n); return(new BitmapNode16 <K, V> (nvs)); }
public static Country Factory(GameState state, KeyValueNode root) { var country = new Country(state, root); var governmentLocation = root.Value; var countryFileNode = state.Loaders.CountryDirectoryLoader.Data.First(x => x.Key == governmentLocation); return(country); }
public void ConfigurationNodeParsesCorrectForEmptyValue() { var retrievedNode = new KeyValueNode("myconfig", new byte[0]); var configNodes = retrievedNode.ParseConfig(new JsonConfigurationParser()); Assert.Equal(configNodes.Count, 1); Assert.Equal(configNodes[0].FullKey, "myconfig"); }
/// <summary> /// returns true if the variable specified in the "which" clause is set and equal to the "value" /// </summary> /// <param name="scope"></param> /// <param name="root"></param> /// <returns></returns> private bool CheckVariable(Scope scope, KeyValueNode root) { var variableInfo = scope.State.GlobalMetadata.Variables; var name = root["which"].Value; var value = root["value"].Value.AsDecimal(); return(variableInfo.ContainsKey(name) && variableInfo[name] == value); }
public GoodState(KeyValueNode root) { var toAdd = Good.BuildGoods(root); foreach (var good in toAdd) { Goods[good.Name] = good; } }
/// <summary> /// handles logical not /// </summary> /// <param name="scope"></param> /// <param name="root"></param> /// <returns></returns> private bool NotHandler(Scope scope, KeyValueNode root) { foreach (var child in root) { return(!EvalCondition(scope, child)); } throw new ApplicationException("A 'NOT' Expression must have a body"); }
public void KeyValueNode_returns_child_nodes(KeyValueNode <string, int> root) { // ACT var result = root.Children(); // ASSERT Assert.Equal(new[] { "leftNode", "rightNode" }, result.Select(n => n.Key)); }
public void KeyValueNode_returns_leaves_nodes(KeyValueNode <string, int> root) { // ACT var result = root.Leaves(); // ASSERT Assert.Equal(new[] { "leftLeaf", "leftRightLeaf", "rightRightLeaf" }, result.Select(n => n.Key)); }
public void KeyValueNode_returns_root_node(KeyValueNode <string, int> root) { // ACT var result = root.DescendantAt(HierarchyPath.Create <string>()); // ASSERT Assert.Same(root, result); }
public string ParseToJSON(KeyValueNode keyValueNode) { // check if you can turn it back into a json var keyValueNodeAsDictionary = JSONParser.ParseToDictionary(keyValueNode); JavaScriptSerializer js = new JavaScriptSerializer(); string keyValueNodeAsJSON = js.Serialize(keyValueNodeAsDictionary); return(keyValueNodeAsJSON); }
public void KeyValueNode_returns_grandchildnodes(KeyValueNode <string, int> root) { // ACT var result = root.DescendantAt(HierarchyPath.Create("leftNode", "leftLeaf")); // ASSERT Assert.Same("leftLeaf", result.Key); }
private void SetGoods(GameState state, KeyValueNode root) { var goodsMap = state.GoodState.Goods; var goods = root["goods_cost"]; foreach (var good in goods) { GoodCost[goodsMap[good.Key]] = good.Value.AsInt(); } }
public void KeyValueNode_is_empty_by_default() { // ARRANGE var node = new KeyValueNode <string, int>(); // ACT & ASSERT Assert.False(node.TryGetKey(out var key)); Assert.False(node.TryGetValue(out var value)); }
private KeyValueNode GetInChain(string key, KeyValueNode node) { for (KeyValueNode n = node; n != null; n = n.NextNode) { if (key.Equals(n.Key)) { return(n); } } return(null); }
/// <summary> /// builds the goods /// </summary> /// <param name="node">the node to build the goods from</param> /// <returns>a list of goods</returns> public static List <Good> BuildGoods(KeyValueNode node) { var result = new List <Good>(); foreach (var child in node) { result = result.Concat(BuildGoodSection(child)).ToList(); } return(result); }
private void SetupTreeView(KeyValueNode keyValueNode) { // setup categories list var keyValueNodes = new List <KeyValueNode>() { keyValueNode, }; viewModel = new KeyValueNodeContainerViewModel(keyValueNodes); NewView.DataContext = viewModel; }
public void KeyValueNode_returns_descendantAndSelf_nodes(KeyValueNode <string, int> root) { // ACT var result = root.DescendantsAndSelf().ToArray(); // ASSERT Assert.False(result.ElementAt(0).HasKey); Assert.Equal(new[] { "leftNode", "rightNode", "leftLeaf", "leftRightLeaf", "rightRightLeaf" }, result.Skip(1).Select(n => n.Key)); }
/// <summary> /// handles logical and /// </summary> /// <param name="scope">the scope</param> /// <param name="root">the key value node</param> /// <returns>anding the conditions together</returns> private bool AndHandler(Scope scope, KeyValueNode root) { var toReturn = true; foreach (var child in root) { toReturn &= EvalCondition(scope, child); } return(toReturn); }
/// <summary> /// handles logical or /// </summary> /// <param name="scope"></param> /// <param name="root"></param> /// <returns></returns> private bool OrHandler(Scope scope, KeyValueNode root) { foreach (var child in root) { if (EvalCondition(scope, child)) { return(true); } } return(false); }