- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
template<typename T>
class Enumerable
{
public:
Enumerable() : enumerableInProcess(false) { };
virtual void begin() = 0;
virtual void end() = 0;
virtual bool enumeration(T* item) = 0;
protected:
bool enumerableInProcess;
};
template<typename T>
class List : Enumerable<T*>
{
public:
class ListItem
{
public:
friend class List;
T item;
ListItem(T item) : item(item), next(nullptr), previous(nullptr)
{
}
private:
class ListItem* next;
class ListItem* previous;
};
/* ... */
void begin()
{
if(enumerableInProcess) {
throw Exception("Error Enumerable!");
}
enumerableInProcess = true;
enumerationItem = first;
}
bool enumeration(T** item)
{
if(enumerableInProcess) {
if(enumerationItem != nullptr) {
(*item) = &(enumerationItem->item);
enumerationItem = enumerationItem->next;
return (true);
} else {
(*item) = nullptr;
return (false);
}
} else {
throw Exception("Error Enumerable!");
}
}
bool enumeration(ListItem **listItem)
{
if(enumerableInProcess) {
if(enumerationItem != nullptr) {
(*listItem) = enumerationItem;
enumerationItem = enumerationItem->next;
return (true);
} else {
(*listItem) = nullptr;
return (false);
}
} else {
throw Exception("Error Enumerable!");
}
}
void end()
{
if(!enumerableInProcess) {
throw Exception("Error Enumerable!");
}
enumerableInProcess = false;
}
private:
const int size;
int count;
ListItem *first;
ListItem *last;
ListItem *enumerationItem;
};
void list_t_1()
{
List<int> list(8);
List<int>::ListItem *item;
list.add(1);
list.add(2);
list.add(4);
list.add(8);
list.add(16);
list.begin();
while(list.enumeration(&item))
{
printf("%i\n", (item->item));
}
list.end();
}
гуро
> джавы
Скорее шарпика.
А еще интригует класс Exception; его существование как бы намекает на то, что чувак хочет построить свой STL с блекджеком и шлюхами.
>return (count == size);
>count += (1);
>count -= (1);
>return (count);
>return (size);
Зачем?
http://liveworkspace.org/code/2oXK1R$0