Click here to Skip to main content
Click here to Skip to main content
 
Add your own
alternative version

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

, 7 Jun 2012 Ms-PL
Linq-To-XML Node Creation for Native C++
elmax-84498.zip
BuildProcessTemplates
Elmax.vsmdi
ElmaxNet
Properties
Elmax
Elmax.vcxproj.user
StringUtils
Local.testsettings
TestNativeElmax
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax
Properties
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
TryoutNet
Properties
Settings.settings
Tryout
res
Tryout.ico
Tryout.vcxproj.user
elmax-ver083.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver084.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver085.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver086.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
elmax-ver087.zip
Elmax.vsmdi
Elmax.vcxproj.user
Local.testsettings
app.ico
TestNativeElmax.vcxproj.user
TestNetElmax.csproj.user
TraceAndTestImpact.testsettings
Settings.settings
Tryout.ico
Tryout.vcxproj.user
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
Settings.settings
Tryout.ico
Tryout.vcxproj.user
#pragma once
#include <string>
#include "Element.h"

namespace Elmax
{
//! Helper class to join similar elements based on certain criteria
class HyperElement
{
public:
	//! Default constructor
	HyperElement(void);
	//! Destructor
	~HyperElement(void);

	//! Join one element to another element
	//! 
	//! @param vecElem1 is the 1st list of elements
	//! @param attrName1 is the name of the attribute, of 1st list, whose value is used to match. If empty, the element value will be used to matched instead
	//! @param vecElem2 is the 2nd list of elements
	//! @param attrName2 is the name of the attribute, of 2nd list, whose value is used to match. If empty, the element value will be used to matched instead
	//! @param bCaseSensitive indicates if the matching is case-sensitive
	//! @returns vector of pair of <element and element>
	static std::vector< std::pair<Elmax::Element, Elmax::Element> >
		JoinOneToOne(
		std::vector<Elmax::Element>& vecElem1,
		const std::wstring& attrName1,
		std::vector<Elmax::Element>& vecElem2,
		const std::wstring& attrName2,
		bool bCaseSensitive);

	//! Join one element to many elements
	//! 
	//! @param vecElem1 is the 1st list of elements
	//! @param attrName1 is the name of the attribute, of 1st list, whose value is used to match. If empty, the element value will be used to matched instead
	//! @param vecElem2 is the 2nd list of elements
	//! @param attrName2 is the name of the attribute, of 2nd list, whose value is used to match. If empty, the element value will be used to matched instead
	//! @param bCaseSensitive indicates if the matching is case-sensitive
	//! @returns vector of pair of <element and vector of elements>
	static std::vector< std::pair<Elmax::Element, std::vector<Elmax::Element> > >
		JoinOneToMany(
		std::vector<Elmax::Element>& vecElem1,
		const std::wstring& attrName1,
		std::vector<Elmax::Element>& vecElem2,
		const std::wstring& attrName2,
		bool bCaseSensitive);

	//! Join one element to another element
	//! 
	//! @param vecElem1 is the 1st list of elements
	//! @param vecElem2 is the 2nd list of elements
	//! @param pred is the predicate to determine if the element matched
	//! @returns vector of pair of <element and element>
	template<typename DoubleElementPredicate>
	static std::vector< std::pair<Elmax::Element, Elmax::Element> >
		JoinOneToOne(
		std::vector<Elmax::Element>& vecElem1,
		std::vector<Elmax::Element>& vecElem2,
		DoubleElementPredicate pred)
	{
		std::vector< std::pair<Elmax::Element, Elmax::Element> > vecResults;
		for(size_t i=0; i<vecElem1.size(); ++i)
		{
			for(size_t j=0; j<vecElem2.size(); ++j)
			{
				if(pred(vecElem1[i], vecElem2[j]))
				{
					vecResults.push_back(std::make_pair<Elmax::Element, Elmax::Element>(vecElem1[i], vecElem2[j]));
					break;
				}
			}
		}

		return vecResults;
	}

	//! Join one element to many elements
	//! 
	//! @param vecElem1 is the 1st list of elements
	//! @param vecElem2 is the 2nd list of elements
	//! @param pred is the predicate to determine if the element matched
	//! @returns vector of pair of <element and vector of elements>
	template<typename DoubleElementPredicate>
	static std::vector< std::pair<Elmax::Element, std::vector<Elmax::Element> > >
		JoinOneToMany(
		std::vector<Elmax::Element>& vecElem1,
		std::vector<Elmax::Element>& vecElem2,
		DoubleElementPredicate pred)
	{
		std::vector< std::pair<Elmax::Element, std::vector<Elmax::Element> > > vecResults;
		bool makepair=false;
		for(size_t i=0; i<vecElem1.size(); ++i)
		{
			makepair=false;

			for(size_t j=0; j<vecElem2.size(); ++j)
			{
				if(pred(vecElem1[i], vecElem2[j]))
				{
					if(makepair==false)
					{
						std::vector<Elmax::Element> vecChild;
						vecResults.push_back(std::make_pair<Elmax::Element, std::vector<Elmax::Element> >(vecElem1[i], vecChild));
						makepair=true;
					}
					vecResults[vecResults.size()-1].second.push_back(vecElem2[j]);
				}
			}
		}

		return vecResults;
	}

private:
	//! Helper function to convert string to lowercase.
	static std::wstring ToLowerCase(std::wstring &str);

};

}

By viewing downloads associated with this article you agree to the Terms of Service 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)

Share

About the Author

Shao Voon Wong
Software Developer
Singapore Singapore
No Biography provided

| Advertise | Privacy | Mobile
Web04 | 2.8.141022.2 | Last Updated 7 Jun 2012
Article Copyright 2011 by Shao Voon Wong
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid