tag:blogger.com,1999:blog-284341572024-03-06T21:13:45.692-08:00The Code JamCode Jam is the place to enjoy codingAnonymoushttp://www.blogger.com/profile/04997739304766791351noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-28434157.post-1171819407110168182007-02-18T09:13:00.000-08:002007-03-23T10:25:33.413-07:00Unicode<p style="margin-bottom: 0in;" align="left">Unicode is a standard way of character encoding which was designed to replace all old encodings S.A. ASCII using the Unicode standard transformation.</p> <p style="margin-bottom: 0in;" align="left"><span lang="">ASCII was a way of representing English characters, numbers </span>and some punctuation marks by giving each character a code between 32 and 126 while other codes are unprintable and reserved for special use. E.g. '\b', '\n', '\a', '\v', '\r'.</p><p style="margin-bottom: 0in;" align="left"><br /></p> <div style="text-align: center;"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://photos1.blogger.com/x/blogger/3751/2663/320/999211/ascii.png" alt="" border="0" />ASCII character set<br /><br /><p style="margin-bottom: 0in;" align="left">ASCII could be represented in 7-bits but conveniently it's stored in 8-bits.</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">Codes from 128 to 255 can be used for any purpose. But IBM-PC had something called OEM character set which provides the first 128 ASCII character set in addition to some drawing characters. Horizontal bars, vertical bars…</p><br /></div><br /><div style="text-align: center;"><img style="margin: 0px auto 10px; display: block; text-align: center; cursor: pointer;" src="http://photos1.blogger.com/x/blogger/3751/2663/320/930154/oem.png" alt="" border="0" />OEM character set<br /><br /><p style="margin-bottom: 0in;" align="left">Here troubles began. Every one can use Codes from 128 to 255 as he wish. And support his regional language. The matter became worse with Asian languages. </p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="center"><i>Unicode provides a unique number for every character,<br />no matter what the platform,<br />no matter what the program,<br />no matter what the language.</i></p> <p style="margin-bottom: 0in;" align="center"><br /></p> <p style="margin-bottom: 0in;" align="left">Originally Unicode was designed to provide a single character set that contains every available character on the Earth. Every character is assigned a value and written as <b>U+XXXX</b> in hex-decimal. So <b>A</b> is assigned a <strong>U+0041<span style="">.</span></strong></p> <p style="margin-bottom: 0in;" align="center"><br /></p> <p style="margin-bottom: 0in; color: rgb(255, 0, 0);" align="left">There are some design principles:</p> <ul><li><p style="margin-bottom: 0in;" align="left">Unicode Standard concerns with characters not glyphs. Glyphs are shapes that appear on screen, a printed document.<br />A character can have more than one glyph.<br />e.g.: A,<span style="font-family:Blackadder ITC, fantasy;"> A</span> and <span style="font-family:Bradley Hand ITC, cursive;">A</span> are different glyphs to letter A. While Ain (<strong><span style="">U+0645</span></strong>) (<span style="font-family:Tahoma;"><span lang="ar-EG">ﻉ </span><span style="font-size: 13pt;font-size:100%;" ><span style="font-family:Arial, sans-serif;">ﻋ ﻊ<span lang="ar-EG"> </span>ﻌ</span></span></span>) has 4 forms (isolated, initial, medial, final). So drawing these glyphs is the responsibility of text rendering while Unicode standards concerns only with characters representation in memory or other media.</p> </li><li><p style="margin-bottom: 0in;" align="left">Plain text is a sequence of character codes while rich text or fancy text contains additional information such as text color, size, font…<br />Unicode standard encodes only plain text.</p> </li><li><p style="margin-bottom: 0in;" align="left">Unicode text is stored in their Logical order. When mixing different languages Unicode still preserves logical order.<br />e.g. : mixing English and Arabic text will be stored in logical order while rendering this text will be according to direction of each language.<br />Other issue is combining marks (accent in some Latin scripts and vowel marks in Arabic (<span style="font-family:Tahoma;"><span lang="ar-EG">التشكيل</span></span>)) follows base characters while in rendering they don't appear linearly.</p> </li><li><p style="margin-bottom: 0in;" align="left">Unicode provides Unification concept that is each character has a unique code. Common letters and punctuation marks are given a unique code regardless of the language.</p> </li><li><p style="margin-bottom: 0in;" align="left">Conversion between Unicode and other standards must be guaranteed.<br />In general a single code in other standard maps to a single code in Unicode.</p> </li></ul> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">These design principles are not satisfied for Unicode implementation. They are only principles.</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left"><b style="color: rgb(51, 51, 255);">Encoding forms for Unicode:</b><span style="color: rgb(51, 51, 255);"><br /></span></p><p style="margin-bottom: 0in;" align="left"><span style="color: rgb(51, 51, 255);"></span><br />There are two major categories for Unicode encoding:</p> <p style="margin-bottom: 0in;" align="left">UTF universal transformation format and UCS universal character set. The major difference between UTF and UCS is that UTF is variable length encoding while UCS is fixed length encoding. </p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in; color: rgb(255, 0, 0);" align="left">UCS-2</p> <p style="margin-bottom: 0in;" align="left">A fixed length encoding consists of 2 bytes sometimes named as plain Unicode.</p> <p style="margin-bottom: 0in;" align="left">Let's encode Hello world! </p> <p style="margin-bottom: 0in;" align="left">U+0048 U+0065 U+006C U+006C U+006F U+0020 U+0077 U+006F U+0072 U+006C U+0064.</p> <p style="margin-bottom: 0in;" align="left">This can be stored as:</p> <p style="margin-bottom: 0in;" align="left">Little-Endian: 4800 6500 6C00 6C00 6F00 2000 7700 6F00 7200 6C00 6400.</p> <p style="margin-bottom: 0in;" align="left">Or Big-Endian: 0048 0065 006C 006C 006F 0020 0077 006F 0072 006C 0064.</p> <p style="margin-bottom: 0in;" align="left">Little-Endian is more common.</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in; color: rgb(255, 0, 0);" align="left">UTF-8:</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <ul><li><p style="margin-bottom: 0in;" align="left">Uses from one to four bytes. All ASCII symbols require one byte.</p> </li><li><p style="margin-bottom: 0in;" align="left">Two bytes for Greek, Armenian, Arabic, Hebrew…</p> </li></ul> <p style="margin-bottom: 0in;" align="left"><br /></p> <table border="1" bordercolor="#000000" cellpadding="7" cellspacing="0" width="572"> <col width="112"> <col width="430"> <tbody><tr valign="top"> <td width="112"> <p align="center">Range</p> </td> <td width="430"> <p align="center">Encoding</p> </td> </tr> <tr valign="top"> <td width="112"> <p align="left">000000–00007F</p> </td> <td width="430"> <p align="left">ASCII characters, byte begins with zero bits. ( coded as original ASCII encoding )</p> </td> </tr> <tr valign="top"> <td width="112"> <p align="left">000080–0007FF</p> </td> <td width="430"> <p align="left">First byte begins with 110 bits the following byte begins with 10.</p> </td> </tr> <tr valign="top"> <td width="112"> <p align="left">000800–00FFFF</p> </td> <td width="430"> <p align="left">First byte begins with 1110 bits; the following bytes begin with 10.</p> </td> </tr> <tr valign="top"> <td width="112"> <p align="left">010000–10FFFF</p> </td> <td width="430"> <p align="left">First byte begins with 11110 bits, the following bytes begin with 10</p> </td> </tr> </tbody></table> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">E.g.: Value of 0x3D6 01111010110 is encoded in 2-bytes 11001111 10010110.</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in; color: rgb(255, 0, 0);" align="left">UTF-16:</p> <ul><li><p style="margin-bottom: 0in;" align="left">Uses 2-bytes or 4-bytes encoding.</p> </li></ul> <ul><li><p style="margin-bottom: 0in;" align="left">More complex than UTF-8, designed mainly to extend the range of UCS-2.</p> </li><li><p style="margin-bottom: 0in;" align="left">For encoding: all codes above U+FFFF must be encoded in two words a (pair)<br />E.g. U+23458 :</p> <ul><li><p style="margin-bottom: 0in;" align="left">First subtract 10000 = 13458.</p> </li><li><p style="margin-bottom: 0in;" align="left">Divide the resulting 20-bit into two halves: 0001001101, 0001011000.</p> </li><li><p style="margin-bottom: 0in;" align="left">Initialize the first 6-bits of the first word with 110110 then add the higher half that is 1101100001001101 or 0xD84D.</p> </li><li><p style="margin-bottom: 0in;" align="left">Initialize the first 6-bits of the second word with 110111 then add the lower half that is 1101110001011000 or 0xDC58.</p> </li><li><p style="margin-bottom: 0in;" align="left">Finally the encoding is 0xD84D 0xDC58.</p> </li></ul> </li><li><p style="margin-bottom: 0in;" align="left">For safety values from U+D800 to U+DBFF and U+DC00 to U+DFFF are not available to represent characters in 2-byte format (used to represent values higher than 0xFFFF).</p> </li></ul> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">Java supports UTF-8 in normal programming through InputStreamReader and OutputStreamWriter.</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">To create a string literal in C you use char str[] = "Hello world!" which is encoded as ASCII to create a literal encoded in UCS-2 use char str[] = L"Hello world!"</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">Here are some values from Unicode:</p> <table border="1" border cellpadding="7" cellspacing="0" width="569" style="color:#000000;"> <col width="174"> <col width="175"> <col width="175"> <tbody><tr valign="top"> <td width="174"> <p align="left">A <b><span style="font-family:Courier New, monospace;">A</span></b> <i><span style="font-family:Bradley Hand ITC, cursive;">A</span></i></p> </td> <td width="175"> <p align="left">Letter A</p> </td> <td width="175"> <p align="left">U+0041</p> </td> </tr> <tr valign="top"> <td width="174"> <p align="left">AB</p> </td> <td width="175"> <p align="left">Letter A + Letter B</p> </td> <td width="175"> <p align="left">U+0041 + U+0042</p> </td> </tr> <tr valign="top"> <td width="174"> <p align="left">a</p> </td> <td width="175"> <p align="left">Small A</p> </td> <td width="175"> <p align="left">U+0061</p> </td> </tr> <tr valign="top"> <td width="174"> <p align="left"><span style="font-family:Tahoma;"><span style="font-size: 13pt;font-size:100%;" ><span style="font-family:Arial, sans-serif;">ﻫ ﻪ ﻬ </span></span><span lang="ar-EG"> </span><span style="font-size: 13pt;font-size:100%;" ><span style="font-family:Andalus;"><span style="color:#ff0000;">ﻫ ﻪ ﻬ</span></span></span></span></p> </td> <td width="175"> <p align="left">Letter Heh</p> </td> <td width="175"> <p align="left">U+0647</p> </td> </tr> <tr valign="top"> <td width="174"> <p align="left"><span style="font-family:Arial, sans-serif;"><span style="font-size: 13pt;font-size:100%;" >َُ</span></span></p> </td> <td width="175"> <p align="left">Damma <span style="font-family:Tahoma;"><span lang="ar-EG">ضمه</span></span></p> </td> <td width="175"> <p align="left">U+064F</p> </td> </tr> <tr valign="top"> <td width="174"> <p align="left"><span style="font-family:Tahoma;"><span lang="ar-EG">نُ</span></span></p> </td> <td width="175"> <p align="left">Noon + Damma<span style="font-family:Tahoma;"><span lang="ar-EG">نون </span></span>+ <span style="font-family:Tahoma;"><span lang="ar-EG">ضمه </span></span> </p> </td> <td width="175"> <p align="left">U+0646 + U+064F</p> </td> </tr> </tbody></table> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">The Unicode Consortium is a non-profit organization that is responsible for Unicode's development.</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">Try playing with Unicode encodings create a simple html file maybe with hex editor write 0048 0065 006C 006C 006F 0020 0077 006F 0072 006C 0064 and view the file using a browser and changing the browser's character encoding.</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">Finally this article was so long than I expected. I couldn't cover every thing I want. You can read THE UNICODE STANDARD book versions 3.0, 4.0 and 5.0.</p> <p style="margin-bottom: 0in;" align="left"><br /></p> <p style="margin-bottom: 0in;" align="left">You can refer to <span style="color:#0000ff;"><u><a href="http://www.unicode.org/">www.unicode.org</a></u></span> for more info.</p><br /></div>Anonymoushttp://www.blogger.com/profile/04997739304766791351noreply@blogger.com1tag:blogger.com,1999:blog-28434157.post-1154985044610481562006-08-07T13:59:00.000-07:002008-01-02T07:32:48.046-08:00Smart pointers<p style="margin-bottom: 0in;"><span style="font-weight: bold; color: rgb(51, 51, 255);">Smart pointers</span> are template classes that store pointers to classes. Smart pointers provide same functionality of built-in pointers with some more smart operations.</p> <p style="margin-bottom: 0in;">First let us look at drawbacks of ordinary pointers:</p> <ol><li><p style="margin-bottom: 0in;">No automatic deletion.</p> </li><li><p style="margin-bottom: 0in;">More than one reference.</p> </li></ol> <p style="margin-bottom: 0in;">Class1* x = new Class1() ;</p> <p style="margin-bottom: 0in;">Class1* y = x ;</p> <p style="margin-bottom: 0in;">Now who will delete Class1()? x or y .</p> <ol start="3"><li><p style="margin-bottom: 0in;">No garbage collection.</p> </li><li><p style="margin-bottom: 0in;">Assignment operator not suitable.</p> </li></ol> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><br /></span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">template <<class>><br />class SmartPointer</class></span></span></p> <p style="margin-bottom: 0in;"> </p> <p style="margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> </span></span></p> <p style="margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> </span></span></p> <p style="margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> </span></span></p> <p style="margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> </span></span></p> <p style="margin-bottom: 0in;"> </p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{<br />public:<br />explicit SmartPointer(T* pointer) : ptr(pointer);<br />SmartPtr& operator=(const SmartPtr&amp;amp; other);<br />~SmartPtr();<br />T& operator*() const<br />{<br />return *ptr;<br />}<br />T* operator->() const<br />{<br />return ptr;<br />}<br />private:<br />T* ptr;<br />};</span></span></p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">Smart pointers do the same operations as ordinary pointers as:</p> <ul style="color: rgb(255, 0, 0);"><li><p style="margin-bottom: 0in;">T& operator*() const</p> </li><li><p style="margin-bottom: 0in;">T* operator->() const</p> </li></ul> <p style="margin-bottom: 0in;">While other smart capabilities can be implemented as:</p> <ul style="color: rgb(255, 0, 0);"><li><p style="margin-bottom: 0in;">Auto initialize pointer to null.</p> </li><li><p style="margin-bottom: 0in;">Destructor frees allocated memory.</p> </li><li><p style="margin-bottom: 0in;">Smart assignment</p> </li></ul> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">template <class></class></span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">SmartPointer <t>& SmartPointer <t>::operator=( SmartPointer <t>& sptr)</t></t></t></span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{</span></span></p> <p style="margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">if (this != &rhs) {<br />delete ptr;<br />ptr = sptr.ptr;<br />sptr.ptr = NULL;<br />}<br />return *this;<br />}<br />return *this;<br />}</span></span></p> <p style="margin-bottom: 0in;">Now we gave a pointer to only the newly assigned pointer and deleted the other one!<br />Other ideas may be implemented as needed as allocating new data and copying it!!</p> <ul><li><p style="margin-bottom: 0in;">Garbage collection(c++ doesn’t have garbage collection so smart pointers can be used for that purpose but how??).</p> </li></ul> <p style="margin-bottom: 0in; color: rgb(51, 51, 255); font-weight: bold;">Smart pointers in STL</p> <ul><li><p style="margin-bottom: 0in;">auto_ptr is an example of smart pointers.</p> </li></ul>Anonymoushttp://www.blogger.com/profile/04997739304766791351noreply@blogger.com0tag:blogger.com,1999:blog-28434157.post-1154263274036544462006-07-30T05:38:00.000-07:002008-01-02T07:33:22.503-08:00Directories in c<p style="margin-bottom: 0in;">This tutorial covers directory in c:</p> <p style="margin-bottom: 0in;">Before proceeding include the following file:</p> <p style="margin-bottom: 0in;"><span style="color: rgb(51, 51, 255);">dirent.h:</span><br />Contains definition of DIR structure. Just similar to FILE structure you will see later.<br />To open a directory use<br />DIR* dir = opendir("..");<br /><span style="color: rgb(0, 153, 0);">".."</span> means open current directiory.<br /><span style="color: rgb(0, 153, 0);">"."</span> means parent directory.<br />else a sub directory or complete path e.g: “e:\\” remember why double slash??</p> <p style="margin-bottom: 0in;">Now the directory is opened how to loop for directories or files in this opened directory??<br /><span style="font-size:85%;"><span style="font-family:Courier New,monospace;">struct dirent* dent = dent=readdir(dir) ;</span></span><br />Reads next entry in the opened directory. entry may be a file or directory.</p> <p style="margin-bottom: 0in;">Finally to close opened directory:<br /><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">closedir(dir);</span></span></p> <p style="margin-bottom: 0in;"> </p> <p style="margin-bottom: 0in;">The following code is self explanatory.</p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">/*</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> file : dir.c</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> A tutorial in opening directories in c.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">*/</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">#include <stdio.h></stdio.h></span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">#include <dirent.h></dirent.h></span></span></p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">int main(int argc,char **argv)</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> /*</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> A directory entry pointer.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> */</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> struct dirent* dent;</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> </span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> /*</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> open current directory.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> ".." means open current directiory.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> </span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> */</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> DIR* dir = opendir("..");</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> </span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> printf("\n***** DIR LISTING *****\n\n");</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> </span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> /*</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> if dir is null then open failed </span></span> </p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> may be due to directory is not present</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> or access is denied.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> */</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> if(dir)</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> {</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> /*</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> readdir reads next direcory entry.</span></span></p> <p style="margin-left: 1.5in; margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">returns struct dirent* which ->d_name contains name of directory entry.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> entry may be a file or directory.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> returns null after finishing all entries.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> */</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> while((dent=readdir(dir)))</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> {</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> FILE* fptr ;</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> printf(dent->d_name);</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> /*</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> try to open dir as a file in read mode.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> if it's opened then it's actually a file.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> else it's a directory.</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> */</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> if(fptr = fopen(dent->d_name,"r"))</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> {</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> printf("\t\tFile") ;</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> fclose(fptr) ;</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> }</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> else</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> printf("\t\tDirectory") ;</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> printf("\n");</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> }</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> /*</span></span></p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> close opened directory. </span></span> </p> <p style="margin-bottom: 0in; color: rgb(0, 153, 0);"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> */</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> closedir(dir);</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> }</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> else</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> printf("Err. opening directory\n");</span></span></p> <p style="margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">printf("\n");</span></span></p> <p style="margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getchar();</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> return 0;</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-bottom: 0in;"> </p>Anonymoushttp://www.blogger.com/profile/04997739304766791351noreply@blogger.com2tag:blogger.com,1999:blog-28434157.post-1152278437829678082006-07-09T09:40:00.000-07:002008-01-02T07:34:07.002-08:00Lvalues and rvalues<div style="text-align: center;"><span style="color: rgb(255, 0, 0);font-size:130%;" >Lvalues and rvalues</span><br /></div><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">Have you found this error?</span><br />main.cc:12: non-lvalue in assignment<br />or any error involved with lvalues.<br /><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">What are lvalues?</span><br />Lvalues are references to objects that appear to left side of assignment statements. This definition may be not accurate as const objects are lvalues although they cannot appear on the left side of an assignment statement. So, this definition can be modified to “references to objects”. So this objects must have memory locations or lvalues have addresses.<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">While rvalues:</span><br />Expressions that can appear on right side of assignment statements. Rvalues cannot appear on left side of assignment statements.<br /><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">Lvalues and rvalues:</span><br />Lvalues can appear in a place that requires rvalues. Lvalues are converted to rvalues. Rvalues cannot be converted to lvalues. Therefore, it is possible to use every lvalue expression as rvalue place, but not vice versa.<br />These operators must be applied to lvalues:<br />1. &<br />2. ++ --<br />3. = += -= *= %= <<= >>= &= ^= |=<br /><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">Examples of lvalues and rvalues:</span><br />1. x = 5; // x is an lvalue; 5 is an rvalue<br />2. array[10] = 3 // array[10] is an lvalue while 3 is an rvalue<br />3. X = Y + 5 ; // X is an lvalue Y + 5 is an rvalue<br />4. A function that returns a reference is an lvalue else is an rvalue expression.<br />5. *ptr = 10 // *ptr is an lvalue; 10 is an rvalue<br />6. x = y ; // x is an lvalue y is an lvalue converted to rvalue.<br />7. Casts are lvalues.<br /> <br /><span style="font-weight: bold; color: rgb(51, 51, 255);">Enumerations and const data:</span><br />Both const data and enumerations can't appear at left side of assignment statements. Enumerations aren't lvalues while const data are lvalues. Enumerations doesn't have addresses. The are translated at compilation time to there numeric values. While static members are constructed at runtime.<br /><br /><span style="font-family:courier new;">class Class1{<br /></span><span style="font-family:courier new;">public:<br /></span><span style="font-family:courier new;"> Class1( int x=100 ):MAX(x){}<br /><br /></span><span style="font-family:courier new;"></span><span style="font-family:courier new;"> const int MAX ;<br /></span><span style="font-family:courier new;">};</span><br /><br />Here MAX is initialized with x. MAX is const and have an address. Every instance of Class1 have a const data MAX and every instance may have different value. You may use static const data if it's common among all instances.<br /><br /><span style="font-family:courier new;">class Class2{<br /><br /></span><span style="font-family:courier new;">public:</span><br /><span style="font-family:courier new;"> </span><br /><span style="font-family:courier new;"> enum { MAX=100 } ;<br /></span><span style="font-family:courier new;"> </span><br /><span style="font-family:courier new;">};</span><br /><br />You may notice that enum can't be static because they aren't lvalues or they aren't constructed at run-time.<br /><br /><span style="font-size:100%;">y+5 = 10 generated the top error.<br />Because y+5 is an rvalue placed in an lvalue place.<br /></span> <p style="border: medium none ; padding: 0in; margin-bottom: 0in;" lang="en-US"> <span style="color: rgb(0, 0, 0);font-size:100%;" >A final notice is #define statements are different from enumerations and const data as they are macros and substituted in a pre-compilation process.</span></p>Anonymoushttp://www.blogger.com/profile/04997739304766791351noreply@blogger.com1tag:blogger.com,1999:blog-28434157.post-1151825852343826272006-07-02T00:27:00.000-07:002008-01-02T07:34:38.034-08:00Metaprogramming<p align="center"><br /><span style="color: rgb(255, 0, 0);font-size:130%;" >Metaprogramming</span></p><span style="color: rgb(255, 0, 0);font-size:130%;" ></span><p align="left"><br />The term "programing a program" reflects metaprogramming. In other words writing a program that writes or maintains other programs. Seem ambiguous: you write a code that generates other code which is actually needed. Thus metaprogramming is generating some thing -actually code- at compile time rather than run time.<br />metaprogramming is very language dependent.<br /><br /><strong><span style="color: rgb(51, 51, 255);">Article Contents:<br /></span></strong><br />1. Uses metaprogramming.<br />2. Benefits of metaprogramming.<br />3. Drawbacks of metaprogramming.<br />4. Examples of metaprogramming.<br />5. Starter example of metaprogramming.<br />6. Other template metaprogramming example.<br />7. Using metaprogramming in numerical analysis.<br />8. Unrolling loops.</p><p><span style="color: rgb(51, 51, 255);"><strong>Uses metaprogramming:</strong></span></p><p>1. Generation of look up test tables.<br />2. Some small functions can be parametrized and used allover the program can be generated by metaprogramming.<br />3. Unroll looping. of course unrolled loops are more efficient than rolled loops.<br />4. Other uses you can imagine. </p><p><strong><span style="color: rgb(51, 51, 255);">Benefits of metaprogramming:</span></strong> </p><p>1. Minimizes code.<br />2. Achieve more functionality.<br />3. Provides less effort for programmers and reduces maintenance effort.<br />4. Meta code is generated at compile time. Thus, results faster programs.</p><p><strong><span style="color: rgb(51, 51, 255);">Drawbacks of metaprogramming:</span></strong></p><p>1. Impossible to debug.<br />2. Hard to trace.<br />3. Ambiguous to some extent.</p><p><strong><span style="color: rgb(51, 51, 255);">Examples of metaprogramming:</span></strong></p><p>1. C/C++ macro processor.<br />2. C++ templates.<br />3. M4 processor.</p><p><span style="color: rgb(51, 51, 255);"><strong>Starter example of metaprogramming:</strong></span></p><p>This small macro can be considered as a metaprogramming:<br /><span style="font-family:courier new;">#define swap(x,y,type){ type __temp = x ; x = y ; y = __temp ; }<br />int x=4 , y=7 ;<br />swap(x,y,int) ;</span></p><p><strong><span style="color: rgb(51, 51, 255);">Other template metaprogramming example:</span></strong></p><p>This template calss has an emumeration data type that reserves 3 ^ n where n is the template parameter.<br />Note that template parameters can be ordinary variables. </p><p><span style="font-family:courier new;">// A template class to compute 3 to power n<br />template<int><br />class Pow3 {<br /> public:<br /> enum { result=3*Pow3<n-1>::result };<br />};<br /><br />// Base condition to end the recursion<br />template<><br />class Pow3<0> {<br /> public:<br /> enum { result = 1 };<br />};<br /><int><n-1> </n-1></int></n-1></int></span></p><p>The line:<br /><span style="font-family:courier new;">enum { result=3*Pow3<n-1>::result }; </n-1></span><br />Assignes result enumeration to result of Pow3<n-1> and till recursivly reaches Pow3<0>.</n-1></p><p><strong><span style="color: rgb(51, 51, 255);">Using metaprogramming in numerical analysis:<br />Bisection method:</span></strong></p><p>Solve the following equation:<br />x*x*x +x*x - 6*x = 0 </p><p><span style="font-family:courier new;">// A template to solve the equation.<br />template <int high="low"><br />class Solve<br />{<br /><br /> public:<br /><br /> // compute the midpoint<br /> enum { mid = (low+high+1)/2 };<br /><br /> // search in the halved interval<br /> // equation is mid*mid*mid +mid*mid - 6*mid<br /> enum { result = ( (mid*mid*mid +mid*mid - 6*mid) > 0 ) ?<br /> Solve<low,mid-1>::result : Solve<mid,high>::result };<br /><br />};<br /><br />template<int><br />class Solve<n,n><br />{<br /> public:<br /> enum { result=N};<br />};<br /><br /><br />#include <iostream><br /><br />int main(void)<br />{<br /> std::cout << "Sol of the equation is" <<>::result;<br /> return 0 ;<br />}<br /><br /><int high="low"><low,mid-1><mid,high><int><n,n><iostream> </iostream></n,n></int></mid,high></low,mid-1></int></iostream></n,n></int></mid,high></low,mid-1></int></span></p><p>The variable mid is used to compute mid point for bisection method<br />mid = (low+high+1)/2<br />while result holds the result.<br />result = ( (mid*mid*mid +mid*mid - 6*mid) > 0 ) ? Solve<low,mid-1>::result : Solve<mid,high>::result <low,mid-1><mid,high><br />This code can't get the zero solution do you know why?<br />Try use the interval 0,1000 this results in large compilation time but same run time.</mid,high></low,mid-1></mid,high></low,mid-1></p><p><strong><span style="color: rgb(51, 51, 255);">Unrolling loops:</span></strong></p><p><span style="font-family:courier new;">// primary template<br />template <int><br />class ADD {<br /> public:<br /> static int result (int* a) {<br /> return *a + ADD<n-1>::result(a+1);<br /> }<br />};<br /><br />// partial specialization as end criteria<br />class ADD<1> {<br /> public:<br /> static int result (int* a) {<br /> return *a ;<br /> }<br />};<br /><br />int main(void)<br />{<br /> int a[] = { 1,3,5 } ;<br /> std::cout << "Adding 3 elements:" <<>::result(a) ;<br /> return 0 ;<br />}<br /><int><n-1></n-1></int></n-1></int></span></p><p>Here we used ADD template class to unroll the addition.<br />Unrolling generally reduces the run time but leads to bigger output file.</p><p><strong><span style="color: rgb(51, 51, 255);">Conclusion:</span></strong></p><p>Metaprogramming enhances program efficiency by computing some thing at compilation time rather than runtime. Also it reduces source code size and increases it's functionality.<br /><br /><strong><span style="color: rgb(51, 51, 255);">Links:</span></strong></p>● <a href="http://en.wikipedia.org/wiki/Metaprogramming">http://en.wikipedia.org/wiki/Metaprogramming</a><br />● <a href="http://en.wikipedia.org/wiki/Template_metaprogramming">http://en.wikipedia.org/wiki/Template_metaprogramming</a><br />● <a href="http://boost-consulting.com/mplbook/">http://boost-consulting.com/mplbook/</a><br />● <a href="http://www-128.ibm.com/developerworks/linux/library/l-metaprog1.html">http://www-128.ibm.com/developerworks/linux/library/l-metaprog1.html</a><br />● <a href="http://www-128.ibm.com/developerworks/linux/library/l-metaprog2.html">http://www-128.ibm.com/developerworks/linux/library/l-metaprog2.html</a><br />● <a href="http://www-128.ibm.com/developerworks/linux/library/l-metaprog3/?ca=dgr-wikiaMetaprogP3">http://www-128.ibm.com/developerworks/linux/library/l-metaprog3/?ca=dgr-wikiaMetaprogP3</a>Anonymoushttp://www.blogger.com/profile/04997739304766791351noreply@blogger.com0tag:blogger.com,1999:blog-28434157.post-1150462804741657422006-06-16T05:53:00.000-07:002008-01-02T07:35:02.560-08:00Q & A Bitwise operations in C/C++<div style="text-align: center;"><span style="font-size:130%;"><span style="color: rgb(255, 0, 0);">Q & A Bitwise operations in C/C++</span></span><br /></div><br />Bitwise operations mean fast machine performance.<br />Using Macros means eliminating time cost of a function call.<br /><span style="font-size:130%;"><br /><span style="color: rgb(0, 153, 0); font-weight: bold;font-size:100%;" >Bitwise operations:</span></span><br /><br />& Bitwise AND<br />| Bitwise OR<br />^ XOR<br />~ One's compliment flip bits<br /><<>> Right shift<span style="color: rgb(0, 153, 0); font-weight: bold;font-size:130%;" ><br /><br /><span style="font-size:100%;">Bitwise Q & A:</span></span><br /><br /># define UNITY 0x00000001<br /><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">Q: I want to raise a number to a power of x 2^x?</span><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">A:</span><br />#define TWOPWR( x ) UNITY << (x) int z = TWOPWR(x) ; <span style="color: rgb(51, 51, 255); font-weight: bold;"><br /><br />Q: I want to a get a bit of position pos from x?</span><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">A:</span><br />#define GETBIT(x,pos) ( ((x) & ( UNITY << (pos) ))!=0 ) unsigned x = 25 ; bool bit = GETBIT(x,5) ; <span style="color: rgb(51, 51, 255); font-weight: bold;"><br /><br />Q: I want to a set a bit of position pos from x by true?</span><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">A:</span><br />#define SETBIT(x,pos) ( (x) | ( UNITY << (pos) ) ) unsigned x = 25 ; x = SETBIT(x,10) ; <span style="color: rgb(51, 51, 255); font-weight: bold;"><br /><br />Q: I want to a reset a bit of position pos from x by false?</span><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">A:</span><br />#define RESETBIT(x,pos) ( (x) & ~( UNITY << (pos) ) ) unsigned x = 25 ; x = RESETBIT(x,0) ; <span style="font-weight: bold; color: rgb(51, 51, 255);"><br /><br />Q: I want to a reset a bit of position pos from x by false?</span><br /><span style="font-weight: bold; color: rgb(51, 51, 255);">A:</span><br />#define ISPWRTWO(x) (!((x) & ((x) - 1))<br />bool bit = ISPWRTWO(5) ;<br /><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">Q: I want to swap 2 variables a, b without external space?</span><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">A:</span><br />#define SWAP(x, y) (((x) ^= (y)), ((y) ^= (x)), ((x) ^= (y)))<br />int d = 8 , e = 9 ;<br />SWAP(d,e) ;<br /><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">Q: In my assembler I want to set n, i, x, ….?</span><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">A: </span>Just use SETBIT Macro and use bit 6<br />#define NPOSITION 6<br />#define SETBIT(x,pos) ( (x) | ( UNITY << (pos) ) ) unsigned x = 25 ; x = SETBIT(x, NPOSITION); Other faster idea is to compute UNITY << style="color: rgb(51, 51, 255); font-weight: bold;">Q: I want to multiply an integer by 640?</span><br /><span style="color: rgb(51, 51, 255); font-weight: bold;">A: </span>Never use multiplications for multiplying an integer with a constant value. instead use addition and shift operations.<br />#define MUL640(x) ( ((x)<<7) + ((x)<<9) )<br />int x = MUL640(3) ;<br />We used 3 operations to multiply x by 640. But they are faster than operator *.<br />x = x *(128 + 512) ;<br />x = x * 128 + x * 512 ;Anonymoushttp://www.blogger.com/profile/04997739304766791351noreply@blogger.com0