Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. -module('1e_sort').
  2.  
  3. -compile(export_all).
  4.  
  5. -define(max_scope, 100000000).
  6. -define(start_step, 1000000).
  7. -define(user_table_num, 10).
  8.  
  9. start(N) ->
  10. ets:new(scope_total_table, [named_table, ordered_set, public]),
  11. [ets:new(erlang:list_to_atom(lists:concat(["user_table_", X])), [named_table, set, public]) || X <- lists:seq(0, ?user_table_num - 1)],
  12. Data = [{lists:concat(["user", X]), rand:uniform(?max_scope)} || X <- lists:seq(1, N)],
  13. [begin
  14. ScopeTableIndex = Scope div ?start_step,
  15. ScopeTableName = erlang:list_to_atom("scope_table_" ++ erlang:integer_to_list(ScopeTableIndex)),
  16. UserTableName = erlang:list_to_atom(lists:concat(["user_table_", erlang:phash2(User, ?user_table_num)])),
  17. ets:insert(UserTableName, {User, Scope}),
  18. case ets:info(ScopeTableName) of
  19. undefined ->
  20. ScopeTableName = ets:new(ScopeTableName, [named_table, ordered_set, public]),
  21. ets:update_counter(scope_total_table, ScopeTableIndex, 1, {ScopeTableIndex, 0}),
  22. ets:update_counter(ScopeTableName, Scope, 1, {Scope, 0}),
  23. ok;
  24. _ ->
  25. ets:update_counter(scope_total_table, ScopeTableIndex, 1, {ScopeTableIndex, 0}),
  26. ets:update_counter(ScopeTableName, Scope, 1, {Scope, 0}),
  27. ok
  28. end
  29. end || {User, Scope} <- Data],
  30.  
  31. UserX = lists:concat(["user", rand:uniform(N)]),
  32. [{_, UserScope}] = ets:lookup(erlang:list_to_atom(lists:concat(["user_table_", erlang:phash2(UserX, ?user_table_num)])), UserX),
  33. ScopeTableIndex = UserScope div ?start_step,
  34. ScopeTableName = erlang:list_to_atom("scope_table_" ++ erlang:integer_to_list(ScopeTableIndex)),
  35. {timer:tc(fun() -> topN(ScopeTableName, UserScope) + topNN(scope_total_table, ScopeTableIndex) end)}.
  36. % listtopN(UserScope, Data),
  37. % ets:tab2list(scope_total_table)}.
  38.  
  39. topN(OrderedSetTable, Key) ->
  40. case ets:next(OrderedSetTable, Key) of
  41. '$end_of_table' ->
  42. 1;
  43. NewKey ->
  44. 1 + topN(OrderedSetTable, NewKey)
  45. end.
  46.  
  47. topNN(Table, Key) ->
  48. case ets:next(Table, Key) of
  49. '$end_of_table' ->
  50. 0;
  51. NewKey ->
  52. [{NewKey, Num}] = ets:lookup(Table, NewKey),
  53. Num + topNN(Table, NewKey)
  54. end.
  55.  
  56. listtopN(Key, List) ->
  57. erlang:length([X || {_, X} <- List, X > Key]) + 1.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement