/* Autogenerated with DRAKON Editor 1.27 */
#include "StringList.h"

// CPP-file header. Your code goes here.

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

static int Compare(
    StringList* collection,
    int leftIndex,
    int rightIndex,
    IStringComparer* comparer
);







static int Compare(
    StringList* collection,
    int leftIndex,
    int rightIndex,
    IStringComparer* comparer
) {
    // item 807
    const std::string* left = collection->Get(leftIndex);
    const std::string* right = collection->Get(rightIndex);
    // item 808
    return comparer->Compare(left, right);
    
}

void Fibonacci(
    int n,
    std::vector<int>* output
) {
    // item 470
    int i;
    
    // item 783
    if (n >= 0) {
    } else {
        // item 784
        printf("Fibonacci: n < 0: %d\n", n);
        abort();
    }
    
    // item 787
    if (output == 0) {
        // item 782
        printf("Fibonacci: output is null.\n");
        abort();
    } else {
    }
    
    // item 791
    output->clear();
    
    // item 4530001
    if (n == 0) {
        // item 457
        output->push_back(0);
        goto exit_door;
    } else {
    }
    
    // item 4530002
    if (n == 1) {
        // item 471
        output->push_back(0);
        output->push_back(1);
        goto exit_door;
    } else {
        // item 790
        output->push_back(0);
        output->push_back(1);
        // item 4630001
        i = 2;
    }
    
    item_4630002 :
    if (i <= n) {
        // item 464
        int f2 = output->at(i - 2);
        int f1 = output->at(i - 1);
        int fib = f1 + f2;
        // item 465
        output->push_back(fib);
        // item 4630003
        i += 1;
        goto item_4630002;
    } else {
    }
    
    exit_door:;
}

void QuickSort(
    StringList* collection,
    int begin,
    int end,
    IStringComparer* comparer
) {
    // item 633
    int i;
    int pivotIndex;
    int storeIndex;
    // item 592
    int length = end - begin;
    int last = end - 1;
    
    // item 5930001
    if ((length == 0) || (length == 1)) {
        goto exit_door;
    } else {
    }
    
    // item 5930003
    if (length == 2) {
    } else {
        // item 614
        pivotIndex = begin + length / 2;
        // item 650
        collection->Swap(pivotIndex, last);
        storeIndex = begin;
        // item 6150001
        i = begin;
        goto item_6150002;
    }
    
    // item 610
    if (Compare(collection, begin, last, comparer) <= 0) {
        goto exit_door;
    } else {
        // item 611
        collection->Swap(begin, last);
        goto exit_door;
    }
    
    item_6150002 :
    if (i < last) {
    } else {
        // item 655
        collection->Swap(storeIndex, last);
        // item 656
        QuickSort(collection, begin, storeIndex, comparer);
        // item 657
        QuickSort(collection, storeIndex + 1, end, comparer);
        goto exit_door;
    }
    
    // item 651
    if (Compare(collection, i, last, comparer) < 0) {
        // item 652
        collection->Swap(i, storeIndex);
        storeIndex++;
    } else {
    }
    
    // item 6150003
    i++;
    goto item_6150002;
    
    exit_door:;
}

void UnexpectedBranch(
    int switchValue
) {
    // item 710
    printf("An unexpected value in the 'select' ('switch') construct: %d\n", switchValue);
    abort();
    
}

 StringList::StringList() {
}

 StringList::~StringList() {
    // item 729
    List::iterator it = _elements.begin();
    
    item_730 :
    if (it == _elements.end()) {
        // item 734
        _elements.resize(0);
    } else {
        // item 732
        delete *it;
        // item 735
        ++it;
        goto item_730;
    }
    
}

void StringList::Add(
    std::string* element
) {
    // item 741
    if (element == 0) {
        // item 742
        printf("StringList::Add: element is null\n");
        abort();
    } else {
    }
    
    // item 745
    _elements.push_back(element);
    
}

void StringList::AddFromCString(
    const char* text
) {
    // item 797
    if (text == 0) {
        // item 798
        printf("StringList::AddFromCString: text is null\n");
        abort();
    } else {
    }
    
    // item 801
    std::string* element = new std::string(text);
    Add(element);
    
}

int StringList::Count() const {
    // item 781
    return (int)_elements.size();
    
}

std::string* StringList::Get(
    int index
) {
    // item 751
    if ((index >= 0) && (index < Count())) {
    } else {
        // item 752
        printf("StringList::Get: index out of range: %d\n", index);
        abort();
    }
    
    // item 755
    return _elements[index];
    
}

void StringList::Swap(
    int index1,
    int index2
) {
    // item 762
    if ((index1 >= 0) && (index1 < Count())) {
    } else {
        // item 763
        printf("StringList::Swap: index1 out of range: %d\n", index1);
        abort();
    }
    
    // item 768
    if ((index2 >= 0) && (index2 < Count())) {
    } else {
        // item 769
        printf("StringList::Swap: index2 out of range: %d\n", index2);
        abort();
    }
    
    // item 773
    if (index1 == index2) {
    } else {
        // item 766
        std::string* old1 = _elements[index1];
        _elements[index1] = _elements[index2];
        _elements[index2] = old1;
    }
    
}

// CPP-file footer. Your code goes here.

