OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [language/] [cxx/] [ustl/] [current/] [tests/] [bvt24.cpp] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
// This file is part of the uSTL library, an STL implementation.
2
//
3
// Copyright (c) 2005-2009 by Mike Sharov <msharov@users.sourceforge.net>
4
// This file is free software, distributed under the MIT License.
5
 
6
#include "stdtest.h"
7
 
8
static void HeapSize (size_t nElements, size_t& layerWidth, size_t& nLayers)
9
{
10
    layerWidth = 0;
11
    nLayers = 0;
12
    for (size_t fts = 0; nElements > fts; fts += layerWidth) {
13
        layerWidth *= 2;
14
        if (!layerWidth)
15
            ++ layerWidth;
16
        ++ nLayers;
17
    }
18
}
19
 
20
static void PrintSpace (size_t n)
21
{
22
    for (uoff_t s = 0; s < n; ++ s)
23
        cout << ' ';
24
}
25
 
26
static void PrintHeap (const vector<int>& v)
27
{
28
    size_t maxWidth, nLayers;
29
    HeapSize (v.size(), maxWidth, nLayers);
30
    vector<int>::const_iterator src (v.begin());
31
    cout << ios::width(3);
32
    maxWidth *= 3;
33
    for (uoff_t i = 0; i < nLayers; ++ i) {
34
        const size_t w = 1 << i;
35
        const size_t spacing = max (0, int(maxWidth / w) - 3);
36
        PrintSpace (spacing / 2);
37
        for (uoff_t j = 0; j < w && src != v.end(); ++ j) {
38
            cout << *src++;
39
            if (j < w - 1 && src != v.end() - 1)
40
                PrintSpace (spacing);
41
        }
42
        cout << endl;
43
    }
44
}
45
 
46
void TestHeapOperations (void)
47
{
48
    static const int c_Values [31] = {  // 31 values make a full 4-layer tree
49
        93, 92, 90, 86, 83, 86, 77, 40, 72, 36, 68, 82, 62, 67, 63, 15,
50
        26, 26, 49, 21, 11, 62, 67, 27, 29, 30, 35, 23, 59, 35, 29
51
    };
52
    vector<int> v;
53
    v.reserve (VectorSize(c_Values));
54
    for (uoff_t i = 0; i < VectorSize(c_Values); ++ i) {
55
        v.push_back (c_Values[i]);
56
        push_heap (v.begin(), v.end());
57
        cout << "------------------------------------------------\n";
58
        if (!is_heap (v.begin(), v.end()))
59
            cout << "Is NOT a heap\n";
60
        PrintHeap (v);
61
    }
62
    cout << "------------------------------------------------\n";
63
    cout << "make_heap on the full range:\n";
64
    v.resize (VectorSize (c_Values));
65
    copy (VectorRange(c_Values), v.begin());
66
    make_heap (v.begin(), v.end());
67
    PrintHeap (v);
68
    if (!is_heap (v.begin(), v.end()))
69
        cout << "Is NOT a heap\n";
70
    cout << "------------------------------------------------\n";
71
    cout << "pop_heap:\n";
72
    pop_heap (v.begin(), v.end());
73
    v.pop_back();
74
    PrintHeap (v);
75
    if (!is_heap (v.begin(), v.end()))
76
        cout << "Is NOT a heap\n";
77
 
78
    cout << "------------------------------------------------\n";
79
    cout << "sort_heap:\n";
80
    v.resize (VectorSize (c_Values));
81
    copy (VectorRange(c_Values), v.begin());
82
    make_heap (v.begin(), v.end());
83
    sort_heap (v.begin(), v.end());
84
    foreach (vector<int>::const_iterator, i, v)
85
        cout << *i;
86
    cout << endl;
87
 
88
    cout << "------------------------------------------------\n";
89
    cout << "priority_queue push and pop:\n";
90
    priority_queue<int> q;
91
    for (uoff_t i = 0; i < VectorSize(c_Values); ++ i)
92
        q.push (c_Values[i]);
93
    while (!q.empty()) {
94
        cout << q.top();
95
        q.pop();
96
    }
97
    cout << endl;
98
}
99
 
100
StdBvtMain (TestHeapOperations)

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.