Click here to Skip to main content
Click here to Skip to main content
 

Linq-To-XML Style of Node Creation for C++

By , 7 Jun 2012
 
elmax-ver083.zip
Elmax.vsmdi
Elmax
Elmax.vcxproj.user
ElmaxNet
Properties
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
TryoutNet
Properties
Settings.settings
elmax-ver084.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Settings.settings
elmax-ver085.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Settings.settings
elmax-ver086.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Settings.settings
elmax-ver087.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Settings.settings
Elmax-ver089.zip
Elmax-ver089
Elmax.vsmdi
Elmax
Elmax.vcxproj.user
StringUtils
ElmaxNet
Properties
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
TryoutNet
Properties
Settings.settings
ElmaxSrcVer082.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Tryout.ico
Tryout.vcxproj.user
Settings.settings
using System;
using System.Collections.Generic;
using System.Text;

namespace Elmax
{
    /// <summary>
    /// Helper class to join similar elements based on certain criteria
    /// </summary>
    public class HyperElement
    {
        public delegate bool DoubleElementPredicate(Elmax.Element elem1, Elmax.Element elem2);

        /// <summary>
        /// Join one element to another element
        /// </summary>
        /// <param name="listElem1">1st list of elements to join to</param>
        /// <param name="attrName1">Attribute name of the 1st list whose value to be matched. If empty, element value will be used to match instead.</param>
        /// <param name="listElem2">2nd list of elements to join to</param>
        /// <param name="attrName2">Attribute name of the 2nd list whose value to be matched. If empty, element value will be used to match instead.</param>
        /// <param name="caseSensitive">States if the comparision is case-sensitive.</param>
        /// <returns>List of pair of element</returns>
        public static List< KeyValuePair<Elmax.Element, Elmax.Element> >
	        JoinOneToOne(
	            List<Elmax.Element> listElem1,
	            string attrName1,
	            List<Elmax.Element> listElem2,
	            string attrName2,
	            bool caseSensitive)
        {
            string str1, str2;
            List<KeyValuePair<Elmax.Element, Elmax.Element>> vecResults = new List<KeyValuePair<Elmax.Element, Elmax.Element>>();
            for (int i = 0; i < listElem1.Count; ++i)
            {
                if (attrName1 == "")
                    str1 = listElem1[i].GetString("");
                else
                    str1 = listElem1[i].Attribute(attrName1).GetString("");

                for (int j = 0; j < listElem2.Count; ++j)
                {
                    if (attrName2 == "")
                        str2 = listElem2[j].GetString("");
                    else
                        str2 = listElem2[j].Attribute(attrName2).GetString("");

                    if (caseSensitive)
                    {
                        if (str1 == str2)
                        {
                            vecResults.Add(new KeyValuePair<Elmax.Element, Elmax.Element>(listElem1[i], listElem2[j]));
                            break;
                        }
                    }
                    else
                    {
                        if (str1.ToLower() == str2.ToLower())
                        {
                            vecResults.Add(new KeyValuePair<Elmax.Element, Elmax.Element>(listElem1[i], listElem2[j]));
                            break;
                        }
                    }
                }

            }

            return vecResults;
        }


        /// <summary>
        /// Join one element to many element
        /// </summary>
        /// <param name="listElem1">1st list of elements to join to</param>
        /// <param name="attrName1">Attribute name of the 1st list whose value to be matched. If empty, element value will be used to match instead.</param>
        /// <param name="listElem2">2nd list of elements to join to</param>
        /// <param name="attrName2">Attribute name of the 2nd list whose value to be matched. If empty, element value will be used to match instead.</param>
        /// <param name="caseSensitive">States if the comparision is case-sensitive.</param>
        /// <returns>List of pair of element and elements</returns>
        public static List<KeyValuePair<Elmax.Element, List<Elmax.Element>>>
            JoinOneToMany(
            List<Elmax.Element> listElem1,
            string attrName1,
            List<Elmax.Element> listElem2,
            string attrName2,
            bool caseSensitive)
        {
            string str1, str2;
            List<KeyValuePair<Elmax.Element, List<Elmax.Element>>> vecResults = new List<KeyValuePair<Elmax.Element, List<Elmax.Element>>>();
            bool makepair = false;
            for (int i = 0; i < listElem1.Count; ++i)
            {
                makepair = false;
                if (attrName1 == "")
                    str1 = listElem1[i].GetString("");
                else
                    str1 = listElem1[i].Attribute(attrName1).GetString("");

                for (int j = 0; j < listElem2.Count; ++j)
                {
                    if (attrName2 == "")
                        str2 = listElem2[j].GetString("");
                    else
                        str2 = listElem2[j].Attribute(attrName2).GetString("");

                    if (caseSensitive)
                    {
                        if (str1 == str2)
                        {
                            if (makepair == false)
                            {
                                List<Elmax.Element> vecChild = new List<Elmax.Element>();
                                vecResults.Add(new KeyValuePair<Elmax.Element, List<Elmax.Element>>(listElem1[i], vecChild));
                                makepair = true;
                            }
                            vecResults[vecResults.Count - 1].Value.Add(listElem2[j]);
                        }
                    }
                    else
                    {
                        if (str1.ToLower() == str2.ToLower())
                        {
                            if (makepair == false)
                            {
                                List<Elmax.Element> vecChild = new List<Elmax.Element>();
                                vecResults.Add(new KeyValuePair<Elmax.Element, List<Elmax.Element>>(listElem1[i], vecChild));
                                makepair = true;
                            }
                            vecResults[vecResults.Count - 1].Value.Add(listElem2[j]);
                        }
                    }
                }

            }

            return vecResults;
        }

        /// <summary>
        /// Join one element to another element, using a predicate
        /// </summary>
        /// <param name="listElem1">1st list of elements to join to</param>
        /// <param name="listElem2">2nd list of elements to join to</param>
        /// <param name="predicate">Determines if the elements are to be joined.</param>
        /// <returns>List of pair of element</returns>
        public static List<KeyValuePair<Elmax.Element, Elmax.Element>>
            JoinOneToOne(
                List<Elmax.Element> listElem1,
                List<Elmax.Element> listElem2,
                DoubleElementPredicate predicate)
        {
            List<KeyValuePair<Elmax.Element, Elmax.Element>> vecResults = new List<KeyValuePair<Elmax.Element, Elmax.Element>>();
            for (int i = 0; i < listElem1.Count; ++i)
            {
                for (int j = 0; j < listElem2.Count; ++j)
                {
                    if (predicate(listElem1[i], listElem2[j]))
                    {
                        vecResults.Add(new KeyValuePair<Elmax.Element, Elmax.Element>(listElem1[i], listElem2[j]));
                        break;
                    }
                }

            }

            return vecResults;
        }

        /// <summary>
        /// Join one element to many element, using a predicate
        /// </summary>
        /// <param name="listElem1">1st list of elements to join to</param>
        /// <param name="listElem2">2nd list of elements to join to</param>
        /// <param name="predicate">Determines if the elements are to be joined.</param>
        /// <returns>List of pair of element and elements</returns>
        public static List<KeyValuePair<Elmax.Element, List<Elmax.Element>>>
            JoinOneToMany(
            List<Elmax.Element> listElem1,
            List<Elmax.Element> listElem2,
            DoubleElementPredicate predicate)
        {
            List<KeyValuePair<Elmax.Element, List<Elmax.Element>>> vecResults = new List<KeyValuePair<Elmax.Element, List<Elmax.Element>>>();
            bool makepair = false;
            for (int i = 0; i < listElem1.Count; ++i)
            {
                makepair = false;

                for (int j = 0; j < listElem2.Count; ++j)
                {
                    if (predicate(listElem1[i], listElem2[j]))
                    {
                        if (makepair == false)
                        {
                            List<Elmax.Element> vecChild = new List<Elmax.Element>();
                            vecResults.Add(new KeyValuePair<Elmax.Element, List<Elmax.Element>>(listElem1[i], vecChild));
                            makepair = true;
                        }
                        vecResults[vecResults.Count - 1].Value.Add(listElem2[j]);
                    }
                }

            }

            return vecResults;
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)

About the Author

Wong Shao Voon
Software Developer
Singapore Singapore
Member
Rather than to write an accolade of skills which I currently possess, these are the technologies, I am currently exploring:
 
  • 3D Graphics(Lighting and Shadow)
  • ASP.NET MVC 4
  • C++14
  • Garbage Collection
  • GPU Computing
  • H.264 video
  • HTML5 and CSS3
  • iOS
  • SQL Server 2012

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 7 Jun 2012
Article Copyright 2011 by Wong Shao Voon
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid