T
timor.super
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.
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.