Advertisement
nassss

Untitled

Mar 15th, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. public bool Add(T item)
  2. {
  3. if (Contains(item))
  4. {
  5. return false;
  6. }
  7. int index = Math.Abs(item.GetHashCode() % this.listWithBuckets.Length);
  8.  
  9. if (this.listWithBuckets[index]==null)
  10. {
  11. listWithBuckets[index] = new SingleLinkedList<T>();
  12. }
  13. SingleNode<T> newNnode = new SingleNode<T>(item);
  14. this.listWithBuckets[index].AddLast(newNnode.Value);
  15. this.Count++;
  16.  
  17. if (this.Count + 1 / this.listWithBuckets.Length > LoadFactor)
  18. {
  19. SingleLinkedList<T>[] newHashSet = new SingleLinkedList<T>[2*listLength]; //initial cap?
  20. foreach (var singleLinkedList in this.listWithBuckets)
  21. {
  22. foreach (var node in singleLinkedList)
  23. {
  24. int newIndex = Math.Abs(node.GetHashCode() % this.listWithBuckets.Length);
  25.  
  26. if (newHashSet[newIndex] != null)
  27. {
  28. newHashSet[newIndex].AddLast(node);
  29. }
  30. else
  31. {
  32. newHashSet[newIndex] = new SingleLinkedList<T>();
  33. newHashSet[newIndex].AddFirst(node);
  34. }
  35. }
  36. }
  37. listLength *= 2;
  38. this.listWithBuckets = newHashSet;
  39. }
  40. return false;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement