1. Patrick Mézard
  2. libphonenumber-csharp

Commits

Patrick Mézard  committed cc0d360

csharp: fix FlyweightMapStorage descriptions packing

Also ensure possibleLengths is correctly sorted.

  • Participants
  • Parent commits 62187f1
  • Branches csharp

Comments (0)

Files changed (3)

File csharp/PhoneNumbers/DefaultMapStorage.cs

View file
  • Ignore whitespace
             var possibleLengthsSet = new HashSet<int>();
             foreach (int prefix in sortedAreaCodeMap.Keys)
             {
-                phoneNumberPrefixes[index++] = prefix;
+                phoneNumberPrefixes[index] = prefix;
+                descriptions[index] = sortedAreaCodeMap[prefix];
+                index++;
                 var lengthOfPrefix = (int)Math.Log10(prefix) + 1;
-                possibleLengths.Add(lengthOfPrefix);
-                if (!possibleLengthsSet.Contains(lengthOfPrefix))
-                {
-                    possibleLengthsSet.Add(lengthOfPrefix);
-                    possibleLengths.Add(lengthOfPrefix);
-                }
+                possibleLengthsSet.Add(lengthOfPrefix);
             }
-            sortedAreaCodeMap.Values.CopyTo(descriptions, 0);
+            possibleLengths.Clear();
+            possibleLengths.AddRange(possibleLengthsSet);
+            possibleLengths.Sort();
         }
     }
 }

File csharp/PhoneNumbers/FlyweightMapStorage.cs

View file
  • Ignore whitespace
         public override void readFromSortedMap(SortedDictionary<int, String> areaCodeMap)
         {
             var descriptionsSet = new HashSet<String>();
-            var descriptionsList = new List<String>();
             numOfEntries = areaCodeMap.Count;
             prefixSizeInBytes = getOptimalNumberOfBytesForValue(areaCodeMap.Keys.Last());
             phoneNumberPrefixes = new ByteBuffer(numOfEntries * prefixSizeInBytes);
                 int prefix = entry.Key;
                 storeWordInBuffer(phoneNumberPrefixes, prefixSizeInBytes, index, prefix);
                 var lengthOfPrefixRef = (int)Math.Log10(prefix) + 1;
-                if (!possibleLengthsSet.Contains(lengthOfPrefixRef))
-                {
-                    possibleLengthsSet.Add(lengthOfPrefixRef);
-                    possibleLengths.Add(lengthOfPrefixRef);
-                }
-                if (!descriptionsSet.Contains(entry.Value))
-                {
-                    descriptionsSet.Add(entry.Value);
-                    descriptionsList.Add(entry.Value);
-                }
+                possibleLengthsSet.Add(lengthOfPrefixRef);
+                descriptionsSet.Add(entry.Value);
                 index++;
             }
-            createDescriptionPool(descriptionsSet, descriptionsList, areaCodeMap);
+            possibleLengths.Clear();
+            possibleLengths.AddRange(possibleLengthsSet);
+            possibleLengths.Sort();
+            createDescriptionPool(descriptionsSet, areaCodeMap);
         }
 
         /**
         * Creates the description pool from the provided set of string descriptions and area code map.
         */
-        private void createDescriptionPool(HashSet<String> descriptionsSet, List<String> descriptionsList,
-            SortedDictionary<int, String> areaCodeMap)
+        private void createDescriptionPool(HashSet<String> descriptionsSet, SortedDictionary<int, String> areaCodeMap)
         {
             // Create the description pool.
-            descIndexSizeInBytes = getOptimalNumberOfBytesForValue(descriptionsList.Count - 1);
+            descIndexSizeInBytes = getOptimalNumberOfBytesForValue(descriptionsSet.Count - 1);
             descriptionIndexes = new ByteBuffer(numOfEntries * descIndexSizeInBytes);
-            descriptionPool = descriptionsList.ToArray();
+            descriptionPool = descriptionsSet.ToArray();
+            Array.Sort(descriptionPool);
 
             // Map the phone number prefixes to the descriptions.
             int index = 0;

File csharp/PhoneNumbers/Properties/AssemblyInfo.cs

View file
  • Ignore whitespace
 //
 // You can specify all the values or you can default the Build and Revision Numbers 
 // by using the '*' as shown below:
-// [assembly: AssemblyVersion("4.9.0.42")]
-[assembly: AssemblyVersion("4.9.0.42")]
-[assembly: AssemblyFileVersion("4.9.0.42")]
+// [assembly: AssemblyVersion("4.9.0.43")]
+[assembly: AssemblyVersion("4.9.0.43")]
+[assembly: AssemblyFileVersion("4.9.0.43")]