コード例 #1
0
        /// <summary>
        /// Create a character frequency dictionary given a string
        /// </summary>
        /// <param name="input"></param>
        /// <returns>A dictionary with the key being characters in the input string and the values
        /// being the number of times the character appears in the input string</returns>
        public static CharacterFrequencyDictionary CreateDictionary(string input)
        {
            var dict = new CharacterFrequencyDictionary();

            foreach (var c in input.ToCharArray())
            {
                if (!dict.ContainsKey(c))
                {
                    dict.Add(c, 0);
                }

                dict[c]++;
            }

            return(dict);
        }
コード例 #2
0
        /// <summary>
        /// create a character frequency dictionary given byte arrays representing the keys
        /// and the values.  Intended to be used with the output of <see cref="GetKeysAsByteArray"/> and
        /// <see cref="GetValuesAsByteArray"/>.
        /// </summary>
        /// <param name="keyArray">The dictionary keys as a byte array from <see cref="GetKeysAsByteArray"/></param>
        /// <param name="valueArray">The dictonary values as a byte array from <see cref="GetKeysAsByteArray"/></param>
        /// <returns></returns>
        public static CharacterFrequencyDictionary CreateDictionary(byte[] keyArray, byte[] valueArray)
        {
            var keys   = Encoding.ASCII.GetString(keyArray).ToCharArray();
            var values = Encoding.ASCII.GetString(valueArray).Split(',').Select(Int32.Parse).ToArray();

            if (keys.Length != values.Length)
            {
                throw new ArgumentException(
                          String.Format("Key array length ({0}) does not match value array length ({1}) ", keys.Length, values.Length));
            }

            var dict = new CharacterFrequencyDictionary();

            for (var i = 0; i < keys.Length; i++)
            {
                dict.Add(keys[i], values[i]);
            }

            return(dict);
        }