Generated on Thu Apr 11 13:59:12 2019 for Gecode by doxygen 1.6.3

sorted.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
00005  *
00006  *  Contributing authors:
00007  *     Christian Schulte <schulte@gecode.org>
00008  *
00009  *  Copyright:
00010  *     Patrick Pekczynski, 2005
00011  *     Christian Schulte, 2007
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "test/int.hh"
00039 
00040 namespace Test { namespace Int {
00041 
00043    namespace Sorted {
00044 
00050 
00052      class SortIntMin {
00053      public:
00055        bool operator()(const int x, const int y) {
00056          return x<y;
00057        }
00058      };
00059 
00061      class NoVar : public Test {
00062      protected:
00064        static const int n = 3;
00065      public:
00067        NoVar(void) : Test("Sorted::NoVar",2*n,0,3) {}
00069        virtual bool solution(const Assignment& xy) const {
00070          int x[n], y[n];
00071          for (int i=0;i<3; i++) {
00072            x[i]=xy[i]; y[i]=xy[n+i];
00073          }
00074 
00075          for (int i=0; i<n-1; i++)
00076            if (y[i]>y[i+1])
00077              return false;
00078 
00079          SortIntMin sim;
00080          Gecode::Support::quicksort<int,SortIntMin>(x,n,sim);
00081 
00082          for (int i=0; i<n; i++)
00083            if (x[i] != y[i])
00084              return false;
00085          return true;
00086        }
00088        virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
00089          Gecode::IntVarArgs x(n), y(n);
00090          for (int i=0; i<n; i++) {
00091            x[i]=xy[i]; y[i]=xy[n+i];
00092          }
00093          Gecode::sorted(home,x,y);
00094        }
00095      };
00096 
00097 
00099      class PermVar : public Test {
00100      protected:
00102        static const int n = 3;
00103      public:
00105        PermVar(void) : Test("Sorted::PermVar",3*n,0,2) {}
00107        virtual bool solution(const Assignment& xyz) const {
00108          int x[n], y[n], z[n];
00109          for (int i=0; i<n; i++) {
00110            x[i]=xyz[i]; y[i]=xyz[n+i]; z[i]=xyz[2*n+i];
00111          }
00112          // check for permutation
00113          for (int i=0; i<n; i++)
00114            for (int j=i+1; j<n; j++)
00115              if (z[i]==z[j])
00116                return false;
00117 
00118          // y must to be sorted
00119          for (int i=0; i<n-1; i++)
00120            if (y[i]>y[i+1])
00121              return false;
00122 
00123          // check whether permutation is in range
00124          for (int i=0; i<n; i++)
00125            if ((z[i] < 0) || (z[i] >= n))
00126              return false;
00127 
00128          // check whether permutation info is correct
00129          for (int i=0; i<n; i++)
00130            if (x[i] != y[z[i]])
00131              return false;
00132 
00133          // check for sorting
00134          SortIntMin sim;
00135          Gecode::Support::quicksort<int,SortIntMin>(x,n,sim);
00136          for (int i=0; i<n; i++)
00137            if (x[i] != y[i])
00138              return false;
00139 
00140          return true;
00141        }
00143        virtual void post(Gecode::Space& home, Gecode::IntVarArray& xyz) {
00144          Gecode::IntVarArgs x(n), y(n), z(n);
00145          for (int i=0; i<n; i++) {
00146            x[i]=xyz[i]; y[i]=xyz[n+i]; z[i]=xyz[2*n+i];
00147          }
00148          Gecode::sorted(home,x,y,z);
00149        }
00150      };
00151 
00152 
00153      NoVar novar;
00154      PermVar permvar;
00156 
00157    }
00158 }}
00159 
00160 // STATISTICS: test-int
00161