#include #include #include namespace regexis024 { void reattach_fa_node_edge(FA_Node **old_node_ptr, FA_Node *new_node) { assert(old_node_ptr); if (*old_node_ptr){ assert((**old_node_ptr).refs); (**old_node_ptr).refs--; } if (new_node) new_node->refs++; *old_node_ptr = new_node; } /* We basically reattch fa.start to node */ void yay_new_start(FA_Container &fa, FA_NodePathPart *node) { assert(node); node->refs++; node->nxt_node = fa.start; fa.start = node; } void add_option_to_fork_node(FA_NodeOfForking *fnode, FA_Node *transition_dest) { fnode->nxt_options.push_back(transition_dest); if(transition_dest) transition_dest->refs++; } void reattach_nxt_node(FA_NodePathPart *node, FA_Node *dest) { reattach_fa_node_edge(&(node->nxt_node), dest); } FA_Node* copy_node_no_container_adjustments(FA_Node& node){ FA_Node* res; /* Using implicitly defined copy constructors */ #define typeCase(etype, ctype) case etype: res = new ctype((ctype&)node); break; switch (node.type) { typeCase(match, FA_NodeOfMatch) typeCase(one_char_read, FA_NodeOfOneCharRead) typeCase(forking, FA_NodeOfForking) typeCase(look_one_behind, FA_NodeOfLookOneBehind) typeCase(look_one_ahead, FA_NodeOfLookOneAhead) typeCase(track_array_mov_imm, FA_NodeOfTrackArrayMovImm) typeCase(track_array_mov_halfinvariant, FA_NodeOfTrackArrayMovHalfinvariant) typeCase(det_char_crossroads, FA_NodeOfDetCharCrossroads) default: assert(false); } #undef typeCase res->refs = 0; res->search_mark = -1; return res; } /* In case when transferring the ownership of this new raw pointer has failed, node is destroyed, exception is thrown */ FA_Node *copy_fa_node(FA_Node& node, FA_Container &fa) { FA_Node* res = copy_node_no_container_adjustments(node); /* Can invalidate ponter res (in which case it also throws exeption, so none of this matters in the end) */ fa.registerNew(res); res->reAdd_references(); return res; } FA_Node *copy_fa_node_to_another_fa(FA_Node& node, FA_Container &resultFa) { FA_Node* res = copy_node_no_container_adjustments(node); resultFa.registerNew(res); return res; } }