Searching for a name

Discussion in 'Ruby' started by Brian Schröder, Oct 19, 2005.

  1. Hello Group,

    I'm currently polishing my priority queue implementation, and I'm in
    need for a name for some methods.

    I have the following behaviour now

    q =3D PriorityQueue.new
    q["0"] =3D 0
    q["1"] =3D 1
    q["2"] =3D 2
    q.delete_min #=3D> ["0", 0]
    q.delete_min_return_key #=3D> "1"
    q.delete_min_return_priority #=3D> 2

    and wonder how I could call the last two methods. I want these,
    because delete_min returns nil if the queue is empty, and I can't
    index nil. So for some code these additional functions are quite
    convenient.

    thanks for the input,

    Brian

    --
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
     
    Brian Schröder, Oct 19, 2005
    #1
    1. Advertising

  2. Brian Schröder wrote:
    > Hello Group,
    >
    > I'm currently polishing my priority queue implementation, and I'm in
    > need for a name for some methods.
    >
    > I have the following behaviour now
    >
    > q = PriorityQueue.new
    > q["0"] = 0
    > q["1"] = 1
    > q["2"] = 2
    > q.delete_min #=> ["0", 0]
    > q.delete_min_return_key #=> "1"
    > q.delete_min_return_priority #=> 2
    >
    > and wonder how I could call the last two methods. I want these,
    > because delete_min returns nil if the queue is empty, and I can't
    > index nil. So for some code these additional functions are quite
    > convenient.


    How about
    #deq
    #deq_key
    #deq_prio

    ?

    Kind regards

    robert
     
    Robert Klemme, Oct 19, 2005
    #2
    1. Advertising

  3. On 19/10/05, Robert Klemme <> wrote:
    > Brian Schr=F6der wrote:
    > > [snip]

    >
    > How about
    > #deq
    > #deq_key
    > #deq_prio
    >
    > ?
    >
    > Kind regards
    >
    > robert
    >


    Hello Robert,

    I assume you use deq as a short for dequeue. In this case I have the
    problem that the name
    dequeue_key would somehow imply that the key was removed while
    deq_prio would imply removing the priority. This does not make sense
    if one knows the datastructure, but I'm not too happy with it.

    Thanks,

    Brian

    --
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
     
    Brian Schröder, Oct 19, 2005
    #3
  4. Brian Schröder wrote:
    > On 19/10/05, Robert Klemme <> wrote:
    >> Brian Schröder wrote:
    >>> [snip]

    >>
    >> How about
    >> #deq
    >> #deq_key
    >> #deq_prio
    >>
    >> ?
    >>
    >> Kind regards
    >>
    >> robert
    >>

    >
    > Hello Robert,
    >
    > I assume you use deq as a short for dequeue. In this case I have the
    > problem that the name
    > dequeue_key would somehow imply that the key was removed while
    > deq_prio would imply removing the priority. This does not make sense
    > if one knows the datastructure, but I'm not too happy with it.


    OTOH: if you know the data structure, you know that the prio is not
    removed. :) Does this method return the prio of the last removed element
    or the current min prio?

    Kind regards

    robert
     
    Robert Klemme, Oct 19, 2005
    #4
  5. On 19/10/05, Robert Klemme <> wrote:
    > Brian Schr=F6der wrote:
    > > On 19/10/05, Robert Klemme <> wrote:
    > >> Brian Schr=F6der wrote:
    > >>> [snip]
    > >>
    > >> How about
    > >> #deq
    > >> #deq_key
    > >> #deq_prio
    > >>
    > >> ?
    > >>
    > >> Kind regards
    > >>
    > >> robert
    > >>

    > >
    > > Hello Robert,
    > >
    > > I assume you use deq as a short for dequeue. In this case I have the
    > > problem that the name
    > > dequeue_key would somehow imply that the key was removed while
    > > deq_prio would imply removing the priority. This does not make sense
    > > if one knows the datastructure, but I'm not too happy with it.

    >
    > OTOH: if you know the data structure, you know that the prio is not
    > removed. :)


    Sorry if I was not clear, english is not my native language. I tried
    to imply exactly what you said.

    > Does this method return the prio of the last removed element
    > or the current min prio?


    "delete_min_return_priority" returns the priority of the element that
    was removed by calling this method.

    >
    > Kind regards
    >
    > robert
    >


    best regards,

    Brian

    --
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
     
    Brian Schröder, Oct 19, 2005
    #5
  6. Brian Schröder

    ES Guest

    Brian Schr=F6der wrote:
    > On 19/10/05, Robert Klemme <> wrote:
    >=20
    >>Brian Schr=F6der wrote:
    >>
    >>>On 19/10/05, Robert Klemme <> wrote:
    >>>
    >>>>Brian Schr=F6der wrote:
    >>>>
    >>>>> [snip]
    >>>>
    >>>>How about
    >>>>#deq
    >>>>#deq_key
    >>>>#deq_prio
    >>>>
    >>>>?
    >>>>
    >>>>Kind regards
    >>>>
    >>>> robert
    >>>>
    >>>
    >>>Hello Robert,
    >>>
    >>>I assume you use deq as a short for dequeue. In this case I have the
    >>>problem that the name
    >>>dequeue_key would somehow imply that the key was removed while
    >>>deq_prio would imply removing the priority. This does not make sense
    >>>if one knows the datastructure, but I'm not too happy with it.

    >>
    >>OTOH: if you know the data structure, you know that the prio is not
    >>removed. :)

    >=20
    >=20
    > Sorry if I was not clear, english is not my native language. I tried
    > to imply exactly what you said.
    >=20
    >=20
    >>Does this method return the prio of the last removed element
    >>or the current min prio?

    >=20
    >=20
    > "delete_min_return_priority" returns the priority of the element that
    > was removed by calling this method.


    I am still not sure :) By 'this method', do you mean delete/dequeue?
    Or does delete_min_return_priority itself delete?

    If it does not delete, then #last_(deleted_)priority is fine; otherwise
    you might do something like #dequeue_by and document the return value.
    It would get excessively verbose to include all that information in the
    method name.

    >>Kind regards
    >>
    >> robert
    >>

    >=20
    >=20
    > best regards,
    >=20
    > Brian


    E
     
    ES, Oct 19, 2005
    #6
  7. On 19/10/05, ES <> wrote:
    > Brian Schr=F6der wrote:
    > > > [snip]

    > >
    > > "delete_min_return_priority" returns the priority of the element that
    > > was removed by calling this method.

    >
    > I am still not sure :) By 'this method', do you mean delete/dequeue?
    > Or does delete_min_return_priority itself delete?
    >
    > If it does not delete, then #last_(deleted_)priority is fine; otherwise
    > you might do something like #dequeue_by and document the return value.
    > It would get excessively verbose to include all that information in the
    > method name.
    >


    delete_min_return_priority deletes the key that has the lowest
    assigned priority and returns this priority. delete_min_return_key
    does the same but returns the key that had the lowest assigned
    priority. Finally delete_min deletes the same key and returns a pair
    [key, priority].

    Maybe the testcases will make it all clear. If I'm not able to express
    myself in english, maybe my ruby is more adequate. All the three tests
    should do the same. Only the return value should differ.

    def test_delete_min
    assert_equal(nil, @q.delete_min, "Empty queue should pop nil")
    @q["n1"] =3D 0
    assert_equal(["n1", 0], @q.delete_min)
    @q["n1"] =3D 0
    @q["n2"] =3D -1
    assert_equal(["n2", -1], @q.delete_min)
    end

    def test_delete_min_return_key
    assert_equal(nil, @q.delete_min_return_key, "Empty queue should pop nil=
    ")
    @q["n1"] =3D 0
    assert_equal("n1", @q.delete_min_return_key)
    @q["n1"] =3D 0
    @q["n2"] =3D -1
    assert_equal("n2", @q.delete_min_return_key)
    end

    def test_delete_min_return_priority
    assert_equal(nil, @q.delete_min_return_priority, "Empty queue
    should pop nil")
    @q["n1"] =3D 0
    assert_equal(0, @q.delete_min_return_priority)
    @q["n1"] =3D 0
    @q["n2"] =3D -1
    assert_equal(-1, @q.delete_min_return_priority)
    end

    best regards,

    Brian

    --
    http://ruby.brian-schroeder.de/

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
     
    Brian Schröder, Oct 19, 2005
    #7
  8. Brian Schröder <> wrote:
    > On 19/10/05, ES <> wrote:
    >> Brian Schröder wrote:
    >>>> [snip]
    >>>
    >>> "delete_min_return_priority" returns the priority of the element
    >>> that was removed by calling this method.

    >>
    >> I am still not sure :) By 'this method', do you mean delete/dequeue?
    >> Or does delete_min_return_priority itself delete?
    >>
    >> If it does not delete, then #last_(deleted_)priority is fine;
    >> otherwise you might do something like #dequeue_by and document the
    >> return value. It would get excessively verbose to include all that
    >> information in the method name.
    >>

    >
    > delete_min_return_priority deletes the key that has the lowest
    > assigned priority and returns this priority.


    Honestly, what would anyone do with this method? Removing the key and
    getting ignoring the prio is fine, but removing an element from the PK
    without knowing which element sounds pretty much useless to me. I would not
    keep it - not even for symmetry reasons...

    > delete_min_return_key
    > does the same but returns the key that had the lowest assigned
    > priority. Finally delete_min deletes the same key and returns a pair
    > [key, priority].


    Both of these seem useful to me. In that case the deq_* approach would make
    sense IMHO.

    Kind regards

    robert
     
    Robert Klemme, Oct 19, 2005
    #8
  9. On 10/19/05, Brian Schr=F6der <> wrote:
    [snip]
    > q.delete_min #=3D> ["0", 0]
    > q.delete_min_return_key #=3D> "1"
    > q.delete_min_return_priority #=3D> 2
    >
    > and wonder how I could call the last two methods. I want these,
    > because delete_min returns nil if the queue is empty, and I can't
    > index nil. So for some code these additional functions are quite
    > convenient.



    Array#shift is a nice name.

    delete_min -> shift
    delete_min_return_key -> shift_key
    delete_min_return_priority -> shift_prio


    --
    Simon Strandgaard
     
    Simon Strandgaard, Oct 19, 2005
    #9
  10. Brian Schröder <> wrote:
    >
    > delete_min_return_priority deletes the key that has the lowest
    > assigned priority and returns this priority. delete_min_return_key
    > does the same but returns the key that had the lowest assigned
    > priority. Finally delete_min deletes the same key and returns a pair
    > [key, priority].


    gsub('delete', 'extract') perhaps

    martin
     
    Martin DeMello, Oct 20, 2005
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?iso-8859-1?B?bW9vcJk=?=
    Replies:
    7
    Views:
    891
    Roedy Green
    Jan 2, 2006
  2. ding feng
    Replies:
    2
    Views:
    2,907
    ding feng
    Jun 25, 2003
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,441
    Joe Smith
    Apr 22, 2007
  4. Jack-2
    Replies:
    3
    Views:
    330
    Jack-2
    Dec 24, 2003
  5. stumblng.tumblr
    Replies:
    1
    Views:
    233
    stumblng.tumblr
    Feb 4, 2008
Loading...

Share This Page