PostgreSQL Source Code  git master
knapsack.h File Reference
#include "nodes/bitmapset.h"
Include dependency graph for knapsack.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

BitmapsetDiscreteKnapsack (int max_weight, int num_items, int *item_weights, double *item_values)
 

Function Documentation

◆ DiscreteKnapsack()

Bitmapset* DiscreteKnapsack ( int  max_weight,
int  num_items,
int *  item_weights,
double *  item_values 
)

Definition at line 54 of file knapsack.c.

References ALLOCSET_SMALL_SIZES, AllocSetContextCreate, Assert, bms_add_member(), bms_add_members(), bms_copy(), bms_del_member(), bms_del_members(), bms_make_singleton(), CurrentMemoryContext, i, MemoryContextDelete(), MemoryContextSwitchTo(), palloc(), and values.

Referenced by consider_groupingsets_paths().

56 {
58  "Knapsack",
60  MemoryContext oldctx = MemoryContextSwitchTo(local_ctx);
61  double *values;
62  Bitmapset **sets;
63  Bitmapset *result;
64  int i,
65  j;
66 
67  Assert(max_weight >= 0);
68  Assert(num_items > 0 && item_weights);
69 
70  values = palloc((1 + max_weight) * sizeof(double));
71  sets = palloc((1 + max_weight) * sizeof(Bitmapset *));
72 
73  for (i = 0; i <= max_weight; ++i)
74  {
75  values[i] = 0;
76  sets[i] = bms_make_singleton(num_items);
77  }
78 
79  for (i = 0; i < num_items; ++i)
80  {
81  int iw = item_weights[i];
82  double iv = item_values ? item_values[i] : 1;
83 
84  for (j = max_weight; j >= iw; --j)
85  {
86  int ow = j - iw;
87 
88  if (values[j] <= values[ow] + iv)
89  {
90  /* copy sets[ow] to sets[j] without realloc */
91  if (j != ow)
92  {
93  sets[j] = bms_del_members(sets[j], sets[j]);
94  sets[j] = bms_add_members(sets[j], sets[ow]);
95  }
96 
97  sets[j] = bms_add_member(sets[j], i);
98 
99  values[j] = values[ow] + iv;
100  }
101  }
102  }
103 
104  MemoryContextSwitchTo(oldctx);
105 
106  result = bms_del_member(bms_copy(sets[max_weight]), num_items);
107 
108  MemoryContextDelete(local_ctx);
109 
110  return result;
111 }
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:211
#define AllocSetContextCreate
Definition: memutils.h:170
Bitmapset * bms_copy(const Bitmapset *a)
Definition: bitmapset.c:74
#define ALLOCSET_SMALL_SIZES
Definition: memutils.h:202
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
Bitmapset * bms_make_singleton(int x)
Definition: bitmapset.c:186
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define Assert(condition)
Definition: c.h:739
Bitmapset * bms_add_member(Bitmapset *a, int x)
Definition: bitmapset.c:736
Bitmapset * bms_del_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:928
static Datum values[MAXATTR]
Definition: bootstrap.c:167
void * palloc(Size size)
Definition: mcxt.c:949
int i
Bitmapset * bms_del_member(Bitmapset *a, int x)
Definition: bitmapset.c:773
Bitmapset * bms_add_members(Bitmapset *a, const Bitmapset *b)
Definition: bitmapset.c:793