The Boost Library; Overview, Code, Applications

Postby Cuchulainn » Wed Mar 25, 2009 6:16 pm

are there any examples of converting awk to boost.regex? For instance, one of the benefits of awk is the ability to change the field/record separator from

space/CR, is this possible in boost??




For regular expressions boost has libraries:



regex (low level)

xpressive (new one, more flexible)

Spirit (full-blown parser)



So, I think YES to your answer. An option is to model AWK fields as boost.Tuples.



Q. Do you read in AWK Ascii files into program?[/i]
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby zeta » Wed Mar 25, 2009 6:54 pm

awesome...



My interest vis a vis doing pattern matching is in parsing and converting *ps files (vector drawings) to XML/SVG for a fancy software suite and (perhaps :) ) search engine.



So the input is ASCII, it's the *ps source. I have statements like:



Code: Select all
q 1.00201 -0.997991 scale

5492.16 -4656.55 -2.37 0 -4.8 0 -2.4 0 -2.4 0 -2.37 0 -7.2 0 -4.78 0

-4.8 0 -2.4 0 -4.78 0 -4.8 0 -4.8 0 -2.37 0 -2.4 0 -4.8 0

-2.4 0 -4.78 0 -2.4 0 -2.4 0 -4.78 0 -2.4 0 -2.4 0 -2.4 0

-2.4 0 -4.78 0 -2.4 0 -2.4 0 -2.4 0 -4.78 0 -2.4 0 -2.4 0

-4.78 0 -4.8 0 -2.4 0 -4.78 0 -4.8 0 -2.4 0 -2.4 0 -2.37 0

-7.2 0 -2.4 0 -2.37 0 -2.4 0 -4.8 0 -2.4 0 S

Q

q 1.00201 -0.997991 scale

5336.47 -4656.55 -2.37 0 -4.8 0 -2.4 0 -4.78 0 -2.4 0 -2.4 0 -2.4 0

-2.38 0 -2.4 0 -2.4 -2.4 -2.4 0 -2.4 0 -2.38 0 0 0 -2.4 -2.43

0 -2.4 -2.4 -9.62 0 0 -2.4 7.22 0 2.4 -2.4 4.83 -2.38 0 -2.4 0

-2.4 0 -2.4 0 -2.4 0 -2.38 0 -2.4 0 -2.4 0 -2.4 0 -2.4 0

-2.38 0 -2.4 0 -2.4 0 -2.4 0 -2.4 0 -2.38 0 -2.4 0 0 0

-2.4 -2.43 -2.4 0 -4.78 0 -2.4 0 0 -2.4 -2.4 0 0 -2.4 -2.4 0

^ -2.4 2.4 -2.38 0 -2.4 -2.4 0 0 -2.4 -2.4 0 -2.4 -2.4 -7.23

0 0 -2.4 -12.02 0 -4.8 -2.38 -16.85 0 -2.4 -2.4 -36.07 0 -12.02 -2.4 -26.45

0 -7.23 -2.4 -75.75 0 -24.05 -2.4 -173.16 0 -40.87 -2.38 -444.89 0 -227.26 -1.2 -1578.77 S

Q





which are polylines. A simple awk solution to grab the n-tuples of line drawings is to use commands 'scale' and 'q' as brackets, ala:



Code: Select all
awk '$4=="scale",$1=="Q" {print $0 > "gHjLp.txt"}' gHjLz.ps





If I could do this in boost, I would move beyond a working prototype to real software..
User avatar
zeta
 
Posts: 9
Joined: Sat Feb 17, 2007 10:49 pm

Postby Cuchulainn » Wed Mar 25, 2009 9:25 pm

zeta,

By the looks of it, I think you need Spirit if you wish to execute actions a la awk.



And *Kleene star* notation is useful for describing the 'grammar' of your .ps file. For example, polyline is (x y z) {3, } <==> 3 or more 3d points etc.



The makers of Spirit actually refer to AWK.



My instinct says 'yes'.
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

parsing postscript

Postby bojan » Thu Mar 26, 2009 9:47 am

It is worth giving XPressive a try, it can do almost everything spirit can do as well as regular expressions. I could imagine it would make a good tool for the postscript example, especially if you want to be able to ignore sections of text which you do not understand how to parse. I've got the draft section on expressive at:



http://www.bnikolic.co.uk/boostqf/xpressive.html
bojan
 
Posts: 4
Joined: Fri Mar 20, 2009 9:49 am

Postby zeta » Thu Mar 26, 2009 4:55 pm

guys, this is great! I'm putting in something at docEng09 on extraction of chemical information, but after this is done I'd like to go into production using boost... my vision is for a search engine for difficult data



tell me more about this kleene star notation..
User avatar
zeta
 
Posts: 9
Joined: Sat Feb 17, 2007 10:49 pm

Postby Cuchulainn » Thu Mar 26, 2009 5:20 pm

The Kleene * is similar to EBNF (Extended Backus Naur Form) for regular expressions and metadata. Maybe Google is the best place for the moment.



Spirit supports all this stuff, as stated in their documentation. As Bojan says, xpressive might less effort to get up and running.



Here is an article on Spirit



http://www.ddj.com/cpp/184401692;jsessi ... tid=268398



Spirit makes it easy to write parsers directly in C++. Thanks to the expressiveness of C++ operator overloading and the flexibility of template metaprogramming, grammars can be written in a high-level syntax very similar to EBNF. These grammar descriptions can mix freely with C++ code. Semantic actions, attached to any part of the grammar, seamlessly link EBNF grammar specifications to C++ code. The intricacies of the parsing engine framework are hidden beneath an intuitive C++/EBNF interface. Using Spirit is an order of a magnitude easier, compared to coding a parser by hand or perhaps even using a stand-alone parser such as YACC. Since everything is done in C++, there is no need to use an external parser generator along with all the excess baggage that comes with it. Spirit is a flexible, lightweight, and simple-to-use C++ library suitable for many parsing related programming tasks.
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby zeta » Thu Mar 26, 2009 6:18 pm

wonderful... looks like a late night for me tonight :)



dan, can you recommend a good design patterns book, perhaps something of yours? Do you have any online design pattern courses??
User avatar
zeta
 
Posts: 9
Joined: Sat Feb 17, 2007 10:49 pm

Postby Cuchulainn » Sat Mar 28, 2009 1:16 pm

zeta,

The classic DP book is by Gamma et al (1995). My book are DP oriented and the forthcoming Monte Carlo book has 7 chapters.

hth
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sat Mar 28, 2009 1:16 pm

4. An Introduction to Smart Pointers in boost

The Boost.Smart_ptr resolves many of the problems that we encounter when managing the lifecycle of resources, in particular dynamically allocated objects. There are six template classes and each one addresses one or more specific use cases associated with object lifecycle. We discuss these smart pointer classes from three main viewpoints or dimensions related to these objects:



. scope and visibility dimension: once an object has been created, we need to determine if the object can be accessed in a single function block or whether it can be accessed in other parts of code

. time dimension: when is an objected destructed and when it is allowed to remove the object from memory

. access dimension: who is the ?owner? of an object (if at all), transfer of ownership, accessing dynamically created objects from different code blocks and containers



In general, smart pointers fall into two main categories, name those with a limited scope and where it is not possible to change ownership (note the contrast with STL?s auto_ptr) and those that survive the code block in which they are defined and where we cannot signal a clear owner of the dynamically created object or array of objects. The six template classes are:



. scoped_ptr: simple sole ownership of single objects. Ownership is not transferable.

. scoped_array: simple sole ownership of arrays. Ownership is not transferable.

. shared_ptr: object ownership and accessibility among multiple pointers.

. shared_array: array ownership and accessibility among multiple pointers. This is non-intrusive.

. weak_ptr: (non-owner) observers of an object owned by shared_ptr.

. intrusive_ptr: this is kind of customized version of shared_ptr because the developer must provide the reference counting mechanism. Using this class allows us to treat shared_ptr as a smart pointer.



In general, the classes scoped_ptr and shared_ptr are the most useful smart pointer types to use in applications.
Attachments
TestScoped001.cpp
(1.19 KiB) Downloaded 856 times
TestSharedPtr001.cpp
(2.1 KiB) Downloaded 891 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun Apr 05, 2009 11:14 am

Here is the first post in a series on modelling ALGORITHMS in C++, STL and boost. We build up to Bind, Lambda, Functions, Signals and Slots.



In total, about 5 posts relating to this topic.



This is the first 101 on function objects.
Attachments
BindPartI.doc
(33 KiB) Downloaded 921 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Thu Apr 09, 2009 11:02 am

Bind, Part II



Introducing boost Bind

The Boost.Bind library extends and improves the functionality that bind1st and bind2nd offer. These functions can be difficult to understand and they are awkward to use. The Boost.Bind resolves these problems by normalizing the syntax and extending the functionality that STL offers, for example:



. Functions and function pointers

. Function objects

. Pointers to member functions and to member data

. Nested binds and functional composition

. Overloaded operators



In short, this library avoid much of the verbose syntax in STL and in this way it is possible to write code that is easy to understand.
Attachments
TestBind101.cpp
(1.03 KiB) Downloaded 937 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun Apr 12, 2009 11:13 am

Bind (and Function), Part III



The concept of functional composition is closely related to mathematics. In general, function map a domain into a range. We can now ?combine? functions in the sense that the range of one function can be the domain of another function. We can carry out this process ad infinitum in principle and it is has many applications in general, for example in the Method of Lines in PDEs and FDM. We now give examples to show how function composition works.



many applications of this technique in comp finance and mathematical physics...
Attachments
TestBindSemiDiscretisation101.cpp
(1.37 KiB) Downloaded 909 times
TestFunctionComposition.cpp
(861 Bytes) Downloaded 883 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Sun Apr 12, 2009 5:17 pm

You might not know this site; easy way to install boost





http://www.boostpro.com/download
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Mon Apr 13, 2009 12:15 pm

Part IV: an example showing how to combne Function, Bind (and Lambda)
Attachments
TestLambda101.cpp
(1.07 KiB) Downloaded 915 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

Postby Cuchulainn » Thu Apr 16, 2009 10:14 pm

Function, Part V (and last)

///////////////////////////////



The Boost.Signals library contains a number of templates that allows developers to implement data and notification patterns, most notably:



. The Observer (Publisher-Subscriber) pattern (GOF 1995)

. Multicast callbacks for functions and function objects

. Event notification in GUI applications



The two central roles in this pattern are the signal and the slot. A signal emits events or signals while a slot is a connection object that receives the signal. In general, we model situations in which there is a one-to-many relation between one source of information (which can change) and several objects that have registered interest in knowing when this information changes. This is precisely the well-known Observer pattern as described in GOF 1995. However, the Boost.Signal library avoids the problems associated with the former pattern, mainly by not using the inheritance mechanism.

Some of the features in the Boost.Signals library that make it attractive as a major component in applications are:



. Signals and slots are loosely coupled; in this sense that are similar to .NET delegates and are more flexible that the way the Observer pattern is implemented in Java

. Role modeling; slots can themselves be signals for other slots (in other words, we can

data dependency graphs)

. We can influence the relative order in which slots are updated by placing them in groups; for example, the slots in group 0 will be notified before the slots in group 1

. We can create slots ?on the fly? using Boost.Bind and Boost.Lambda

. Integration with Boost.Function

. We can use Combiners to write your own event mechanisms



The Boost.Signals library can be adapted to suit a number of designs and system patterns such as PAC, MVC, Layers and Mediator (see GOF 1995, POSA 1996). It is strategic. It has a number of applications to computational finance for example recalculating the risk in a portfolio of structured product when the interest rate is ?bumped?.
Attachments
TestSignals.cpp
(713 Bytes) Downloaded 900 times
User avatar
Cuchulainn
 
Posts: 677
Joined: Mon Dec 18, 2006 2:48 pm
Location: Amsterdam, the Netherlands

PreviousNext

Return to C++

Who is online

Users browsing this forum: No registered users and 1 guest

cron