std::vector<std::vector<int>> GetCombinations(const std::vector<int>& input)
{
	if (input.size() == 1)
	{
		return { input };
	}
	if (input.size() == 2)
	{
		return 
		{
			{input[0], input[1]},
			{input[1], input[0]}
		};
	}

	std::vector<std::vector<int>> result;
	for (const auto& element : input)
	{
		std::vector<int> diffSet = input;
		diffSet.erase(std::find(diffSet.begin(), diffSet.end(), element));

		auto& arrangements = GetCombinations(diffSet);
		for (auto& arrangement : arrangements)
		{
			arrangement.insert(arrangement.begin(), element);
			result.push_back(arrangement);
		}
	}
	return result;
}
Example #2
0
 vector<vector<int> > combinationSum(vector<int> &candidates, int target) {  
   // Start typing your C/C++ solution below  
   // DO NOT write int main() function  
   vector<vector<int> > result;  
   vector<int> solution;  
   int sum=0;  
   std::sort(candidates.begin(), candidates.end());  
   GetCombinations(candidates,sum, 0, target, solution, result);  
   return result;  
 }  
int _tmain(int argc, _TCHAR* argv[])
{
	std::vector<int> input = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

	clock_t begin = clock();
	auto result = GetCombinations(input);
	clock_t end = clock();
	double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
	std::cout << elapsed_secs << std::endl;
	std::cout << "1 millions lexicographic permutation is :" ;
	const auto& oneMilliont = result[1000000];
	for (const auto& digit : oneMilliont)
	{
		std::cout << digit << " , ";
	}	
	std::cout << "\nTotal number of permutations is " << result.size() << std::endl;

	return 0;
}
Example #4
0
void OBSHotkeyWidget::Save(std::vector<obs_key_combination_t> &combinations)
{
	GetCombinations(combinations);
	Apply();

	auto AtomicUpdate = [&]()
	{
		ignoreChangedBindings = true;

		obs_hotkey_load_bindings(id,
				combinations.data(), combinations.size());

		ignoreChangedBindings = false;
	};
	using AtomicUpdate_t = decltype(&AtomicUpdate);

	obs_hotkey_update_atomic([](void *d)
	{
		(*static_cast<AtomicUpdate_t>(d))();
	}, static_cast<void*>(&AtomicUpdate));
}
Example #5
0
 void GetCombinations(  
   vector<int>& candidates,  
   int& sum,  
   int level,  
   int target,  
   vector<int>& solution,  
   vector<vector<int> >& result)  
 {  
   if(sum > target) return;  
   if(sum == target)  
   {  
     result.push_back(solution);  
     return;  
   }  
   for(int i =level; i< candidates.size(); i++)  
   {  
     sum+=candidates[i];  
     solution.push_back(candidates[i]);  
     GetCombinations(candidates, sum, i, target, solution, result);  
     solution.pop_back();  
     sum-=candidates[i];        
   }  
 }  
void pvdb::Cluster::Test()
{
  {
    static bool is_tested = false;
    if (is_tested) return;
    is_tested = true;
  }
  TRACE("Cluster::Test started");
  //Test operator== and operator!=
  {
    const std::vector<boost::shared_ptr<pvdb::Cluster> > tmp_tests_1 = pvdb::ClusterFactory::GetTests();
    assert(std::count_if(tmp_tests_1.begin(),tmp_tests_1.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return !p; } ) == 0); //FIX 2013-01-02
    //assert(std::all_of(tmp_tests_1.begin(),tmp_tests_1.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return p; } )); //BUG IN CROSSCOMPILER 2013-01-02
    const int sz = static_cast<int>(tmp_tests_1.size());
    for (int i=0; i!=sz; ++i)
    {
      const std::vector<boost::shared_ptr<pvdb::Cluster> > tmp_tests_a = pvdb::ClusterFactory::GetTests(); //For crosscompiler
      assert(std::count_if(tmp_tests_a.begin(),tmp_tests_a.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return !p; } ) == 0);
      //assert(std::all_of(tmp_tests_a.begin(),tmp_tests_a.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return p; } ));
      const boost::shared_ptr<const pvdb::Cluster> a = tmp_tests_a.at(i);
      assert(a);
      const std::vector<boost::shared_ptr<pvdb::Cluster> > tmp_tests_b = pvdb::ClusterFactory::GetTests(); //For crosscompiler
      assert(std::count_if(tmp_tests_b.begin(),tmp_tests_b.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return !p; } ) == 0);
      //assert(std::all_of(tmp_tests_b.begin(),tmp_tests_b.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return p; } ));
      const boost::shared_ptr<pvdb::Cluster> b = tmp_tests_b.at(i);
      assert(a); assert(b);
      assert(a==a);
      assert(a==b);
      assert(b==a);
      assert(b==b);
      for (int j=0; j!=sz; ++j)
      {
        const std::vector<boost::shared_ptr<pvdb::Cluster> > tmp_tests_c = pvdb::ClusterFactory::GetTests(); //For crosscompiler
        assert(std::count_if(tmp_tests_c.begin(),tmp_tests_c.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return !p; } ) == 0);
        //assert(std::all_of(tmp_tests_c.begin(),tmp_tests_c.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return p; } ));
        const std::vector<boost::shared_ptr<pvdb::Cluster> > tmp_tests_d = pvdb::ClusterFactory::GetTests(); //For crosscompiler
        assert(std::count_if(tmp_tests_d.begin(),tmp_tests_d.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return !p; } ) == 0);
        //assert(std::all_of(tmp_tests_d.begin(),tmp_tests_d.end(),[](const boost::shared_ptr<pvdb::Cluster>& p) { return p; } ));

        const boost::shared_ptr<const pvdb::Cluster> c = tmp_tests_c.at(j);
        assert(c);
        const boost::shared_ptr<      pvdb::Cluster> d = tmp_tests_d.at(j);
        assert(c); assert(d);
        assert(c==c); assert(c==d); assert(d==c); assert(d==d);
        if (i==j)
        {
          assert(a==c); assert(a==d);
          assert(b==c); assert(b==d);
          assert(c==a); assert(c==b);
          assert(d==a); assert(d==b);
        }
        else
        {
          assert(a!=c); assert(a!=d);
          assert(b!=c); assert(b!=d);
          assert(c!=a); assert(c!=b);
          assert(d!=a); assert(d!=b);
        }
      }
    }
  }
  //Test all Clusters with each combination of Concepts
  {
    const std::vector<std::vector<boost::shared_ptr<pvdb::Concept> > > v
      = GetCombinations(pvdb::ConceptFactory::GetTests());
    std::for_each(v.begin(),v.end(),
      [](const std::vector<boost::shared_ptr<pvdb::Concept> >& concepts)
      {
        const boost::shared_ptr<pvdb::Cluster> c(new pvdb::Cluster(concepts));
        assert(c);
        const std::string s = ToXml(c);
        const boost::shared_ptr<pvdb::Cluster> d = FromXml(s);
        assert(d);
        assert(c == d);
      }
    );
  }
  TRACE("Cluster::Test finished successfully");
}
void ribi::pvdb::TestHelperFunctions()
{
  {
    static bool is_tested = false;
    if (is_tested) return;
    is_tested = true;
  }
  TRACE("Started TestHelperFunctions");
  //GetRegexMatches
  {
    const std::string s = "In the Netherlands, 1234 AB and 2345 BC are valid zip codes";
    std::vector<std::string> expected;
    expected.push_back("1234 AB");
    expected.push_back("2345 BC");
    {
      const std::string r = "(\\d{4} [A-Z]{2})";
      assert(pvdb::GetRegexMatches(s,QRegExp(r.c_str())) == expected);
    }
  }
  {
    const std::string s = "<concept><name>Concept with examples</name><example>Example 1</example><example>Example 2</example><example>Example 3</example></concept>";
    assert(std::count(s.begin(),s.end(),'\b') == 0);
    std::vector<std::string> expected;
    expected.push_back("<example>Example 1</example>");
    expected.push_back("<example>Example 2</example>");
    expected.push_back("<example>Example 3</example>");
    {
      const std::string r = "(<example>.*</example>)";
      assert(pvdb::GetRegexMatches(s,QRegExp(r.c_str())) == expected);
    }
  }
  //GetCombinations
  //Assume the number of elements is correct
  assert(GetCombinations(std::vector<int>( {         } ) ).size() ==  1);
  assert(GetCombinations(std::vector<int>( {1        } ) ).size() ==  2);
  assert(GetCombinations(std::vector<int>( {1,2      } ) ).size() ==  4);
  assert(GetCombinations(std::vector<int>( {1,2,3    } ) ).size() ==  8);
  assert(GetCombinations(std::vector<int>( {1,2,3,4  } ) ).size() == 16);
  assert(GetCombinations(std::vector<int>( {1,2,3,4,5} ) ).size() == 32);
  //Assume the elements are correct
  {
    const std::vector<std::vector<int> > v = GetCombinations(std::vector<int>( { 1 } ) );
    const std::vector<int> expected_0 = {};
    const std::vector<int> expected_1 = {1};
    assert(std::count(v.begin(),v.end(),expected_0));
    assert(std::count(v.begin(),v.end(),expected_1));
  }
  {
    const std::vector<std::vector<int> > v = GetCombinations(std::vector<int>( { 1,2 } ) );
    const std::vector<int> expected_0 = {};
    const std::vector<int> expected_1 = {1};
    const std::vector<int> expected_2 = {2};
    const std::vector<int> expected_3 = {1,2};
    assert(std::count(v.begin(),v.end(),expected_0));
    assert(std::count(v.begin(),v.end(),expected_1));
    assert(std::count(v.begin(),v.end(),expected_2));
    assert(std::count(v.begin(),v.end(),expected_3));
  }
  {
    const std::vector<std::vector<int> > v = GetCombinations(std::vector<int>( { 1,2,3 } ) );
    const std::vector<int> expected_0 = {};
    const std::vector<int> expected_1 = {1};
    const std::vector<int> expected_2 = {2};
    const std::vector<int> expected_3 = {3};
    const std::vector<int> expected_4 = {1,2};
    const std::vector<int> expected_5 = {1,3};
    const std::vector<int> expected_6 = {2,3};
    const std::vector<int> expected_7 = {1,2,3};
    assert(std::count(v.begin(),v.end(),expected_0));
    assert(std::count(v.begin(),v.end(),expected_1));
    assert(std::count(v.begin(),v.end(),expected_2));
    assert(std::count(v.begin(),v.end(),expected_3));
    assert(std::count(v.begin(),v.end(),expected_4));
    assert(std::count(v.begin(),v.end(),expected_5));
    assert(std::count(v.begin(),v.end(),expected_6));
    assert(std::count(v.begin(),v.end(),expected_7));
  }
  {
    const std::vector<std::vector<int> > v = GetCombinations(std::vector<int>( { 1,2,3,4 } ) );
    const std::vector<int> expected_0 = {};
    const std::vector<int> expected_1 = {1};
    const std::vector<int> expected_2 = {2};
    const std::vector<int> expected_3 = {3};
    const std::vector<int> expected_4 = {4};
    const std::vector<int> expected_5 = {1,2};
    const std::vector<int> expected_6 = {1,3};
    const std::vector<int> expected_7 = {1,4};
    const std::vector<int> expected_8 = {2,3};
    const std::vector<int> expected_9 = {2,4};
    const std::vector<int> expected_10 = {3,4};
    const std::vector<int> expected_11 = {1,2,3};
    const std::vector<int> expected_12 = {1,2,4};
    const std::vector<int> expected_13 = {1,3,4};
    const std::vector<int> expected_14 = {2,3,4};
    const std::vector<int> expected_15 = {1,2,3,4};
    assert(std::count(v.begin(),v.end(),expected_0));
    assert(std::count(v.begin(),v.end(),expected_1));
    assert(std::count(v.begin(),v.end(),expected_2));
    assert(std::count(v.begin(),v.end(),expected_3));
    assert(std::count(v.begin(),v.end(),expected_4));
    assert(std::count(v.begin(),v.end(),expected_5));
    assert(std::count(v.begin(),v.end(),expected_6));
    assert(std::count(v.begin(),v.end(),expected_7));
    assert(std::count(v.begin(),v.end(),expected_8));
    assert(std::count(v.begin(),v.end(),expected_9));
    assert(std::count(v.begin(),v.end(),expected_10));
    assert(std::count(v.begin(),v.end(),expected_11));
    assert(std::count(v.begin(),v.end(),expected_12));
    assert(std::count(v.begin(),v.end(),expected_13));
    assert(std::count(v.begin(),v.end(),expected_14));
    assert(std::count(v.begin(),v.end(),expected_15));
  }
  //Wordwrap
  {
    const auto v {
      "",
      "1",
      "12",
      "123",
      "1234",
      "12345",
      "123456",
      "1234567",
      "12345678",
      "123456789",
      "1234567890",
      "12345678901",
      "123456789012",
      "1234567890123",
      "12345678901234",
      "123456789012345",
      "1234567890123456",
      "12345678901234567",
      "123456789012345678",
      "1234567890123456789",
      "12345678901234567890",
      "123456789012345678901",
      "1234567890123456789012",
      "12345678901234567890123",
      "123456789012345678901234",
      "1234567890123456789012345",
      "12345678901234567890123456",
      "123456789012345678901234567",
      "1234567890123456789012345678",
      "12345678901234567890123456789",
      "123456789012345678901234567890",
      "1234567890123456789012345678901",
      "12345678901234567890123456789012",
      "123456789012345678901234567890123",
      "1234567890123456789012345678901234",
      "12345678901234567890123456789012345",
      "123456789012345678901234567890123456",
      "1234567890123456789012345678901234567",
      "12345678901234567890123456789012345678",
      "123456789012345678901234567890123456789",
      "1234567890123456789012345678901234567890",
      "1 1",
      "12 12",
      "123 123",
      "1234 1234",
      "12345 12345",
      "123456 123456",
      "1234567 1234567",
      "12345678 8",
      "123456789 9",
      "1234567890 0",
      "1234567890 1234567890",
      "1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      "1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890 1234567890",
      " 1",
      "  1",
      "  1 ",
      "  1  ",
      "  1 2 ",
      "  1 23 ",
      "  12 34  ",
      "  12  34  ",
      "   12   34   ",
      "   12   34   5",
      "   12   34   5 ",
      "   12   34   5 6",
      "0   12   34   5 6",
      "0   12   34   5 6  ",
      "                    ",
      "                      ",
      "                        ",
      "                            ",
      "                                    ",
      "                                                    ",
      "                                                                                     "
    };
    for (int len=1; len!=1000; ++len)
    {
      for (const std::string& s: v)
      {
        //Wordwrap calls Unwordwrap
        Wordwrap(s,len);
      }
    }
  }
  TRACE("TestHelperFunctions finished successfully");
}
void ribi::pvdb::Cluster::Test() noexcept
{
    {
        static bool is_tested = false;
        if (is_tested) return;
        is_tested = true;
    }
#ifdef MXE_SUPPORTS_THREADS
    std::thread t(
        []
    {
#endif

        TRACE("Cluster::Test started");
        //Test operator== and operator!=
        {
            const std::vector<boost::shared_ptr<ribi::pvdb::Cluster> > tmp_tests_1 = ribi::pvdb::ClusterFactory::GetTests();
            const int sz = static_cast<int>(tmp_tests_1.size());
            for (int i=0; i!=sz; ++i)
            {
                const std::vector<boost::shared_ptr<ribi::pvdb::Cluster> > tmp_tests_a = ribi::pvdb::ClusterFactory::GetTests(); //For crosscompiler
                const boost::shared_ptr<const ribi::pvdb::Cluster> a = tmp_tests_a.at(i);
                if (!a) continue;
                assert(a);
                const std::vector<boost::shared_ptr<ribi::pvdb::Cluster> > tmp_tests_b = ribi::pvdb::ClusterFactory::GetTests(); //For crosscompiler
                const boost::shared_ptr<ribi::pvdb::Cluster> b = tmp_tests_b.at(i);
                if (!b) continue;
                assert(a);
                assert(b);
                assert(operator==(*a,*a));
                assert(operator==(*a,*b));
                assert(operator==(*b,*a));
                assert(operator==(*b,*b));
                for (int j=0; j!=sz; ++j)
                {
                    const std::vector<boost::shared_ptr<ribi::pvdb::Cluster> > tmp_tests_c = ribi::pvdb::ClusterFactory::GetTests(); //For crosscompiler
                    const std::vector<boost::shared_ptr<ribi::pvdb::Cluster> > tmp_tests_d = ribi::pvdb::ClusterFactory::GetTests(); //For crosscompiler
                    const boost::shared_ptr<const ribi::pvdb::Cluster> c = tmp_tests_c.at(j);
                    if (!c) continue;
                    assert(c);
                    const boost::shared_ptr<ribi::pvdb::Cluster> d = tmp_tests_d.at(j);
                    if (!d) continue;
                    assert(c);
                    assert(d);
                    assert(operator==(*c,*c));
                    assert(operator==(*c,*d));
                    assert(operator==(*d,*c));
                    assert(operator==(*d,*d));
                    if (i==j)
                    {
                        assert(operator==(*a,*c));
                        assert(operator==(*a,*d));
                        assert(operator==(*b,*c));
                        assert(operator==(*b,*d));
                        assert(operator==(*c,*a));
                        assert(operator==(*c,*b));
                        assert(operator==(*d,*a));
                        assert(operator==(*d,*b));
                    }
                    else
                    {
                        assert(!operator==(*a,*c));
                        assert(!operator==(*a,*d));
                        assert(!operator==(*b,*c));
                        assert(!operator==(*b,*d));
                        assert(!operator==(*c,*a));
                        assert(!operator==(*c,*b));
                        assert(!operator==(*d,*a));
                        assert(!operator==(*d,*b));
                    }
                }
            }
        }
        //Test all Clusters with each combination of Concepts
        {
            const std::vector<std::vector<boost::shared_ptr<ribi::cmap::Concept> > > v
            = GetCombinations(cmap::ConceptFactory().GetTests());
            std::for_each(v.begin(),v.end(),
            [](const std::vector<boost::shared_ptr<ribi::cmap::Concept> >& concepts)
            {
                const boost::shared_ptr<ribi::pvdb::Cluster> c(new ribi::pvdb::Cluster(concepts));
                assert(c);
                const std::string s = ToXml(c);
                const boost::shared_ptr<ribi::pvdb::Cluster> d = FromXml(s);
                assert(d);
                assert(c != d);
                assert(operator==(*c,*d));
            }
                         );
        }
        TRACE("Cluster::Test finished successfully");

#ifdef MXE_SUPPORTS_THREADS
    }
    );
    t.detach();
#endif
}