//========================== EXTENSION ==========================// private static void AddToHead( ref BucketValue bucketVal, ref MModelNativeTextMMFBase.Pair pair ) { bucketVal.NextBucket = new BucketRef() { Language = pair.Language, Weight = pair.Weight, NextBucket = bucketVal.NextBucket, }; }
/*public bool TryAddToEndOfChain( ref LanguageNativeTextMMFConfig.Pair pair ) { int i = FindEntry( pair.TextPtr ); if ( i >= 0 ) { AddToHead( ref _Entries[ i ].value, ref pair ); return (true); } return (false); }*/ unsafe internal void AddNewOrToEndOfChain( ref MModelNativeTextMMFBase.Pair pair ) { int hashCode = _Comparer.GetHashCode( pair.TextPtr ) & 0x7FFFFFFF; int targetBucket = hashCode % _Buckets.Length; #region [.try merge with exists.] for ( int i = _Buckets[ targetBucket ]; i >= 0; i = _Entries[ i ].next ) { if ( _Entries[ i ].hashCode == hashCode && _Comparer.Equals( _Entries[ i ].key, pair.TextPtr ) ) { //add to end of chain AddToHead( ref _Entries[ i ].value, ref pair ); return; } } #endregion #region [.add new.] int index; if ( _FreeCount > 0 ) { index = _FreeList; _FreeList = _Entries[ index ].next; _FreeCount--; } else { if ( _Count == _Entries.Length ) { Resize(); targetBucket = hashCode % _Buckets.Length; } index = _Count; _Count++; } var textPtr = StringsHelper.AllocHGlobalAndCopy( pair.TextPtr, pair.TextLength ); _Entries[ index ].hashCode = hashCode; _Entries[ index ].next = _Buckets[ targetBucket ]; _Entries[ index ].key = textPtr; _Entries[ index ].value = new BucketValue( pair.Language, pair.Weight ); _Buckets[ targetBucket ] = index; #endregion }
unsafe private void LoadMMFCallbackRoutine(ref MModelNativeTextMMFBase.Pair pair) { BucketValue bucketVal; if (Dictionary.TryGetValue(pair.TextPtr, out bucketVal)) { var bucketRef = new BucketRef() { Language = pair.Language, Weight = pair.Weight, NextBucket = bucketVal.NextBucket, }; bucketVal.NextBucket = bucketRef; Dictionary[pair.TextPtr] = bucketVal; #region commented. previous /* * var bucketRef = new BucketRef() { Language = pair.Language, Weight = pair.Weight }; * if ( bucketVal.NextBucket == null ) * { * bucketVal.NextBucket = bucketRef; * * DictionaryIntptr[ pair.TextPtr ] = bucketVal; * } * else * { * var br = bucketVal.NextBucket; * for (; br.NextBucket != null; br = br.NextBucket ); * br.NextBucket = bucketRef; * } */ #endregion } else { var textPtr = StringsHelper.AllocHGlobalAndCopy(pair.TextPtr, pair.TextLength); Dictionary.Add(textPtr, new BucketValue(pair.Language, pair.Weight)); } }