Improve an IEnumerable function

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi group,

imagine I want to count the number of a word in a text. See my actual
code (don't pay attention to the int factor) :

List<KeyValuePair<string, int>> listThingsToFind = new
List<KeyValuePair<string, int>>();
listThingsToFind.Add(new KeyValuePair<string, int>("error", 10));
listThingsToFind.Add(new KeyValuePair<string, int>("value", 50));
MyCSharp2Class myClass = new MyCSharp2Class(listThingsToFind, data);
foreach (KeyValuePair<int, string> akp in myClass.GetValues())
{
}

///// ............


class MyCSharp2Class
{
private readonly List<KeyValuePair<string, int>> _list;
private readonly string _data;

public MyCSharp2Class(List<KeyValuePair<string, int>>
listThingsToFind, string data)
{
_list = listThingsToFind;
_data = data;
}

public IEnumerable<KeyValuePair<int, string>> GetValues()
{
foreach (KeyValuePair<string, int> aThingToFind in
_list)
{
string savData = _data;
int cpt = 0;
int pos = savData.IndexOf(aThingToFind.Key,
StringComparison.InvariantCultureIgnoreCase);
while (savData.Length > 0 && pos >= 0)
{
cpt++;
savData = savData.Substring(pos +
aThingToFind.Key.Length);
pos = savData.IndexOf(aThingToFind.Key,
StringComparison.InvariantCultureIgnoreCase);
}
yield return new KeyValuePair<int, string>(cpt *
aThingToFind.Value, aThingToFind.Key);
}
}
}

I'm using the yield return functionnality to construct my IEnumerable
object. This is working well, but imagine I have to enum twice,
with :

foreach (KeyValuePair<int, string> akp in myClass.GetValues())
{
}
foreach (KeyValuePair<int, string> akp in myClass.GetValues())
{
}

I'm parsing the data string 2 times, even if I've already done it one
time and there's no need to parse one time more.
I would like to optimize my IEnumerable function, and I had the idea
to construct an intern IEnumerable object (a list for example) to
store the result, and If the result is already stored, return only the
list.

Something like this :

class MyCSharp2Class
{
private readonly List<KeyValuePair<string, int>> _list;
private readonly string _data;
private List<KeyValuePair<int, string>>
_internIenumerable;

public MyCSharp2Class(List<KeyValuePair<string, int>>
listThingsToFind, string data)
{
_list = listThingsToFind;
_data = data;
}

public IEnumerable<KeyValuePair<int, string>> GetValues()
{
if (_internIenumerable == null)
{
_internIenumerable = new List<KeyValuePair<int,
string>>();
foreach (KeyValuePair<string, int> aThingToFind in
_list)
{
string savData = _data;
int cpt = 0;
int pos = savData.IndexOf(aThingToFind.Key,
StringComparison.InvariantCultureIgnoreCase);
while (savData.Length > 0 && pos >= 0)
{
cpt++;
savData = savData.Substring(pos +
aThingToFind.Key.Length);
pos = savData.IndexOf(aThingToFind.Key,
StringComparison.InvariantCultureIgnoreCase);
}
KeyValuePair<int, string> aValue = new
KeyValuePair<int, string>(cpt*aThingToFind.Value, aThingToFind.Key);
_internIenumerable.Add(aValue);
yield return aValue;
}
}
else
return _internIenumerable;
}
}

but, I can't return a list for this function.

Do you have an idea what to do ? Of course, I could store the first
enum in the main function, but that's not my purpose ...

Thanks for your help,

Best regards,
S.

.



Relevant Pages

  • Re: Brian Kernighan, maybe Im not worthy, maybe Im scum
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • RE: Controling Modal Dialogs (Solution)
    ... doesn't return until the 'modal' browser returns. ... string varOptions) ... public void DocumentComplete ... int rc = winDisp.Invoke(rgDispId, ref guid, 0, ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • Gcc compatible header file
    ... A string collection is a table of zero terminated strings that will grow ... typedef struct _StringCollection StringCollection; ... int; ... bool; ...
    (comp.lang.c)
  • Kernighan and Pikes "Beautiful" Code
    ... conformant string. ... int repeats, reps; ... ref satisfierLength); ...
    (comp.programming)
  • Re: FTP CD command
    ... public const int GENERIC_WRITE = 0x40000000; ... string lpszProxyName, ... public static extern IntPtr InternetConnect ( ... public static extern bool FtpGetCurrentDirectory ( ...
    (microsoft.public.dotnet.languages.vb)