## 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; }

## Algorithm for answering queries of the type “largest interval contained in the given interval”

I have been wondering over the following problem:

Given a set $$S$$ of intervals on the number line. We can do two operations on them:

1. Add a new interval $$[l,r]$$ to $$S$$
2. Given an interval $$[ql, qr]$$, which is possibly not in $$S$$, find the longest interval from $$S$$ which is contained entirely in $$[ql, qr]$$

If I didn’t have operation $$1$$ I have found a solution using a persistent segment tree.

For the full version, there is the trivial solution which runs in $$O(QN)$$. I also found that using interval tree, I can directly find which intervals intersect the query interval and then I can check them manually. However, is there an even better solution? Maybe $$O(NlogN)$$ or $$O(Nlog^2N)$$

## Copy data from one CSV File to another, in successive interval based on number of rows using batch script

I have requirement where I am having one CSV file say FILE1 which is getting generated dynamically daily with n number of rows. I have another CSV with n number of rows FILE2(with one time data). My requirement is to copy data from FILE2 to FILE1 but number of rows after merging should not exceed 500k. And remaining data should get appended on next day File1.

For example- Day1: File1 is generated with 100k rows and File2 is kept in repository having total of 700k rows then: Pick only 400K records from File2 and append in File1. So that total should not exceed 500k

Day 2: File1 is generated with 50k rows then append the remaining rows from File2.

Similarly File1 rows can vary and if after merging it exceeds 500k records then rest data from FILE2 should be picked up in next day batch.

I am new to batch script and have somehow manage to merge 2 Files but I am not able to get the row count and merge partially as per requiremnet any help is appreciated.

## Finding the max XOR of two numbers in an interval: can we do better than quadratic?

Suppose we’re given two numbers $l$ and $r$ and that we want to find $\max{(i\oplus j)}$ for $l\le i,\,j\le r$ .

The naïve algorithm simply checks all possible pairs; for instance in ruby we’d have:

def max_xor(l, r)   max = 0    (l..r).each do |i|     (i..r).each do |j|       if (i ^ j > max)         max = i ^ j       end     end   end    max end

I sense that we can do better than quadratic. Is there a better algorithm for this problem?

## How to invert Fisher’s exact test to construct 95% confidence interval

I would appreciate any help to invert Fisher’s exact test to construct 95% confidence interval for difference in means.

# sample data

data<-structure(list(X1 = c(17L, 33L, 49L, 65L, 81L, 97L), pupid = 1080001:1080006,      visit = c("Visit 1, 1998", "Visit 1, 1998", "Visit 1, 1998",      "Visit 1, 1998", "Visit 1, 1998", "Visit 1, 1998"), schid = c(108L,      108L, 108L, 108L, 108L, 108L), totpar98 = c(0.666666686534882,      0.666666686534882, 0.333333343267441, 0, 0, 0), wgrp = c(3L,      3L, 3L, 3L, 3L, 3L), attendance.count = c(16L, 16L, 16L,      16L, 16L, 16L), missing.totpar98 = c(0, 0, 0, 0, 0, 0), attendance.rate = c(0,      0, 0, 0, 0, 0), treatment.indicator = c(0, 0, 0, 0, 0, 0)), row.names = c(NA,  -6L), groups = structure(list(pupid = 1080001:1080006, .rows = list(     1L, 2L, 3L, 4L, 5L, 6L)), row.names = c(NA, -6L), class = c("tbl_df",  "tbl", "data.frame"), .drop = FALSE), class = c("grouped_df",  "tbl_df", "tbl", "data.frame"))

# my attempt

fisher_CI<-function(Y, treat, n_sim, taus){   pval<-rep(NA, length(taus))   for (i in 1:length(taus)){     #create a potential outcome table     Y01<-cbind(Y, Y)     Y01[treat==1,1]<-Y[treat==1] - taus[i]     Y01[treat==0,2]<-Y[treat==0] + taus[i]     #conduct a test as before     t_obs<-abs(mean(Y01[treat==1,2]) - mean(Y01[treat==0,1]))     t_sim<-rep(NA, n_sim)     for (b in 1:n_sim){       treat_sim<-sample(treat, size = length(treat))       t_sim[b]<-abs(mean(Y01[treat_sim==1,2]) - mean(Y01[treat_sim==0,2]))     }      #obtain p-value     pval[i]<-mean(t_sim>t_obs)   }   return(pval)}  #pick a candidate range: tau =[-1, 20] #run over the grid taus<-seq(-1, 20, by =0.5) fr_ci<-fisher_CI(DT$totpar98, DT$  treatment.indicator, 500, taus) #select the region alpha<-0.05; accept<-fr_ci>=alpha/2 & fr_ci<=(1 -alpha/2) CI<-taus[accept] cat("lb =", min(CI), "ub =", max(CI))  #plot plot(xlab = "tau", ylab = "pval(tau)", fr_ci, pch = 21, bg = ifelse(accept==1, 'black', 'gray')) abline(h=1-alpha/2, col = 'red') abline(h=alpha/2, col = 'red')

Thanks in advance. Please, let me know if something is not clear in my code.

## Calculating the confidence interval for a CD

I am performing a few simulations and I want to check if the results I obtain from them follow my analytical solution. My analytical solution predicts the CDF. Hence I want to calculate the confidence interval based on the cumulative distributions of the results obtained from the simulation. Hence I was wondering if I can use the Confidence interval based on my simulation results to calculate the CI for the CDF.

## What should be the interval for warmup script scheduled?

There are lots of Sharepoint wake up/warm up script available. But what should be the scheduled interval? I think it is based on particular environment. It is my understanding:

1. Set IISReset at 4am and run warmup 2 minutes after. (ref: http://weblogs.asp.net/owscott/archive/2013/04/06/why-is-the-iis-default-app-pool-recycle-set-to-1740-minutes.aspx)

2. If number of visit is very low (close to zero), I will schedule to run every 15 minutes because application pool idle timeout is 20 min (MS default).

3. Some people said Sharepoint crawler will do the same thing as warmup script. However I doubt “incremental crawl” will visit every site collection. Also it may spend for hours to complete.

Am I understand the IIS + Sharepoint behaviour correctly?

Mark

## Rate limiting variable duration operations over a time interval

I want a rate-limiter that permits, at most, N operations over T seconds. I found a sample implementation here but it makes an assumption that operations complete in a predictable, consistent amount of time. I don’t want to make that assumption.

My attempt to solve this simplifies the approach in the linked article by requiring the caller to signal when their operation completes. A timer fires every T seconds, releasing a SlimSemaphore the number of completed operations since the timer previously fired.

I’m looking for feedback on potential bugs, areas for improvement, and ways to make this more efficient.