tbtrees.nim 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. discard """
  2. output: '''
  3. www.amazon.com: 207.171.182.16
  4. www.apple.com: 17.112.152.32
  5. www.cnn.com: 64.236.16.20
  6. www.cs.princeton.edu: abc
  7. www.dell.com: 143.166.224.230
  8. www.ebay.com: 66.135.192.87
  9. www.espn.com: 199.181.135.201
  10. www.google.com: 216.239.41.99
  11. www.microsoft.com: 207.126.99.140
  12. www.nytimes.com: 199.239.136.200
  13. www.princeton.edu: 128.112.128.15
  14. www.simpsons.com: 209.052.165.60
  15. www.slashdot.org: 66.35.250.151
  16. www.weather.com: 63.111.66.11
  17. www.yahoo.com: 216.109.118.65
  18. www.yale.edu: 130.132.143.21
  19. '''
  20. """
  21. import compiler/btrees
  22. import random, tables
  23. proc main =
  24. var st = initBTree[string, string]()
  25. st.add("www.cs.princeton.edu", "abc")
  26. st.add("www.princeton.edu", "128.112.128.15")
  27. st.add("www.yale.edu", "130.132.143.21")
  28. st.add("www.simpsons.com", "209.052.165.60")
  29. st.add("www.apple.com", "17.112.152.32")
  30. st.add("www.amazon.com", "207.171.182.16")
  31. st.add("www.ebay.com", "66.135.192.87")
  32. st.add("www.cnn.com", "64.236.16.20")
  33. st.add("www.google.com", "216.239.41.99")
  34. st.add("www.nytimes.com", "199.239.136.200")
  35. st.add("www.microsoft.com", "207.126.99.140")
  36. st.add("www.dell.com", "143.166.224.230")
  37. st.add("www.slashdot.org", "66.35.250.151")
  38. st.add("www.espn.com", "199.181.135.201")
  39. st.add("www.weather.com", "63.111.66.11")
  40. st.add("www.yahoo.com", "216.109.118.65")
  41. doAssert st.getOrDefault("www.cs.princeton.edu") == "abc"
  42. doAssert st.getOrDefault("www.harvardsucks.com") == ""
  43. doAssert st.getOrDefault("www.simpsons.com") == "209.052.165.60"
  44. doAssert st.getOrDefault("www.apple.com") == "17.112.152.32"
  45. doAssert st.getOrDefault("www.ebay.com") == "66.135.192.87"
  46. doAssert st.getOrDefault("www.dell.com") == "143.166.224.230"
  47. doAssert(st.len == 16)
  48. for k, v in st:
  49. echo k, ": ", v
  50. when false:
  51. var b2 = initBTree[string, string]()
  52. const iters = 10_000
  53. for i in 1..iters:
  54. b2.add($i, $(iters - i))
  55. for i in 1..iters:
  56. let x = b2.getOrDefault($i)
  57. if x != $(iters - i):
  58. echo "got ", x, ", but expected ", iters - i
  59. echo b2.len
  60. when true:
  61. var b2 = initBTree[int, string]()
  62. var t2 = initTable[int, string]()
  63. const iters = 100
  64. for i in 1..iters:
  65. let x = rand(high(int))
  66. if not t2.hasKey(x):
  67. doAssert b2.getOrDefault(x).len == 0, " what, tree has this element " & $x
  68. t2[x] = $x
  69. b2.add(x, $x)
  70. doAssert(b2.len == t2.len, "unique entries " & $b2.len)
  71. for k, v in t2:
  72. doAssert $k == v
  73. doAssert b2.getOrDefault(k) == $k
  74. main()