ExceptIntersectSetOperator::ExceptIntersectSetOperator( std::vector<Table*>& input_tables, Table* output_table, bool is_all, bool is_except) : SetOperator(input_tables, output_table, is_all), m_is_except(is_except) { if (!is_except) { // For intersect we want to start with the smalest table std::vector<Table*>::iterator minTableIt = std::min_element(m_input_tables.begin(), m_input_tables.end(), TableSizeLess()); std::swap( m_input_tables[0], *minTableIt); } }
bool ExceptIntersectSetOperator::processTuples() { // Map to keep candidate tuples. The key is the tuple itself // The value - tuple's repeat count in the final table. TupleMap tuples; assert( ! m_input_tables.empty()); size_t ii = m_input_tablerefs.size(); while (ii--) { m_input_tables[ii] = m_input_tablerefs[ii].getTable(); } if ( ! m_is_except) { // For intersect we want to start with the smallest table std::vector<Table*>::iterator minTableIt = std::min_element(m_input_tables.begin(), m_input_tables.end(), TableSizeLess()); std::swap(m_input_tables[0], *minTableIt); } // Collect all tuples from the first set Table* input_table = m_input_tables[0]; collectTuples(*input_table, tuples); // // For each remaining input table, collect its tuple into a separate map // and substract/intersect it from/with the first one // TupleMap next_tuples; for (size_t ctr = 1, cnt = m_input_tables.size(); ctr < cnt; ctr++) { next_tuples.clear(); input_table = m_input_tables[ctr]; assert(input_table); collectTuples(*input_table, next_tuples); if (m_is_except) { exceptTupleMaps(tuples, next_tuples); } else { intersectTupleMaps(tuples, next_tuples); } } // Insert remaining tuples to the output table for (TupleMap::const_iterator mapIt = tuples.begin(); mapIt != tuples.end(); ++mapIt) { TableTuple tuple = mapIt->first; for (size_t i = 0; i < mapIt->second; ++i) { m_output_table->insertTempTuple(tuple); } } return true; }