@@ -442,7 +442,7 @@ public void Add(TKey key, TValue value)
442
442
// Decrease the number of free slots.
443
443
_freeSlotsCount -- ;
444
444
}
445
- else if ( _hashTableStore [ hashcode ] != null && _hashTableStore [ hashcode ] . Count > 0 )
445
+ else if ( _hashTableStore [ hashcode ] . Count > 0 )
446
446
{
447
447
if ( _hashTableStore [ hashcode ] . ContainsKey ( key ) == true )
448
448
throw new ArgumentException ( "Key already exists in the hash table." ) ;
@@ -582,7 +582,7 @@ public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
582
582
583
583
int i = arrayIndex ;
584
584
int hashTableIndex = 0 ;
585
- int countOfElements = ( array . Length - arrayIndex ) ;
585
+ var currentChainNode = new DLinkedListNode < TKey , TValue > ( ) ;
586
586
587
587
while ( true )
588
588
{
@@ -591,30 +591,19 @@ public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
591
591
if ( i >= array . Length )
592
592
break ;
593
593
594
- if ( _hashTableStore [ hashTableIndex ] != null && _hashTableStore [ hashTableIndex ] . Count > 0 )
594
+ if ( _hashTableStore [ hashTableIndex ] != null )
595
595
{
596
- if ( _hashTableStore [ hashTableIndex ] . Count == 1 )
596
+ currentChainNode = _hashTableStore [ hashTableIndex ] . Head ;
597
+ while ( currentChainNode != null && i < array . Length )
597
598
{
598
- pair = new KeyValuePair < TKey , TValue > ( _hashTableStore [ hashTableIndex ] . First . Key , _hashTableStore [ hashTableIndex ] . First . Value ) ;
599
+ pair = new KeyValuePair < TKey , TValue > ( currentChainNode . Key , currentChainNode . Value ) ;
599
600
array [ i ] = pair ;
600
601
i ++ ;
601
602
hashTableIndex ++ ;
602
- }
603
- else
604
- {
605
- var headOfChain = _hashTableStore [ hashTableIndex ] . Head ;
606
-
607
- while ( i < array . Length )
608
- {
609
- pair = new KeyValuePair < TKey , TValue > ( headOfChain . Key , headOfChain . Value ) ;
610
- array [ i ] = pair ;
611
- i ++ ;
612
- hashTableIndex ++ ;
613
603
614
- headOfChain = headOfChain . Next ;
615
- }
616
- } //end-if-else
617
- } //end-if
604
+ currentChainNode = currentChainNode . Next ;
605
+ }
606
+ }
618
607
else
619
608
{
620
609
hashTableIndex ++ ;
0 commit comments