v icon indicating copy to clipboard operation
v copied to clipboard

array should has a method pop_left

Open kbkpbot opened this issue 8 months ago • 4 comments

Describe the feature

array should has a method pop_left, this is similar with pop(), but remove from the first element.

Use Case

This help building an array as FIFO.

Proposed Solution

No response

Other Information

No response

Acknowledgements

  • [ ] I may be able to implement this feature request
  • [ ] This feature might incur a breaking change

Version used

V 0.4.10 ed7a8a34f1815816984aaa1d2982d2a5fb9a97f2.52c7130

Environment details (OS name and version, etc.)

V full version V 0.4.10 ed7a8a34f1815816984aaa1d2982d2a5fb9a97f2.52c7130
OS linux, Ubuntu 24.04.2 LTS
Processor 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-9700 CPU @ 3.00GHz
Memory 6.62GB/15.51GB
V executable /media/HD/github/kbkpbot/v/v
V last modified time 2025-05-25 10:51:48
V home dir OK, value: /media/HD/github/kbkpbot/v
VMODULES OK, value: /home/mars/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /media/HD/github/kbkpbot/v/vlib/db/pool
Git version git version 2.43.0
V git status weekly.2025.17-165-g0cd14a29
.git/config present true
cc version cc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
gcc version gcc (Ubuntu 13.3.0-6ubuntu2~24.04) 13.3.0
clang version Ubuntu clang version 18.1.3 (1ubuntu1)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Ubuntu GLIBC 2.39-0ubuntu8.4) 2.39

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

kbkpbot avatar May 27 '25 15:05 kbkpbot

Connected to Huly®: V_0.6-22956

huly-for-github[bot] avatar May 27 '25 15:05 huly-for-github[bot]

We already have https://github.com/vlang/v/blob/master/vlib/datatypes/queue.v

Delta456 avatar May 27 '25 16:05 Delta456

I couldn't find a finite circular buffer in datatypes so wrote a very simpler one, with only two methods push and peek:

pub struct Logger[T] {
mut:
	content []T
	pos     int
}

pub fn new_logger[T](size int) Logger[T] {
	s := if size > 0 { size } else { 1 }
	return Logger[T] {
		content: []T{ len: s, cap: s }
	}
}

pub fn (mut cb Logger[T]) push(element T) {
	cb.content[cb.pos] = element
	next := cb.pos + 1
	cb.pos = if cb.content.len > next { next } else { 0 }
}

pub fn (cb Logger[T]) peek(size int) []T {
	// content:  empty len=5 pos=0
	// pushes:   a,b,c,d,e,f,g
	// content:  f,g,c,d,e     pos=2 
	// elements: g,f,e,d,c
	mut elements := []T{}
	mut s := 0
	pos := cb.pos
	for i := pos-1; i >= 0; i-- {
		elements << cb.content[i] // g,f
		s++
		if s >= size {
			return elements
		}
	}
	for i := cb.content.len-1; i >= pos; i-- {
		elements << cb.content[i] // e,d,c
		s++
		if s >= size {
			return elements
		}
	}
	return elements
}
fn test_logger() {

	mut cb := new_logger[string](5)
	for s in [ 'a', 'b', 'c', 'd', 'e', 'f', 'g' ] {
		cb.push(s)
	}
	a1 := cb.peek(5)
	assert a1.str() == "['g', 'f', 'e', 'd', 'c']"

	a2 := cb.peek(3)
	assert a2.str() == "['g', 'f', 'e']"

	for s in [ 'h', 'i' ] {
		cb.push(s)
	}
	a3 := cb.peek(4)
	assert a3.str() == "['i', 'h', 'g', 'f']"
}

jorgeluismireles avatar May 27 '25 17:05 jorgeluismireles

We already have https://github.com/vlang/v/blob/master/vlib/datatypes/queue.v

OK, but I checked the code, and found it seems missing some funcs, such as next(), clear(), is_full()... Also it just a wrapper of LinkedList.

kbkpbot avatar May 27 '25 23:05 kbkpbot