Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #include once "crt.bi"
  2.  
  3. #include once "tree.bi"
  4.  
  5. dim shared as node_t ptr tree_root
  6. dim shared as integer tree_size
  7. dim shared as any ptr tree_mutex
  8.  
  9. sub tree_init _
  10. ( _
  11. )
  12.  
  13. tree_mutex = mutexcreate( )
  14.  
  15. end sub
  16.  
  17. sub tree_deinit _
  18. ( _
  19. )
  20.  
  21. mutexdestroy( tree_mutex )
  22.  
  23. end sub
  24.  
  25. function tree_find _
  26. ( _
  27. byval s as zstring ptr _
  28. ) as node_t ptr ptr
  29.  
  30. mutexlock( tree_mutex )
  31.  
  32. dim as node_t ptr ptr node = @tree_root
  33.  
  34. while *node
  35. dim as integer result = strcmp( strptr( (*node)->s ), s )
  36. if result > 0 then
  37. node = @(*node)->l
  38. elseif result < 0 then
  39. node = @(*node)->r
  40. else
  41. exit while
  42. end if
  43. wend
  44.  
  45. function = node
  46.  
  47. mutexunlock( tree_mutex )
  48.  
  49. end function
  50.  
  51. sub tree_add _
  52. ( _
  53. byref s as string _
  54. )
  55.  
  56. dim as node_t ptr ptr node = tree_find( s )
  57.  
  58. mutexlock( tree_mutex )
  59.  
  60. if *node = NULL then
  61. *node = callocate( sizeof( node_t ) )
  62. (*node)->s = s
  63. tree_size += 1
  64. end if
  65.  
  66. (*node)->count += 1
  67.  
  68. mutexunlock( tree_mutex )
  69.  
  70. end sub
  71.  
  72. dim shared as node_t ptr ptr list
  73. dim shared as integer list_len
  74.  
  75. sub tree_to_list _
  76. ( _
  77. byval node as node_t ptr _
  78. )
  79.  
  80. if node then
  81. tree_to_list( node->l )
  82. if node->count >= 10 then
  83. list[list_len] = node
  84. list_len += 1
  85. end if
  86. tree_to_list( node->r )
  87. end if
  88.  
  89. end sub
  90.  
  91. function mycmp cdecl _
  92. ( _
  93. byval _l as any ptr, _
  94. byval _r as any ptr _
  95. ) as integer
  96.  
  97. dim as node_t ptr l = *cast( node_t ptr ptr, _l )
  98. dim as node_t ptr r = *cast( node_t ptr ptr, _r )
  99.  
  100. if l->count > r->count then
  101. function = -1
  102. elseif l->count < r->count then
  103. function = 1
  104. end if
  105.  
  106. end function
  107.  
  108. sub tree_output _
  109. ( _
  110. )
  111.  
  112. list = callocate( tree_size * sizeof( node_t ptr ) )
  113.  
  114. tree_to_list( tree_root )
  115.  
  116. qsort( list, list_len, sizeof( node_t ptr ), @mycmp )
  117.  
  118. for i as integer = 0 to list_len - 1
  119. printf( !"%i %s\n", list[i]->count, list[i]->s )
  120. next i
  121.  
  122. deallocate( list )
  123.  
  124. end sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement