I currently have have a HashSet of NElement objects. Each NElement object has a unique Element field, and an integer n.
Here are 2 operations I need to do with the data:
- Iterate over all the values in collection.
- With
Element e, search the collection for an instance ofNElementthat haseand process it.
Here’s an example of #2:
public void Add(NElement ne) {
foreach(NElement ne2 in elements) { //elements is the HashSet
if(ne2.element == ne.element) {
ne2.Number += ne.Number; //Number is the integer
return;
}
}
elements.Add(ne);
}
I think there is a better way to accomplish this using a collection other than a List or Set. Any suggestions?
A possible solution would be a bit of a different design. A molecular formula consists of a bunch of elements along with how many of those elements there are. So a possible solution is to have a
MolecularFormulaclass that wraps this information, which is based in a.Map<Element, int>
A possible example:
This is hastily thrown together and not very efficient at all, but it should give you an idea of a possible option.