Collections challenge (MRU)

C

CMM

First let me say that maybe I'm having a "duh" moment and perhaps I'm
missing something... but it seems to me that no one thing in the
System.Collections namespace (even in .NET 2.0) even comes close to the
still-useful-today VB intrinsic Collection. Here's the challenge (I know I'm
totally missing something here)....

Implement a full featured MRU (Most Recently Used) list using
System.Collections.

Use Case:
1) Current MRU: MyNotes.txt, MySpreadsheet1.txt, SomeOtherFile.txt
2) User opens "myspreadsheet1.txt" (note the case... the file was renamed
sometime between step 1and2
3) New MRU: MyNotes.txt, SomeOtherFile.txt, *myspreadsheet1.txt*

Requirements:

1) Items must be stored in their original case, but searches on the
collection must be case-insensitive. No storing items using ToLower()
cheating crap to work around the framework's case-sensitive collections.

2) Items must be stored oldest to newest (or newest to oldest... it doesn't
really matter).

3) When adding a new item (case preserved), a matching item (case
insensitive) already in the collection gets popped out.
Well, Dictionary(Of String) seems to be the answer.... as the default
comparator for Key is case-insensitive...
MRUDictionary.Remove(file) <-- case insensitive
MRUDictionary.Add(file, file) <-- new case preserved

BUT...

4) The collection should allow access by index AND it must be "trimmable."
Sub TrimMRU()
If MRU.Count > 10 Then
Dim delta As Integer = MRU.Count - 10
For tally As Integer = 1 To delta
MRU.RemoveAt(0) '<-- oldest items removed
Next tally
End If
End Sub
Dictionary does not support this. You cannot access items "by index."

Anyone have an answer? This is easily done using "Collection"...... but is
there no "framework" equivalent without doing a lot of "kludgy" code?
 
C

CMM

Oh and the collection must be serializable (like, able to be stored in
My.Settings).

Since the original post, I decided to just "forget about it" and go with the
intrinsic VB Collection object... only to find that it's not serializable...
so the values can't be stored in My.Settings. Sure, I can write an algorithm
to convert it... but, I'm looking for an easy non-kludgy solution.
 
C

Cor Ligthert [MVP]

Carlos,

Even Herfried agrees that the old VB collection should have been placed in
the VB compatible namespace instead in the normal VB namespace. It should
not been used. It is so much different from any other collection in Net
(which AFAIK implements all ICollection) that it is embarrassing that the
names are kept the same.

Just my thought,

Cor

CMM said:
Oh and the collection must be serializable (like, able to be stored in
My.Settings).

Since the original post, I decided to just "forget about it" and go with
the intrinsic VB Collection object... only to find that it's not
serializable... so the values can't be stored in My.Settings. Sure, I can
write an algorithm to convert it... but, I'm looking for an easy
non-kludgy solution.
 
C

CMM

How so? I would agree if there was an alternative with all the same
features. So far, from what I have seen, there is NOT ONE framework-based
collection that does what VB-Collection does... as it is a hybrid of
Dictionary and List. Dictionary does not maintain FIFO order... and List is
not Keyed.

Maybe I'm missing something? :)

Hey Cor... hotshot... solve the MRU problem. ;-)

--
-C. Moya
www.cmoya.com
Cor Ligthert said:
Carlos,

Even Herfried agrees that the old VB collection should have been placed in
the VB compatible namespace instead in the normal VB namespace. It should
not been used. It is so much different from any other collection in Net
(which AFAIK implements all ICollection) that it is embarrassing that the
names are kept the same.

Just my thought,

Cor
 
C

Cor Ligthert [MVP]

Carlos,

I have not your problem complete in mind, however are you sure that the
SortedList does not solve your problem. In your text is so much that is as
well written in the description for this class. (I never have used it yet)
You have an overloaded amount of questions so maybe can you do that with
that class by inheriting it.

http://msdn.microsoft.com/library/d...rlrfsystemcollectionssortedlistclasstopic.asp

Although your problem is majory sounds in my opinion a stack do I believe
that you cannot use that or you should add very much to it.

http://msdn.microsoft.com/library/d...tml/frlrfsystemcollectionsstackclasstopic.asp


I hope this helps something,

Cor
 
C

CMM

6 years later and the Framework still falls short of stuff that was so easy
in VB.Classic.

SortedList: Definately not. FIFO order is not maintained.
Stack: Doesn't provide a way to remove duplicate values. Aside from
Contains() being case-sensitive, you still have NO way to pop out a
duplicate that's in the middle of the stack (AFAIK).

Come on... MRU is a simple concept. While my original post was detailed...
it's essentially very simple. Simply study how the Visual Studio MRU list in
the File menu works... or in any app for that matter. Traditionally you
accomplish it via either a lot of brute force array work... or slightly
easiser using VB-Collection.

You SHOULD be able to EASILY accomplish it using at least one of the
collections in .NET but their case-sensitivity (I HATE THAT... not all
collections provide an IComparer option in their constructor) and lack of
indexes makes it extremely difficult.
 
C

CMM

Or to make it even simpler

MRU.Push("Newsgroup")
MRU.Push("Carlos")
MRU.Push("Cor")
MRU.Push("carlos")
=
Newsgroup
Cor
carlos

It MUST work this way. None of that "Why don't you call ToLower before you
add the string?" crap.

At first glance List(Of String) might work.
List.Remove(s)
List.Add(s)
Aaargh case-sensitivity breaks it!!! (i.e. "carlos" does not trump
"Carlos".)

Well, how about one of the dictionaries?
Dictionary.Remove(s)
Dictionary.Add(s, s)
That works!!!! ... because the key in a dictionary is case insensitive.
But now, what if you want to trim the list? Call RemoveAt(0) n-times?
There's no way. There's no "index" access to a Dictionary.
 
L

Larry Lard

CMM said:
First let me say that maybe I'm having a "duh" moment and perhaps I'm
missing something... but it seems to me that no one thing in the
System.Collections namespace (even in .NET 2.0) even comes close to the
still-useful-today VB intrinsic Collection. Here's the challenge (I know I'm
totally missing something here).... [snip]
Anyone have an answer? This is easily done using "Collection"...... but is
there no "framework" equivalent without doing a lot of "kludgy" code?

The framework doesn't claim to have a solution to every problem.
There's nothing stopping you from creating your own collection class
that behaves the way you want it to. I'm sure I can't be the only
person who wrote their own QueueOfT class, pre-2.0. I still use my
DequeueOfT classes, although now of course I have re implemented as
Dequeue(Of T). What I didn't do was rant about the framework not
providing a double ended queue (I mean, isn't this ESSENTIAL??!??!?!!)
for me.
 
C

CMM

It wasn't a rant. It was a challenge (using System.Collections)... And MRU
is not an uncommon programming challenge... in fact, the new
TextBox/ComboBox "AutoComplete" features use it. And if any of your apps do
FileNew/Open/Save stuff... so should you.

And there is indeed a simple solution for the rest of us (without writing
oodles of code). The VB intrinsic Collection object. I guess MS didn't put
it in the "Compatibility" namespace graveyard for a reason. That is because
it's still useful.

Its serialization was easy too. Simply loop through it and dump it into a
StringCollection (which is serializable) for "My.Settings" use or anything
else that requires serialization.


--
-C. Moya
www.cmoya.com
Larry Lard said:
First let me say that maybe I'm having a "duh" moment and perhaps I'm
missing something... but it seems to me that no one thing in the
System.Collections namespace (even in .NET 2.0) even comes close to the
still-useful-today VB intrinsic Collection. Here's the challenge (I know
I'm
totally missing something here).... [snip]
Anyone have an answer? This is easily done using "Collection"...... but
is
there no "framework" equivalent without doing a lot of "kludgy" code?

The framework doesn't claim to have a solution to every problem.
There's nothing stopping you from creating your own collection class
that behaves the way you want it to. I'm sure I can't be the only
person who wrote their own QueueOfT class, pre-2.0. I still use my
DequeueOfT classes, although now of course I have re implemented as
Dequeue(Of T). What I didn't do was rant about the framework not
providing a double ended queue (I mean, isn't this ESSENTIAL??!??!?!!)
for me.
 
C

CMM

And I would also note, that this would be easy if the Framework collections
made consistent use of IComparer. Some collection types allow it to be
passed in the constructure and some don't. Most notably (and stupidly)
StringCollection does not.... so the nifty CaseInsensitiveComparer sits
there not being used where it is most useful.

Now that IS a rant!

--
-C. Moya
www.cmoya.com
CMM said:
It wasn't a rant. It was a challenge (using System.Collections)... And
MRU is not an uncommon programming challenge... in fact, the new
TextBox/ComboBox "AutoComplete" features use it. And if any of your apps
do FileNew/Open/Save stuff... so should you.

And there is indeed a simple solution for the rest of us (without writing
oodles of code). The VB intrinsic Collection object. I guess MS didn't put
it in the "Compatibility" namespace graveyard for a reason. That is
because it's still useful.

Its serialization was easy too. Simply loop through it and dump it into a
StringCollection (which is serializable) for "My.Settings" use or anything
else that requires serialization.


--
-C. Moya
www.cmoya.com
Larry Lard said:
First let me say that maybe I'm having a "duh" moment and perhaps I'm
missing something... but it seems to me that no one thing in the
System.Collections namespace (even in .NET 2.0) even comes close to the
still-useful-today VB intrinsic Collection. Here's the challenge (I know
I'm
totally missing something here).... [snip]
Anyone have an answer? This is easily done using "Collection"...... but
is
there no "framework" equivalent without doing a lot of "kludgy" code?

The framework doesn't claim to have a solution to every problem.
There's nothing stopping you from creating your own collection class
that behaves the way you want it to. I'm sure I can't be the only
person who wrote their own QueueOfT class, pre-2.0. I still use my
DequeueOfT classes, although now of course I have re implemented as
Dequeue(Of T). What I didn't do was rant about the framework not
providing a double ended queue (I mean, isn't this ESSENTIAL??!??!?!!)
for me.
 
G

Guest

CMM,

Would the NameValueCollection (or one that you create from
NameObjectCollectionBase) work for you?

Kerry Moorman
 
C

CMM

Actually, I think it might! When I first reviewed it, I dismissed it because
I couldn't see a way of truncating the collection when necessary (usually
accomplished via RemoveAt(0))
But it is possible with the NameValueCollection using this (somewhat
unintuitive) technique:

col.Remove(col.Keys.Item(0))

I knew I was missing something.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top