<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Marius Bancila's Blog &#187; Articles &amp; Tutorials</title>
	<atom:link href="http://mariusbancila.ro/blog/category/articles_and_tutorials/feed/" rel="self" type="application/rss+xml" />
	<link>http://mariusbancila.ro/blog</link>
	<description>Sharing my opinions and ideas!</description>
	<lastBuildDate>Fri, 06 Apr 2012 13:45:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>DocProject Tips and Tricks</title>
		<link>http://mariusbancila.ro/blog/2012/03/23/docproject-tips-and-tricks/</link>
		<comments>http://mariusbancila.ro/blog/2012/03/23/docproject-tips-and-tricks/#comments</comments>
		<pubDate>Fri, 23 Mar 2012 10:02:05 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Documentation]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[docproject]]></category>
		<category><![CDATA[documentation]]></category>
		<category><![CDATA[sandcastle]]></category>
		<category><![CDATA[shfb]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=1238</guid>
		<description><![CDATA[DocProject with Sandcastle are a great choice for creating MSDN-style documentation. You can author conceptual documentation using MAML (Microsoft Assistance Markup Language) and reference documentation from managed assemblies and XML comments. Here is the documentation for DocProject. Writing conceptual documentation I ran into some problems that were not straight forward to solve. Hopefully these tips [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://docproject.codeplex.com/">DocProject</a> with <a href="http://sandcastle.codeplex.com/">Sandcastle</a> are a great choice for creating MSDN-style documentation. You can author conceptual documentation using MAML (Microsoft Assistance Markup Language) and reference documentation from managed assemblies and XML comments. Here is the <a href="http://docproject.codeplex.com/documentation">documentation for DocProject</a>. Writing conceptual documentation I ran into some problems that were not straight forward to solve. Hopefully these tips will help you if you have the same problems.</p>
<p><strong>Overloaded Methods</strong><br />
The <tt>codeEntityReference</tt> tag allows you to link to API reference documentation (namespaces, types, methods, properties, fields). You have to prefix the fully qualified name of the entity with <strong>N:</strong> for namespaces, <strong>T:</strong> for types, <strong>P:</strong> for properties, <strong>F:</strong> for fields and <strong>M:</strong> for methods. What if you have overloaded methods and you want to link to the page that displays all of them, not one particular method? The <strong>O:</strong> prefix Visual Studio is expecting (for XML comments) does not work. <a href="http://shfb.codeplex.com/">Sandcastle Help File Builder</a> (SHFB) expects <strong>Overload:</strong> instead.</p>
<pre name="code" class="csharp">
  &lt;codeEntityReference&gt;Overload:YourNamespace.YourClass.YouOverloadedMethod&lt;/codeEntityReference&gt;
</pre>
<p><strong>Methods with arguments</strong><br />
If you want to link to API reference documentation for a method that take some parameters you must specify those parameter&#8217;s type in parenthesis.</p>
<pre name="code" class="csharp">
  &lt;codeEntityReference&gt;M:YourNamespace.YourClass.Function(System.String)&lt;/codeEntityReference&gt;
</pre>
<p>However, if the method takes more than one parameter, you must separate the argument types with a comma and <em>use no whitespaces between them</em>. If you have whitespaces the API reference will not be resolved.</p>
<pre name="code" class="csharp">
  &lt;codeEntityReference&gt;M:YourNamespace.YourClass.Function(System.String,System.Int32[])&lt;/codeEntityReference&gt;
</pre>
<p><strong>Constructors</strong><br />
Constructors are special methods so you must use a special notation for them: instead of using their name use the <strong>#ctor</strong> tag.</p>
<pre name="code" class="csharp">
  &lt;codeEntityReference&gt;M:YourNamespace.YourClass.#ctor&lt;/codeEntityReference&gt;
</pre>
<p>Of course, if the constructor has some arguments you must specify them in parenthesis:</p>
<pre name="code" class="csharp">
  &lt;codeEntityReference&gt;M:YourNamespace.YourClass.#ctor(System.String)&lt;/codeEntityReference&gt;
</pre>
<p><strong>Generics</strong><br />
When it comes to generics you must use the special notation with ` (grave accent). The grave accent (`) indicates the level and the number after it is the number of the generic types. Here are the rules (as indicated in the <a href="http://en.csharp-online.net/ECMA-334%3A_E.3.1_ID_string_format">ECMA specification</a> ):</p>
<ul>
<li>Arguments that define generic type parameters have an appended grave accent character followed by the number of type parameters (ex. T`1)</li>
<li>For nested types, the number is based upon the of new type parameters on the nested type(ex. T`1.Nested`2)</li>
<li>Arguments that refer to generic type parameters on types are encoded using a single grave accent character (`) followed by the zero-based index of the type parameter.</li>
<li>Arguments that use generic parameters on methods use double grave accent characters (&#8220;) followed by the zero-based index of the type-parameter instead of the single grave accent used for parameters on types.</li>
</ul>
<p>For instance, let&#8217;s say your class looks like this:</p>
<pre name="code" class="csharp">
namespace foo
{
   class bar&lt;T&gt;
   {
   }
}
</pre>
<p>If you want to link to the generic bar type you must do it like this:</p>
<pre name="code" class="csharp">
  &lt;codeEntityReference&gt;T:foo.bar`1&lt;/codeEntityReference&gt;
</pre>
<p>What if you had a generic method too?</p>
<pre name="code" class="csharp">
namespace foo
{
   class bar&lt;T&gt;
   {
      public TR func1&lt;TR&gt;(string s)
      {
         return (default)TR; // just something dummy
      }

      public TR func2&lt;TR;&gt;(string s, TR arg)
      {
         return (default)TR; // just something dummy
      }

      public void func3&lt;T1, T2&gt;(T a, T1 b, T2 c)
      {
      }
   }
}
</pre>
<p>Then you must refer to the function as follows:</p>
<pre name="code" class="csharp">
  // function is generic, but no generic argument is used
  &lt;codeEntityReference&gt;M:foo.bar`1.func1``1(System.String)&lt;/codeEntityReference&gt;

  // function is generic and has a generic argument on the method => use `` + 0-based index of type
  &lt;codeEntityReference&gt;M:foo.bar`1.func2``1(System.String,``0)&lt;/codeEntityReference&gt;

  // function is generic and has generic arguments on type and on the method
  // first argument type comes from the type => `0
  // second and third argument types come from the method, in the order they were declared => ``0 and ``1
  &lt;codeEntityReference&gt;M:foo.bar`1.func3``1(`0,``0,``1)&lt;/codeEntityReference&gt;
</pre>
<p><strong>API Reference Documentation</strong><br />
When you author documentation you probably want to put together conceptual documentation and API reference documentation. The later you build from external sources, i.e. assemblies and XML files with the XML-comments you wrote in the source code. Question is where and how do you insert this API reference? The answer was not very easy to find; luckily a colleague of mine was able to help me with the answer. </p>
<p>First of all, there is only one place in the hierarchy of your topics hierarchy where you can insert it. So no matter from how many sources you build this, it all goes into one place. To set the place you must edit the <strong>topics.xml</strong> file (under Help\Settings). This file looks like this (of course, this is a dummy sample):</p>
<pre name="code" class="xml">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;topics&gt;
  &lt;topic id="cec0ab12-05b5-4a00-ab8a-391d67625997" file="topic1.aml"&gt;
    &lt;topic id="854aeed0-510e-4ca1-b2bb-12104ba9eada" file="topic1\topic11.aml"&gt;
  &lt;/topic&gt;
&lt;/topics&gt;
</pre>
<p>There is a special tag you can insert as a child of a topic. It&#8217;s called <strong>stoc</strong> and looks like this:</p>
<pre name="code" class="xml">
&lt;?xml version="1.0" encoding="utf-8"?&gt;
&lt;topics&gt;
  &lt;topic id="cec0ab12-05b5-4a00-ab8a-391d67625997" file="topic1.aml"&gt;
    &lt;stoc project="Project" /&gt;
  &lt;/topic&gt;
&lt;/topics&gt;
</pre>
<p>This will tell the builder to insert under topic1 all the API reference documentation.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2012/03/23/docproject-tips-and-tricks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>32-bit and 64-bit COM Servers</title>
		<link>http://mariusbancila.ro/blog/2010/11/04/32-bit-and-64-bit-com-servers/</link>
		<comments>http://mariusbancila.ro/blog/2010/11/04/32-bit-and-64-bit-com-servers/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 13:59:23 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[64bit]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[registry]]></category>
		<category><![CDATA[x64]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=775</guid>
		<description><![CDATA[It is possible to register both 32-bit and 64-bit versions of the same COM server on 64-bit machine. This leads to several questions such as how are they registered and which one of the two is used. I will try to answer them below. But first, let&#8217;s start with an example. Example Let&#8217;s say we [...]]]></description>
			<content:encoded><![CDATA[<p>It is possible to register both 32-bit and 64-bit versions of the same COM server on 64-bit machine. This leads to several questions such as how are they registered and which one of the two is used. I will try to answer them below. But first, let&#8217;s start with an example.</p>
<p><strong>Example</strong><br />
Let&#8217;s say we have the a simple COM local server called COM3264Server.exe. There is just one interface called ICoCOM3264Server. Here is the IDL file:</p>
<pre name="code" class="cpp">
[
	object,
	uuid(733C70A7-F7EC-4C15-85D2-5CDB14F4110B),
	dual,
	nonextensible,
	pointer_default(unique)
]
interface ICoCOM3264Server : IDispatch{
   [id(1), helpstring("Says hello")] HRESULT SayHello(void);
};
[
	uuid(2F25FC66-2380-42FD-8476-8B5917FB1BF1),
	version(1.0),
]
library COM3264ServerLib
{
	importlib("stdole2.tlb");
	[
		uuid(9268A299-E817-4C5D-8627-C2582B66F16D)
	]
	coclass CoCOM3264Server
	{
		[default] interface ICoCOM3264Server;
	};
};
</pre>
<p>The implementation of the method SayHello() is straight forward; it just displays a message box with a text that varies between the two architectures, x64 and x86.</p>
<pre name="code" class="cpp">
STDMETHODIMP CCoCOM3264Server::SayHello(void)
{
#ifdef _WIN64
   ::MessageBox(NULL, _T("Hello from 64-bit COM server!"), _T("COM3264Server"), MB_OK);
#else
   ::MessageBox(NULL, _T("Hello from 32-bit COM server!"), _T("COM3264Server"), MB_OK);
#endif

   return S_OK;
}
</pre>
<p><strong>Registry</strong><br />
When you register the COM server, the 32-bit and 64-bit versions are registered in different parts of the registry. On 64-bit machine, the registry has two views (or modes):</p>
<ul>
<li>a native view, for 64-bit application; (e.g. registry path for CLSIDs is HKEY_CLASSES_ROOT\CLSID\)</li>
<li>a WOW64 view, which enables redirections for 32-bit applications, a process transparent to the user (e.g. registry path for CLSIDs is HKEY_CLASSES_ROOT\Wow6432Node\CLSID\)</li>
</ul>
<p>Here is the Registry registration of the (native) 64-bit COM server (notice the registry key in the status bar of the editor, and the path to the server executable).<br />
<img alt="" src="/blog/wp-content/uploads/2010/11/com3264server_reg64.png" title="Native registry view" class="alignnone" width="891" height="319" /></p>
<p>On, the other hand, the 32-bit COM server is registered under the Wow6432 node.<br />
<img alt="" src="/blog/wp-content/uploads/2010/11/com3264server_reg32.png" title="Wow64 registry view" class="alignnone" width="891" height="319" /></p>
<p>So if both versions are registered in Windows Registry, which one is picked? Well, both the server and the client can specify which architecture to use:</p>
<ul>
<li>the COM server can do this via the <strong>PreferredServerBitness</strong> Registry value</li>
<li>the client can do this using one of the flags <strong>CLSCTX_ACTIVATE_32_BIT_SERVER</strong> and <strong>CLSCTX_ACTIVATE_64_BIT_SERVER</strong>, which overrides the server preference</li>
</ul>
<p>If neither the client nor the server specifies a preference, then:</p>
<ul>
<li>If the computer that hosts the server is running Windows Server 2003 with Service Pack 1 (SP1) or a later system, then COM will try to match the server architecture to the client architecture. In other words, for a 32-bit client, COM will activate a 32-bit server if available; otherwise it will activate a 64-bit version of the server. For a 64-bit client, COM will activate a 64-bit server if available; otherwise it will activate a 32-bit server.</li>
<li>If the computer that hosts the server is running Windows XP or Windows Server 2003 without SP1 or later installed, then COM will prefer a 64-bit version of the server if available; otherwise it will activate a 32-bit version of the server.</li>
</ul>
<p><strong>Server Preference</strong><br />
The server can specify its preferred architecture in the <a href="http://msdn.microsoft.com/en-us/library/ms694319%28v=VS.85%29.aspx">PreferredServerBitness</a> value under AppId (available only on 64-bit Windows). This integer value can be:</p>
<ul>
<li><strong>1</strong>: Match the server architecture to the client architecture. For example, if the client is 32-bit, use a 32-bit version of the server, if it is available. If not, the client&#8217;s activation request will fail.</li>
<li><strong>2</strong>: Use a 32-bit version of the server. If one does not exist, the client&#8217;s activation request will fail.</li>
<li><strong>3</strong>: Use a 64-bit version of the server. If one does not exist, the client&#8217;s activation request will fail.</li>
</ul>
<p>Here is the value set in Registry to specify the 64-bit architecture.<br />
<img alt="" src="/blog/wp-content/uploads/2010/11/com3264server_regappid.png" title="Registry AppId" class="alignnone" width="891" height="319" /></p>
<p>When you run the client, it launches the 64-bit version and in my example the following window pops-up:<br />
<img alt="" src="/blog/wp-content/uploads/2010/11/com3264server_64bit.png" title="64-bit COM message" class="alignnone" width="214" height="144" /></p>
<p>If I change the value to indicate the 32-bit architecture, the other COM server is launched and the displayed message is:<br />
<img alt="" src="/blog/wp-content/uploads/2010/11/com3264server_32bit.png" title="32-bit COM message" class="alignnone" width="214" height="144" /></p>
<p><strong>Client Preference</strong><br />
The client code I used so far looked like this:</p>
<pre name="code" class="cpp">
   ICoCOM3264Server* pServer;

   HRESULT hr = ::CoCreateInstance(
      CLSID_CoCOM3264Server,
      NULL,
      CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,
      IID_ICoCOM3264Server,
      (void**)&#038;pServer);

   if(SUCCEEDED(hr))
   {
      pServer->SayHello();

      pServer->Release();
   }
</pre>
<p>However, the 64-bit version of Windows added new flags to the <a href="http://msdn.microsoft.com/en-us/library/ms693716%28v=VS.85%29.aspx">CLSCTX</a> enumeration.</p>
<ul>
<li><strong>CLSCTX_ACTIVATE_32_BIT_SERVER</strong>: used to activate or connect to a 32-bit version of the server; fail if one is not registered.</li>
<li><strong>CLSCTX_ACTIVATE_64_BIT_SERVER</strong>: used to activate or connect to a 64 bit version of the server; fail if one is not registered.</li>
</ul>
<p>As I mentioned earlier, if the client uses one of these flags, it overrides the server preference (specified via the PreferredServerBitness Registry value). </p>
<p>In the following example, the client requests the 64-bit COM server:</p>
<pre name="code" class="cpp">
   HRESULT hr = ::CoCreateInstance(
      CLSID_CoCOM3264Server,
      NULL,
      CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER|CLSCTX_ACTIVATE_64_BIT_SERVER,
      IID_ICoCOM3264Server,
      (void**)&#038;pServer);
</pre>
<p>And if you run it, no matter what the server specified, the 64-bit COM server is launched.</p>
<p>To read more about the subject see the MSDN links above.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/11/04/32-bit-and-64-bit-com-servers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>COM Apartments</title>
		<link>http://mariusbancila.ro/blog/2010/06/28/com-apartments/</link>
		<comments>http://mariusbancila.ro/blog/2010/06/28/com-apartments/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 16:39:51 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[COM]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[apartments]]></category>
		<category><![CDATA[ATL]]></category>
		<category><![CDATA[MTA]]></category>
		<category><![CDATA[NTA]]></category>
		<category><![CDATA[STA]]></category>
		<category><![CDATA[thread-safe]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=571</guid>
		<description><![CDATA[If you work with COM, apartments is one of the concepts you must comprehend, because it&#8217;s an important topic. Before explaining what apartments are let&#8217;s think about classes and objects regardless of COM. When you build a class, you know (or you should) whether objects of that class will be used from a single thread, [...]]]></description>
			<content:encoded><![CDATA[<p>If you work with COM, apartments is one of the concepts you must comprehend, because it&#8217;s an important topic. Before explaining what apartments are let&#8217;s think about classes and objects regardless of COM. When you build a class, you know (or you should) whether objects of that class will be used from a single thread, or from multiple threads. In the later case, if those threads might access shared data at the same time, you must synchronize the access to that data (using critical sections, mutexes, or others). So when you create your class you either make it thread-safe or not. If it&#8217;s not thread safe, objects of that class can only be accessed from one thread at a time, if it&#8217;s thread-safe, then objects of that class can be accessed from different threads at the same time.</p>
<p>Now, the same rule applies in the COM world. Your coclasses can either be thread-safe or not. If the are thread-safe, you can access one object from different threads at a time, otherwise not. Here enter the apartments. So what is an apartment? An apartment is an environment in which COM objects can live. It&#8217;s not a thread, nor a process, but it handles access from COM clients to COM objects. There are several types of apartments: single-threaded apartments (STA), multi-threaded apartments (MTA) and neutral-threaded apartments (NTA).</p>
<p><strong>Single-Threaded Apartments</strong><br />
An STA allows only one thread at a time to access a COM object. This is  achieved using a hidden window with a message pump. Calls from clients living in different threads are queued with the message pump. Only when the current call from a thread ends, the next call can proceed.</p>
<p>Assume you had a COM object called CoUserGroups that implements an interface IUserGroups that provides two methods: one called Add that adds a new user group, and one called Delete that deletes a user group. Since both methods work on the same list of user groups, adding and deleting is not possible at the same time without synchronizing access. But since such synchronization was not implemented, the COM object specifies that it can leave only in an STA, and let the apartment do the synchronization of calls from clients living in different threads.</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/06/com_sta.png" title="COM STA" class="alignnone" width="688" height="326" /></p>
<p><strong>Multi-Threaded Apartments</strong><br />
An MTA allows any number of threads to access a COM objects. However, the COM objects must be thread-safe, otherwise your application will behave unexpectedly and even crash.</p>
<p>Going back to the previous example, if CoUserGroups was implemented in a thread-safe manner, then it would be possible for clients living in different threads to access it. In this case there would be no need for an apartment level synchronization. The COM coclass specifies that it can live in an MTA and when simultaneous calls from different threads are received they are directed immediately to the COM object. This situation is shown in the next image.</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/06/com_mta.png" title="COM MTA" class="alignnone" width="688" height="327" /></p>
<p><strong>Neutral-Threaded Apartments</strong><br />
NTAs, like MTAs, allow multiple threads to enter one apartment, but once a thread entered the apartment it acquires an apartment wide lock that will block the other threads, until the current thread exists the apartment. Calls into NTA (from STA or MTA in the same process) do not generate context switches; the thread leaves the apartment in which it executes and enters the NTA without any context switch, which increases performance. This model was introduced with COM+ (in Windows 2000) and is meant for components with no user interface. </p>
<p>A process can contain several apartments:</p>
<ul>
<li>zero or one MTA</li>
<li>zero or one NTA</li>
<li>zero, one or several STAs; the first STA created for a process is called the main STA</li>
</ul>
<p>As a COM client, you specify the apartment you want to join with a call to CoInitializeEx(). This methods must be called from each thread.</p>
<pre name="code" class="cpp">
HRESULT CoInitializeEx(void * pvReserved, DWORD dwCoInit);
</pre>
<p>The second parameters is a set of flags specifying the initialization options for the thread. To join the unique MTA, use COINIT_MULTITHREADED. To join a new or existing STA, use COINIT_APARTMENTTHREADED. Function CoInitialize() calls CoInitializeEx() specifying COINIT_APARTMENTTHREADED for the flags. </p>
<p><strong>How to specify the threading model allowed for a coclass?</strong><br />
A coclass can specify the type of apartment it can join. If you&#8217;re using ATL you can specify that when you create the coclass. The next image shows the available options:</p>
<p><img alt="" src="/blog/wp-content/uploads/2010/06/com_atlapartment.png" title="ATL apartment choices" class="alignnone" width="615" height="520" /><br />
What they mean:</p>
<ul>
<li><strong>Single</strong>: object wants to join the main STA (the first STA created into the process)</li>
<li><strong>Apartment</strong>: object wants to join one of the STAs in the process</li>
<li><strong>Both</strong>: object wants to join either an STA or the MTA</li>
<li><strong>Free</strong>: object wants to join the MTA</li>
<li><strong>Neutral</strong>: object wants to join the NTA</li>
</ul>
<p>ATL adds the appropriate value to the registry script it creates for your coclass. COM depends entirely on the registry, and the threading model is also specified in the registry. Here is an example:</p>
<pre name="code" class="cpp">
HKCR
{
	NoRemove CLSID
	{
		ForceRemove {80CFA233-86CC-44E3-9A62-BC498D8F2A0E} = s 'CoUserGroups Class'
		{
			ForceRemove Programmable
			InprocServer32 = s '%MODULE%'
			{
				val ThreadingModel = s 'Apartment'
			}
			TypeLib = s '{B8965B61-2A5A-4F34-B9F8-D8859452D345}'
			Version = s '1.0'
		}
	}
}
</pre>
<p>When that is merged into the Windows Registry, it looks like this:<br />
<img alt="" src="/blog/wp-content/uploads/2010/06/com_regthreadmodel.png" title="Threading model in Registry" class="alignnone" width="793" height="370" /></p>
<p>The possible values in registry are:</p>
<ul>
<li>no value specified: equivalent of ATL &#8216;Single&#8217;</li>
<li><strong>Apartment</strong>: equivalent of ATL &#8216;Apartment&#8217;</li>
<li><strong>Free</strong>: equivalent of ATL &#8216;Free&#8217;</li>
<li><strong>Both</strong>: equivalent of ATL &#8216;Both&#8217;</li>
<li><strong>Neutral</strong>: equivalent of ATL &#8216;Neutral&#8217;</li>
</ul>
<p>If you want to read more about COM apartments I suggests articles like this <a href="http://www.codeguru.com/cpp/com-tech/activex/apts/article.php/c5529">one</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2010/06/28/com-apartments/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>.NET out string[] to Automation SAFEARRAY**</title>
		<link>http://mariusbancila.ro/blog/2009/07/23/net-out-string-to-automation-safearray/</link>
		<comments>http://mariusbancila.ro/blog/2009/07/23/net-out-string-to-automation-safearray/#comments</comments>
		<pubDate>Thu, 23 Jul 2009 11:44:13 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[MFC]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Windows Programming]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[COM]]></category>
		<category><![CDATA[SAFEARRAY]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=328</guid>
		<description><![CDATA[.NET allows you to expose components as COM and consume them from unmanaged code. There are many references on how to this (and you can only start with MSDN), and I will not talk about that part. What I want to explain here is something different. Suppose you have this interface: [Guid("2F8433FE-4771-4037-B6B2-ED5F6585ED04")] [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface [...]]]></description>
			<content:encoded><![CDATA[<p>.NET allows you to expose components as COM and consume them from unmanaged code. There are many references on how to this (and you can only start with <a href="http://msdn.microsoft.com/en-us/library/zsfww439(VS.71).aspx" target="_blank">MSDN</a>), and I will not talk about that part. What I want to explain here is something different. Suppose you have this interface:</p>
<pre name="code" class="csharp">
[Guid("2F8433FE-4771-4037-B6B2-ED5F6585ED04")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IAccounts
{
      [DispId(1)]
      string[] GetUsers();
}
</pre>
<p>Method GetUsers() returns an array on string representing the user names. But what if you also wanted the user passwords or addresses? Since this is exposed as COM, you cannot return an array of User. But you can return multiple arrays of string. So, how would you deal with out string[]? This is what I want to show you in this tutorial.</p>
<p>This is a .NET interface exposed to COM. It has two methods, GetUsers() that returns an array of string representing user names, and GetUsers2() that returns an array of strings as an output parameters and a bool as return type, indicating whether any user was found.</p>
<pre name="code" class="csharp">
namespace SampleLibrary
{
   [Guid("2F8433FE-4771-4037-B6B2-ED5F6585ED04")]
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   public interface IAccounts
   {
      [DispId(1)]
      string[] GetUsers();

      [DispId(2)]
      bool GetUsers2(out string [] users);
   }
}
</pre>
<p>And this is the implementation:</p>
<pre name="code" class="csharp">
namespace SampleLibrary
{
   [Guid("C4713144-5D29-4c65-BF9C-188B1B7CD2B6")]
   [ClassInterface(ClassInterfaceType.None)]
   [ProgId("SampleLibrary.DataQuery")]
   public class Accounts : IAccounts
   {
      List&lt; string &gt; m_users;

      public Accounts()
      {
         m_users = new List&lt; string &gt; {
            "marius.bancila",
            "john.doe",
            "anna.kepler"
         };
      }

      #region IDataQuery Members

      public string[] GetUsers()
      {
         return m_users.ToArray();
      }

      public bool GetUsers2(out string[] users)
      {
         users = m_users.ToArray();

         return users.Length &gt; 0;
      }

      #endregion
   }
}
</pre>
<p>Note: If you are trying this example make sure you set the ComVisible attribute to true, either for each type or per assembly (in AssemblyInfo.cs)</p>
<pre name="code" class="csharp">
[assembly: ComVisible(true)]
</pre>
<p>Second, you have to check the &#8220;Register for COM interop&#8221; setting in the Build page of the project properties.</p>
<p>The first thing to do in C++ is importing the .TLB file that was generated by regasm.exe.</p>
<pre name="code" class="cpp">
#import "SampleLibrary.tlb"
using namespace SampleLibrary;
</pre>
<p>If we look in the .TLB file, we can see how the IAccounts interface looks like:</p>
<pre name="code" class="cpp">
struct __declspec(uuid("2f8433fe-4771-4037-b6b2-ed5f6585ed04"))
IAccounts : IDispatch
{
    //
    // Wrapper methods for error-handling
    //

    // Methods:
    SAFEARRAY * GetUsers ( );
    VARIANT_BOOL GetUsers2 (
        SAFEARRAY * * users );
};
</pre>
<p>The following C++ functions, GetUsers1() retrieves the users users list using method GetUsers() from IAccounts. It puts the users in a CStringArray (notice that this container does not have an assignment operator, so the only way to return such an array is with a reference in the parameters list).</p>
<pre name="code" class="cpp">
void GetUsers1(CStringArray&amp; arrUsers)
{
   IAccountsPtr pAccounts(__uuidof(Accounts));

   SAFEARRAY* sarrUsers = pAccounts-&gt;GetUsers();

   _variant_t varUsers;
   varUsers.parray = sarrUsers;
   varUsers.vt = VT_ARRAY | VT_BSTR;

   UnpackBstrArray(varUsers, arrUsers);
   SafeArrayDestroy(sarrUsers);

   pAccounts-&gt;Release();
}
</pre>
<p>UnpackBstrArray() is a function (see below) that extracts the elements of a SAFEARRAY and adds them to a CStringArray.</p>
<p>Function GetUsers2() uses the second method, GetUsers2() from IAccounts. This needs the address of a pointer to a SAFEARRAY (i.e. SAFEARRAY**) that will hold the values returned by the COM method. This time we have to create an empty SAFEARRAY and then pass its address to the COM method. The rest is similar to the previous case.</p>
<pre name="code" class="cpp">
void GetUsers2(CStringArray&amp; arrUsers)
{
   IAccountsPtr pAccounts(__uuidof(Accounts));

   SAFEARRAYBOUND aDim[1];
   aDim[0].lLbound = 0;
   aDim[0].cElements = 0;

   SAFEARRAY* sarrUsers = SafeArrayCreate(VT_BSTR, 1, aDim);

   VARIANT_BOOL ret = pAccounts-&gt;GetUsers2(&amp;sarrUsers);
   if(ret != VARIANT_FALSE)
   {
      _variant_t varUsers;
      varUsers.parray = sarrUsers;
      varUsers.vt = VT_ARRAY | VT_BSTR;
      UnpackBstrArray(varUsers, arrUsers);
   }

   SafeArrayDestroy(sarrUsers);

   pAccounts-&gt;Release();
}
</pre>
<p>The helper method UnpackBstrArray() used previous looks like this:</p>
<pre name="code" class="cpp">
void UnpackBstrArrayHelper(VARIANT* pvarArrayIn, CStringArray* pstrarrValues)
{
   if (!pstrarrValues || !pvarArrayIn || pvarArrayIn->vt == VT_EMPTY)
      return;

   pstrarrValues-&gt;RemoveAll();

   VARIANT* pvarArray = pvarArrayIn;
   SAFEARRAY* parrValues = NULL;

   SAFEARRAYBOUND arrayBounds[1];
   arrayBounds[0].lLbound = 0;
   arrayBounds[0].cElements = 0;

   if((pvarArray-&gt;vt &#038; (VT_VARIANT|VT_BYREF|VT_ARRAY)) == (VT_VARIANT|VT_BYREF) &#038;&#038;
      NULL != pvarArray-&gt;pvarVal &#038;&#038;
      (pvarArray-&gt;pvarVal-&gt;vt &#038; VT_ARRAY))
   {
      pvarArray = pvarArray-&gt;pvarVal;
   }

   if (pvarArray-&gt;vt &#038; VT_ARRAY)
   {
      if (VT_BYREF &amp; pvarArray-&gt;vt)
         parrValues = *pvarArray-&gt;pparray;
      else
         parrValues = pvarArray-&gt;parray;
   }
   else
      return;

   if (parrValues != NULL)
   {
      HRESULT hr = SafeArrayGetLBound(parrValues, 1, &amp;arrayBounds[0].lLbound);
      hr = SafeArrayGetUBound(parrValues, 1, (long*)&amp;arrayBounds[0].cElements);
      arrayBounds[0].cElements -= arrayBounds[0].lLbound;
      arrayBounds[0].cElements += 1;
   }

   if (arrayBounds[0].cElements &gt; 0)
   {
      for (ULONG i = 0; i &lt; arrayBounds[0].cElements; i++)
      {
         LONG lIndex = (LONG)i;
         CString strValue = _T("");

         VARTYPE vType;
         BSTR bstrItem;

         ::SafeArrayGetVartype(parrValues, &amp;vType);
         HRESULT hr = ::SafeArrayGetElement(parrValues, &amp;lIndex, &amp;bstrItem);

         if(SUCCEEDED(hr))
         {
            switch(vType)
            {
            case VT_BSTR:
               strValue = (LPCTSTR)bstrItem;
               break;
            }

            ::SysFreeString(bstrItem);
         }

         pstrarrValues-&gt;Add(strValue);
      }
   }
}

void UnpackBstrArray( const _variant_t &amp;var, CStringArray &amp;strarrValues  )
{
   UnpackBstrArrayHelper( &amp;(VARIANT)const_cast&lt; _variant_t &#038; &gt;(var), &amp;strarrValues );
}
</pre>
<p>Attached you can find a demo project (C# and C++) with the complete example show in this tutorial.</p>
<a class="downloadlink" href="http://mariusbancila.ro/blog/wp-content/plugins/download-monitor/download.php?id=4" title="Version1.0 downloaded 365 times" >output SAFEARRAY** example (365)</a>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/07/23/net-out-string-to-automation-safearray/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Project Tuva</title>
		<link>http://mariusbancila.ro/blog/2009/07/17/project-tuva/</link>
		<comments>http://mariusbancila.ro/blog/2009/07/17/project-tuva/#comments</comments>
		<pubDate>Fri, 17 Jul 2009 07:39:21 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[fyenman]]></category>
		<category><![CDATA[lectures]]></category>
		<category><![CDATA[physics]]></category>
		<category><![CDATA[tuva]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=323</guid>
		<description><![CDATA[Project Tuva is an enhanced video player created by Microsoft Research to freely host the lectures give my Richard Feynman at the Cornell University in the &#8217;60s. Bill Gates saw the lectures two decades ago, was impressed with them and wanted to make them freely available. Now, it finally happened. You can watch them at [...]]]></description>
			<content:encoded><![CDATA[<p>Project Tuva is an enhanced video player created by Microsoft Research to freely host the lectures give my <a href="http://en.wikipedia.org/wiki/Richard_Feynman">Richard Feynman</a> at the Cornell University in the &#8217;60s. Bill Gates saw the lectures two decades ago, was impressed with them and wanted to make them freely available. Now, it finally happened. You can watch them at <a href="http://research.microsoft.com/apps/tools/tuva/index.html">Microsoft Research</a>.</p>
<p>The seven lectures given by professor Feynman are:</p>
<ul>
<li>Law of Gravity</li>
<li>The Relation of Mathematics and Physics</li>
<li>The Great Conservation Principles</li>
<li>Symmetry in Physical Law</li>
<li>The Distinction of Past and Future</li>
<li>Probability and Uncertainty &#8211; The Quantum Mechanical View of Nature</li>
<li>Seeking New Laws</li>
</ul>
<p>These are great lectures given by one of the greatest physicists of the 20th century. They really worth watching.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/07/17/project-tuva/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Evaluate Expressions &#8211; Part 4: Evaluate the Abstract Syntax Tree</title>
		<link>http://mariusbancila.ro/blog/2009/02/06/evaluate-expressions-%e2%80%93-part-4-evaluate-the-abstract-syntax-tree/</link>
		<comments>http://mariusbancila.ro/blog/2009/02/06/evaluate-expressions-%e2%80%93-part-4-evaluate-the-abstract-syntax-tree/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 06:00:45 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Articles & Tutorials]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[AST]]></category>
		<category><![CDATA[expressions]]></category>
		<category><![CDATA[grammar]]></category>
		<category><![CDATA[parser]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=151</guid>
		<description><![CDATA[Evaluate Expressions &#8211; Part 1: The Approaches Evaluate Expressions &#8211; Part 2: Parse the Expression Evaluate Expressions &#8211; Part 3: Building the Abstract Syntax Tree Evaluate Expressions &#8211; Part 4: Evaluate the Abstract Syntax Tree So far we have managed to parse the text representing an expression and build an abstract syntax tree. The only [...]]]></description>
			<content:encoded><![CDATA[<p>
<b><a href="http://mariusbancila.ro/blog/?p=148">Evaluate Expressions &#8211; Part 1: The Approaches</a></b><br />
<b><a href="http://mariusbancila.ro/blog/?p=149">Evaluate Expressions &#8211; Part 2: Parse the Expression</a></b><br />
<b><a href="http://mariusbancila.ro/blog/?p=150">Evaluate Expressions &#8211; Part 3: Building the Abstract Syntax Tree</a></b><br />
<b>Evaluate Expressions &#8211; Part 4: Evaluate the Abstract Syntax Tree</b>
</p>
<p>So far we have managed to parse the text representing an expression and build an abstract syntax tree. The only thing left, and the simplest of them all, is traversing this abstract syntax tree and evaluating the expression is represents.</p>
<p>In pseudo code, this would look like this:</p>
<pre class="prettyprint">
double Evaluate(subtree)
{
   if(subtree is numeric)
      return value;
   else
   {
      op = subtree.operator
      v1 = Evaluate(subtree.left)
      v2 = Evaluate(subtree.right)
      return v1 op v2;
   }
}
</pre>
<p>We actually have to check for one more type of node, the one representing an unary expression. However, the evaluation function is as simple as this:</p>
<pre class="prettyprint">
class Evaluator
{
   double EvaluateSubtree(ASTNode* ast)
   {
      if(ast == NULL)
         throw EvaluatorException("Incorrect syntax tree!");

      if(ast->Type == NumberValue)
         return ast->Value;
      else if(ast->Type == UnaryMinus)
         return -EvaluateSubtree(ast->Left);
      else
      {
         double v1 = EvaluateSubtree(ast->Left);
         double v2 = EvaluateSubtree(ast->Right);
         switch(ast->Type)
         {
         case OperatorPlus:  return v1 + v2;
         case OperatorMinus: return v1 - v2;
         case OperatorMul:   return v1 * v2;
         case OperatorDiv:   return v1 / v2;
         }
      }

      throw EvaluatorException("Incorrect syntax tree!");
   }

public:
   double Evaluate(ASTNode* ast)
   {
      if(ast == NULL)
         throw EvaluatorException("Incorrect abstract syntax tree");

      return EvaluateSubtree(ast);
   }
};
</pre>
<p>The exception class is defined as:</p>
<pre class="prettyprint">
class EvaluatorException : public std::exception
{
public:
   EvaluatorException(const std::string&amp; message):
      std::exception(message.c_str())
      {
      }
};
</pre>
<p>So let&#8217;s try it:</p>
<pre class="prettyprint">
void Test(const char* text)
{
   Parser parser;

   try
   {
      ASTNode* ast = parser.Parse(text);

      try
      {
         Evaluator eval;
         double val = eval.Evaluate(ast);

         std::cout << text << " = " << val << std::endl;
      }
      catch(EvaluatorException&amp; ex)
      {
		  std::cout << text << " t " << ex.what() << std::endl;
      }

      delete ast;
   }
   catch(ParserException&amp; ex)
   {
      std::cout << text << " t " << ex.what() << std::endl;
   }
}

int main()
{
   Test("1+2+3+4");
   Test("1*2*3*4");
   Test("1-2-3-4");
   Test("1/2/3/4");
   Test("1*2+3*4");
   Test("1+2*3+4");
   Test("(1+2)*(3+4)");
   Test("1+(2*3)*(4+5)");
   Test("1+(2*3)/4+5");
   Test("5/(4+3)/2");
   Test("1 + 2.5");
   Test("125");
   Test("-1");
   Test("-1+(-2)");
   Test("-1+(-2.0)");

   Test("   1*2,5");
   Test("   1*2.5e2");
   Test("M1 + 2.5");
   Test("1 + 2&amp;5");
   Test("1 * 2.5.6");
   Test("1 ** 2.5");
   Test("*1 / 2.5");

   return 0;
}
</pre>
<p>The output of this test program is:</p>
<pre class="prettyprint">
1+2+3+4 = 10
1*2*3*4 = 24
1-2-3-4 = -8
1/2/3/4 = 0.0416667
1*2+3*4 = 14
1+2*3+4 = 11
(1+2)*(3+4) = 21
1+(2*3)*(4+5) = 55
1+(2*3)/4+5 = 7.5
5/(4+3)/2 = 0.357143
1 + 2.5 = 3.5
125 = 125
-1 = -1
-1+(-2) = -3
-1+(-2.0) = -3
   1*2,5         Unexpected token ',' at position 6
   1*2.5e2       Unexpected token 'e' at position 8
M1 + 2.5         Unexpected token 'M' at position 0
1 + 2&amp;5          Unexpected token '&amp;' at position 5
1 * 2.5.6        Unexpected token '.' at position 7
1 ** 2.5         Unexpected token '*' at position 4
*1 / 2.5         Unexpected token '*' at position 1
</pre>
<p>And that's it. Define the grammar, build the parser, insert semantic actions and build the abstract syntax tree and then traverse it and evaluate the expression. If you are interested in understanding the grammar, and the parsing in a deeper manner than I presented in this posts, I suggest you read more articles. The purpose was not to teach compilers theory, but put it to a practical purpose.</p>
<p><a href="http://www.mariusbancila.ro/archives/cpp/EvalExpression.zip">Here</a> you can download a Visual Studio 2008 project with the code contained in this tutorial.</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/02/06/evaluate-expressions-%e2%80%93-part-4-evaluate-the-abstract-syntax-tree/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Evaluating Expressions &#8211; Part 3: Building the AST</title>
		<link>http://mariusbancila.ro/blog/2009/02/05/evaluating-expressions-part-3-building-the-ast/</link>
		<comments>http://mariusbancila.ro/blog/2009/02/05/evaluating-expressions-part-3-building-the-ast/#comments</comments>
		<pubDate>Thu, 05 Feb 2009 06:00:14 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Articles & Tutorials]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[AST]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=150</guid>
		<description><![CDATA[In my previous post we&#8217;ve parsed an exception verifying whether it&#8217;s correct or not syntactically. But we still have to evaluate it. To be able to do that we&#8217;ll have to build an abstract syntax tree. This can be done by modifying the previous code and inserting semantic action. That means we do something more [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://mariusbancila.ro/blog/?p=149">previous post</a> we&#8217;ve parsed an exception verifying whether it&#8217;s correct or not syntactically. But we still have to evaluate it. To be able to do that we&#8217;ll have to build an abstract syntax tree. This can be done by modifying the previous code and inserting semantic action. That means we do something more when we match productions.</p>
<p>An abstract syntax tree is a binary tree. The inner nodes will represent operators and leafs will be numerical values.</p>
<p>Here is how a node in the AST will look:</p>
<p><img style="vertical-align: middle;" src="/blog/wp-content/uploads/2009/02/ast_node.png" alt="AST node" width="245" height="51" /></p>
<p>It is defined like this:</p>
<pre class="prettyprint">
enum ASTNodeType
{
   Undefined,
   OperatorPlus,
   OperatorMinus,
   OperatorMul,
   OperatorDiv,
   UnaryMinus,
   NumberValue
};

class ASTNode
{
public:
   ASTNodeType Type;
   double      Value;
   ASTNode*    Left;
   ASTNode*    Right;

   ASTNode()
   {
      Type = Undefined;
      Value = 0;
      Left = NULL;
      Right = NULL;
   }

   ~ASTNode()
   {
      delete Left;
      delete Right;
   }
};
</pre>
<p>For the expression 1+2*3, the AST will be:</p>
<p><img style="vertical-align: middle;" src="/blog/wp-content/uploads/2009/02/ast_example.png" alt="AST example" width="665" height="246" /></p>
<p>We&#8217;ll build this tree by inserting semantic actions and adding nodes according to the following rules:</p>
<p><img style="vertical-align: middle;" src="/blog/wp-content/uploads/2009/02/ast_semanticrules.png" alt="AST semantic rules" width="659" height="229" /></p>
<p>You&#8217;ll probably notice that based on these rules the AST shown above will be modified a little bit, with some additional nodes for operators + and *, having on the left a leaf node with the neutral element for the operation (zero for + and 1 for *), and on the right a node corresponding to a TERM or FACTOR. This won&#8217;t affect the evaluation.</p>
<p>The Parser class will change so that the functions corresponding to the non-terminal symbols EXP, EXP1, TERM, TERM1 and FACTOR will return an ASTNode* instead of void. That is the node created as a semantic action.</p>
<pre class="prettyprint">
class Parser
{
   Token m_crtToken;
   const char* m_Text;
   size_t m_Index;

private:

   ASTNode* Expression()
   {
      ASTNode* tnode = Term();
      ASTNode* e1node = Expression1();

      return CreateNode(OperatorPlus, tnode, e1node);
   }

   ASTNode* Expression1()
   {
      ASTNode* tnode;
      ASTNode* e1node;

      switch(m_crtToken.Type)
      {
      case Plus:
         GetNextToken();
         tnode = Term();
         e1node = Expression1();

         return CreateNode(OperatorPlus, e1node, tnode);

      case Minus:
         GetNextToken();
         tnode = Term();
         e1node = Expression1();

         return CreateNode(OperatorMinus, e1node, tnode);
      }

      return CreateNodeNumber(0);
   }

   ASTNode* Term()
   {
      ASTNode* fnode = Factor();
      ASTNode* t1node = Term1();

      return CreateNode(OperatorMul, fnode, t1node);
   }

   ASTNode* Term1()
   {
      ASTNode* fnode;
      ASTNode* t1node;

      switch(m_crtToken.Type)
      {
      case Mul:
         GetNextToken();
         fnode = Factor();
         t1node = Term1();
         return CreateNode(OperatorMul, t1node, fnode);

      case Div:
         GetNextToken();
         fnode = Factor();
         t1node = Term1();
         return CreateNode(OperatorDiv, t1node, fnode);
      }

      return CreateNodeNumber(1);
   }

   ASTNode* Factor()
   {
      ASTNode* node;
      switch(m_crtToken.Type)
      {
      case OpenParenthesis:
         GetNextToken();
         node = Expression();
         Match(')');
         return node;

      case Minus:
         GetNextToken();
		 node = Factor();
         return CreateUnaryNode(node);

      case Number:
         {
            double value = m_crtToken.Value;
            GetNextToken();
            return CreateNodeNumber(value);
         }

      default:
         {
            std::stringstream sstr;
            sstr << "Unexpected token '" << m_crtToken.Symbol << "' at position " << m_Index;
            throw ParserException(sstr.str(), m_Index);
         }
      }
   }

   ASTNode* CreateNode(ASTNodeType type, ASTNode* left, ASTNode* right)
   {
      ASTNode* node = new ASTNode;
      node->Type = type;
      node->Left = left;
      node->Right = right;

      return node;
   }

   ASTNode* CreateUnaryNode(ASTNode* left)
   {
      ASTNode* node = new ASTNode;
      node->Type = UnaryMinus;
      node->Left = left;
      node->Right = NULL;

      return node;
   }

   ASTNode* CreateNodeNumber(double value)
   {
      ASTNode* node = new ASTNode;
      node->Type = NumberValue;
      node->Value = value;

      return node;
   }

   void Match(char expected)
   {
      if(m_Text[m_Index-1] == expected)
         GetNextToken();
      else
      {
         std::stringstream sstr;
         sstr << "Expected token '" << expected << "' at position " << m_Index;
         throw ParserException(sstr.str(), m_Index);
      }
   }

   void SkipWhitespaces()
   {
      while(isspace(m_Text[m_Index])) m_Index++;
   }

   void GetNextToken()
   {
      SkipWhitespaces();

	  m_crtToken.Value = 0;
	  m_crtToken.Symbol = 0;

      if(m_Text[m_Index] == 0)
      {
         m_crtToken.Type = EndOfText;
         return;
      }

      if(isdigit(m_Text[m_Index]))
      {
         m_crtToken.Type = Number;
		 m_crtToken.Value = GetNumber();
         return;
      }

      m_crtToken.Type = Error;

      switch(m_Text[m_Index])
      {
      case '+': m_crtToken.Type = Plus; break;
      case '-': m_crtToken.Type = Minus; break;
      case '*': m_crtToken.Type = Mul; break;
      case '/': m_crtToken.Type = Div; break;
      case '(': m_crtToken.Type = OpenParenthesis; break;
      case ')': m_crtToken.Type = ClosedParenthesis; break;
      }

      if(m_crtToken.Type != Error)
	  {
         m_crtToken.Symbol = m_Text[m_Index];
         m_Index++;
	  }
      else
      {
         std::stringstream sstr;
         sstr << "Unexpected token '" << m_Text[m_Index] << "' at position " << m_Index;
         throw ParserException(sstr.str(), m_Index);
      }
   }

   double GetNumber()
   {
      SkipWhitespaces();

      int index = m_Index;
      while(isdigit(m_Text[m_Index])) m_Index++;
      if(m_Text[m_Index] == '.') m_Index++;
      while(isdigit(m_Text[m_Index])) m_Index++;

      if(m_Index - index == 0)
         throw ParserException("Number expected but not found!", m_Index);

      char buffer[32] = {0};
      memcpy(buffer, &amp;m_Text[index], m_Index - index);

      return atof(buffer);
   }

public:
   ASTNode* Parse(const char* text)
   {
      m_Text = text;
      m_Index = 0;
      GetNextToken();

      return Expression();
   }
};
</pre>
<p>Now the Parse() method will return the created abstract syntax tree. We will see how to evaluate the expression by traversing this tree in the next post.</p>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/02/05/evaluating-expressions-part-3-building-the-ast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Evaluating Expressions &#8211; Part 2: Parse the Expression</title>
		<link>http://mariusbancila.ro/blog/2009/02/04/evaluating-expressions-part-2-parse-the-expression/</link>
		<comments>http://mariusbancila.ro/blog/2009/02/04/evaluating-expressions-part-2-parse-the-expression/#comments</comments>
		<pubDate>Wed, 04 Feb 2009 13:32:25 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Articles & Tutorials]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[analyzer]]></category>
		<category><![CDATA[AST]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[factor]]></category>
		<category><![CDATA[parser]]></category>
		<category><![CDATA[term]]></category>
		<category><![CDATA[tree]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=149</guid>
		<description><![CDATA[In my previous post I have provided some background theory for evaluating expressions with abstract syntax trees. As I was mentioning, the first step towards this goal is to parse the expression, make sure it is correct syntactically. This is what I&#8217;ll show you in this post. Having the grammar defined, we&#8217;ll make one function [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://mariusbancila.ro/blog/?p=148">previous post</a> I have provided some background theory for evaluating expressions with abstract syntax trees. As I was mentioning, the first step towards this goal is to parse the expression, make sure it is correct syntactically. This is what I&#8217;ll show you in this post.</p>
<p>Having the grammar defined, we&#8217;ll make one function for each non-terminal symbol (EXP, EXP1, TERM, TERM1, FACTOR).</p>
<p>Simply put the code will look like this:</p>
<pre class="prettyprint">
   void Expression()
   {
      Term();
      Expression1();
   }

   void Expression1()
   {
      switch(current_token)
      {
      case '+':
         GetNextToken();
         Term();
         Expression1();
         break;

      case '-':
         GetNextToken();
         Term();
         Expression1();
         break;
      }
   }
</pre>
<p>However, I want to make it a little bit more organized, so the first thing to do will be defining a <tt>Token</tt> structure that will indicate the type of last extracted token and if the case its value (for numbers). A token is basically a symbol extracted (one at a time) from the input text. The possible tokens will be the arithmetical operators (&#8216;+&#8217;, &#8216;-&#8217;, &#8216;/&#8217;, &#8216;*&#8217;), the parentheses (&#8216;(&#8216; and &#8216;)&#8217;), numbers and the end of the text.</p>
<p>Here is how I defined the token type and the token:</p>
<pre class="prettyprint">
enum TokenType
{
   Error,
   Plus,
   Minus,
   Mul,
   Div,
   EndOfText,
   OpenParenthesis,
   ClosedParenthesis,
   Number
};

struct Token
{
   TokenType	Type;
   double		Value;
   char		Symbol;

   Token():Type(Error), Value(0), Symbol(0)
   {}
};
</pre>
<p>To be able to do the parsing, we&#8217;ll need some helper functions:</p>
<ul>
<li><b>SkipWhitespaces()</b>, skips all whitespaces between two tokens:
<pre class="prettyprint">
   void SkipWhitespaces()
   {
      while(isspace(m_Text[m_Index])) m_Index++;
   }
</pre>
</li>
<li><b>GetNextToken()</b>, extracts the next token from the text; if an illegal token appears it throws an exception
<pre class="prettyprint">
   void GetNextToken()
   {
      // ignore white spaces
      SkipWhitespaces();

      m_crtToken.Value = 0;
      m_crtToken.Symbol = 0;

      // test for the end of text
      if(m_Text[m_Index] == 0)
      {
         m_crtToken.Type = EndOfText;
         return;
      }

      // if the current character is a digit read a number
      if(isdigit(m_Text[m_Index]))
      {
         m_crtToken.Type = Number;
         m_crtToken.Value = GetNumber();
         return;
      }

      m_crtToken.Type = Error;

      // check if the current character is an operator or parentheses
      switch(m_Text[m_Index])
      {
      case '+': m_crtToken.Type = Plus; break;
      case '-': m_crtToken.Type = Minus; break;
      case '*': m_crtToken.Type = Mul; break;
      case '/': m_crtToken.Type = Div; break;
      case '(': m_crtToken.Type = OpenParenthesis; break;
      case ')': m_crtToken.Type = ClosedParenthesis; break;
      }

      if(m_crtToken.Type != Error)
      {
         m_crtToken.Symbol = m_Text[m_Index];
         m_Index++;
      }
      else
      {
         std::stringstream sstr;
         sstr << "Unexpected token '" << m_Text[m_Index] << "' at position " << m_Index;
         throw ParserException(sstr.str(), m_Index);
      }
   }
</pre>
</li>
<li><b>GetNumber()</b> extracts a number from the input text from the current position; the purpose of this tutorial is didactical, so this function is quite simple: it reads integers and doubles with '.' As the decimal point; it doesn't read numbers in a format like 123.3E+2.
<pre class="prettyprint">
   double GetNumber()
   {
      SkipWhitespaces();

      int index = m_Index;
      while(isdigit(m_Text[m_Index])) m_Index++;
      if(m_Text[m_Index] == '.') m_Index++;
      while(isdigit(m_Text[m_Index])) m_Index++;

      if(m_Index - index == 0)
         throw ParserException("Number expected but not found!", m_Index);

      char buffer[32] = {0};
      memcpy(buffer, &#038;m_Text[index], m_Index - index);

      return atof(buffer);
   }
</pre>
</li>
</ul>
<p>With these defined, we can build the parser for the specified grammar.</p>
<pre class="prettyprint">
class Parser
{
   Token m_crtToken;
   const char* m_Text;
   size_t m_Index;

private:

   void Expression()
   {
      Term();
      Expression1();
   }

   void Expression1()
   {
      switch(m_crtToken.Type)
      {
      case Plus:
         GetNextToken();
         Term();
         Expression1();
         break;

      case Minus:
         GetNextToken();
         Term();
         Expression1();
         break;
      }
   }

   void Term()
   {
      Factor();
      Term1();
   }

   void Term1()
   {
      switch(m_crtToken.Type)
      {
      case Mul:
         GetNextToken();
         Factor();
         Term1();
         break;

      case Div:
         GetNextToken();
         Factor();
         Term1();
         break;
      }
   }

   void Factor()
   {
      switch(m_crtToken.Type)
      {
      case OpenParenthesis:
         GetNextToken();
         Expression();
         Match(')');
         break;

      case Minus:
         GetNextToken();
         Factor();
         break;

      case Number:
         GetNextToken();
         break;

      default:
         {
            std::stringstream sstr;
            sstr << "Unexpected token '" << m_crtToken.Symbol << "' at position " << m_Index;
            throw ParserException(sstr.str(), m_Index);
         }
      }
   }

   void Match(char expected)
   {
      if(m_Text[m_Index-1] == expected)
         GetNextToken();
      else
      {
         std::stringstream sstr;
         sstr << "Expected token '" << expected << "' at position " << m_Index;
         throw ParserException(sstr.str(), m_Index);
      }
   }

   void SkipWhitespaces()
   {
      while(isspace(m_Text[m_Index])) m_Index++;
   }

   void GetNextToken()
   {
      // ignore white spaces
      SkipWhitespaces();

      m_crtToken.Value = 0;
      m_crtToken.Symbol = 0;

      // test for the end of text
      if(m_Text[m_Index] == 0)
      {
         m_crtToken.Type = EndOfText;
         return;
      }

      // if the current character is a digit read a number
      if(isdigit(m_Text[m_Index]))
      {
         m_crtToken.Type = Number;
         m_crtToken.Value = GetNumber();
         return;
      }

      m_crtToken.Type = Error;

      // check if the current character is an operator or parentheses
      switch(m_Text[m_Index])
      {
      case '+': m_crtToken.Type = Plus; break;
      case '-': m_crtToken.Type = Minus; break;
      case '*': m_crtToken.Type = Mul; break;
      case '/': m_crtToken.Type = Div; break;
      case '(': m_crtToken.Type = OpenParenthesis; break;
      case ')': m_crtToken.Type = ClosedParenthesis; break;
      }

      if(m_crtToken.Type != Error)
      {
         m_crtToken.Symbol = m_Text[m_Index];
         m_Index++;
      }
      else
      {
         std::stringstream sstr;
         sstr << "Unexpected token '" << m_Text[m_Index] << "' at position " << m_Index;
         throw ParserException(sstr.str(), m_Index);
      }
   }

   double GetNumber()
   {
      SkipWhitespaces();

      int index = m_Index;
      while(isdigit(m_Text[m_Index])) m_Index++;
      if(m_Text[m_Index] == '.') m_Index++;
      while(isdigit(m_Text[m_Index])) m_Index++;

      if(m_Index - index == 0)
         throw ParserException("Number expected but not found!", m_Index);

      char buffer[32] = {0};
      memcpy(buffer, &amp;m_Text[index], m_Index - index);

      return atof(buffer);
   }

public:
   void Parse(const char* text)
   {
      m_Text = text;
      m_Index = 0;
      GetNextToken();

      Expression();
   }
};
</pre>
<p>The exception class is defined like this:</p>
<pre class="prettyprint">
class ParserException : public std::exception
{
   int m_Pos;

public:
   ParserException(const std::string&amp; message, int pos):
      std::exception(message.c_str()),
      m_Pos(pos)
   {
   }
};
</pre>
<p>As you can see, the code for the grammar production is quite simple and straight forward. Now, let's put it to the test.</p>
<pre class="prettyprint">
void Test(const char* text)
{
   Parser parser;
   try
   {
      parser.Parse(text);
      std::cout << """ << text << ""t OK" << std::endl;
   }
   catch(ParserException&amp; ex)
   {
      std::cout << """ << text << ""t " << ex.what() << std::endl;
   }
}

int main()
{
   Test("1+2+3+4");
   Test("1*2*3*4");
   Test("1-2-3-4");
   Test("1/2/3/4");
   Test("1*2+3*4");
   Test("1+2*3+4");
   Test("(1+2)*(3+4)");
   Test("1+(2*3)*(4+5)");
   Test("1+(2*3)/4+5");
   Test("5/(4+3)/2");
   Test("1 + 2.5");
   Test("125");
   Test("-1");
   Test("-1+(-2)");
   Test("-1+(-2.0)");

   Test("   1*2,5");
   Test("   1*2.5e2");
   Test("M1 + 2.5");
   Test("1 + 2&amp;5");
   Test("1 * 2.5.6");
   Test("1 ** 2.5");
   Test("*1 / 2.5");

   return 0;
}
</pre>
<p>The output for this testing program is:</p>
<pre class="prettyprint">
"1+2+3+4"        OK
"1*2*3*4"        OK
"1-2-3-4"        OK
"1/2/3/4"        OK
"1*2+3*4"        OK
"1+2*3+4"        OK
"(1+2)*(3+4)"    OK
"1+(2*3)*(4+5)"  OK
"1+(2*3)/4+5"    OK
"5/(4+3)/2"      OK
"1 + 2.5"        OK
"125"    OK
"-1"     OK
"-1+(-2)"        OK
"-1+(-2.0)"      OK
"   1*2,5"       Unexpected token ',' at position 6
"   1*2.5e2"     Unexpected token 'e' at position 8
"M1 + 2.5"       Unexpected token 'M' at position 0
"1 + 2&amp;5"        Unexpected token '&amp;' at position 5
"1 * 2.5.6"      Unexpected token '.' at position 7
"1 ** 2.5"       Unexpected token '*' at position 4
"*1 / 2.5"       Unexpected token '*' at position 1
</pre>
<p>Which is exactly what we expected: it validates correct expressions and throws an exception when the exception is incorrect.</p>
<p>In the next post I'll show how to modify this code to build an abstract syntax tree.</p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/02/04/evaluating-expressions-part-2-parse-the-expression/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Evaluating Expressions &#8211; Part 1: The Approaches</title>
		<link>http://mariusbancila.ro/blog/2009/02/03/evaluating-expressions-part-1/</link>
		<comments>http://mariusbancila.ro/blog/2009/02/03/evaluating-expressions-part-1/#comments</comments>
		<pubDate>Tue, 03 Feb 2009 14:56:12 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[Articles & Tutorials]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[AST]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[RPN]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=148</guid>
		<description><![CDATA[I was discussing a few days ago about evaluating expressions and I decided to explain how you can build an evaluator. I will do this in a series of posts, getting one step more in each post. I will use C++, but the approaches are the same regardless the language. Let&#8217;s consider this expression: 1+2*3. [...]]]></description>
			<content:encoded><![CDATA[<p>I was discussing a few days ago about evaluating expressions and I decided to explain how you can build an evaluator. I will do this in a series of posts, getting one step more in each post. I will use C++, but the approaches are the same regardless the language.</p>
<p>Let&#8217;s consider this expression: 1+2*3. The value of this expression is 7. But how do you evaluate it in a language like C++ if you get it as a string? First of all this is a so called &#8220;infix&#8221; notation. There are also prefix and postfix notation. The terms infix, prefix and postfix refer to the position of the operator related to the operands:</p>
<ul>
<li><b>Prefix</b>: <i>operator</i> operand1 operand2 (ex: + 1 2)</li>
<li><b>Infix</b>: operand1 <i>operator</i> operand2 (ex: 1 + 2)</li>
<li><b>Postfix</b>: operand1 operand2 <i>operator</i> (ex: 1 2 +)</li>
</ul>
<p>The human understandable notation is infix. But it turns out that trying to parse a string with infix expression, from left to right and evaluate it is not possible. Because you cannot now what in advance and operators have different precedence; and there are parentheses too.</p>
<p>To solve the problem you&#8217;d have to build a helper structure representing the infix expression. There are two possibilities:</p>
<ul>
<li><a href="http://en.wikipedia.org/wiki/Reverse_Polish_notation" target="_blank">Reverse Polish Notation</a> (RPN) implies transforming the infix expression in a postfix expression and then evaluating it from left to right. 1 + 2*3 is transformed into 1 2 3 * +. You go from left to right until you find an operator, evaluate the expression and then replace it in the stack.</li>
<li><a href="http://en.wikipedia.org/wiki/Abstract_syntax_tree" target="_blank">
<p>Abstract Syntax Tree</a> (AST) is an abstract representation of an expression, with inner nodes representing operators and leafs representing numbers.</p>
<p><img style="vertical-align: middle;" src="/blog/wp-content/uploads/2009/02/ast.png" alt="Abstract Syntax Tree" width="177" height="154" /></p>
</li>
</ul>
<p>The RPN is harder to build and evaluate in my opinion, so I will focus on the approach with the AST.</p>
<p>We build an AST while parsing the expression. First, we&#8217;ll have to define the grammar for the expression. Otherwise we wouldn&#8217;t know what to parse. </p>
<pre class="prettyprint">
EXP -> EXP + EXP | EXP - EXP | EXP * EXP | EXP / EXP | - EXP | (EXP) | number
</pre>
<p>First, this grammar is recursive, as you can see, but another important problem is that it does not represent the precedence of the operators. For this reasons, a better grammar is this:</p>
<pre class="prettyprint">
EXP    -> EXP + TERM |
          EXP - TERM |
          TERM
TERM   -> TERM * FACTOR |
          TERM / FACTOR |
          FACTOR
FACTOR -> ( EXP ) | - EXP | number
</pre>
<p>These rules written above are called productions. The symbols used are:</p>
<ul>
<li>EXP, TERM, FACTOR are called non-terminal symbols</li>
<li>+, -, /, *, (, ) number are called terminal symbols</li>
<li>EXT is the start symbol</li>
</ul>
<p>While the grammar has the correct operator precedence, it&#8217;s still recursive, or more precisely, left-recursive. You can see that EXP goes into EXP then operator + then TERM. You never reach to match operator + because you have start again and again with a new expression. There are techniques for eliminating this recursion and the result is:</p>
<pre class="prettyprint">
EXP    -> TERM EXP1
EXP1   -> + TERM EXP1 |
          - TERM EXP1 |
          epsilon
TERM   -> FACTOR TERM1
TERM1  -> * FACTOR TERM1 |
          / FACTOR TERM1 |
          epsilon
FACTOR -> ( EXP ) | - EXP | number
</pre>
<p>&#8216;epsilon&#8217; here means &#8216;nothing&#8217;.</p>
<p>With the theory (well, this is just the tip of the iceberg, but should be a good start for you) in place we&#8217;ll have to do three things:</p>
<ul>
<li><a href="http://mariusbancila.ro/blog/?p=149">Parse the expression</a></li>
<li><a href="http://mariusbancila.ro/blog/?p=150">Build the abstract syntax tree</a></li>
<li><a href="http://mariusbancila.ro/blog/?p=151">Evaluate the abstract syntax tree</a></li>
</ul>
<p>The first two steps will be done the same time, but I&#8217;ll take them one at a time and explain it in details.</p>
<p>Before you continue with the implementation details, I suggest you read more about both RPN and AST and grammars.</p>
<p>Here are several references:</p>
<ul>
<li><a href="http://www.csse.monash.edu.au/~lloyd/tildeProgLang/Grammar/" target="_blank">Syntax, Grammar</a></li>
<li><a href="http://www.csse.monash.edu.au/~lloyd/tildeProgLang/Grammar/Arith-Exp/" target="_blank">Arithmetic Expressions</a></li>
<li><a href="http://www.csse.monash.edu.au/~lloyd/tildeProgLang/Grammar/Abstract/" target="_blank">Abstract Syntax</a></li>
<li><a href="http://www.csse.monash.edu.au/~lloyd/tildeProgLang/Grammar/Top-Down/" target="_blank">Top-Down Parsing</a></li>
<ul>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2009/02/03/evaluating-expressions-part-1/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Arrays in F#</title>
		<link>http://mariusbancila.ro/blog/2008/03/26/arrays-in-f/</link>
		<comments>http://mariusbancila.ro/blog/2008/03/26/arrays-in-f/#comments</comments>
		<pubDate>Wed, 26 Mar 2008 08:31:31 +0000</pubDate>
		<dc:creator>Marius Bancila</dc:creator>
				<category><![CDATA[F#]]></category>
		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://mariusbancila.ro/blog/?p=109</guid>
		<description><![CDATA[Yesterday I wrote about list in F#. Today I&#8217;ll write about arrays, which unlike lists are a mutable flat storage and cannot be resized. That means you have to create a new array if you want to remove or add elements. Advantages include constant look-up time and the fact that they can store a large [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I wrote about list in F#. Today I&#8217;ll write about arrays, which unlike lists are a mutable flat storage and cannot be resized. That means you have to create a new array if you want to remove or add elements. Advantages include constant look-up time and the fact that they can store a large amount of data.</p>
<p>You can create a literal array in a similar way with the lists, placing the elements between [| |]:</p>
<pre class="prettyprint">
let data1 = [|1;2;3;4|]
printfn "data1: %a" output_any data1
</pre>
<pre class="console">
data1: [|1; 2; 3; 4|]
</pre>
<p>The empty literal array is [||].</p>
<p>To create an array you can either use Array.create or Array.init. They both create and initialize an array, but the second makes a lambda expression, which allows advance initialization possibilities. The following creates an array with 10 elements initialized to 1:
</p>
<pre class="prettyprint">
let data2 = Array.create 10 1
printfn "data2: %a" output_any data2
</pre>
<p>Here is the output:</p>
<pre class="console">
data2: [|1; 1; 1; 1; 1; 1; 1; 1; 1; 1|]
</pre>
<p>The same can be achieved using Array.init:</p>
<pre class="prettyprint">
let data3 = Array.init 10 (fun x -> 1)
printfn "data3: %a" output_any data3
</pre>
<pre class="console">
data3: [|1; 1; 1; 1; 1; 1; 1; 1; 1; 1|]
</pre>
<p>But we can use Array.init to initialize the elements from 1 to N for instance:</p>
<pre class="prettyprint">
let data4 = Array.init 10 (fun x -> x+1)
printfn "data4: %a" output_any data4
</pre>
<pre class="console">
data4: [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10|]
</pre>
<p>The arrays are mutable data structures. Elements are accessed with .[] or .(). The following code shows how to set the elements of an array:</p>
<pre class="prettyprint">
let data5 = Array.create 10 0
for i = 0 to (Array.length data5)-1 do
   data5.[i] <- i+1

printfn "data5: %a" output_any data5
</pre>
<pre class="console">
data5: [|1; 2; 3; 4; 5; 6; 7; 8; 9; 10|]
</pre>
<p>You can iterate over the elements of an array with Array.iter and Array.iteri, the second also providing access to the index of the elements.</p>
<pre class="prettyprint">
data4 |> Array.iter (fun x -> printf "%d " x)
printfn ""

data4 |> Array.iteri (fun i x -> printfn "data4(%d) = %d" i x)
</pre>
<pre class="console">
1 2 3 4 5 6 7 8 9 10
data4(0) = 1
data4(1) = 2
data4(2) = 3
data4(3) = 4
data4(4) = 5
data4(5) = 6
data4(6) = 7
data4(7) = 8
data4(8) = 9
data4(9) = 10
</pre>
<p>Retrieving the length of the array can either be done with Array.length arr or with arr.Length.</p>
<pre class="prettyprint">
for i = 0 to data4.Length-1 do
   printfn "data4(%d) = %d" i data4.(i)
</pre>
<pre class="console">
data4(0) = 1
data4(1) = 2
data4(2) = 3
data4(3) = 4
data4(4) = 5
data4(5) = 6
data4(6) = 7
data4(7) = 8
data4(8) = 9
data4(9) = 10
</pre>
<p>Like the lists, arrays provide mapping that creates a new array by applying a function on all the elements of an array (with Array.map) or two arrays (with Array.map2).</p>
<pre class="prettyprint">
let data6 = data4 |> Array.map (fun x -> x*2)
printfn "data6: %a" output_any data6

let data7 = Array.map2 (fun x y -> x+y) data4 data6
printfn "data7: %a" output_any data7
</pre>
<pre class="console">
data6: [|2; 4; 6; 8; 10; 12; 14; 16; 18; 20|]
data7: [|3; 6; 9; 12; 15; 18; 21; 24; 27; 30|]
</pre>
<p>A copy of an array can be done with Array.copy.</p>
<pre class="prettyprint">
let data7 = Array.copy data6
printfn "data7: %a" output_any data7
</pre>
<pre class="console">
data7: [|2; 4; 6; 8; 10; 12; 14; 16; 18; 20|]
</pre>
<p>Appending elements to an array is also possible with Array.append, but the result is a new array, created by concatenating two arrays.</p>
<pre class="prettyprint">
let data8 = Array.append data7 [|100|]
printfn "data8: %a" output_any data8
</pre>
<pre class="console">
data8: [|2; 4; 6; 8; 10; 12; 14; 16; 18; 20; 100|]
</pre>
<p>The last operation of arrays I'm going to mention here is the folding, which allows applying a function to all the elements of an array, threading an accumulator argument in the process. The following example shows how to compute the sum of the elements of an array.
</p>
<pre class="prettyprint">
let data9 = [|1;2;3;4|]
let sum1 = (Array.fold_left (fun acc x-> x + acc) 0 data9)
let sum2 = (Array.fold_right (fun acc x-> x + acc) data9 0)
printfn "sum1 = %d" sum1
printfn "sum2 = %d" sum2
</pre>
<pre class="console">
sum1 = 10
sum2 = 10
</pre>
<p></p>
]]></content:encoded>
			<wfw:commentRss>http://mariusbancila.ro/blog/2008/03/26/arrays-in-f/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

