circular-list

Least expensive way to construct cyclic list in Haskell

落花浮王杯 提交于 2019-12-10 13:46:24
问题 So if I want to construct a circular list of n 0's and 1 1, which of the following ways is better/cheaper? And is there an even better/cheaper way? Taking into account that n is an Integer and may be large (though realistically its not going to exceed 2^32). aZerosAndOnes :: Integer -> [Int] aZerosAndOnes n | n >= 0 = cycle (genericReplicate n 0 ++ [1]) | otherwise = [] versus bZerosAndOnes :: Integer -> [Int] bZerosAndOnes n | n >= 0 = tail (cycle (1 : genericReplicate n 0)) | otherwise = []

Trashes in copied std::list

强颜欢笑 提交于 2019-12-10 11:19:45
问题 I have graph class that looks like: class Graph { public: typedef unsigned int size_type; typedef std::list<size_type> Neighbours; protected: size_type m_nodes_count, m_edges_count; public: Graph(size_type nodes_count = 0) : m_nodes_count(nodes_count), m_edges_count(0) {} virtual bool is_edge(size_type from, size_type to) = 0; virtual Neighbours neighbours(size_type node) = 0; virtual Graph& add_edge(size_type from, size_type to) = 0; virtual void delete_edge(size_type from, size_type to) = 0

Can Circular Lists be defined in Erlang?

喜夏-厌秋 提交于 2019-12-07 18:28:46
问题 is it possible to define a circular list in erlang? http://en.wikipedia.org/wiki/Linked_list first question would be what exactly a circular list mean in erlang? is it with two elements, one element its self and next to it address to the next element, stored in a list? if so i can say there is a possibility of defining a circular list in erlang. but i need clarification weather is it what i think a circular list is in erlang? 回答1: There is no built-in list mechanism to do it. However, you can

Trashes in copied std::list

前提是你 提交于 2019-12-06 05:32:47
I have graph class that looks like: class Graph { public: typedef unsigned int size_type; typedef std::list<size_type> Neighbours; protected: size_type m_nodes_count, m_edges_count; public: Graph(size_type nodes_count = 0) : m_nodes_count(nodes_count), m_edges_count(0) {} virtual bool is_edge(size_type from, size_type to) = 0; virtual Neighbours neighbours(size_type node) = 0; virtual Graph& add_edge(size_type from, size_type to) = 0; virtual void delete_edge(size_type from, size_type to) = 0; size_type nodes_count() { return m_nodes_count; } size_type edges_count() { return m_edges_count; }

How to delete elements of a circular list until there is only one element left using python? [closed]

邮差的信 提交于 2019-12-05 12:22:40
Closed . This question needs to be more focused . It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post . Closed 11 months ago . How would I go about iterating through a list from 1-100, where I delete every other element starting with the first element, and repeat that step until there in only one element left in the list. Would I have to use a circular linked list, or can it be done just using loops and conditional statements? If you assume the circular list structure, when the last element of the list

What's the neatest way to define circular lists with Scala?

不问归期 提交于 2019-12-05 08:46:29
Here is my attempt: case class A(val a: A, val b: Int){ override def toString() = b.toString } lazy val x: A = A(y, 0) lazy val y: A = A(z, 1) lazy val z: A = A(x, 2) The problem comes when trying to do anything with x; causing x to be evaluated starts off a circular evaluation going through x, y, z and ends in a stack overflow. Is there a way of specifying that val a should be computed lazily? You need to make A.a itself lazy. You can do it by turning it into a by name parameter that is used to initialize a lazy field: class A(a0: => A, val b: Int){ lazy val a = a0 override def toString() = b

Efficient circular list

可紊 提交于 2019-12-04 20:06:20
问题 I want a simple yet efficient circular buffer/queue. If I use std::vector , I have to do this: if ( v.size() >= limit ) { std::vector<int> it = v.begin(); v.insert( it, data ); v.erase( it+1 ); } Is there any simpler solution? 回答1: You want to maintain the size of the buffer, overwriting older items. Just overwrite the old ones as time goes on. If you want to deal with the case where nItems < limit, then you would need to deal with that, this is just a simple example of using modulo to insert

Why exactly do we need a “Circular Linked List” (singly or doubly) data structure?

六眼飞鱼酱① 提交于 2019-12-04 07:42:10
问题 Why exactly do we need a "Circular Linked List" (singly or doubly) data structure? What problem does it solve that is evident with simple Linked Lists (singly or doubly)? 回答1: A simple example is keeping track of whose turn it is in a multi-player board game. Put all the players in a circular linked list. After a player takes his turn, advance to the next player in the list. This will cause the program to cycle indefinitely among the players. To traverse a circular linked list, store a

What is the difference between a cyclic list and an infinite list in haskell?

本秂侑毒 提交于 2019-12-04 00:07:24
Referencing @dfeuer's answer to this question: Least expensive way to construct cyclic list in Haskell , which says that using cyclic lists 'defeats' the garbage collector as it has to keep everything you've consumed from a cyclic list allocated till you drop the reference to any cons cells in the list. Apparently in Haskell a cyclic list and an infinite list are two separate things. This blog ( https://unspecified.wordpress.com/2010/03/30/a-doubly-linked-list-in-haskell/ ) says that if you implement cycle as follows: cycle xs = xs ++ cycle xs it is an infinite list, not a cyclic list. To make

Efficient circular list

懵懂的女人 提交于 2019-12-03 12:45:13
I want a simple yet efficient circular buffer/queue. If I use std::vector , I have to do this: if ( v.size() >= limit ) { std::vector<int> it = v.begin(); v.insert( it, data ); v.erase( it+1 ); } Is there any simpler solution? You want to maintain the size of the buffer, overwriting older items. Just overwrite the old ones as time goes on. If you want to deal with the case where nItems < limit, then you would need to deal with that, this is just a simple example of using modulo to insert into a fixed size buffer. std::vector<int> data(10); for (int i = 0 ; i < 100 ; ++i) { data[i%10] = i; }