## Interval partitioning problem different approach – arrange lectures in minimum number of classrooms

The problem of scheduling lectures in minimum number of classrooms is as follows: Find minimum number of classrooms to schedule all lecture so that no two occur at the same time in the same room.

The common algorithm that I find in books is:

Sort intervals by starting time so that s1 ≤ s2 ≤ ... ≤ sn. d ← 0 //number of classrooms for j = 1 to n {  if (lecture j is compatible with some classroom k)  schedule lecture j in classroom k  else  allocate a new classroom d + 1  schedule lecture j in classroom d + 1  d ← d + 1 } 

Now, I was thinking of an alternate approach where I sort my lectures by finishing times in ascending order and every time I check if lecture j is compatible with some classroom k and there are multiple classrooms that are compatible with that lecture, I schedule it in the classroom which the last jobs finish time in that classroom is closest to that jobs start time, i.e minimise the time a classroom is empty.

Sort intervals by starting time so that f1 ≤ f2 ≤ ... ≤ fn. d ← 0 //number of classrooms for j = 1 to n {  if (lecture j is compatible with some classroom k)  schedule lecture j in classroom k which was used last  else  allocate a new classroom d + 1  schedule lecture j in classroom d + 1  d ← d + 1 } 

I would like to know if this approach is right(not necessarily optimal). I have dry run it on a couple of cases, and looks to be okay. If yes, how can I prove its correctness? If not, how can what changes can I make the algorithm work.

## Given a set of intervals $(I_n)_n$ contained in $[0, L]$, compute the longest interval in $[0, L]$ which has empty intersection with all $(I_n)_n$

Let be $$(I_n)_n$$ a set of $$p$$ intervals each contained in $$[0, L]$$ for $$L \geq 1$$.

I define $$(J_n = [a_n, b_n])_n$$ the set of intervals which have empty intersection with $$I_n$$ for all $$n \in [[1, p]]$$.

I’d like to efficiently compute $$\max_n (b_n – a_n + 1)$$.

A basic idea I’d try would be to:

(1) Create a segment tree for $$(I_n)_n$$ in $$O(p \ln p)$$

(2) Iterate over $$[0, L]$$ and count the longest line before encountering an interval covered by $$(I_n)_n$$ (resetting the “max value” to 0 until the next of a certain $$J_q$$)

Which could give me an algorithm in $$O(L + p \ln p)$$, the problem is that $$L$$ is really big in my instances, I’d like to have an algorithm which does not depend on $$L$$.

## How Segment trees are used to answer interval stabbing query?

I’m new here and to Algorithms in general. I have a question that bugs and I have searched google without any fruitful results sadly.

Can anybody explain to me how segment trees are used to answer interval stabbing queries? I have searched and searched and only come with the beginning of the line. From my understanding I need to take all the end points of every interval and build a segment tree then what?

How can we know the original intervals now? How can we check whether a given point is present in any of them and report all those intervals with the point in them.

## DP for Weighted Interval Scheduling: Can we sort start time in ascending order instead of the finish time?

Problem In the weighted interval scheduling problem, we want to find the maximum-weight subset of nonoverlapping jobs, given a set $$J$$ of jobs that have weights associated with them. Job $$i \in J$$ has a start time $$s_i$$, a finish time $$f_i$$, and a weight $$w_i > 0$$. We seek to find an optimal schedule — a subset $$\cal{O}$$ of non-overlapping jobs in $$J$$ with the maximum possible sum of weights.

Dynamic Programming Solution In almost all textbooks or materials I could get access to, a DP algorithm is given as follows: First all jobs are sorted in order of ascending finish time: $$f_1\le f_2 \le \dots \le f_n$$. Let $$OPT(i)$$ be the maximum total weight of non-overlapping jobs in $$\{1,2, \cdots ,i\}$$, $$p_i$$ be the largest index $$j < i$$ such that job $$j$$ and $$i$$ are compatible. Then we see that $$OPT(i) = \max\{{w_i + OPT(p_i), OPT(i-1)}\}$$

My question I think I could understand why this DP algorithm works. However I didn’t see the necessasity for the sorting step. What if we sort the finish time in ascending order instead? As far as I can see, it’s also correct.

## VectorPlot interval as a union of intervals

What would you suggest me to do if I want to use VectorPlot in a certain region of a plane? e.g.:

data1 = {{3.3, 2.97, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3.8,  4.2}, {3.85, 3.65, 3.76, 3.41, 3.31, 3.33, 3.35, 3.3, 3.38, 3.44,  3.6, 3.65, 4., 4.2, 4.45}, {4.48, 4.35, 4.45, 4.28, 4.22, 4.25,  4.31, 4.3, 4.32, 4.37, 4.45, 4.46, 4.6, 4.67, 4.76}, {5.08, 5.09,  5.17, 5.25, 5.23, 5.27, 5.27, 5.28, 5.3, 5.3, 5.35, 5.3, 5.27,  5.23, 5.22}, {5.75, 5.85, 5.25, 6.18, 6.31, 6.30, 6.45, 6.44,  6.65, 6.62, 6.52, 6.41, 6.17, 6.02, 5.86}, {7.5, 7.22, 12, 12, 12,  12, 12, 12, 12, 12, 12, 12, 12, 6.33, 6.22}, {6.56, 6.74, 7.01,  7.28, 7.41, 7.52, 7.52, 7.53, 7.51, 7.45, 7.36, 7.22, 6.95, 6.63,  6.5}, {6.9, 7.03, 7.08, 7.22, 7.3, 7.32, 7.35, 7.34, 7.29, 7.23,  7.18, 7.12, 7.0, 6.84, 6.7}}; 

Interpolating:

intF = Interpolation[Flatten[MapIndexed[{#2, #} &, Transpose@data1, {2}], 1],InterpolationOrder -> {1, 1}, Method -> "Hermite"]; cp = ContourPlot[intF[x, y], {x, 1, 15}, {y, 0.9, 8}, Contours -> Range[0.1, 12], AspectRatio -> Automatic, ImageSize -> Medium, PlotLegends -> {Automatic}];  sp = StreamPlot[Evaluate[-D[intF[x, y], {{x, y}}]], {x, 1, 15}, {y, u} , StreamScale -> Coarse, StreamStyle -> {"Arrow", Directive[Thin, Blue]}, StreamPoints -> Fine, AspectRatio -> Automatic, VectorScale -> Automatic]; vp := VectorPlot[Evaluate[-D[intF[x, y], {{x, y}}]], {x, 1, 15}, {y, 1.5, 8}, VectorScale -> Automatic, VectorStyle -> {LighterBlue}] sp2 = MapAt[{Opacity[0.25], #} &, sp, 1];  Show[cp, vp, sp2] 

In this case I want to constrain the values given to y in:

vp := VectorPlot[Evaluate[-D[intF[x, y], {{x, y}}]], {x, 1, 15}, {y, 1.5, 8}, VectorScale -> Automatic, VectorStyle -> {LighterBlue}] 

Something like:

u=IntervalUnion[{1,5.9},{6.1,8}] 

then:

vp := VectorPlot[Evaluate[-D[intF[x, y], {{x, y}}]], {x, 1, 15}, {y, u}, VectorScale -> Automatic, VectorStyle -> {LighterBlue}] 

is this possible?

Edit: the idea is to avoid vectors in the region of the upper bar:

because those tend to modify the scale of the vectors and its magnitudes.

## Check which interval range does a number lie in between

I have an array of time variables t(1), t(2), ..., t(n). and corresponding variables v(1), v(2), ..., v(n). Given a time tx, I would like to efficiently find between which interval (t(i), t(i+1)] does the number lie. Correspondingly, the variable vx needs to be interpolated which can be done easily afterwards.

P.S: The time t(i=1) is initialized with t1 and is always increasing and greater than zero.

## How many floating point ops were performed worldwide over a time interval

I am looking for information regarding the evolution of computing capability. Specifically I would like to know how many floating point operations were performed worldwide from, say, the deployment of ENIAC to the first moon landing… Or any other time interval before, say, the ’80s. is this a question that I can estimate based on existing data?

## How to compute function rolling averages maximum for each possible interval length?

I’m searching for a fast way to calculate

$$g(k):=\max_{0 for $$0

given some discreet function $$f$$ defined on $$[0; T]$$.

Is there algorithm to do it faster then naive $$O(n^2)$$? If so how?

## Term for a title automatically changing at an interval?

I’m looking for a search term and ultimately examples of website which have something along the line of:

This is Beatiful 

Where the word Beautiful would change after a few seconds to Amazing or Stunning.

I know how to implement this but it must have a name? For now, all I was able to find were questions on how to implement something like this.

With some more research a stumbled onto a typing carousel or text carousel which come very close to the effect.

## Generic Macro Generated Interval Heap in C

When I saw this amazing data structure I couldn’t stop myself from trying it! I first sought resources such as this and an article about it here. It basically works as a Max-Heap and a Min-Heap at the same time. I was heavily based on these amazing Java implementations here and here.

This implementation is a macro that generates code for whichever data type you wish to work with and it is part of the C Macro Collections library currently hosted on GitHub and further to be improved when needed.

The main macro is INTERVALHEAP_GENERATE with the following parameters:

• PFX – Functions prefix
• SNAME – The struct name
• FMOD – Function modifier (currently only static or empty)
• V – Data type you wish to work with

All you need to get started is the header intervalheap.h. Now since a question is limited to 65536 characters I had to strip down a lot of code and lines so that it could fit in this question since the macro takes a lot of characters. The code might seem a bit squished together but you can find the original code (and maybe in the future, updated) here.

intervalheap.h

#ifndef CMC_INTERVALHEAP_H #define CMC_INTERVALHEAP_H  #include <stdlib.h> #include <stdbool.h> #include <string.h>  #define INTERVALHEAP_GENERATE(PFX, SNAME, FMOD, V)    \     INTERVALHEAP_GENERATE_HEADER(PFX, SNAME, FMOD, V) \     INTERVALHEAP_GENERATE_SOURCE(PFX, SNAME, FMOD, V)  /* HEADER ********************************************************************/ #define INTERVALHEAP_GENERATE_HEADER(PFX, SNAME, FMOD, V)                    \                                                                              \     /* Heap Structure */                                                     \     typedef struct SNAME##_s                                                 \     {                                                                        \         /* Dynamic array of nodes */                                         \         struct SNAME##_node_s *buffer;                                       \         /* Current array capacity (how many nodes can be stored) */          \         size_t capacity;                                                     \         /* Current amount of nodes in the dynamic array */                   \         size_t size;                                                         \         /* Current amount of elements in the heap */                         \         size_t count;                                                        \         /* Element comparison function */                                    \         int (*cmp)(V, V);                                                    \         /* Function that returns an iterator to the start of the heap */     \         struct SNAME##_iter_s (*it_start)(struct SNAME##_s *);               \         /* Function that returns an iterator to the end of the heap */       \         struct SNAME##_iter_s (*it_end)(struct SNAME##_s *);                 \     } SNAME, *SNAME##_ptr;                                                   \                                                                              \     /* Heap Node */                                                          \     typedef struct SNAME##_node_s                                            \     {                                                                        \         /* 0 - Value belonging to the MinHeap */                             \         /* 1 - Value belonging to the MaxHeap */                             \         V data[2];                                                           \     } SNAME##_node, *SNAME##_node_ptr;                                       \                                                                              \     /* Heap Iterator */                                                      \     typedef struct SNAME##_iter_s                                            \     {                                                                        \         /* Target heap */                                                    \         struct SNAME##_s *target;                                            \         /* Cursor's position (index) */                                      \         size_t cursor;                                                       \         /* If the iterator has reached the start of the iteration */         \         bool start;                                                          \         /* If the iterator has reached the end of the iteration */           \         bool end;                                                            \     } SNAME##_iter, *SNAME##_iter_ptr;                                       \                                                                              \     FMOD SNAME *PFX##_new(size_t capacity, int (*compare)(V, V));            \     FMOD void PFX##_clear(SNAME *_heap_);                                    \     FMOD void PFX##_free(SNAME *_heap_);                                     \     FMOD bool PFX##_insert(SNAME *_heap_, V element);                        \     FMOD bool PFX##_remove_max(SNAME *_heap_, V *result);                    \     FMOD bool PFX##_remove_min(SNAME *_heap_, V *result);                    \     FMOD bool PFX##_insert_if(SNAME *_heap_, V element, bool condition);     \     FMOD bool PFX##_remove_max_if(SNAME *_heap_, V *result, bool condition); \     FMOD bool PFX##_remove_min_if(SNAME *_heap_, V *result, bool condition); \     FMOD bool PFX##_update_max(SNAME *_heap_, V element);                    \     FMOD bool PFX##_update_min(SNAME *_heap_, V element);                    \     FMOD bool PFX##_max(SNAME *_heap_, V *value);                            \     FMOD bool PFX##_min(SNAME *_heap_, V *value);                            \     FMOD bool PFX##_contains(SNAME *_heap_, V element);                      \     FMOD bool PFX##_empty(SNAME *_heap_);                                    \     FMOD bool PFX##_full(SNAME *_heap_);                                     \     FMOD size_t PFX##_count(SNAME *_heap_);                                  \     FMOD size_t PFX##_capacity(SNAME *_heap_);                               \                                                                              \     FMOD SNAME##_iter *PFX##_iter_new(SNAME *target);                        \     FMOD void PFX##_iter_free(SNAME##_iter *iter);                           \     FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target);            \     FMOD bool PFX##_iter_start(SNAME##_iter *iter);                          \     FMOD bool PFX##_iter_end(SNAME##_iter *iter);                            \     FMOD void PFX##_iter_to_start(SNAME##_iter *iter);                       \     FMOD void PFX##_iter_to_end(SNAME##_iter *iter);                         \     FMOD bool PFX##_iter_next(SNAME##_iter *iter);                           \     FMOD bool PFX##_iter_prev(SNAME##_iter *iter);                           \     FMOD V PFX##_iter_value(SNAME##_iter *iter);                             \     FMOD size_t PFX##_iter_index(SNAME##_iter *iter);                        \                                                                              \     /* Default Value */                                                      \     static inline V PFX##_impl_default_value(void)                           \     {                                                                        \         V _empty_value_;                                                     \                                                                              \         memset(&_empty_value_, 0, sizeof(V));                                \                                                                              \         return _empty_value_;                                                \     }  #define INTERVALHEAP_GENERATE_SOURCE(PFX, SNAME, FMOD, V)                                   \                                                                                             \     /* Implementation Detail Functions */                                                   \     static bool PFX##_impl_grow(SNAME *_heap_);                                             \     static void PFX##_impl_float_up_max(SNAME *_heap_);                                     \     static void PFX##_impl_float_up_min(SNAME *_heap_);                                     \     static void PFX##_impl_float_down_max(SNAME *_heap_);                                   \     static void PFX##_impl_float_down_min(SNAME *_heap_);                                   \     static SNAME##_iter PFX##_impl_it_start(SNAME *_heap_);                                 \     static SNAME##_iter PFX##_impl_it_end(SNAME *_heap_);                                   \                                                                                             \     FMOD SNAME *PFX##_new(size_t capacity, int (*compare)(V, V))                            \     {                                                                                       \         SNAME *_heap_ = malloc(sizeof(SNAME));                                              \         if (!_heap_)                                                                        \             return NULL;                                                                    \                                                                                             \         /* Since each node can store two elements, divide the actual capacity by 2 */       \         /* Round the capacity of nodes up */                                                \         capacity = capacity % 2 == 0 ? capacity / 2 : (capacity + 1) / 2;                   \         _heap_->buffer = malloc(sizeof(SNAME##_node) * capacity);                           \                                                                                             \         if (!_heap_->buffer)                                                                \         {                                                                                   \             free(_heap_);                                                                   \             return NULL;                                                                    \         }                                                                                   \         memset(_heap_->buffer, 0, sizeof(SNAME##_node) * capacity);                         \         _heap_->capacity = capacity;                                                        \         _heap_->size = 0;                                                                   \         _heap_->count = 0;                                                                  \         _heap_->cmp = compare;                                                              \         _heap_->it_start = PFX##_impl_it_start;                                             \         _heap_->it_end = PFX##_impl_it_end;                                                 \         return _heap_;                                                                      \     }                                                                                       \                                                                                             \     FMOD void PFX##_clear(SNAME *_heap_)                                                    \     {                                                                                       \         memset(_heap_->buffer, 0, sizeof(V) * _heap_->capacity);                            \         _heap_->size = 0;                                                                   \         _heap_->count = 0;                                                                  \     }                                                                                       \                                                                                             \     FMOD void PFX##_free(SNAME *_heap_)                                                     \     {                                                                                       \         free(_heap_->buffer);                                                               \         free(_heap_);                                                                       \     }                                                                                       \                                                                                             \     FMOD bool PFX##_insert(SNAME *_heap_, V element)                                        \     {                                                                                       \         if (PFX##_full(_heap_))                                                             \         {                                                                                   \             if (!PFX##_impl_grow(_heap_))                                                   \                 return false;                                                               \         }                                                                                   \                                                                                             \         if (PFX##_count(_heap_) % 2 == 0)                                                   \         {                                                                                   \             /* Occupying a new node */                                                      \             _heap_->buffer[_heap_->size].data[0] = element;                                 \             _heap_->buffer[_heap_->size].data[1] = PFX##_impl_default_value();              \                                                                                             \             _heap_->size++;                                                                 \         }                                                                                   \         else                                                                                \         {                                                                                   \             SNAME##_node *curr_node = &(_heap_->buffer[_heap_->size - 1]);                  \                                                                                             \             if (_heap_->cmp(curr_node->data[0], element) > 0)                               \             {                                                                               \                 curr_node->data[1] = curr_node->data[0];                                    \                 curr_node->data[0] = element;                                               \             }                                                                               \             else                                                                            \             {                                                                               \                 curr_node->data[1] = element;                                               \             }                                                                               \         }                                                                                   \                                                                                             \         _heap_->count++;                                                                    \                                                                                             \         if (PFX##_count(_heap_) <= 2)                                                       \             return true;                                                                    \                                                                                             \         SNAME##_node *parent = &(_heap_->buffer[(_heap_->size - 1) / 2]);                   \                                                                                             \         if (_heap_->cmp(parent->data[0], element) > 0)                                      \             PFX##_impl_float_up_min(_heap_);                                                \         else if (_heap_->cmp(parent->data[1], element) < 0)                                 \             PFX##_impl_float_up_max(_heap_);                                                \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD bool PFX##_remove_max(SNAME *_heap_, V *result)                                    \     {                                                                                       \         if (PFX##_empty(_heap_))                                                            \             return false;                                                                   \                                                                                             \         if (PFX##_count(_heap_) == 1)                                                       \         {                                                                                   \             *result = _heap_->buffer[0].data[0];                                            \             _heap_->buffer[0].data[0] = PFX##_impl_default_value();                         \             _heap_->count--;                                                                \             return true;                                                                    \         }                                                                                   \         else                                                                                \             *result = _heap_->buffer[0].data[1];                                            \                                                                                             \         SNAME##_node *last_node = &(_heap_->buffer[_heap_->size - 1]);                      \                                                                                             \         if (PFX##_count(_heap_) % 2 == 1)                                                   \         {                                                                                   \             _heap_->buffer[0].data[1] = last_node->data[0];                                 \             last_node->data[0] = PFX##_impl_default_value();                                \             _heap_->size--;                                                                 \         }                                                                                   \         else                                                                                \         {                                                                                   \             _heap_->buffer[0].data[1] = last_node->data[1];                                 \                                                                                             \             last_node->data[1] = PFX##_impl_default_value();                                \         }                                                                                   \                                                                                             \         _heap_->count--;                                                                    \                                                                                             \         PFX##_impl_float_down_max(_heap_);                                                  \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD bool PFX##_remove_min(SNAME *_heap_, V *result)                                    \     {                                                                                       \         if (PFX##_empty(_heap_))                                                            \             return false;                                                                   \                                                                                             \         *result = _heap_->buffer[0].data[0];                                                \                                                                                             \         if (PFX##_count(_heap_) == 1)                                                       \         {                                                                                   \             _heap_->buffer[0].data[0] = PFX##_impl_default_value();                         \                                                                                             \             _heap_->count--;                                                                \                                                                                             \             return true;                                                                    \         }                                                                                   \                                                                                             \         SNAME##_node *last_node = &(_heap_->buffer[_heap_->size - 1]);                      \                                                                                             \         _heap_->buffer[0].data[0] = last_node->data[0];                                     \                                                                                             \         if (PFX##_count(_heap_) % 2 == 1)                                                   \         {                                                                                   \             last_node->data[0] = PFX##_impl_default_value();                                \                                                                                             \             _heap_->size--;                                                                 \         }                                                                                   \         else                                                                                \         {                                                                                   \             last_node->data[0] = last_node->data[1];                                        \             last_node->data[1] = PFX##_impl_default_value();                                \         }                                                                                   \                                                                                             \         _heap_->count--;                                                                    \                                                                                             \         PFX##_impl_float_down_min(_heap_);                                                  \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD bool PFX##_insert_if(SNAME *_heap_, V element, bool condition)                     \     {                                                                                       \         if (condition)                                                                      \             return PFX##_insert(_heap_, element);                                           \                                                                                             \         return false;                                                                       \     }                                                                                       \                                                                                             \     FMOD bool PFX##_remove_max_if(SNAME *_heap_, V *result, bool condition)                 \     {                                                                                       \         if (condition)                                                                      \             return PFX##_remove_max(_heap_, result);                                        \                                                                                             \         return false;                                                                       \     }                                                                                       \                                                                                             \     FMOD bool PFX##_remove_min_if(SNAME *_heap_, V *result, bool condition)                 \     {                                                                                       \         if (condition)                                                                      \             return PFX##_remove_min(_heap_, result);                                        \                                                                                             \         return false;                                                                       \     }                                                                                       \                                                                                             \     FMOD bool PFX##_update_max(SNAME *_heap_, V element)                                    \     {                                                                                       \         if (PFX##_empty(_heap_))                                                            \             return false;                                                                   \                                                                                             \         if (PFX##_count(_heap_) == 1)                                                       \         {                                                                                   \             _heap_->buffer[0].data[0] = element;                                            \         }                                                                                   \         else if (_heap_->cmp(element, _heap_->buffer[0].data[0]) < 0)                       \         {                                                                                   \             _heap_->buffer[0].data[1] = _heap_->buffer[0].data[0];                          \             _heap_->buffer[0].data[0] = element;                                            \                                                                                             \             PFX##_impl_float_down_max(_heap_);                                              \         }                                                                                   \         else                                                                                \         {                                                                                   \             _heap_->buffer[0].data[1] = element;                                            \                                                                                             \             PFX##_impl_float_down_max(_heap_);                                              \         }                                                                                   \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD bool PFX##_update_min(SNAME *_heap_, V element)                                    \     {                                                                                       \         if (PFX##_empty(_heap_))                                                            \             return false;                                                                   \                                                                                             \         if (PFX##_count(_heap_) == 1)                                                       \         {                                                                                   \             _heap_->buffer[0].data[0] = element;                                            \         }                                                                                   \         else if (_heap_->cmp(element, _heap_->buffer[0].data[1]) > 0)                       \         {                                                                                   \             _heap_->buffer[0].data[0] = _heap_->buffer[0].data[1];                          \             _heap_->buffer[0].data[1] = element;                                            \                                                                                             \             PFX##_impl_float_down_min(_heap_);                                              \         }                                                                                   \         else                                                                                \         {                                                                                   \             _heap_->buffer[0].data[0] = element;                                            \                                                                                             \             PFX##_impl_float_down_min(_heap_);                                              \         }                                                                                   \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD bool PFX##_max(SNAME *_heap_, V *value)                                            \     {                                                                                       \         if (PFX##_empty(_heap_))                                                            \             return false;                                                                   \                                                                                             \         if (PFX##_count(_heap_) == 1)                                                       \             *value = _heap_->buffer[0].data[0];                                             \         else                                                                                \             *value = _heap_->buffer[0].data[1];                                             \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD bool PFX##_min(SNAME *_heap_, V *value)                                            \     {                                                                                       \         if (PFX##_empty(_heap_))                                                            \             return false;                                                                   \                                                                                             \         *value = _heap_->buffer[0].data[0];                                                 \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD bool PFX##_contains(SNAME *_heap_, V element)                                      \     {                                                                                       \         for (size_t i = 0; i < _heap_->count; i++)                                          \         {                                                                                   \             if (_heap_->cmp(_heap_->buffer[i / 2].data[i % 2], element) == 0)               \                 return true;                                                                \         }                                                                                   \                                                                                             \         return false;                                                                       \     }                                                                                       \                                                                                             \     FMOD bool PFX##_empty(SNAME *_heap_)                                                    \     {                                                                                       \         return _heap_->count == 0;                                                          \     }                                                                                       \                                                                                             \     FMOD bool PFX##_full(SNAME *_heap_)                                                     \     {                                                                                       \         /* The heap is full if all nodes are completely filled */                           \         return _heap_->size >= _heap_->capacity && _heap_->count % 2 == 0;                  \     }                                                                                       \                                                                                             \     FMOD size_t PFX##_count(SNAME *_heap_)                                                  \     {                                                                                       \         return _heap_->count;                                                               \     }                                                                                       \                                                                                             \     FMOD size_t PFX##_capacity(SNAME *_heap_)                                               \     {                                                                                       \         /* Multiply by 2 since each node can store two elements */                          \         return _heap_->capacity * 2;                                                        \     }                                                                                       \                                                                                             \     FMOD SNAME##_iter *PFX##_iter_new(SNAME *target)                                        \     {                                                                                       \         SNAME##_iter *iter = malloc(sizeof(SNAME##_iter));                                  \                                                                                             \         if (!iter)                                                                          \             return NULL;                                                                    \                                                                                             \         PFX##_iter_init(iter, target);                                                      \                                                                                             \         return iter;                                                                        \     }                                                                                       \                                                                                             \     FMOD void PFX##_iter_free(SNAME##_iter *iter)                                           \     {                                                                                       \         free(iter);                                                                         \     }                                                                                       \                                                                                             \     FMOD void PFX##_iter_init(SNAME##_iter *iter, SNAME *target)                            \     {                                                                                       \         iter->target = target;                                                              \         iter->cursor = 0;                                                                   \         iter->start = true;                                                                 \         iter->end = PFX##_empty(target);                                                    \     }                                                                                       \                                                                                             \     FMOD bool PFX##_iter_start(SNAME##_iter *iter)                                          \     {                                                                                       \         return PFX##_empty(iter->target) || iter->start;                                    \     }                                                                                       \                                                                                             \     FMOD bool PFX##_iter_end(SNAME##_iter *iter)                                            \     {                                                                                       \         return PFX##_empty(iter->target) || iter->end;                                      \     }                                                                                       \                                                                                             \     FMOD void PFX##_iter_to_start(SNAME##_iter *iter)                                       \     {                                                                                       \         iter->cursor = 0;                                                                   \         iter->start = true;                                                                 \         iter->end = PFX##_empty(iter->target);                                              \     }                                                                                       \                                                                                             \     FMOD void PFX##_iter_to_end(SNAME##_iter *iter)                                         \     {                                                                                       \         iter->cursor = iter->target->count - 1;                                             \         iter->start = PFX##_empty(iter->target);                                            \         iter->end = true;                                                                   \     }                                                                                       \                                                                                             \     FMOD bool PFX##_iter_next(SNAME##_iter *iter)                                           \     {                                                                                       \         if (iter->end)                                                                      \             return false;                                                                   \                                                                                             \         iter->start = false;                                                                \                                                                                             \         if (iter->cursor == iter->target->count - 1)                                        \             iter->end = true;                                                               \         else                                                                                \             iter->cursor++;                                                                 \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD bool PFX##_iter_prev(SNAME##_iter *iter)                                           \     {                                                                                       \         if (iter->start)                                                                    \             return false;                                                                   \                                                                                             \         iter->end = false;                                                                  \                                                                                             \         if (iter->cursor == 0)                                                              \             iter->start = true;                                                             \         else                                                                                \             iter->cursor--;                                                                 \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     FMOD V PFX##_iter_value(SNAME##_iter *iter)                                             \     {                                                                                       \         if (PFX##_empty(iter->target))                                                      \             return PFX##_impl_default_value();                                              \                                                                                             \         return iter->target->buffer[iter->cursor / 2].data[iter->cursor % 2];               \     }                                                                                       \                                                                                             \     FMOD size_t PFX##_iter_index(SNAME##_iter *iter)                                        \     {                                                                                       \         return iter->cursor;                                                                \     }                                                                                       \                                                                                             \     static bool PFX##_impl_grow(SNAME *_heap_)                                              \     {                                                                                       \         size_t new_cap = _heap_->capacity * 2;                                              \                                                                                             \         SNAME##_node *new_buffer = realloc(_heap_->buffer, sizeof(SNAME##_node) * new_cap); \                                                                                             \         if (!new_buffer)                                                                    \             return false;                                                                   \                                                                                             \         memset(new_buffer + _heap_->capacity, 0, sizeof(SNAME##_node) * _heap_->capacity);  \                                                                                             \         _heap_->buffer = new_buffer;                                                        \         _heap_->capacity = new_cap;                                                         \                                                                                             \         return true;                                                                        \     }                                                                                       \                                                                                             \     static void PFX##_impl_float_up_max(SNAME *_heap_)                                      \     {                                                                                       \         size_t index = _heap_->size - 1;                                                    \                                                                                             \         SNAME##_node *curr_node = &(_heap_->buffer[index]);                                 \                                                                                             \         while (index > 0)                                                                   \         {                                                                                   \             /* Parent index */                                                              \             size_t P_index = (index - 1) / 2;                                               \                                                                                             \             SNAME##_node *parent = &(_heap_->buffer[P_index]);                              \                                                                                             \             if (index == _heap_->size - 1 && PFX##_count(_heap_) % 2 != 0)                  \             {                                                                               \                 if (_heap_->cmp(curr_node->data[0], parent->data[1]) < 0)                   \                     break;                                                                  \                                                                                             \                 V tmp = curr_node->data[0];                                                 \                 curr_node->data[0] = parent->data[1];                                       \                 parent->data[1] = tmp;                                                      \             }                                                                               \             else                                                                            \             {                                                                               \                 if (_heap_->cmp(curr_node->data[1], parent->data[1]) < 0)                   \                     break;                                                                  \                                                                                             \                 V tmp = curr_node->data[1];                                                 \                 curr_node->data[1] = parent->data[1];                                       \                 parent->data[1] = tmp;                                                      \             }                                                                               \                                                                                             \             index = P_index;                                                                \             curr_node = parent;                                                             \         }                                                                                   \     }                                                                                       \                                                                                             \     static void PFX##_impl_float_up_min(SNAME *_heap_)                                      \     {                                                                                       \         size_t index = _heap_->size - 1;                                                    \         SNAME##_node *curr_node = &(_heap_->buffer[index]);                                 \                                                                                             \         while (index > 0)                                                                   \         {                                                                                   \             size_t P_index = (index - 1) / 2;                                               \             SNAME##_node *parent = &(_heap_->buffer[P_index]);                              \                                                                                             \             if (_heap_->cmp(curr_node->data[0], parent->data[0]) >= 0)                      \                 break;                                                                      \                                                                                             \             V tmp = curr_node->data[0];                                                     \             curr_node->data[0] = parent->data[0];                                           \             parent->data[0] = tmp;                                                          \             index = P_index;                                                                \             curr_node = parent;                                                             \         }                                                                                   \     }                                                                                       \                                                                                             \     static void PFX##_impl_float_down_max(SNAME *_heap_)                                    \     {                                                                                       \         size_t index = 0;                                                                   \         SNAME##_node *curr_node = &(_heap_->buffer[index]);                                 \                                                                                             \         while (true)                                                                        \         {                                                                                   \             if (2 * index + 1 >= _heap_->size)                                              \                 break;                                                                      \                                                                                             \             size_t child;                                                                   \             size_t L_index = index * 2 + 1;                                                 \             size_t R_index = index * 2 + 2;                                                 \                                                                                             \             if (R_index < _heap_->size)                                                     \             {                                                                               \                 SNAME##_node *L = &(_heap_->buffer[L_index]);                               \                 SNAME##_node *R = &(_heap_->buffer[R_index]);                               \                                                                                             \                 if (R_index == _heap_->size - 1 && PFX##_count(_heap_) % 2 != 0)            \                     child = _heap_->cmp(L->data[1], R->data[0]) > 0 ? L_index : R_index;    \                 else                                                                        \                     child = _heap_->cmp(L->data[1], R->data[1]) > 0 ? L_index : R_index;    \             }                                                                               \             else                                                                            \                 child = L_index;                                                            \                                                                                             \             SNAME##_node *child_node = &(_heap_->buffer[child]);                            \                                                                                             \             if (child == _heap_->size - 1 && PFX##_count(_heap_) % 2 != 0)                  \             {                                                                               \                 if (_heap_->cmp(curr_node->data[1], child_node->data[0]) >= 0)              \                     break;                                                                  \                                                                                             \                 V tmp = child_node->data[0];                                                \                 child_node->data[0] = curr_node->data[1];                                   \                 curr_node->data[1] = tmp;                                                   \             }                                                                               \             else                                                                            \             {                                                                               \                 if (_heap_->cmp(curr_node->data[1], child_node->data[1]) >= 0)              \                     break;                                                                  \                                                                                             \                 V tmp = child_node->data[1];                                                \                 child_node->data[1] = curr_node->data[1];                                   \                 curr_node->data[1] = tmp;                                                   \                                                                                             \                 if (_heap_->cmp(child_node->data[0], child_node->data[1]) > 0)              \                 {                                                                           \                     tmp = child_node->data[0];                                              \                     child_node->data[0] = child_node->data[1];                              \                     child_node->data[1] = tmp;                                              \                 }                                                                           \             }                                                                               \             index = child;                                                                  \             curr_node = child_node;                                                         \         }                                                                                   \     }                                                                                       \                                                                                             \     static void PFX##_impl_float_down_min(SNAME *_heap_)                                    \     {                                                                                       \         size_t index = 0;                                                                   \                                                                                             \         SNAME##_node *curr_node = &(_heap_->buffer[index]);                                 \                                                                                             \         while (true)                                                                        \         {                                                                                   \             if (2 * index + 1 >= _heap_->size)                                              \                 break;                                                                      \                                                                                             \             size_t child;                                                                   \             size_t L_index = index * 2 + 1;                                                 \             size_t R_index = index * 2 + 2;                                                 \                                                                                             \             if (R_index < _heap_->size)                                                     \             {                                                                               \                 SNAME##_node *L = &(_heap_->buffer[L_index]);                               \                 SNAME##_node *R = &(_heap_->buffer[R_index]);                               \                 child = _heap_->cmp(L->data[0], R->data[0]) < 0 ? L_index : R_index;        \             }                                                                               \             else                                                                            \                 child = L_index;                                                            \                                                                                             \             SNAME##_node *child_node = &(_heap_->buffer[child]);                            \                                                                                             \             if (_heap_->cmp(curr_node->data[0], child_node->data[0]) < 0)                   \                 break;                                                                      \                                                                                             \             V tmp = child_node->data[0];                                                    \             child_node->data[0] = curr_node->data[0];                                       \             curr_node->data[0] = tmp;                                                       \                                                                                             \             if (child != _heap_->size - 1 || PFX##_count(_heap_) % 2 == 0)                  \             {                                                                               \                 if (_heap_->cmp(child_node->data[0], child_node->data[1]) > 0)              \                 {                                                                           \                     tmp = child_node->data[0];                                              \                     child_node->data[0] = child_node->data[1];                              \                     child_node->data[1] = tmp;                                              \                 }                                                                           \             }                                                                               \             index = child;                                                                  \             curr_node = child_node;                                                         \         }                                                                                   \     }                                                                                       \                                                                                             \     static SNAME##_iter PFX##_impl_it_start(SNAME *_heap_)                                  \     {                                                                                       \         SNAME##_iter iter;                                                                  \         PFX##_iter_init(&iter, _heap_);                                                     \         PFX##_iter_to_start(&iter);                                                         \         return iter;                                                                        \     }                                                                                       \                                                                                             \     static SNAME##_iter PFX##_impl_it_end(SNAME *_heap_)                                    \     {                                                                                       \         SNAME##_iter iter;                                                                  \         PFX##_iter_init(&iter, _heap_);                                                     \         PFX##_iter_to_end(&iter);                                                           \         return iter;                                                                        \     }  #endif /* CMC_INTERVALHEAP_H */ 

test.c

#include "intervalheap.h" #include <stdio.h>  /* Generate the Interval Heap */ INTERVALHEAP_GENERATE(h, heap, , int)  /* Comparison function */ int intcmp(int a, int b) {     return (a > b) - (a < b); }  #define TOTAL 100  int main(void) {     heap *my_heap = h_new(50, intcmp);      for (size_t i = 1; i <= TOTAL; i++)     {         h_insert(my_heap, i);     }      size_t half = h_count(my_heap) / 2;      for (size_t i = 0; !h_empty(my_heap); i++)     {         int r;          i < half ? h_remove_max(my_heap, &r) : h_remove_min(my_heap, &r);          printf("%d ", r);     }      h_free(my_heap);      return 0; }