File indexing completed on 2026-04-09 07:49:41
0001 #pragma once
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020 #include <cassert>
0021 #include <vector>
0022 #include "snd.hh"
0023 #include "sn.h"
0024
0025 struct sndtree
0026 {
0027 static int CommonTree_PlaceLeaves( const std::vector<int>& leaves, int op ) ;
0028 static int Build_r(sn* n, int& num_leaves_placed, const std::vector<int>& leaves, int d );
0029 };
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 inline int sndtree::CommonTree_PlaceLeaves( const std::vector<int>& leaves, int op )
0054 {
0055 int num_leaves = leaves.size() ;
0056 std::vector<int> leaftypes ;
0057 snd::GetTypes(leaftypes, leaves);
0058
0059 sn* n = sn::CommonOperatorTypeTree( leaftypes, op );
0060
0061 int num_leaves_placed = 0 ;
0062 int root = Build_r(n, num_leaves_placed, leaves, 0 );
0063
0064 bool expect_leaves = num_leaves_placed == num_leaves ;
0065 if(!expect_leaves) std::cerr << "sndtree::CommonTree_PlaceLeaves UNEXPECTED LEAVES " << std::endl;
0066 assert( expect_leaves );
0067
0068 delete n ;
0069
0070 snd* r = snd::Get_(root);
0071 r->sibdex = 0 ;
0072
0073 return root ;
0074 }
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087 inline int sndtree::Build_r(sn* n, int& num_leaves_placed, const std::vector<int>& leaves, int d )
0088 {
0089 int N = -1 ;
0090 if( n->is_operator() )
0091 {
0092 int op = n->typecode ;
0093 int nc = n->num_child();
0094 bool nc_expect = nc == 2 ;
0095 if(!nc_expect) std::cerr << "sndtree::Build_r nc_expect " << std::endl ;
0096 assert( nc_expect );
0097 sn* l = n->get_child(0);
0098 sn* r = n->get_child(1);
0099 int L = Build_r(l, num_leaves_placed, leaves, d+1) ;
0100 int R = Build_r(r, num_leaves_placed, leaves, d+1) ;
0101 N = snd::Boolean( op, L, R );
0102 }
0103 else
0104 {
0105 N = leaves[num_leaves_placed] ;
0106 num_leaves_placed += 1 ;
0107 }
0108 return N ;
0109 }
0110
0111