/* Autogenerated with DRAKON Editor 1.29 */
#include "cpp_demo.h"

#include <stdlib.h>
#include <iostream>
using std::cout;
using std::endl;

// Символы Юникод

static void Fill(
    StringList* output,
    const char* strings[]
);

static void IterationDemo();

static void Print(
    StringList* array
);

static void PrintHello();

static void PrintIntsArrow(
    const std::vector<int>* collection
);

static void PrintIntsBackward(
    const std::vector<int>* collection,
    int n
);

static void PrintIntsFor(
    const std::vector<int>* collection
);

static void QuickSortDemo();

static void StringsAreSorted(
    StringList* array
);







static void Fill(
    StringList* output,
    const char* strings[]
) {
    // item 557
    const char** current = strings;
    
    item_559 :
    if (*current == 0) {
    } else {
        // item 558
        output->AddFromCString(*current);
        // item 560
        current++;
        goto item_559;
    }
    
}

static void IterationDemo() {
    // item 529
    int n = 15;
    // item 504
    std::vector<int> sequence;
    // item 447
    cout << "iteration demo" << endl;
    // item 505
    Fibonacci(n, &sequence);
    // item 507
    PrintIntsArrow(&sequence);
    PrintIntsBackward(&sequence, n);
    PrintIntsFor(&sequence);
    // item 506
    cout << endl;
    
}

static void Print(
    StringList* array
) {
    // item 571
    int i;
    int length = array->Count();
    // item 5720001
    i = 0;
    
    item_5720002 :
    if (i < length) {
        // item 573
        cout << *array->Get(i) << " ";
        // item 5720003
        i++;
        goto item_5720002;
    } else {
        // item 575
        cout << endl;
    }
    
}

static void PrintHello() {
    // item 722
    cout << "DRAKON-C++ demo" << endl;
    cout << "===============" << endl;
    
}

static void PrintIntsArrow(
    const std::vector<int>* collection
) {
    // item 497
    int i, length;
    // item 484
    cout << "using if and arrow:" << endl;
    // item 479
    length = (int)collection->size();
    i = 0;
    
    item_480 :
    if (i < length) {
        // item 478
        cout << collection->at(i) << " ";
        // item 482
        i++;
        goto item_480;
    } else {
        // item 485
        cout << endl;
    }
    
}

static void PrintIntsBackward(
    const std::vector<int>* collection,
    int n
) {
    item_522 :
    if (n >= 0) {
        // item 724
        cout << collection->at(n) << " ";
        // item 524
        n--;
        goto item_522;
    } else {
        // item 528
        cout << endl;
    }
    
}

static void PrintIntsFor(
    const std::vector<int>* collection
) {
    // item 500
    int i, length;
    // item 499
    cout << "using for:" << endl;
    // item 498
    length = (int)collection->size();
    // item 4920001
    i = 0;
    
    item_4920002 :
    if (i < length) {
        // item 725
        cout << collection->at(i) << " ";
        // item 4920003
        i++;
        goto item_4920002;
    } else {
        // item 501
        cout << endl;
    }
    
}

static void QuickSortDemo() {
    // item 541
    StringList unsorted;
    StringList sorted;
    StringList reverse;
    StringList empty;
    StringList flat;
    // item 540
    const char* unsortedA[] = { "the", "sooner", "we", "start", "this", "the", "better", 0 };
    const char* sortedA[] = { "aa", "bb", "cc", "dd", "ee", "ff", 0 };
    const char* reverseA[] = { "ff", "ee", "dd", "cc", "bb", "aa", 0 };
    const char* emptyA[] = { 0 };
    const char* flatA[] = { "flat", "flat", "flat", "flat", "flat", 0};
    // item 577
    cout << "quick sort demo" << endl;
    // item 563
    Fill(&unsorted, unsortedA);
    Fill(&sorted, sortedA);
    Fill(&reverse, reverseA);
    Fill(&empty, emptyA);
    Fill(&flat, flatA);
    // item 733
    SimpleComparer comparer;
    // item 669
    QuickSort(&unsorted, 0, unsorted.Count(), &comparer);
    QuickSort(&sorted, 0, sorted.Count(), &comparer);
    QuickSort(&reverse, 0, reverse.Count(), &comparer);
    QuickSort(&empty, 0, empty.Count(), &comparer);
    QuickSort(&flat, 0, flat.Count(), &comparer);
    // item 576
    Print(&unsorted);
    Print(&sorted);
    Print(&reverse);
    Print(&empty);
    Print(&flat);
    // item 711
    StringsAreSorted(&unsorted);
    StringsAreSorted(&sorted);
    StringsAreSorted(&reverse);
    StringsAreSorted(&empty);
    StringsAreSorted(&flat);
    
}

static void StringsAreSorted(
    StringList* array
) {
    // item 692
    int i, j;
    std::string* current;
    std::string* after;
    int length = array->Count();
    // item 6830001
    i = 0;
    
    item_6830002 :
    if (i < length) {
        // item 685
        current = array->Get(i);
        // item 6860001
        j = i + 1;
    } else {
        goto exit_door;
    }
    
    item_6860002 :
    if (j < length) {
        // item 688
        after = array->Get(j);
    } else {
        // item 6830003
        i += 1;
        goto item_6830002;
    }
    
    // item 726
    if (*current > *after) {
        // item 689
        cout << "Collection is not sorted." << endl;
        abort();
    } else {
        // item 6860003
        j += 1;
        goto item_6860002;
    }
    
    exit_door:;
}

int main(
    int argc,
    char** argv
) {
    // item 734
    int result = 0;
    // item 6
    PrintHello();
    // item 509
    IterationDemo();
    QuickSortDemo();
    // item 13
    return result;
    
}

int SimpleComparer::Compare(
    const std::string* left,
    const std::string* right
) const {
    // item 732
    int result = left->compare(*right);
    return result;
    
}



