private static void ShellSortAscending(SortedListIntPtrKey <Pair[]> .Tuple[] array) { for (int arrayLength = array.Length, gap = (arrayLength >> 1); 0 < gap; gap = (gap >> 1)) { for (int i = 0, len = arrayLength - gap; i < len; i++) { int j = i + gap; SortedListIntPtrKey <Pair[]> .Tuple tmp = array[j]; while (gap <= j) { var k = j - gap; var t = array[k]; if (0 <= SortedListIntPtrKey <Pair[]> .CompareRoutine(tmp.Key, t.Key)) { break; } array[j] = t; j = k; } array[j] = tmp; } } }
private static void MergeSorted(ref SortedListIntPtrKey <Pair[]> .Tuple[] array) { var emptyCount = 0; var i_prev = 0; var t_prev = array[0]; var arrayLength = array.Length; var pair_exists = default(Pair); for (int i = 1; i < arrayLength; i++) { var t_curr = array[i]; //comparing native-strings, given that duplicates have the same addresses if (t_prev.Key == t_curr.Key) /*full comparing native-strings (same addresses & char-to-char compare)*/ /*if ( SortedListIntPtrKey< pair_t[] >.CompareRoutine( t_prev.Key, t_curr.Key ) == 0 )*/ { array[i].Key = IntPtr.Zero; emptyCount++; var pairs = t_curr.Value; for (int j = 0, ln = pairs.Length; j < ln; j++) { var pair = pairs[j]; if (!tempBufferPairs.TryAddOrGetExists(pair, ref pair_exists)) { pair_exists.BaseMorphoForm.AppendMorphoFormEndings(pair.BaseMorphoForm); } } pairs = t_prev.Value; for (int j = 0, ln = pairs.Length; j < ln; j++) { var pair = pairs[j]; if (!tempBufferPairs.TryAddOrGetExists(pair, ref pair_exists)) { pair_exists.BaseMorphoForm.AppendMorphoFormEndings(pair.BaseMorphoForm); } } var pairs_new = TempBufferPairsToArrayAndClear(); array[i_prev].Value = pairs_new; } else { t_prev = t_curr; i_prev = i; } } if (emptyCount != 0) { var array_new = new SortedListIntPtrKey <Pair[]> .Tuple[arrayLength - emptyCount]; for (int i = 0, j = 0; i < arrayLength; i++) { var t_curr = array[i]; if (t_curr.Key != IntPtr.Zero) { array_new[j++] = t_curr; } } array = array_new; } }
/// добавление слова и всех его форм в словарь /// wordBase - marshaled-слово /// morphoType - морфотип /// nounType - тип сущетсвительного public void AddWord(char *wordBase, MorphoTypeNative morphoType, ref MorphoAttributePair?nounType) { var baseMorphoForm = new BaseMorphoFormNative(wordBase, morphoType); for (TreeDictionaryNative _this = this, _this_next; ;) { var first_char = _UPPER_INVARIANT_MAP[*wordBase]; #region [.сохранение характеристик if end-of-word.] if (first_char == '\0') { var len = morphoType.MorphoFormEndingUpperAndMorphoAttributes.Length; SortedListIntPtrKey <Pair[]> .Tuple[] tuples; int tuplesOffset; if (!_this.HasEndings()) { tuplesOffset = 0; tuples = new SortedListIntPtrKey <Pair[]> .Tuple[len]; } else { tuplesOffset = _this._endings.Count; tuples = new SortedListIntPtrKey <Pair[]> .Tuple[len + tuplesOffset]; for (int i = 0; i < tuplesOffset; i++) { tuples[i] = _this._endings.Array[i]; } } for (int i = 0; i < len; i++) { var p = morphoType.MorphoFormEndingUpperAndMorphoAttributes[i]; var pairs_current_len = p.MorphoAttributes.Length; var pairs_current = new Pair[pairs_current_len]; for (int j = 0; j < pairs_current_len; j++) { var ma = MorphoAttributePair.GetMorphoAttribute(morphoType, p.MorphoAttributes[j], ref nounType); pairs_current[j] = new Pair(baseMorphoForm, ma); } tuples[i + tuplesOffset] = new SortedListIntPtrKey <Pair[]> .Tuple() { Key = p.EndingUpper, Value = pairs_current }; } ShellSortAscending(tuples); MergeSorted(ref tuples); _this._endings.SetArray(tuples); return; } #endregion if (!_this._slots.TryGetValue(first_char, out _this_next)) { /// добавление новой буквы _this_next = new TreeDictionaryNative(); _this._slots.Add(first_char, _this_next); } _this = _this_next; wordBase++; } }