void SetCharacterPriority(char word) { int sortedDataCount = (int)m_sortedData.Count; // If the list is empty, just create a wcharData and add it into list. if (sortedDataCount == 0) { WcharData pData = new WcharData(1, word); m_sortedData.Add(pData); return; } // Otherwise, try to find it. int iFoundIndex = -1; for (int i = 0; i < sortedDataCount; i++) { WcharData pData = m_sortedData[i] as WcharData; if (pData.m_char == word) { iFoundIndex = i; break; } } if (iFoundIndex != -1) { // The char exists, increase the priority vlaue. WcharData pData = m_sortedData[iFoundIndex] as WcharData; pData.m_priority += 1; } else { WcharData pData = new WcharData(1, word); m_sortedData.Add(pData); } }
//For debugging void DisplaySortedWords() { int count = m_sortedData.Count; Debug.Log("Total words = " + count.ToString()); for (int i = 0; i < count; i++) { WcharData data = m_sortedData[i] as WcharData; Debug.Log("\t Word" + i.ToString() + "[" + data.m_char + " " + data.m_priority.ToString() + "]"); } }
void AddDefaultChars() { short theChar; for (theChar = 32; theChar <= 126; theChar++) { // If the character doesn't allready exists then add it if (!IsCharExists((char)theChar)) { WcharData pData = new WcharData(1, (char)theChar); m_sortedData.Add(pData); } } }
string GetUnicodeString() { int count = m_sortedData.Count; char[] wordList = new char[count]; for (int i = 0; i < count; i++) { WcharData data = m_sortedData[i] as WcharData; wordList[i] = data.m_char; } string str = new string(wordList); return(str); }
//check if a given char exists in the m_sortedData list bool IsCharExists(char theChar) { int count = m_sortedData.Count; for (int i = 0; i < count; i++) { WcharData data = m_sortedData[i] as WcharData; if ((data != null) && (data.m_char == theChar)) { return(true); } } return(false); }
//Quick sort int CompareFunc(WcharData data1, WcharData data2) { if (data1.m_priority == data2.m_priority) { return(0); } else { if (data1.m_priority >= data2.m_priority) { return(-1); } else { return(1); } } }
//sort the data - slow function void SortUniqueData() { int sortedDataCount = (int)m_sortedData.Count; for (int i = 0; i < sortedDataCount - 1; i++) { WcharData pData0 = m_sortedData[i] as WcharData; for (int j = i + 1; j < sortedDataCount; j++) { WcharData pData1 = m_sortedData[j] as WcharData; if (pData0.m_priority < pData1.m_priority) { // Swap m_sortedData[i] = pData1; m_sortedData[j] = pData0; pData0 = pData1; } } } }