Skip to content

Commit

Permalink
Add slice DeQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen Quan committed Feb 23, 2021
1 parent bff2a19 commit 1163820
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backend/collection/deque.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package collection

// DeQue 双端队列
type DeQue interface {
// DeQueue 双端队列
type DeQueue interface {
// 实现队列接口
Queue
AddFirst(e Element) error
Expand Down
1 change: 1 addition & 0 deletions errs/err.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var (
IndexOutOfBound = errors.New("index out of bound")
NoSuchElement = errors.New("no such element")
IllegalState = errors.New("illegal state")
NilPointer = errors.New("nil pointer")
)

// indexOutOfBoundsException 实现 errs 接口
Expand Down
204 changes: 204 additions & 0 deletions queue/slice_dequeue.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
* Copyright 2021 Chen Quan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package queue

import (
"fmt"
"github.com/chenquan/go-util/backend/collection"
"github.com/chenquan/go-util/errs"
)

type SliceDeQueue struct {
elements []collection.Element
head int
tail int
}

func (s *SliceDeQueue) Size() int {
panic("")
}

func (s *SliceDeQueue) IsEmpty() bool {
return s.Size() == 0
}

func (s *SliceDeQueue) Contains(e collection.Element) (bool, error) {
panic("")
}

func (s *SliceDeQueue) Add(e collection.Element) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) Remove(e collection.Element) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) ContainsAll(collection collection.Collection) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) AddAll(collection collection.Collection) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) RemoveAll(collection collection.Collection) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) RetainAll(collection collection.Collection) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) Clear() error {
panic("implement me")
}

func (s *SliceDeQueue) Equals(collection collection.Collection) bool {
panic("implement me")
}

func (s *SliceDeQueue) Slice() []collection.Element {
panic("implement me")
}

func (s *SliceDeQueue) Iterator() collection.Iterator {
panic("implement me")
}

func (s *SliceDeQueue) Offer(e collection.Element) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) Poll() collection.Element {
panic("implement me")
}

func (s *SliceDeQueue) Delete() (collection.Element, error) {
panic("implement me")
}

func (s *SliceDeQueue) Element() (collection.Element, error) {
return s.GetFirst()
}

func (s *SliceDeQueue) Peek() collection.Element {
return s.elements[s.head]
}

func (s *SliceDeQueue) AddFirst(e collection.Element) error {
if e == nil {
return errs.NilPointer
}
s.head = (s.head - 1) & (len(s.elements) - 1)
s.elements[s.head] = e
if s.head == s.tail {
if err := s.doubleCapacity(); err != nil {
return err
}
}
return nil
}

func (s *SliceDeQueue) AddLast(e collection.Element) error {
if e == nil {
return errs.NilPointer
}
s.elements[s.tail] = e
s.tail = (s.tail + 1) & (len(s.elements) - 1)
if s.tail == s.head {
if err := s.doubleCapacity(); err != nil {
return err
}
}
return nil
}

func (s *SliceDeQueue) RemoveFirst() (collection.Element, error) {
element := s.elements[s.head]
if element == nil {
return nil, errs.NoSuchElement
}
s.elements[s.head] = nil
s.head = (s.head + 1) & (len(s.elements) - 1)
return element, nil
}

func (s *SliceDeQueue) RemoveLast() (collection.Element, error) {
t := (s.tail - 1) & (len(s.elements) - 1)
element := s.elements[t]
if element == nil {
return nil, errs.NoSuchElement
}
s.elements[t] = nil
s.tail = t
return element, nil
}

func (s *SliceDeQueue) GetFirst() (collection.Element, error) {
element := s.elements[s.head]
if element == nil {
return nil, errs.NoSuchElement
}
return element, nil
}

func (s *SliceDeQueue) GetLast() (collection.Element, error) {
element := s.elements[(s.tail-1)&(len(s.elements)-1)]
if element == nil {
return nil, errs.NoSuchElement
}
return element, nil
}

func (s *SliceDeQueue) RemoveFirstOccurrence(e collection.Element) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) RemoveLastOccurrence(e collection.Element) (bool, error) {
panic("implement me")
}

func (s *SliceDeQueue) Push(e collection.Element) error {
return s.AddFirst(e)
}

func (s *SliceDeQueue) Pop() (collection.Element, error) {
return s.RemoveFirst()
}

func (s *SliceDeQueue) DescendingIterator() collection.Iterator {
panic("implement me")
}

func (s *SliceDeQueue) doubleCapacity() error {
n := len(s.elements)
h := s.head
r := n - h
newSize := n << 1
if newSize < 0 {
return fmt.Errorf("deque too big")
}
elements := make([]collection.Element, newSize)
copy(elements[0:r], s.elements[h:n])
copy(elements[r:r+h], s.elements[0:h])
s.elements = elements
s.head = 0
s.tail = n
return nil
}
32 changes: 32 additions & 0 deletions queue/slice_dequeue_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 2021 Chen Quan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package queue

import (
"fmt"
"testing"
)

func Test1(t *testing.T) {
s := make([]int, 1, 2)
fmt.Println(s)
s[0] = 1
//s[1]=3
copy(s, []int{33, 3})
fmt.Println(s)
}

0 comments on commit 1163820

Please sign in to comment.