IDictionaryEnumerator approach

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Wavemaker

    #1

    IDictionaryEnumerator approach

    I'm writing a class that implements the IDictionary interface. Along
    with this, I'm implementing an IDictionaryEnum erator class. For the
    IDictionaryEnum erator, the documentation says the following:

    "An enumerator remains valid as long as the collection remains
    unchanged. If changes are made to the collection, such as adding,
    modifying or deleting elements, the enumerator is irrecoverably
    invalidated and the next call to MoveNext or Reset throws an
    InvalidOperatio nException."

    So any IDictionaryEnum erator object needs to be notified when its
    related collection is changed in some way.

    I was thinking about implementing this using an event. Whenever the
    collection is changed, it triggers the event. Each time an
    IDictionaryEnum erator is created, it registers itself with the
    event so that it will be notified when its collection has changed
    and can invalidate itself.

    When an IDictionaryEnum erator recieves notification that the
    collection has changed, it sets an flag to indicate that it's
    invalid and also removes itself from the collection's invalidate
    event so that it will not be notified more than once.

    Make sense?



  • Jon Skeet

    #2
    Re: IDictionaryEnum erator approach

    Wavemaker <jabberdabber@N OSPAMhotmail.co m> wrote:
    <snip>
    [color=blue]
    > Make sense?[/color]

    Absolutely. Alternatively, the collection could keep a version number
    which the enumerator checked on each call.

    --
    Jon Skeet - <skeet@pobox.co m>
    Pobox has been discontinued as a separate service, and all existing customers moved to the Fastmail platform.

    If replying to the group, please do not mail me too

    Comment

    • Peter Rilling

      #3
      Re: IDictionaryEnum erator approach

      You could do this but there may be a more elegant way. This is the way the
      ..NET collections and dictionaries work.

      Each collection has a version variable (no relation to the assembly
      versions). When the collection changes, the version is incremented. So the
      version might start at zero, you add an element, now the version is one, and
      so on.

      When you instantiate the IDictionaryEnum erator, you could pass in the
      dictionary instance and the enumerator could record the version for later
      reference.

      Now, on each access of the enumerator you would check the version from when
      the enumerator was created with the current version on the dictionary
      object. If they are the same, no changes have been made. A lot less
      overhead than firing events.

      "Wavemaker" <jabberdabber@N OSPAMhotmail.co m> wrote in message
      news:lhwYa.5840 6$cF.20055@rwcr nsc53...[color=blue]
      > I'm writing a class that implements the IDictionary interface. Along
      > with this, I'm implementing an IDictionaryEnum erator class. For the
      > IDictionaryEnum erator, the documentation says the following:
      >
      > "An enumerator remains valid as long as the collection remains
      > unchanged. If changes are made to the collection, such as adding,
      > modifying or deleting elements, the enumerator is irrecoverably
      > invalidated and the next call to MoveNext or Reset throws an
      > InvalidOperatio nException."
      >
      > So any IDictionaryEnum erator object needs to be notified when its
      > related collection is changed in some way.
      >
      > I was thinking about implementing this using an event. Whenever the
      > collection is changed, it triggers the event. Each time an
      > IDictionaryEnum erator is created, it registers itself with the
      > event so that it will be notified when its collection has changed
      > and can invalidate itself.
      >
      > When an IDictionaryEnum erator recieves notification that the
      > collection has changed, it sets an flag to indicate that it's
      > invalid and also removes itself from the collection's invalidate
      > event so that it will not be notified more than once.
      >
      > Make sense?
      >
      >
      >[/color]


      Comment

      • Jon

        #4
        Re: IDictionaryEnum erator approach

        Too much work. Try:

        Adding a version field to the collection & enumerator.
        Increment the collection version on every change.
        Set the enumerator version during construction.
        Now just compare versions in MoveNext or Reset.

        "Wavemaker" <jabberdabber@N OSPAMhotmail.co m> wrote in message news:lhwYa.5840 6$cF.20055@rwcr nsc53...[color=blue]
        > I'm writing a class that implements the IDictionary interface. Along
        > with this, I'm implementing an IDictionaryEnum erator class. For the
        > IDictionaryEnum erator, the documentation says the following:
        >
        > "An enumerator remains valid as long as the collection remains
        > unchanged. If changes are made to the collection, such as adding,
        > modifying or deleting elements, the enumerator is irrecoverably
        > invalidated and the next call to MoveNext or Reset throws an
        > InvalidOperatio nException."
        >
        > So any IDictionaryEnum erator object needs to be notified when its
        > related collection is changed in some way.
        >
        > I was thinking about implementing this using an event. Whenever the
        > collection is changed, it triggers the event. Each time an
        > IDictionaryEnum erator is created, it registers itself with the
        > event so that it will be notified when its collection has changed
        > and can invalidate itself.
        >
        > When an IDictionaryEnum erator recieves notification that the
        > collection has changed, it sets an flag to indicate that it's
        > invalid and also removes itself from the collection's invalidate
        > event so that it will not be notified more than once.
        >
        > Make sense?
        >
        >
        >[/color]


        Comment

        • Wavemaker

          #5
          Re: IDictionaryEnum erator approach


          "Peter Rilling" wrote:

          <snip>
          [color=blue]
          > Now, on each access of the enumerator you would
          > check the version from when the enumerator was
          > created with the current version on the dictionary
          > object. If they are the same, no changes have
          > been made. A lot less overhead than firing events.[/color]

          This is much more elegant. Thank you! And thanks to the others who
          have made the same suggestion.


          Comment

          Working...