<commit_before>#ifndef ARRAYLIST_H
#define ARRAYLIST_H

#include <stdlib.h>
#include <assert.h>

#include "System/Macros.h"

/*
===============================================================================================

 ArrayList implements the List interface for bounded number of items.

===============================================================================================
*/

template<typename T, unsigned size>
class ArrayList
{
public:
    ArrayList();
    
    T&          Get(unsigned i);

    bool        Append(T& t);
    bool        Prepend(T& t);
    
    bool        Remove(T& t);
    T*          Remove(T* t);
    void        Clear();
    
    unsigned    GetLength();
    unsigned    GetSize();
    
    T*          First();
    T*          Last();
    
    T*          Next(T* t);
    T*          Prev(T* t);

    bool        Contains(T& t);

private:
    T           buffer[size];
    unsigned    num;
};

/*
===============================================================================
*/

template<typename T, unsigned size>
ArrayList<T, size>::ArrayList()
{
    num = 0;
}

template<typename T, unsigned size>
T& ArrayList<T, size>::Get(unsigned i)
{
    assert(i <= size);

    return buffer[i];
}

template<typename T, unsigned size>
bool ArrayList<T, size>::Append(T& t)
{
    if (num == size)
        return false;
    
    Get(num) = t;
    num++;
    
    return true;
}

template<typename T, unsigned size>
bool ArrayList<T, size>::Prepend(T& t)
{
    if (num == size)
        return false;

    // shift all elems
    memmove(buffer + 1, buffer, num * sizeof(T));
    
    Get(0) = t;
    num++;
    
    return true;
}

template<typename T, unsigned size>
unsigned ArrayList<T, size>::GetLength()
{
    return num;
}

template<typename T, unsigned size>
unsigned ArrayList<T, size>::GetSize()
{
    return size;
}

template<typename T, unsigned size>
bool ArrayList<T, size>::Remove(T& t)
{
    T*  it;

    for (it = First(); it != NULL; it = Next(it))
    {
        if (*it == t)
        {
            Remove(it);
            return true;
        }
    }
    
    return false;
}

template<typename T, unsigned size>
T* ArrayList<T, size>::Remove(T* t)
{
    unsigned    pos;
    
    assert(t >= buffer && t < buffer + num);
    
    pos = (unsigned) (t - buffer);
    if (pos == num - 1)
    {
        // last elem
        num--;
        return NULL;
    }
    
    memmove(buffer + pos, buffer + pos + 1, (num - pos - 1) * sizeof(T));
    num--;

    return &Get(pos);
}

template<typename T, unsigned size>
void ArrayList<T, size>::Clear()
{
    num = 0;
}

template<typename T, unsigned size>
T* ArrayList<T, size>::First()
{
    if (num == 0)
        return NULL;
    
    return &buffer[0];
}

template<typename T, unsigned size>
T* ArrayList<T, size>::Last()
{
    if (num == 0)
        return NULL;
    
    return &buffer[num - 1];
}

template<typename T, unsigned size>
T* ArrayList<T, size>::Next(T* t)
{
    unsigned    pos;
    
    assert(t >= buffer && t < buffer + num);
    
    pos = (unsigned) (t - buffer);
    if (pos == num - 1)
    {
        // last elem
        return NULL;
    }
    
    return &Get(pos + 1);
}

template<typename T, unsigned size>
T* ArrayList<T, size>::Prev(T* t)
{
    unsigned    pos;
    
    assert(t >= buffer && t < buffer + num);
    
    pos = t - buffer;
    if (pos == 0)
    {
        // first elem
        return NULL;
    }
    
    return &Get(pos - 1);
}

template<typename T, unsigned size>
bool ArrayList<T, size>::Contains(T& t)
{
    T* it;
    <commit_msg>Changed assert to ASSERT in ArrayList.<commit_after>#ifndef ARRAYLIST_H
#define ARRAYLIST_H

#include <stdlib.h>

#include "System/Macros.h"

/*
===============================================================================================

 ArrayList implements the List interface for bounded number of items.

===============================================================================================
*/

template<typename T, unsigned size>
class ArrayList
{
public:
    ArrayList();
    
    T&          Get(unsigned i);

    bool        Append(T& t);
    bool        Prepend(T& t);
    
    bool        Remove(T& t);
    T*          Remove(T* t);
    void        Clear();
    
    unsigned    GetLength();
    unsigned    GetSize();
    
    T*          First();
    T*          Last();
    
    T*          Next(T* t);
    T*          Prev(T* t);

    bool        Contains(T& t);

private:
    T           buffer[size];
    unsigned    num;
};

/*
===============================================================================
*/

template<typename T, unsigned size>
ArrayList<T, size>::ArrayList()
{
    num = 0;
}

template<typename T, unsigned size>
T& ArrayList<T, size>::Get(unsigned i)
{
    ASSERT(i <= size);

    return buffer[i];
}

template<typename T, unsigned size>
bool ArrayList<T, size>::Append(T& t)
{
    if (num == size)
        return false;
    
    Get(num) = t;
    num++;
    
    return true;
}

template<typename T, unsigned size>
bool ArrayList<T, size>::Prepend(T& t)
{
    if (num == size)
        return false;

    // shift all elems
    memmove(buffer + 1, buffer, num * sizeof(T));
    
    Get(0) = t;
    num++;
    
    return true;
}

template<typename T, unsigned size>
unsigned ArrayList<T, size>::GetLength()
{
    return num;
}

template<typename T, unsigned size>
unsigned ArrayList<T, size>::GetSize()
{
    return size;
}

template<typename T, unsigned size>
bool ArrayList<T, size>::Remove(T& t)
{
    T*  it;

    for (it = First(); it != NULL; it = Next(it))
    {
        if (*it == t)
        {
            Remove(it);
            return true;
        }
    }
    
    return false;
}

template<typename T, unsigned size>
T* ArrayList<T, size>::Remove(T* t)
{
    unsigned    pos;
    
    ASSERT(t >= buffer && t < buffer + num);
    
    pos = (unsigned) (t - buffer);
    if (pos == num - 1)
    {
        // last elem
        num--;
        return NULL;
    }
    
    memmove(buffer + pos, buffer + pos + 1, (num - pos - 1) * sizeof(T));
    num--;

    return &Get(pos);
}

template<typename T, unsigned size>
void ArrayList<T, size>::Clear()
{
    num = 0;
}

template<typename T, unsigned size>
T* ArrayList<T, size>::First()
{
    if (num == 0)
        return NULL;
    
    return &buffer[0];
}

template<typename T, unsigned size>
T* ArrayList<T, size>::Last()
{
    if (num == 0)
        return NULL;
    
    return &buffer[num - 1];
}

template<typename T, unsigned size>
T* ArrayList<T, size>::Next(T* t)
{
    unsigned    pos;
    
    ASSERT(t >= buffer && t < buffer + num);
    
    pos = (unsigned) (t - buffer);
    if (pos == num - 1)
    {
        // last elem
        return NULL;
    }
    
    return &Get(pos + 1);
}

template<typename T, unsigned size>
T* ArrayList<T, size>::Prev(T* t)
{
    unsigned    pos;
    
    ASSERT(t >= buffer && t < buffer + num);
    
    pos = t - buffer;
    if (pos == 0)
    {
        // first elem
        return NULL;
    }
    
    return &Get(pos - 1);
}

template<typename T, unsigned size>
bool ArrayList<T, size>::Contains(T& t)
{
    T* it;
    
{'fraction_non_alphanumeric': 0.21057786483839372, 'fraction_numerical': 0.0041975654120610046, 'mean_word_length': 2.2461398728428703, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 70, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4533600', 'n_tokens_mistral': 2324, 'n_tokens_neox': 2253, 'n_words': 643}