<<< Constant scalar | Lambda Home | Exclusive OR: operator_xor helper function >>> |
Common Text Transformation Library http://cttl.sourceforge.net/
When constant scalars are joined by the overloaded operator^()
const_scalar( 1 )^const_scalar( 2 )^const_scalar( 3 )
the resulting expression formulates an object that is roughly equivalent to an anonymous struct
struct { static const int x = 1; static const int y = 2; static const int z = 3; } anonymous;
CTTL library refers to such object as a constant single-dimensional lambda composite. A composite is resolved by the compiler at compile time, with no run-time overhead.
More complex lambda composites are possible, including hierarchical composite trees. The nodes of the resulting object are sequentially traversable and directly navigable via a subscript-style access to each terminal node in the composite tree. The subscript index for direct access is also a lambda composite, assembled entirely from the constant scalar terminal nodes.
Lambda composites become extremely useful in lambda expressions that annotate and populate branches of grammar trees, which are the output data structures of many parser programs.
When the upcoming C++0x standard replaces C++03, it will be possible to instantiate standalone composite structures by the auto keyword:
auto composite = initializer;
Keywords: lambda_composite_tree.cpp, dump_lambda, get_lambda_size, capacity_, const_scalar, inode_default_descriptor, inode_writer
// lambda_composite_tree.cpp // Program demonstrates usage of lambda composite tree. //#define CTTL_TRACE_RULES // automatically turns lambda tracing on #include "cttl/cttl.h" #include "lambda/lambda.h" #include "utils/inode.h" #include "example/lambda/syntax/composite/dump_lambda.h" using namespace cttl; template< typename LambdaT > void auto_Cpp0X( LambdaT tree ) { // Display tree content dump_lambda( tree ); std::cout << "\n\tTree size: " << get_lambda_size( tree ) << "\n\tTree capacity: " << tree.capacity_ << " (number of nodes in the tree)" << '\n' ; // Deposit data into the tree nodes tree.top( const_scalar( 1 ) ) = -10; tree.top( const_scalar( 2 ) ) = -20; // Stack operations on tree nodes: tree.push( const_scalar( 3 )^const_scalar( 0 ), 100 ); tree.push( const_scalar( 3 )^const_scalar( 0 ), 200 ); tree.push( const_scalar( 3 )^const_scalar( 0 ), 300 ); tree.top( const_scalar( 3 )^const_scalar( 1 ) ) = -310; // Display tree content dump_lambda( tree ); std::cout << "\n\tTree size: " << get_lambda_size( tree ) << "\n\tTree capacity: " << tree.capacity_ << " (number of nodes in the tree)" << '\n' ; // Serialize tree into a vector: std::vector< int > vect_tree( 1 ); inode_writer< inode_default_descriptor, std::vector< int > > writer( vect_tree ); // Note 1: the stack nodes must be populated; // If a stack is empty, the assert should fail. // Note 2: only top element of the stack gets serialized; // The stack content remains unchanged as a result of serialization. writer <<= tree; //Display vector: std::cout << std::endl << "\tResulting vector:" << std::endl ; std::copy( vect_tree.begin(), vect_tree.end(), std::ostream_iterator< int >( std::cout, "; " ) ); std::cout << std::endl; } int main(/*int argc, char* argv[]*/) { // Under C++0x a tree can be instantiated as a standalone object: // auto tree = initializer; // For now, using a surrogate function auto_Cpp0X() // to demo the lambda composite structure: auto_Cpp0X( // Construct a tree scalar( 1 ) ^ ( scalar( 10 ) ^ scalar( 11 ) ) ^ scalar( 2 ) ^ ( make_stack( 30 ) ^ scalar( 31 ) ) ^ scalar( 4 ) ); return 0; } /*Output: -------------------------------------------------------------- subscript size stack_ptr top --------- ----- ---------- ---------------------- 0 1 0 1 1^0 1 0 10 1^1 1 0 11 2 1 0 2 3^0 0 0x22febc empty 3^1 1 0 31 4 1 0 4 Tree size: 6 Tree capacity: 7 (number of nodes in the tree) -------------------------------------------------------------- subscript size stack_ptr top --------- ----- ---------- ---------------------- 0 1 0 1 1^0 1 0 -10 1^1 1 0 11 2 1 0 -20 3^0 3 0x22febc 300 3^1 1 0 -310 4 1 0 4 Tree size: 9 Tree capacity: 7 (number of nodes in the tree) Resulting vector: 0; 1; -10; 11; -20; 300; -310; 4; */
//////////////////////////////////////////////////////////////////////// // // This file is part of Common Text Transformation Library. // Copyright (C) 1997-2009 by Igor Kholodov. // // Common Text Transformation Library is free software: you can // redistribute it and/or modify it under the terms of the // GNU Lesser General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // Common Text Transformation Library is distributed in the hope // that it will be useful, but WITHOUT ANY WARRANTY; without even // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // PURPOSE. See the GNU Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with Common Text Transformation Library. // If not, see <http://www.gnu.org/licenses/>. // // mailto:cttl@users.sourceforge.net // http://cttl.sourceforge.net/ // http://sourceforge.net/projects/cttl/ // //////////////////////////////////////////////////////////////////////// /**@file dump_lambda.h * @brief Defines lambda diagnostic structures. * */ // dump_lambda.h #ifndef _DUMP_LAMBDA_H_INCLUDED_ #define _DUMP_LAMBDA_H_INCLUDED_ struct dump_subscript_terminal { size_t position; dump_subscript_terminal() : position( 0 ) { } template< typename TerminalT > void operator() ( TerminalT const& ) { std::cout << ( position++ ? "^" : "" ) << TerminalT::const_value_ ; } }; // struct dump_subscript_terminal template< typename LambdaT > struct dump_lambda_terminals { LambdaT const& lambda; dump_lambda_terminals( LambdaT const& lambda_ ) : lambda( lambda_ ) { } private: /**Assignment is disabled.*/ dump_lambda_terminals< LambdaT >& operator=( dump_lambda_terminals< LambdaT > const& ); public: static void dump_header() { std::cout << std::endl << "--------------------------------------------------------------" << std::endl << "subscript" <<'\t'<< "size" <<'\t'<< "stack_ptr" <<'\t'<<'\t'<< "top" << std::endl << "---------" <<'\t'<< "-----" <<'\t'<< "----------" <<'\t'<< "----------------------" << std::endl ; } template< typename SubscriptLambdaT > void operator() ( SubscriptLambdaT subscript_ ) const { dump_subscript_terminal dump_sub; subscript_.traverse_top_down( dump_sub ); //subscript_.traverse_bottom_up( dump_sub ); std::cout << '\t' << '\t' << lambda.size( subscript_ ) << '\t' << lambda.stack_ptr( subscript_ ) << '\t' << '\t' ; if ( lambda.size( subscript_ ) ) { #if ( defined( CTTL_TRACE_DEPOSITS ) ) lambda.top( subscript_ ); #else std::cout << lambda.top( subscript_ ) ; #endif // CTTL_TRACE_DEPOSITS } else { std::cout << "empty\t" ; } std::cout << std::endl ; } }; // struct dump_lambda_terminals template< typename LambdaT > void dump_lambda( LambdaT const& lambda_ ) { dump_lambda_terminals< LambdaT > dump_dep( lambda_ ); dump_dep.dump_header(); lambda_.subscript_top_down( dump_dep ); } template< typename LambdaT > struct lambda_size { LambdaT const& lambda; size_t size; lambda_size( LambdaT const& lambda_ ) : lambda( lambda_ ), size( 0 ) { } private: /**Assignment is disabled.*/ lambda_size< LambdaT >& operator=( lambda_size< LambdaT > const& ); public: template< typename SubscriptLambdaT > void operator() ( SubscriptLambdaT subscript_ ) { size += lambda.size( subscript_ ); } }; // struct lambda_size template< typename LambdaT > size_t get_lambda_size( LambdaT const& lambda_ ) { lambda_size< LambdaT > size_functor( lambda_ ); lambda_.subscript_top_down( size_functor ); return size_functor.size; } #endif //_DUMP_LAMBDA_H_INCLUDED_
Copyright © 1997-2009 Igor Kholodov mailto:cttl@users.sourceforge.net.
Permission to copy and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.
<<< Constant scalar | Lambda Home | Exclusive OR: operator_xor helper function >>> |