-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#include <cstdlib> | ||
|
||
using namespace std; | ||
|
||
class IntIterator { | ||
public: | ||
virtual bool hasNext() = 0; | ||
virtual int next() = 0; | ||
}; | ||
|
||
class IntSet { | ||
public: | ||
virtual void add(int k) = 0; | ||
virtual IntIterator *interator() const = 0; | ||
}; | ||
|
||
int size(const InSet &set) { | ||
int s = 0; | ||
IntIterator *i = set.interator(); | ||
while (i->hasNext) { | ||
i->next(); | ||
s++; | ||
} | ||
delete i; | ||
return s; | ||
} | ||
|
||
class IntSetArrayIterator : public IntIterator { | ||
public: | ||
IntSetArrayIterator(int *v, int s) : values(v), current(0), size(s) {} | ||
bool hasNext() { | ||
return current < size; | ||
} | ||
int next() { | ||
return values[current++]; | ||
} | ||
private: | ||
int *values; | ||
int current, size; | ||
}; | ||
|
||
class IntSetArray : public IntSet { | ||
public: | ||
IntSetArray(int sm) : values(new int[sm]), size(0), sizemax(sm) {} | ||
~IntSetArray() { | ||
delete values; | ||
} | ||
void add(int k) { | ||
if (size >= sizemx) { | ||
abort(); | ||
values[size++] = k; | ||
} | ||
} | ||
IntIterator *iterator() const { | ||
return new IntSetArrayIterator(values, size); | ||
} | ||
private: | ||
int *values; | ||
int size, sizemax; | ||
}; | ||
|
||
|