summaryrefslogtreecommitdiff
path: root/src/policy.c
blob: e1682f27910867b635f61645374f3b7e12851fc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
/*
 * Copyright (c) 2007, Novell Inc.
 *
 * This program is licensed under the BSD license, read LICENSE.BSD
 * for further information
 */

/*
 * Generic policy interface for SAT solver
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include "solver.h"
#include "solver_private.h"
#include "evr.h"
#include "policy.h"
#include "poolvendor.h"
#include "poolarch.h"
#include "linkedpkg.h"
#include "cplxdeps.h"



/*-----------------------------------------------------------------*/

/*
 * prep for prune_best_version
 *   sort by name
 */

static int
prune_to_best_version_sortcmp(const void *ap, const void *bp, void *dp)
{
  Pool *pool = dp;
  int r;
  Id a = *(Id *)ap;
  Id b = *(Id *)bp;
  Solvable *sa, *sb;

  sa = pool->solvables + a;
  sb = pool->solvables + b;
  r = sa->name - sb->name;
  if (r)
    {
      const char *na, *nb;
      /* different names. We use real strcmp here so that the result
       * is not depending on some random solvable order */
      na = pool_id2str(pool, sa->name);
      nb = pool_id2str(pool, sb->name);
      return strcmp(na, nb);
    }
  if (sa->arch != sb->arch)
    {
      unsigned int aa, ab;
      aa = pool_arch2score(pool, sa->arch);
      ab = pool_arch2score(pool, sb->arch);
      if (aa != ab && aa > 1 && ab > 1)
	return aa < ab ? -1 : 1;	/* lowest score first */
    }

  /* the same name, bring installed solvables to the front */
  if (pool->installed)
    {
      if (sa->repo == pool->installed)
	{
	  if (sb->repo != pool->installed)
	    return -1;
	}
      else if (sb->repo == pool->installed)
	return 1;	
    }
  /* sort by repository sub-prio (installed repo handled above) */
  r = (sb->repo ? sb->repo->subpriority : 0) - (sa->repo ? sa->repo->subpriority : 0);
  if (r)
    return r;
  /* no idea about the order, sort by id */
  return a - b;
}


/*
 * prune to repository with highest priority.
 * does not prune installed solvables.
 */

static void
prune_to_highest_prio(Pool *pool, Queue *plist)
{
  int i, j;
  Solvable *s;
  int bestprio = 0, bestprioset = 0;

  /* prune to highest priority */
  for (i = 0; i < plist->count; i++)  /* find highest prio in queue */
    {
      s = pool->solvables + plist->elements[i];
      if (pool->installed && s->repo == pool->installed)
	continue;
      if (!bestprioset || s->repo->priority > bestprio)
	{
	  bestprio = s->repo->priority;
	  bestprioset = 1;
	}
    }
  if (!bestprioset)
    return;
  for (i = j = 0; i < plist->count; i++) /* remove all with lower prio */
    {
      s = pool->solvables + plist->elements[i];
      if (s->repo->priority == bestprio || (pool->installed && s->repo == pool->installed))
	plist->elements[j++] = plist->elements[i];
    }
  plist->count = j;
}


/* installed packages involed in a dup operation can only be kept
 * if they are identical to a non-installed one */
static void
solver_prune_installed_dup_packages(Solver *solv, Queue *plist)
{
  Pool *pool = solv->pool;
  int i, j, bestprio = 0;

  /* find bestprio (again) */
  for (i = 0; i < plist->count; i++)
    {
      Solvable *s = pool->solvables + plist->elements[i];
      if (s->repo != pool->installed)
	{
	  bestprio = s->repo->priority;
	  break;
	}
    }
  if (i == plist->count)
    return;	/* only installed packages, could not find prio */
  for (i = j = 0; i < plist->count; i++)
    {
      Id p = plist->elements[i];
      Solvable *s = pool->solvables + p;
      if (s->repo != pool->installed && s->repo->priority < bestprio)
	continue;
      if (s->repo == pool->installed && (solv->dupinvolvedmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, p))))
	{
	  Id p2, pp2;
	  int keepit = 0;
	  FOR_PROVIDES(p2, pp2, s->name)
	    {
	      Solvable *s2 = pool->solvables + p2;
	      if (s2->repo == pool->installed || s2->evr != s->evr || s2->repo->priority < bestprio)
		continue;
	      if (!solvable_identical(s, s2))
		continue;
	      keepit = 1;
	      if (s2->repo->priority > bestprio)
		{
		  /* new max prio! */
		  bestprio = s2->repo->priority;
		  j = 0;
		}
	    }
	  if (!keepit)
	    continue;	/* no identical package found, ignore installed package */
	}
      plist->elements[j++] = p;
    }
  if (j)
    plist->count = j;
}

/*
 * like prune_to_highest_prio, but calls solver prune_installed_dup_packages
 * when there are dup packages
 */
static inline void
solver_prune_to_highest_prio(Solver *solv, Queue *plist)
{
  prune_to_highest_prio(solv->pool, plist);
  if (plist->count > 1 && solv->pool->installed && (solv->dupinvolvedmap_all || solv->dupinvolvedmap.size))
    solver_prune_installed_dup_packages(solv, plist);
}


static void
solver_prune_to_highest_prio_per_name(Solver *solv, Queue *plist)
{
  Pool *pool = solv->pool;
  Queue pq;
  int i, j, k;
  Id name;

  queue_init(&pq);
  solv_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);
  queue_push(&pq, plist->elements[0]);
  name = pool->solvables[pq.elements[0]].name;
  for (i = 1, j = 0; i < plist->count; i++)
    {
      if (pool->solvables[plist->elements[i]].name != name)
	{
	  name = pool->solvables[plist->elements[i]].name;
	  if (pq.count > 2)
	    solver_prune_to_highest_prio(solv, &pq);
	  for (k = 0; k < pq.count; k++)
	    plist->elements[j++] = pq.elements[k];
	  queue_empty(&pq);
	}
      queue_push(&pq, plist->elements[i]);
    }
  if (pq.count > 2)
    solver_prune_to_highest_prio(solv, &pq);
  for (k = 0; k < pq.count; k++)
    plist->elements[j++] = pq.elements[k];
  queue_free(&pq);
  plist->count = j;
}


#ifdef ENABLE_COMPLEX_DEPS

/* simple fixed-size hash for package ids */
#define CPLXDEPHASH_EMPTY(elements) (memset(elements, 0, sizeof(Id) * 256))
#define CPLXDEPHASH_SET(elements, p) (elements[(p) & 255] |= (1 << ((p) >> 8 & 31)))
#define CPLXDEPHASH_TST(elements, p) (elements[(p) & 255] && (elements[(p) & 255] & (1 << ((p) >> 8 & 31))))

static void
check_complex_dep(Solver *solv, Id dep, Map *m, Queue **cqp)
{
  Pool *pool = solv->pool;
  Queue q;
  queue_init(&q);
  Id p;
  int i, qcnt;

#if 0
  printf("check_complex_dep %s\n", pool_dep2str(pool, dep));
#endif
  i = pool_normalize_complex_dep(pool, dep, &q, CPLXDEPS_EXPAND);
  if (i == 0 || i == 1)
    {
      queue_free(&q);
      return;
    }
  qcnt = q.count;
  for (i = 0; i < qcnt; i++)
    {
      /* we rely on the fact that blocks are ordered here.
       * if we reach a positive element, we know that we
       * saw all negative ones */
      for (; (p = q.elements[i]) < 0; i++)
	{
	  if (solv->decisionmap[-p] < 0)
	    break;
	  if (solv->decisionmap[-p] == 0)
	    queue_push(&q, -p);		/* undecided negative literal */
	}
      if (p <= 0)
	{
#if 0
	  printf("complex dep block cannot be true or no pos literals\n");
#endif
	  while (q.elements[i])
	    i++;
	  if (qcnt != q.count)
	    queue_truncate(&q, qcnt);
	  continue;
	}
      if (qcnt == q.count)
	{
	  /* all negative literals installed, add positive literals to map */
	  for (; (p = q.elements[i]) != 0; i++)
	    MAPSET(m, p);
	}
      else
	{
	  /* at least one undecided negative literal, postpone */
	  int j, k;
	  Queue *cq;
#if 0
	  printf("add new complex dep block\n");
	  for (j = qcnt; j < q.count; j++)
	    printf("  - %s\n", pool_solvid2str(pool, q.elements[j]));
#endif
	  while (q.elements[i])
	    i++;
	  if (!(cq = *cqp))
	    {
	      cq = solv_calloc(1, sizeof(Queue));
	      queue_init(cq);
	      queue_insertn(cq, 0, 256, 0);	/* allocate hash area */
	      *cqp = cq;
	    }
	  for (j = qcnt; j < q.count; j++)
	    {
	      p = q.elements[j];
	      /* check if we already have this (dep, p) entry */
	      for (k = 256; k < cq->count; k += 2)
		if (cq->elements[k + 1] == dep && cq->elements[k] == p)
		  break;
	      if (k == cq->count)
		{
		  /* a new one. add to cq and hash */
	          queue_push2(cq, p, dep);
		  CPLXDEPHASH_SET(cq->elements, p);
		}
	    }
	  queue_truncate(&q, qcnt);
	}
    }
  queue_free(&q);
}

static void
recheck_complex_deps(Solver *solv, Id p, Map *m, Queue **cqp)
{
  Queue *cq = *cqp;
  Id pp;
  int i;
#if 0
  printf("recheck_complex_deps for package %s\n", pool_solvid2str(solv->pool, p));
#endif
  /* make sure that we don't have a false hit */
  for (i = 256; i < cq->count; i += 2)
    if (cq->elements[i] == p)
      break;
  if (i == cq->count)
    return;	/* false alert */
  if (solv->decisionmap[p] <= 0)
    return;	/* just in case... */

  /* rebuild the hash, call check_complex_dep for our package */
  CPLXDEPHASH_EMPTY(cq->elements);
  for (i = 256; i < cq->count; i += 2)
    if ((pp = cq->elements[i]) == p)
      {
	Id dep = cq->elements[i + 1];
	queue_deleten(cq, i, 2);
	i -= 2;
        check_complex_dep(solv, dep, m, &cq);
      }
    else
      CPLXDEPHASH_SET(cq->elements, pp);
}

#endif


void
policy_update_recommendsmap(Solver *solv)
{
  Pool *pool = solv->pool;
  Solvable *s;
  Id p, pp, rec, *recp, sug, *sugp;

  if (solv->recommends_index < 0)
    {
      MAPZERO(&solv->recommendsmap);
      MAPZERO(&solv->suggestsmap);
#ifdef ENABLE_COMPLEX_DEPS
      if (solv->recommendscplxq)
	{
	  queue_free(solv->recommendscplxq);
	  solv->recommendscplxq = solv_free(solv->recommendscplxq);
	}
      if (solv->suggestscplxq)
	{
	  queue_free(solv->suggestscplxq);
	  solv->suggestscplxq = solv_free(solv->suggestscplxq);
	}
#endif
      solv->recommends_index = 0;
    }
  while (solv->recommends_index < solv->decisionq.count)
    {
      p = solv->decisionq.elements[solv->recommends_index++];
      if (p < 0)
	continue;
      s = pool->solvables + p;
#ifdef ENABLE_COMPLEX_DEPS
      /* re-check postponed complex blocks */
      if (solv->recommendscplxq && CPLXDEPHASH_TST(solv->recommendscplxq->elements, p))
        recheck_complex_deps(solv, p, &solv->recommendsmap, &solv->recommendscplxq);
      if (solv->suggestscplxq && CPLXDEPHASH_TST(solv->suggestscplxq->elements, p))
        recheck_complex_deps(solv, p, &solv->suggestsmap, &solv->suggestscplxq);
#endif
      if (s->recommends)
	{
	  recp = s->repo->idarraydata + s->recommends;
          while ((rec = *recp++) != 0)
	    {
#ifdef ENABLE_COMPLEX_DEPS
	      if (pool_is_complex_dep(pool, rec))
		{
		  check_complex_dep(solv, rec, &solv->recommendsmap, &solv->recommendscplxq);
		  continue;
		}
#endif
	      FOR_PROVIDES(p, pp, rec)
	        MAPSET(&solv->recommendsmap, p);
	    }
	}
      if (s->suggests)
	{
	  sugp = s->repo->idarraydata + s->suggests;
          while ((sug = *sugp++) != 0)
	    {
#ifdef ENABLE_COMPLEX_DEPS
	      if (pool_is_complex_dep(pool, sug))
		{
		  check_complex_dep(solv, sug, &solv->suggestsmap, &solv->suggestscplxq);
		  continue;
		}
#endif
	      FOR_PROVIDES(p, pp, sug)
	        MAPSET(&solv->suggestsmap, p);
	    }
	}
    }
}

/* bring suggested/enhanced packages to front
 * installed packages count as suggested */
static void
prefer_suggested(Solver *solv, Queue *plist)
{
  Pool *pool = solv->pool;
  int i, count;

  /* update our recommendsmap/suggestsmap */
  if (solv->recommends_index < solv->decisionq.count)
    policy_update_recommendsmap(solv);

  for (i = 0, count = plist->count; i < count; i++)
    {
      Id p = plist->elements[i];
      Solvable *s = pool->solvables + p;
      if ((pool->installed && s->repo == pool->installed) ||
          MAPTST(&solv->suggestsmap, p) ||
          solver_is_enhancing(solv, s))
	continue;	/* good package */
      /* bring to back */
     if (i < plist->count - 1)
	{
	  memmove(plist->elements + i, plist->elements + i + 1, (plist->count - 1 - i) * sizeof(Id));
	  plist->elements[plist->count - 1] = p;
	}
      i--;
      count--;
    }
}

static int
sort_by_favorq_cmp(const void *ap, const void *bp, void *dp)
{
  const Id *a = ap, *b = bp, *d = dp;
  return d[b[0]] - d[a[0]];
}

static void
sort_by_favorq(Queue *favorq, Id *el, int cnt)
{
  int i;
  /* map to offsets into favorq */
  for (i = 0; i < cnt; i++)
    {
      Id p = el[i];
      /* lookup p in sorted favorq */
      int med = 0, low = 0;
      int high = favorq->count / 2;
      while (low != high)
	{
	  med = (low + high) / 2;
	  Id pp = favorq->elements[2 * med];
	  if (pp < p)
	    low = med;
	  else if (pp > p)
	    high = med;
	  else
	    break;
	}
      while(med && favorq->elements[2 * med - 2] == p)
	med--;
      if (favorq->elements[2 * med] == p)
        el[i] = 2 * med + 1;
      else
        el[i] = 0;	/* hmm */
    }
  /* sort by position */
  solv_sort(el, cnt, sizeof(Id), sort_by_favorq_cmp, favorq->elements);
  /* map back */
  for (i = 0; i < cnt; i++)
    if (el[i])
      el[i] = favorq->elements[el[i] - 1];
}

/* bring favored packages to front and disfavored packages to back */
void
policy_prefer_favored(Solver *solv, Queue *plist)
{
  int i, fav, disfav, count;
  if (!solv->favormap.size)
    return;
  for (i = fav = disfav = 0, count = plist->count; i < count; i++)
    {
      Id p = plist->elements[i];
      if (!MAPTST(&solv->favormap, p))
	continue;
      if (solv->isdisfavormap.size && MAPTST(&solv->isdisfavormap, p))
	{
	  /* disfavored package. bring to back */
	 if (i < plist->count - 1)
	    {
	      memmove(plist->elements + i, plist->elements + i + 1, (plist->count - 1 - i) * sizeof(Id));
	      plist->elements[plist->count - 1] = p;
	    }
	  i--;
	  count--;
	  disfav++;
	}
      else
	{
	  /* favored package. bring to front */
	  if (i > fav)
	    memmove(plist->elements + fav + 1, plist->elements + fav, (i - fav) * sizeof(Id));
	  plist->elements[fav++] = p;
	}
    }
  /* if we have multiple favored/disfavored packages, sort by favorq index */
  if (fav > 1)
    sort_by_favorq(solv->favorq, plist->elements, fav);
  if (disfav > 1)
    sort_by_favorq(solv->favorq, plist->elements + plist->count - disfav, disfav);
}

/*
 * prune to recommended/suggested packages.
 * does not prune installed packages (they are also somewhat recommended).
 */
static void
prune_to_recommended(Solver *solv, Queue *plist)
{
  Pool *pool = solv->pool;
  int i, j, k, ninst;
  Solvable *s;
  Id p;

  ninst = 0;
  if (pool->installed)
    {
      for (i = 0; i < plist->count; i++)
	{
	  p = plist->elements[i];
	  s = pool->solvables + p;
	  if (pool->installed && s->repo == pool->installed)
	    ninst++;
	}
    }
  if (plist->count - ninst < 2)
    return;

  /* update our recommendsmap/suggestsmap */
  if (solv->recommends_index < solv->decisionq.count)
    policy_update_recommendsmap(solv);

  /* prune to recommended/supplemented */
  ninst = 0;
  for (i = j = 0; i < plist->count; i++)
    {
      p = plist->elements[i];
      s = pool->solvables + p;
      if (pool->installed && s->repo == pool->installed)
	{
	  ninst++;
	  if (j)
	    plist->elements[j++] = p;
	  continue;
	}
      if (!MAPTST(&solv->recommendsmap, p))
	if (!solver_is_supplementing(solv, s))
	  continue;
      if (!j && ninst)
	{
	  for (k = 0; j < ninst; k++)
	    {
	      s = pool->solvables + plist->elements[k];
	      if (pool->installed && s->repo == pool->installed)
	        plist->elements[j++] = plist->elements[k];
	    }
	}
      plist->elements[j++] = p;
    }
  if (j)
    plist->count = j;

#if 0
  /* anything left to prune? */
  if (plist->count - ninst < 2)
    return;

  /* prune to suggested/enhanced */
  ninst = 0;
  for (i = j = 0; i < plist->count; i++)
    {
      p = plist->elements[i];
      s = pool->solvables + p;
      if (pool->installed && s->repo == pool->installed)
	{
	  ninst++;
	  if (j)
	    plist->elements[j++] = p;
	  continue;
	}
      if (!MAPTST(&solv->suggestsmap, p))
        if (!solver_is_enhancing(solv, s))
	  continue;
      if (!j && ninst)
	{
	  for (k = 0; j < ninst; k++)
	    {
	      s = pool->solvables + plist->elements[k];
	      if (pool->installed && s->repo == pool->installed)
	        plist->elements[j++] = plist->elements[k];
	    }
	}
      plist->elements[j++] = p;
    }
  if (j)
    plist->count = j;
#endif
}

static void
prune_to_best_arch(const Pool *pool, Queue *plist)
{
  Id a, bestscore;
  Solvable *s;
  int i, j;

  if (!pool->id2arch || plist->count < 2)
    return;
  bestscore = 0;
  for (i = 0; i < plist->count; i++)
    {
      s = pool->solvables + plist->elements[i];
      a = pool_arch2score(pool, s->arch);
      if (a && a != 1 && (!bestscore || a < bestscore))
	bestscore = a;
    }
  if (!bestscore)
    return;
  for (i = j = 0; i < plist->count; i++)
    {
      s = pool->solvables + plist->elements[i];
      a = pool_arch2score(pool, s->arch);
      if (!a)
	continue;
      /* a == 1 -> noarch */
      if (a != 1 && ((a ^ bestscore) & 0xffff0000) != 0)
	continue;
      plist->elements[j++] = plist->elements[i];
    }
  if (j)
    plist->count = j;
}


struct trj_data {
  Pool *pool;
  Queue *plist;
  Id *stack;
  Id nstack;
  Id *low;
  Id firstidx;
  Id idx;
};

/* This is Tarjan's SCC algorithm, slightly modified */
static void
trj_visit(struct trj_data *trj, Id node)
{
  Id *low = trj->low;
  Pool *pool = trj->pool;
  Queue *plist = trj->plist;
  Id myidx, stackstart;
  Solvable *s;
  int i;
  Id p, pp, obs, *obsp;

  low[node] = myidx = trj->idx++;
  trj->stack[(stackstart = trj->nstack++)] = node;

  s = pool->solvables + plist->elements[node];
  if (s->obsoletes)
    {
      obsp = s->repo->idarraydata + s->obsoletes;
      while ((obs = *obsp++) != 0)
	{
	  FOR_PROVIDES(p, pp, obs)
	    {
	      Solvable *ps = pool->solvables + p;
	      if (ps->name == s->name)
		continue;
	      if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
		continue;
	      if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
		continue;
	      /* hmm, expensive. should use hash if plist is big */
	      for (i = 0; i < plist->count; i++)
		{
		  if (node != i && plist->elements[i] == p)
		    {
		      Id l = low[i];
		      if (!l)
			{
			  if (!ps->obsoletes)
			    {
			      /* don't bother */
			      trj->idx++;
			      low[i] = -1;
			      continue;
			    }
			  trj_visit(trj, i);
			  l = low[i];
			}
		      if (l < 0)
			continue;
		      if (l < trj->firstidx)
			{
			  int k;
			  /* this means we have reached an old SCC found earlier.
			   * delete it as we obsolete it */
			  for (k = l; ; k++)
			    {
			      if (low[trj->stack[k]] == l)
				low[trj->stack[k]] = -1;
			      else
				break;
			    }
			}
		      else if (l < low[node])
			low[node] = l;
		    }
		}
	    }
	}
    }
  if (low[node] == myidx)	/* found a SCC? */
    {
      /* we're only interested in SCCs that contain the first node,
       * as all others are "obsoleted" */
      if (myidx != trj->firstidx)
	myidx = -1;
      for (i = stackstart; i < trj->nstack; i++)
	low[trj->stack[i]] = myidx;
      trj->nstack = stackstart;	/* empty stack */
    }
}

/*
 * remove entries from plist that are obsoleted by other entries
 * with different name.
 */
static void
prune_obsoleted(Pool *pool, Queue *plist)
{
  Id data_buf[2 * 16], *data;
  struct trj_data trj;
  int i, j;
  Solvable *s;

  if (plist->count <= 16)
    {
      memset(data_buf, 0, sizeof(data_buf));
      data = data_buf;
    }
  else
    data = solv_calloc(plist->count, 2 * sizeof(Id));
  trj.pool = pool;
  trj.plist = plist;
  trj.low = data;
  trj.idx = 1;
  trj.stack = data + plist->count - 1;	/* -1 so we can index with idx (which starts with 1) */
  for (i = 0; i < plist->count; i++)
    {
      if (trj.low[i])
	continue;
      s = pool->solvables + plist->elements[i];
      if (s->obsoletes)
	{
	  trj.firstidx = trj.nstack = trj.idx;
          trj_visit(&trj, i);
	}
      else
        {
          Id myidx = trj.idx++;
          trj.low[i] = myidx;
          trj.stack[myidx] = i;
        }
    }
  for (i = j = 0; i < plist->count; i++)
    if (trj.low[i] >= 0)
      plist->elements[j++] = plist->elements[i];
  plist->count = j;
  if (data != data_buf)
    solv_free(data);
}

/* this is prune_obsoleted special-cased for two elements */
static void
prune_obsoleted_2(Pool *pool, Queue *plist)
{
  int i;
  Solvable *s;
  Id p, pp, obs, *obsp;
  Id other;
  int obmap = 0;

  for (i = 0; i < 2; i++)
    {
      s = pool->solvables + plist->elements[i];
      other = plist->elements[1 - i];
      if (s->obsoletes)
	{
	  obsp = s->repo->idarraydata + s->obsoletes;
	  while ((obs = *obsp++) != 0)
	    {
	      FOR_PROVIDES(p, pp, obs)
		{
		  Solvable *ps;
		  if (p != other)
		    continue;
		  ps = pool->solvables + p;
		  if (ps->name == s->name)
		    continue;
		  if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
		    continue;
		  if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
		    continue;
		  obmap |= 1 << i;
		  break;
		}
	      if (p)
		break;
	    }
	}
    }
  if (obmap == 0 || obmap == 3)
    return;
  if (obmap == 2)
    plist->elements[0] = plist->elements[1];
  plist->count = 1;
}

/*
 * bring those elements to the front of the queue that
 * have a installed solvable with the same name
 */
static void
move_installed_to_front(Pool *pool, Queue *plist)
{
  int i, j;
  Solvable *s;
  Id p, pp;

  if (!pool->installed)
    return;
  for (i = j = 0; i < plist->count; i++)
    {
      s = pool->solvables + plist->elements[i];
      if (s->repo != pool->installed)
        {
          FOR_PROVIDES(p, pp, s->name)
	    {
	      Solvable *ps = pool->solvables + p;
	      if (s->name == ps->name && ps->repo == pool->installed)
		{
		  s = ps;
		  break;
		}
	    }
        }
      if (s->repo == pool->installed)
	{
	  if (i != j)
	    {
	      p = plist->elements[i];
              if (i - j == 1)
		plist->elements[i] = plist->elements[j];
	      else
	        memmove(plist->elements + j + 1, plist->elements + j, (i - j) * sizeof(Id));
	      plist->elements[j] = p;
	    }
	  else if (j + 2 == plist->count)
	    break;	/* no need to check last element if all prev ones are installed */
	  j++;
	}
    }
}

/*
 * prune_to_best_version
 *
 * sort list of packages (given through plist) by name and evr
 * return result through plist
 */
void
prune_to_best_version(Pool *pool, Queue *plist)
{
  int i, j, r;
  Solvable *s, *best;

  if (plist->count < 2)		/* no need to prune for a single entry */
    return;
  POOL_DEBUG(SOLV_DEBUG_POLICY, "prune_to_best_version %d\n", plist->count);

  /* sort by name first, prefer installed */
  solv_sort(plist->elements, plist->count, sizeof(Id), prune_to_best_version_sortcmp, pool);

  /* now find best 'per name' */
  best = 0;
  for (i = j = 0; i < plist->count; i++)
    {
      s = pool->solvables + plist->elements[i];

      POOL_DEBUG(SOLV_DEBUG_POLICY, "- %s [%d]%s\n",
		 pool_solvable2str(pool, s), plist->elements[i], 
		 (pool->installed && s->repo == pool->installed) ? "I" : "");

      if (!best)		/* if no best yet, the current is best */
        {
          best = s;
          continue;
        }

      /* name switch: finish group, re-init */
      if (best->name != s->name)   /* new name */
        {
          plist->elements[j++] = best - pool->solvables; /* move old best to front */
          best = s;		/* take current as new best */
          continue;
        }
      r = best->evr != s->evr ? pool_evrcmp(pool, best->evr, s->evr, EVRCMP_COMPARE) : 0;
#ifdef ENABLE_LINKED_PKGS
      if (r == 0 && has_package_link(pool, s))
        r = pool_link_evrcmp(pool, best, s);
#endif
      if (r < 0)
	best = s;
    }
  plist->elements[j++] = best - pool->solvables;	/* finish last group */
  plist->count = j;

  /* we reduced the list to one package per name, now look at
   * package obsoletes */
  if (plist->count > 1)
    {
      if (plist->count == 2)
        prune_obsoleted_2(pool, plist);
      else
        prune_obsoleted(pool, plist);
    }
}


static int
sort_by_name_evr_sortcmp(const void *ap, const void *bp, void *dp)
{
  Pool *pool = dp;
  Id a, *aa = (Id *)ap;
  Id b, *bb = (Id *)bp;
  Id r = aa[1] - bb[1];
  if (r)
    return r < 0 ? -1 : 1;
  if (aa[2] == bb[2])
    return 0;
  a = aa[2] < 0 ? -aa[2] : aa[2];
  b = bb[2] < 0 ? -bb[2] : bb[2];
  r = pool_evrcmp(pool, b, a, pool->disttype != DISTTYPE_DEB ? EVRCMP_MATCH_RELEASE : EVRCMP_COMPARE);
  if (!r && (aa[2] < 0 || bb[2] < 0))
    {
      if (bb[2] >= 0)
	return 1;
      if (aa[2] >= 0)
	return -1;
    }
  return r;
}

/* common end of sort_by_srcversion and sort_by_common_dep */
static void
sort_by_name_evr_array(Pool *pool, Queue *plist, int count, int ent)
{
  Id lastname;
  int i, j, bad, havebad;
  Id *pp, *elements = plist->elements;

  if (ent < 2)
    {
      queue_truncate(plist, count);
      return;
    }
  solv_sort(elements + count * 2, ent, sizeof(Id) * 3, sort_by_name_evr_sortcmp, pool);
  lastname = 0;
  bad = havebad = 0;
  for (i = 0, pp = elements + count * 2; i < ent; i++, pp += 3)
    {
      if (lastname && pp[1] == lastname)
	{
          if (pp[0] != pp[-3] && sort_by_name_evr_sortcmp(pp - 3, pp, pool) == -1)
	    {
#if 0
	      printf("%s - %s: bad %s %s - %s\n", pool_solvid2str(pool, elements[pp[-3]]), pool_solvid2str(pool, elements[pp[0]]), pool_dep2str(pool, lastname), pool_id2str(pool, pp[-1] < 0 ? -pp[-1] : pp[-1]), pool_id2str(pool, pp[2] < 0 ? -pp[2] : pp[2]));
#endif
	      bad++;
	      havebad = 1;
	    }
	}
      else
	{
	  bad = 0;
	  lastname = pp[1];
	}
      elements[count + pp[0]] += bad;
    }

#if 0
for (i = 0; i < count; i++)
  printf("%s badness %d\n", pool_solvid2str(pool, elements[i]), elements[count + i]);
#endif

  if (havebad)
    {
      /* simple stable insertion sort */
      if (pool->installed)
	for (i = 0; i < count; i++)
	  if (pool->solvables[elements[i]].repo == pool->installed)
	    elements[i + count] = 0;
      for (i = 1; i < count; i++)
	for (j = i, pp = elements + count + j; j > 0; j--, pp--)
	  if (pp[-1] > pp[0])
	    {
	      Id *pp2 = pp - count;
	      Id p = pp[-1];
	      pp[-1] = pp[0];
	      pp[0] = p;
	      p = pp2[-1];
	      pp2[-1] = pp2[0];
	      pp2[0] = p;
	    }
	  else
	    break;
    }
  queue_truncate(plist, count);
}

#if 0
static void
sort_by_srcversion(Pool *pool, Queue *plist)
{
  int i, count = plist->count, ent = 0;
  queue_insertn(plist, count, count, 0);
  for (i = 0; i < count; i++)
    {
      Id name, evr, p = plist->elements[i];
      Solvable *s = pool->solvables + p;
      if (solvable_lookup_void(s, SOLVABLE_SOURCENAME))
	name = s->name;
      else
        name = solvable_lookup_id(s, SOLVABLE_SOURCENAME);
      if (solvable_lookup_void(s, SOLVABLE_SOURCEEVR))
	evr = s->evr;
      else
        evr = solvable_lookup_id(s, SOLVABLE_SOURCEEVR);
      if (!name || !evr || ISRELDEP(evr))
	continue;
      queue_push(plist, i);
      queue_push2(plist, name, evr);
      ent++;
    }
  sort_by_name_evr_array(pool, plist, count, ent);
}
#endif

static void
sort_by_common_dep(Pool *pool, Queue *plist)
{
  int i, count = plist->count, ent = 0;
  Id id, *dp;
  queue_insertn(plist, count, count, 0);
  for (i = 0; i < count; i++)
    {
      Id p = plist->elements[i];
      Solvable *s = pool->solvables + p;
      if (!s->provides)
	continue;
      for (dp = s->repo->idarraydata + s->provides; (id = *dp++) != 0; )
	{
	  Reldep *rd;
	  if (!ISRELDEP(id))
	    continue;
	  rd = GETRELDEP(pool, id);
	  if ((rd->flags == REL_EQ || rd->flags == (REL_EQ | REL_LT) || rd->flags == REL_LT) && !ISRELDEP(rd->evr))
	    {
	      if (rd->flags == REL_EQ)
		{
		  /* ignore hashes */
		  const char *s = pool_id2str(pool, rd->evr);
		  if (strlen(s) >= 4)
		    {
		      while ((*s >= 'a' && *s <= 'f') || (*s >= '0' && *s <= '9'))
			s++;
		      if (!*s)
			continue;
		    }
		}
	      queue_push(plist, i);
	      queue_push2(plist, rd->name, rd->flags == REL_LT ? -rd->evr : rd->evr);
	      ent++;
	    }
	}
    }
  sort_by_name_evr_array(pool, plist, count, ent);
}

/* check if we have an update candidate */
static void
dislike_old_versions(Pool *pool, Queue *plist)
{
  int i, count;

  for (i = 0, count = plist->count; i < count; i++)
    {
      Id p = plist->elements[i];
      Solvable *s = pool->solvables + p;
      Repo *repo = s->repo;
      Id q, qq;
      int bad = 0;

      if (!repo || repo == pool->installed)
	continue;
      FOR_PROVIDES(q, qq, s->name)
	{
	  Solvable *qs = pool->solvables + q;
	  if (q == p)
	    continue;
	  if (s->name != qs->name || s->arch != qs->arch)
	    continue;
	  if (repo->priority != qs->repo->priority)
	    {
	      if (repo->priority > qs->repo->priority)
		continue;
	      bad = 1;
	      break;
	    }
	  if (pool_evrcmp(pool, qs->evr, s->evr, EVRCMP_COMPARE) > 0)
	    {
	      bad = 1;
	      break;
	    }
	}
      if (!bad)
	continue;
      /* bring to back */
      if (i < plist->count - 1)
	{
	  memmove(plist->elements + i, plist->elements + i + 1, (plist->count - 1 - i) * sizeof(Id));
	  plist->elements[plist->count - 1] = p;
	}
      i--;
      count--;
    }
}


/* special lang package handling for urpm */
/* see https://bugs.mageia.org/show_bug.cgi?id=18315 */

static int
urpm_reorder_cmp(const void *ap, const void *bp, void *dp)
{
  return ((Id *)bp)[1] - ((Id *)ap)[1];
}

static void
urpm_reorder(Solver *solv, Queue *plist)
{
  Pool *pool = solv->pool;
  int i, count = plist->count;
  /* add locale score to packages */
  queue_insertn(plist, count, count, 0);
  for (i = count - 1; i >= 0; i--)
    {
      Solvable *s = pool->solvables + plist->elements[i];
      int score = 1;
      const char *sn = pool_id2str(pool, s->name);

      if (!strncmp(sn, "kernel-", 7))
	{
	  const char *devel = strstr(sn, "-devel-");
	  if (devel && strlen(sn) < 256)
	    {
	      char kn[256];
	      Id p, pp, knid;
	      memcpy(kn, sn, devel - sn);
	      strcpy(kn + (devel - sn), devel + 6);
	      knid = pool_str2id(pool, kn, 0);
	      if (knid)
		{
		  FOR_PROVIDES(p, pp, knid)
		    {
		      if (solv->decisionmap[p] > 0)
			{
			  score = 4;
			  break;
			}
		      else if (pool->installed && pool->solvables[p].repo == pool->installed)
			score = 3;
		    }
		}
	    }
	}
      else if ((sn = strstr(sn, "-kernel-")) != 0)
	{
	  sn += 8;
	  if (strlen(sn) < 256 - 8 && *sn >= '0' && *sn <= '9' && sn[1] == '.')
	    {
	      const char *flavor = strchr(sn, '-');
	      if (flavor)
		{
		  const char *release = strchr(flavor + 1, '-');
		  if (release)
		    {
		      char kn[256];
		      Id p, pp, knid;
		      memcpy(kn, "kernel", 7);
		      memcpy(kn + 6, flavor, release - flavor + 1);
		      memcpy(kn + 6 + (release - flavor) + 1, sn, flavor - sn);
		      strcpy(kn + 6 + (release + 1 - sn), release);
		      knid = pool_str2id(pool, kn, 0);
		      if (knid)
			{
			  FOR_PROVIDES(p, pp, knid)
			    {
			      if (solv->decisionmap[p] > 0)
				{
				  score = 4;
				  break;
				}
			      if (pool->installed && pool->solvables[p].repo == pool->installed)
				score = 3;
			    }
			}
		    }
		}
	    }
	}
      if (score == 1 && s->requires)
	{
	  Id id, *idp, p, pp;
	  const char *deps;
	  for (idp = s->repo->idarraydata + s->requires; (id = *idp) != 0; idp++)
	    {
	      while (ISRELDEP(id))
		{
		  Reldep *rd = GETRELDEP(pool, id);
		  id = rd->name;
		}
	      deps = strstr(pool_id2str(pool, id), "locales-");
	      if (!deps)
		continue;
	      if (!strncmp(deps + 8, "en", 2))
		score = 2;
	      else
		{
		  score = 0;
		  FOR_PROVIDES(p, pp, id)
		    {
		      if (solv->decisionmap[p] > 0)
			{
			  score = 4;
			  break;
			}
		      if (pool->installed && pool->solvables[p].repo == pool->installed)
			score = 3;
		    }
		  break;
		}
	    }
	}
      plist->elements[i * 2] = plist->elements[i];
      plist->elements[i * 2 + 1] = score;
    }
  solv_sort(plist->elements, count, sizeof(Id) * 2, urpm_reorder_cmp, pool);
  for (i = 0; i < count; i++)
    plist->elements[i] = plist->elements[2 * i];
  queue_truncate(plist, count);
}


/*
 *  POLICY_MODE_CHOOSE:     default, do all pruning steps
 *  POLICY_MODE_RECOMMEND:  leave out prune_to_recommended
 *  POLICY_MODE_SUGGEST:    leave out prune_to_recommended, do prio pruning just per name
 */
void
policy_filter_unwanted(Solver *solv, Queue *plist, int mode)
{
  Pool *pool = solv->pool;
  if (mode == POLICY_MODE_SUPPLEMENT)
    {
      /* reorder only */
      dislike_old_versions(pool, plist);
      sort_by_common_dep(pool, plist);
      if (solv->urpmreorder)
        urpm_reorder(solv, plist);
      prefer_suggested(solv, plist);
      policy_prefer_favored(solv, plist);
      return;
    }
  if (plist->count > 1)
    {
      if (mode != POLICY_MODE_SUGGEST)
        solver_prune_to_highest_prio(solv, plist);
      else
        solver_prune_to_highest_prio_per_name(solv, plist);
    }
  if (plist->count > 1)
    prune_to_best_arch(pool, plist);
  if (plist->count > 1)
    prune_to_best_version(pool, plist);
  if (plist->count > 1 && (mode == POLICY_MODE_CHOOSE || mode == POLICY_MODE_CHOOSE_NOREORDER))
    {
      prune_to_recommended(solv, plist);
      if (plist->count > 1 && mode != POLICY_MODE_CHOOSE_NOREORDER)
	{
	  /* do some fancy reordering */
#if 0
	  sort_by_srcversion(pool, plist);
#endif
	  dislike_old_versions(pool, plist);
	  sort_by_common_dep(pool, plist);
	  move_installed_to_front(pool, plist);
	  if (solv->urpmreorder)
	    urpm_reorder(solv, plist);
	  prefer_suggested(solv, plist);
	  policy_prefer_favored(solv, plist);
	}
    }
}

void
pool_best_solvables(Pool *pool, Queue *plist, int flags)
{
  if (plist->count > 1)
    prune_to_highest_prio(pool, plist);
  if (plist->count > 1)
    prune_to_best_arch(pool, plist);
  if (plist->count > 1)
    prune_to_best_version(pool, plist);
  if (plist->count > 1)
    {
      dislike_old_versions(pool, plist);
      sort_by_common_dep(pool, plist);
      move_installed_to_front(pool, plist);
    }
}


/* check if there is an illegal architecture change if
 * installed solvable s1 is replaced by s2 */
int
policy_illegal_archchange(Solver *solv, Solvable *s1, Solvable *s2)
{
  Pool *pool = solv->pool;
  Id a1 = s1->arch, a2 = s2->arch;

  /* we allow changes to/from noarch */
  if (a1 == a2 || a1 == pool->noarchid || a2 == pool->noarchid)
    return 0;
  if (!pool->id2arch)
    return 0;
  a1 = pool_arch2score(pool, a1);
  a2 = pool_arch2score(pool, a2);
  if (((a1 ^ a2) & 0xffff0000) != 0)
    return 1;
  return 0;
}

/* check if there is an illegal vendor change if
 * installed solvable s1 is replaced by s2 */
int
policy_illegal_vendorchange(Solver *solv, Solvable *s1, Solvable *s2)
{
  Pool *pool = solv->pool;
  Id v1, v2;
  Id vendormask1, vendormask2;

  if (pool->custom_vendorcheck)
     return pool->custom_vendorcheck(pool, s1, s2);

  /* treat a missing vendor as empty string */
  v1 = s1->vendor ? s1->vendor : ID_EMPTY;
  v2 = s2->vendor ? s2->vendor : ID_EMPTY;
  if (v1 == v2)
    return 0;
  vendormask1 = pool_vendor2mask(pool, v1);
  if (!vendormask1)
    return 1;	/* can't match */
  vendormask2 = pool_vendor2mask(pool, v2);
  if ((vendormask1 & vendormask2) != 0)
    return 0;
  return 1;	/* no class matches */
}

/* check if it is illegal to replace installed
 * package "is" with package "s" (which must obsolete "is")
 */
int
policy_is_illegal(Solver *solv, Solvable *is, Solvable *s, int ignore)
{
  Pool *pool = solv->pool;
  int ret = 0;
  int duppkg = solv->dupinvolvedmap_all || (solv->dupinvolvedmap.size && MAPTST(&solv->dupinvolvedmap, is - pool->solvables));
  if (!(ignore & POLICY_ILLEGAL_DOWNGRADE) && !(duppkg ? solv->dup_allowdowngrade : solv->allowdowngrade))
    {
      if (is->name == s->name && pool_evrcmp(pool, is->evr, s->evr, EVRCMP_COMPARE) > 0)
	ret |= POLICY_ILLEGAL_DOWNGRADE;
    }
  if (!(ignore & POLICY_ILLEGAL_ARCHCHANGE) && !(duppkg ? solv->dup_allowarchchange : solv->allowarchchange))
    {
      if (is->arch != s->arch && policy_illegal_archchange(solv, is, s))
	ret |= POLICY_ILLEGAL_ARCHCHANGE;
    }
  if (!(ignore & POLICY_ILLEGAL_VENDORCHANGE) && !(duppkg ? solv->dup_allowvendorchange : solv->allowvendorchange))
    {
      if (is->vendor != s->vendor && policy_illegal_vendorchange(solv, is, s))
	ret |= POLICY_ILLEGAL_VENDORCHANGE;
    }
  if (!(ignore & POLICY_ILLEGAL_NAMECHANGE) && !(duppkg ? solv->dup_allownamechange : solv->allownamechange))
    {
      if (is->name != s->name)
	ret |= POLICY_ILLEGAL_NAMECHANGE;
    }
  return ret;
}

/*-------------------------------------------------------------------
 *
 * create reverse obsoletes map for installed solvables
 *
 * For each installed solvable find which packages with *different* names
 * obsolete the solvable.
 * This index is used in policy_findupdatepackages() below.
 */
void
policy_create_obsolete_index(Solver *solv)
{
  Pool *pool = solv->pool;
  Solvable *s;
  Repo *installed = solv->installed;
  Id p, pp, obs, *obsp, *obsoletes, *obsoletes_data;
  int i, n, cnt;

  solv->obsoletes = solv_free(solv->obsoletes);
  solv->obsoletes_data = solv_free(solv->obsoletes_data);
  if (!installed || installed->start == installed->end)
    return;
  cnt = installed->end - installed->start;
  solv->obsoletes = obsoletes = solv_calloc(cnt, sizeof(Id));
  for (i = 1; i < pool->nsolvables; i++)
    {
      s = pool->solvables + i;
      if (!s->obsoletes)
	continue;
      if (!pool_installable(pool, s))
	continue;
      obsp = s->repo->idarraydata + s->obsoletes;
      while ((obs = *obsp++) != 0)
	{
	  FOR_PROVIDES(p, pp, obs)
	    {
	      Solvable *ps = pool->solvables + p;;
	      if (ps->repo != installed)
		continue;
	      if (ps->name == s->name)
		continue;
	      if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
		continue;
	      if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
		continue;
	      obsoletes[p - installed->start]++;
	    }
	}
    }
  n = 0;
  for (i = 0; i < cnt; i++)
    if (obsoletes[i])
      {
        n += obsoletes[i] + 1;
        obsoletes[i] = n;
      }
  solv->obsoletes_data = obsoletes_data = solv_calloc(n + 1, sizeof(Id));
  POOL_DEBUG(SOLV_DEBUG_STATS, "obsoletes data: %d entries\n", n + 1);
  for (i = pool->nsolvables - 1; i > 0; i--)
    {
      s = pool->solvables + i;
      if (!s->obsoletes)
	continue;
      if (!pool_installable(pool, s))
	continue;
      obsp = s->repo->idarraydata + s->obsoletes;
      while ((obs = *obsp++) != 0)
	{
	  FOR_PROVIDES(p, pp, obs)
	    {
	      Solvable *ps = pool->solvables + p;;
	      if (ps->repo != installed)
		continue;
	      if (ps->name == s->name)
		continue;
	      if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps, obs))
		continue;
	      if (pool->obsoleteusescolors && !pool_colormatch(pool, s, ps))
		continue;
	      if (obsoletes_data[obsoletes[p - installed->start]] != i)
		obsoletes_data[--obsoletes[p - installed->start]] = i;
	    }
	}
    }
}


/*
 * find update candidates
 *
 * s: installed solvable to be updated
 * qs: [out] queue to hold Ids of candidates
 * allow_all: 0 = dont allow downgrades, 1 = allow all candidates
 *            2 = dup mode
 *
 */
void
policy_findupdatepackages(Solver *solv, Solvable *s, Queue *qs, int allow_all)
{
  /* installed packages get a special upgrade allowed rule */
  Pool *pool = solv->pool;
  Id p, pp, n, p2, pp2;
  Id obs, *obsp;
  Solvable *ps;
  int haveprovobs = 0;
  int allowdowngrade = allow_all ? 1 : solv->allowdowngrade;
  int allownamechange = allow_all ? 1 : solv->allownamechange;
  int allowarchchange = allow_all ? 1 : solv->allowarchchange;
  int allowvendorchange = allow_all ? 1 : solv->allowvendorchange;
  if (allow_all == 2)
    {
      allowdowngrade = solv->dup_allowdowngrade;
      allownamechange = solv->dup_allownamechange;
      allowarchchange = solv->dup_allowarchchange;
      allowvendorchange = solv->dup_allowvendorchange;
    }

  queue_empty(qs);

  n = s - pool->solvables;

  /*
   * look for updates for s
   */
  FOR_PROVIDES(p, pp, s->name)	/* every provider of s' name */
    {
      if (p == n)		/* skip itself */
	continue;

      ps = pool->solvables + p;
      if (s->name == ps->name)	/* name match */
	{
	  if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
	    continue;
	  if (!allowdowngrade && pool_evrcmp(pool, s->evr, ps->evr, EVRCMP_COMPARE) > 0)
	    continue;
	}
      else if (!allownamechange)
	continue;
      else if ((!solv->noupdateprovide || solv->needupdateprovide) && ps->obsoletes)   /* provides/obsoletes combination ? */
	{
	  /* check if package ps obsoletes installed package s */
	  /* implicitobsoleteusescolors is somewhat wrong here, but we nevertheless
	   * use it to limit our update candidates */
	  if ((pool->obsoleteusescolors || pool->implicitobsoleteusescolors) && !pool_colormatch(pool, s, ps))
	    continue;
	  obsp = ps->repo->idarraydata + ps->obsoletes;
	  while ((obs = *obsp++) != 0)	/* for all obsoletes */
	    {
	      FOR_PROVIDES(p2, pp2, obs)   /* and all matching providers of the obsoletes */
		{
		  Solvable *ps2 = pool->solvables + p2;
		  if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, ps2, obs))
		    continue;
		  if (p2 == n)		/* match ! */
		    break;
		}
	      if (p2)			/* match! */
		break;
	    }
	  if (!obs)			/* continue if no match */
	    continue;
	  /* here we have 'p' with a matching provides/obsoletes combination
	   * thus flagging p as a valid update candidate for s
	   */
	  haveprovobs = 1;
	}
      else
        continue;
      if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
	continue;
      if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
	continue;
      queue_push(qs, p);
    }
  if (!allownamechange)
    return;
  /* if we have found some valid candidates and noupdateprovide is not set, we're
     done. otherwise we fallback to all obsoletes */
  if (solv->needupdateprovide || (!solv->noupdateprovide && haveprovobs))
    return;
  if (solv->obsoletes && solv->obsoletes[n - solv->installed->start])
    {
      Id *opp;
      for (opp = solv->obsoletes_data + solv->obsoletes[n - solv->installed->start]; (p = *opp++) != 0;)
	{
	  ps = pool->solvables + p;
	  if (!allowarchchange && s->arch != ps->arch && policy_illegal_archchange(solv, s, ps))
	    continue;
	  if (!allowvendorchange && s->vendor != ps->vendor && policy_illegal_vendorchange(solv, s, ps))
	    continue;
	  /* implicitobsoleteusescolors is somewhat wrong here, but we nevertheless
	   * use it to limit our update candidates */
	  if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, ps))
	    continue;
	  queue_push(qs, p);
	}
    }
}

const char *
policy_illegal2str(Solver *solv, int illegal, Solvable *s, Solvable *rs)
{
  Pool *pool = solv->pool;
  const char *str;
  if (illegal == POLICY_ILLEGAL_DOWNGRADE)
    {
      str = pool_tmpjoin(pool, "downgrade of ", pool_solvable2str(pool, s), 0);
      return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
    }
  if (illegal == POLICY_ILLEGAL_NAMECHANGE)
    {
      str = pool_tmpjoin(pool, "name change of ", pool_solvable2str(pool, s), 0);
      return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
    }
  if (illegal == POLICY_ILLEGAL_ARCHCHANGE)
    {
      str = pool_tmpjoin(pool, "architecture change of ", pool_solvable2str(pool, s), 0);
      return pool_tmpappend(pool, str, " to ", pool_solvable2str(pool, rs));
    }
  if (illegal == POLICY_ILLEGAL_VENDORCHANGE)
    {
      str = pool_tmpjoin(pool, "vendor change from '", pool_id2str(pool, s->vendor), "' (");
      if (rs->vendor)
	{
          str = pool_tmpappend(pool, str, pool_solvable2str(pool, s), ") to '");
          str = pool_tmpappend(pool, str, pool_id2str(pool, rs->vendor), "' (");
	}
      else
        str = pool_tmpappend(pool, str, pool_solvable2str(pool, s), ") to no vendor (");
      return pool_tmpappend(pool, str, pool_solvable2str(pool, rs), ")");
    }
  return "unknown illegal change";
}