zoukankan      html  css  js  c++  java
  • Erlang Queue

    ]}
    7> queue:in(f,v(6)).
    {[f,e,d,c,b],[a]}
    8> queue:in(g,v(7)).
    {[g,f,e,d,c,b],[a]}
    9> 
    复制代码

      

      出队列通常复杂度也是咋O(1),最差的情况是O(len(Q));对于RearList和FrontList都有数据的情况下,取出一个数据元素仅仅是从FrontList中取出头元素,所以时间复杂度也是1.如果恰好取出了FrontList的最后一个元素,就要做前后端数据元素的转移.
    复制代码
    %% O(1) amortized, O(len(Q)) worst case
    out({[],[]}=Q) ->
        {empty,Q};
    out({[V],[]}) ->
        {{value,V},{[],[]}};
    out({[Y|In],[]}) ->
        [V|Out] = lists:reverse(In, []),
        {{value,V},{[Y],Out}};
    out({In,[V]}) when is_list(In) ->
        {{value,V},r2f(In)};
    out({In,[V|Out]}) when is_list(In) ->
        {{value,V},{In,Out}};
    out(Q) ->
        erlang:error(badarg, [Q]).
    复制代码
    复制代码
    10> queue:out(v(8)).  
    {{value,a},{[g,f],[b,c,d,e]}}
    11> {_,R}=queue:out(v(8)).
    {{value,a},{[g,f],[b,c,d,e]}}
    12> {_,R2}=queue:out(R).   
    {{value,b},{[g,f],[c,d,e]}}
    13> {_,R3}=queue:out(R2).
    {{value,c},{[g,f],[d,e]}}
    14> {_,R4}=queue:out(R3).
    {{value,d},{[g,f],[e]}}
    15> 
    复制代码
     
      类似的情况还有get方法,大多数情况下复杂度是O(1),最差的情况下需要去RearList的最后一个元素,时间复杂度是O(len(Q)).
    复制代码
    %% O(1) since the queue is supposed to be well formed
    get({[],[]}=Q) ->
        erlang:error(empty, [Q]);
    get({R,F}) when is_list(R), is_list(F) ->
        get(R, F);
    get(Q) ->
        erlang:error(badarg, [Q]).
    
    get(R, [H|_]) when is_list(R) ->
        H;
    get([H], []) ->
        H;
    get([_|R], []) -> % malformed queue -> O(len(Q))
        lists:last(R).
    复制代码

      

     逆转整个队列这样的操作时间复杂度也是O(1),因为只需要把前后段两个List互换就可以了;
    复制代码
    %% O(1)
    reverse({R,F}) when is_list(R), is_list(F) ->
        {F,R};
    reverse(Q) ->
        erlang:error(badarg, [Q]).
    复制代码

    三种API

        queue是双端数据结构.从front端进入,从rear端出.模块的接口比较丰富,分为: "Original API", the "Extended API" and the "Okasaki API".Original API 和 Extended API 接口都可以用队列进出模型去理解,其中进行了reverse操作都会添加"_r"的后缀.
     
    官方文档中的说法:

    The "Original API" item removal functions return compound terms with both the removed item and the resulting queue. The "Extended API" contain alternative functions that build less garbage as well as functions for just inspecting the queue ends. Also the "Okasaki API" functions build less garbage.

    The "Okasaki API" is inspired by "Purely Functional Data structures" by Chris Okasaki. It regards queues as lists. The API is by many regarded as strange and avoidable. For example many reverse operations have lexically reversed names, some with more readable but perhaps less understandable aliases.
    Learn you some Erlang的解释:
    Original API
    The original API contains the functions at the base of the queue concept, including: new/0, for creating empty queues, in/2, for inserting new elements, out/1, for removing elements, and then functions to convert to lists, reverse the queue, look if a particular value is part of it, etc.
    Extended API
    The extended API mainly adds some introspection power and flexibility: it lets you do things such as looking at the front of the queue without removing the first element (see get/1 orpeek/1), removing elements without caring about them (drop/1), etc. These functions are not essential to the concept of queues, but they're still useful in general.
    Okasaki API
    The Okasaki API is a bit weird. It's derived from Chris Okasaki's Purely Functional Data Structures. The API provides operations similar to what was available in the two previous APIs, but some of the function names are written backwards and the whole thing is relatively peculiar. Unless you do know you want this API, I wouldn't bother with it.
     晚安! 
     
     
     
    分类: Erlang
  • 相关阅读:
    Java中获取键盘输入值的三种方法
    java多线程处理与数据集合
    Apachetomcat学习
    Java之枚举
    多例设计模式与枚举
    权限管理(数据库部分)
    hdu 2458(最大独立集)
    hdu 1507(最大匹配)
    hdu 1350+hdu 1960(最小路径覆盖)
    hdu 1845(最大匹配)
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2584535.html
Copyright © 2011-2022 走看看