PageRenderTime 12ms CodeModel.GetById 1ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/gollection/gollection.go

https://code.google.com/
Go | 178 lines | 40 code | 14 blank | 124 comment | 0 complexity | 026b753f87fa9016555babe6f9780468 MD5 | raw file
  1/*
  2* 
  3* Copyright Ioan - Ciprian Tandau
  4* All rights reserved.
  5* This code is governed by GPL3 which can be found in the
  6* root of the project.
  7*
  8*/
  9
 10
 11package gollection
 12
 13/*
 14* This is an equality function prototype; a predicate that
 15* returns true if its arguments are equal and false
 16* otherwise. The concrete function implementation
 17* defines what equal means.
 18*/
 19type Equal func(o1 interface{}, o2 interface{}) bool
 20
 21/*
 22* This is a hash function prototype. It must consistently return
 23* the same value for the same object
 24*/
 25type Hash func(o1 interface{}) int
 26
 27
 28/*
 29* This is an iterator interface. Unless otherwise specified, 
 30* there is no defined behavior if the iteration goes beyond 
 31* the last element of the collection
 32*/
 33type Iterator interface {
 34	/*
 35	* Returns the next value of the iterated collection
 36	* and a validity flag. If the validity flag is false
 37	* then the iterator reached the end of the collection
 38	* and the value should not be read. The value should
 39	* only be read if the validity flag is true
 40	*/
 41	Next() (value interface{}, valid bool)
 42	/*
 43	* Removes the last element returned by the iterator
 44	* This method is allowed to do nothing but implementations
 45	* should document the behavior of the method
 46	*/
 47	Remove()
 48}
 49
 50/*
 51* Collection interface. Declares operations that add  elements
 52* to the collection, remove elements from the collection and
 53* iterates over the elements of the collection.
 54*/
 55type Collection interface {
 56	/* Add an object to the collection */
 57	Add(object interface{}) bool
 58	/* 
 59	* Add all the objects from the given collection
 60	* to this collection
 61	*/
 62	AddAll(c Collection) bool
 63	/* Clear all elements from this collection */
 64	Clear()
 65	/*
 66	* true if the object equals any of the objects
 67	* in this collection and false otherwise. The 
 68	* object equality is tested using the equal 
 69	* function.
 70	*/
 71	Contains(object interface{}, equal Equal) bool
 72	/*
 73	* Like Contains but returns true only if all
 74	* the elements of the given collection are
 75	* contained in this collection
 76	*/
 77	ContainsAll(c Collection, equal Equal) bool
 78	/*
 79	* true if this collection has no elements and
 80	* false otherwise.
 81	*/
 82	IsEmpty() bool
 83	/*
 84	* removes all the elements that equal object
 85	* from this collection
 86	*/
 87	Remove(object interface{}, equal Equal)
 88	/*
 89	* Like Remove but for each elements in the 
 90	* given collection
 91	*/
 92	RemoveAll(c Collection, equal Equal)
 93	/*
 94	* Removes all the elements from this collection
 95	* that are not equal to at least one element
 96	* in the given collection
 97	*/
 98	RetainAll(c Collection, equal Equal)
 99	/*
100	* Returns the number of elements in this collection
101	*/
102	Size() int
103	/*
104	* Returns an iterator over all the elements
105	* in this collection.
106	*/
107	Iterator() Iterator
108}
109
110/*
111* Declares the Stack interface. All the Collection interface
112* operations inherited and, in addition, there are stack specific
113* operations: peek, pop, push. 
114*/
115type Stack interface {
116	Collection
117
118	/*
119	* Return the element at the top of the stack without
120	* removing it.
121	*/
122	Peek() interface{}
123	/*
124	* Remove and return the element at the top of the stack 
125	*/
126	Pop() interface{}
127	/* Push a new element to the top of the stack */
128	Push(element interface{}) bool
129	/*
130	* Search for an element that equals the given element
131	* and return it's position in the stack. Return -1 if 
132	* the search finds no match.
133	*/
134	Search(element interface{}, equal Equal) int
135}
136
137/*
138* Queue interface. The queue has a tail where elements are pushed
139* and a head where elements are removed in a FIFO fashion.
140*/
141type Queue interface {
142	Collection
143
144	/* Adds a new element to the queue */
145	AddTail(element interface{}) bool
146	/* 
147	* Returns the oldest element of the queue (the head)
148	* without removing it.
149	*/
150	PeekHead() interface{}
151	/* Removes the head of the queue and returns it */
152	PopHead() interface{}
153}
154
155/* 
156* A double-ended queue interface. It generalizes the queue
157* interface and adds operations symmetrical operations to the 
158* other ends of the queue. The Iterator() method returns 
159* a forward iterator that iterates over the elements from 
160* the head to the tail. A reverse iterator is also provided
161* that iterates over the elements from the tail toward the head.
162*/
163type Deque interface {
164	Queue
165
166	/* Adds a new element to the head of the queue */
167	AddHead(element interface{}) bool
168	/* Returns the tail element without removing it */
169	PeekTail() interface{}
170	/* Removes and returns the tail of the queue */
171	PopTail() interface{}
172	/* 
173	* Creates and returns a reverse iterator that will
174	* iterate over the elements from the tail toward the head
175	*/
176	ReverseIterator() Iterator
177}
178