2016-06-22 01:09:48 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2015, Emir Pasic
|
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above copyright notice, this
|
|
|
|
list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package containers
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// IteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
|
2016-06-22 17:59:08 +00:00
|
|
|
type IteratorWithIndex interface {
|
2016-06-24 19:52:16 +00:00
|
|
|
// Next moves the iterator to the next element and returns true if there was a next element in the container.
|
2016-06-23 22:08:04 +00:00
|
|
|
// If Next() returns true, then next element's index and value can be retrieved by Index() and Value().
|
2016-06-26 18:50:49 +00:00
|
|
|
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
|
2016-06-22 17:59:08 +00:00
|
|
|
// Modifies the state of the iterator.
|
2016-06-22 01:09:48 +00:00
|
|
|
Next() bool
|
2016-06-24 19:52:16 +00:00
|
|
|
// Value returns the current element's value.
|
2016-06-22 17:59:08 +00:00
|
|
|
// Does not modify the state of the iterator.
|
2016-06-22 01:09:48 +00:00
|
|
|
Value() interface{}
|
2016-06-24 19:52:16 +00:00
|
|
|
// Index returns the current element's index.
|
2016-06-22 17:59:08 +00:00
|
|
|
// Does not modify the state of the iterator.
|
|
|
|
Index() int
|
2016-06-26 18:50:49 +00:00
|
|
|
// Reset sets the iterator to the initial state.
|
|
|
|
// Call Next() to fetch the first element if any.
|
|
|
|
Reset()
|
2016-06-22 17:59:08 +00:00
|
|
|
}
|
|
|
|
|
2016-06-24 19:52:16 +00:00
|
|
|
// IteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
|
2016-06-22 17:59:08 +00:00
|
|
|
type IteratorWithKey interface {
|
2016-06-24 19:52:16 +00:00
|
|
|
// Next moves the iterator to the next element and returns true if there was a next element in the container.
|
2016-06-23 22:08:04 +00:00
|
|
|
// If Next() returns true, then next element's key and value can be retrieved by Key() and Value().
|
2016-06-26 18:50:49 +00:00
|
|
|
// If Next() was called for the first time, then it will point the iterator to the first element if it exists.
|
2016-06-22 17:59:08 +00:00
|
|
|
// Modifies the state of the iterator.
|
|
|
|
Next() bool
|
2016-06-24 19:52:16 +00:00
|
|
|
// Value returns the current element's value.
|
2016-06-22 17:59:08 +00:00
|
|
|
// Does not modify the state of the iterator.
|
|
|
|
Value() interface{}
|
2016-06-24 19:52:16 +00:00
|
|
|
// Key returns the current element's key.
|
2016-06-22 17:59:08 +00:00
|
|
|
// Does not modify the state of the iterator.
|
|
|
|
Key() interface{}
|
2016-06-26 18:50:49 +00:00
|
|
|
// Reset sets the iterator to the initial state.
|
|
|
|
// Call Next() to fetch the first element if any.
|
|
|
|
Reset()
|
2016-06-22 01:09:48 +00:00
|
|
|
}
|
2016-06-25 16:17:48 +00:00
|
|
|
|
|
|
|
// ReverseIteratorWithIndex is stateful iterator for ordered containers whose values can be fetched by an index.
|
|
|
|
//
|
2016-06-26 20:27:08 +00:00
|
|
|
// Essentially it is the same as IteratorWithIndex, but provides additional:
|
|
|
|
//
|
|
|
|
// Prev() function to enable traversal in reverse
|
|
|
|
//
|
|
|
|
// Last() function to move the iterator to the last element.
|
2016-06-25 16:17:48 +00:00
|
|
|
type ReverseIteratorWithIndex interface {
|
|
|
|
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
|
|
|
|
// If Prev() returns true, then previous element's index and value can be retrieved by Index() and Value().
|
|
|
|
// Modifies the state of the iterator.
|
|
|
|
Prev() bool
|
2016-06-26 20:27:08 +00:00
|
|
|
// Last moves the iterator to the last element and returns true if there was a last element in the container.
|
|
|
|
// If Last() returns true, then last element's index and value can be retrieved by Index() and Value().
|
|
|
|
// Modifies the state of the iterator.
|
|
|
|
Last() bool
|
2016-06-25 16:17:48 +00:00
|
|
|
|
|
|
|
IteratorWithIndex
|
|
|
|
// Next() bool
|
|
|
|
// Value() interface{}
|
|
|
|
// Index() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReverseIteratorWithKey is a stateful iterator for ordered containers whose elements are key value pairs.
|
|
|
|
//
|
2016-06-26 20:27:08 +00:00
|
|
|
// Essentially it is the same as IteratorWithKey, but provides additional:
|
|
|
|
//
|
|
|
|
// Prev() function to enable traversal in reverse
|
|
|
|
//
|
|
|
|
// Last() function to move the iterator to the last element.
|
2016-06-25 16:17:48 +00:00
|
|
|
type ReverseIteratorWithKey interface {
|
|
|
|
// Prev moves the iterator to the previous element and returns true if there was a previous element in the container.
|
|
|
|
// If Prev() returns true, then previous element's index and value can be retrieved by Key() and Value().
|
|
|
|
// Modifies the state of the iterator.
|
|
|
|
Prev() bool
|
2016-06-26 20:27:08 +00:00
|
|
|
// Last moves the iterator to the last element and returns true if there was a last element in the container.
|
|
|
|
// If Last() returns true, then last element's index and value can be retrieved by Key() and Value().
|
|
|
|
// Modifies the state of the iterator.
|
|
|
|
Last() bool
|
2016-06-25 16:17:48 +00:00
|
|
|
|
|
|
|
IteratorWithKey
|
|
|
|
// Next() bool
|
|
|
|
// Value() interface{}
|
|
|
|
// Key() interface{}
|
|
|
|
}
|