Re: Why after writeln the binaryHeap become empty?

2019-06-19 Thread Mike Parker via Digitalmars-d-learn
On Wednesday, 19 June 2019 at 06:00:28 UTC, Jonathan M Davis wrote: On Tuesday, June 18, 2019 10:27:46 PM MDT lili via Do you known reason for why Dlang Range are consumed by iterating over them. I this design is strange. If you want an overview of ranges, you can watch this:

Re: Why after writeln the binaryHeap become empty?

2019-06-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, June 18, 2019 10:27:46 PM MDT lili via Digitalmars-d-learn wrote: > On Tuesday, 18 June 2019 at 17:25:51 UTC, Johannes Loher wrote: > > The result of heapify is a BinaryHeap, which is a range. writeln > > basically prints ranges by iterating over them and printing &

Re: Why after writeln the binaryHeap become empty?

2019-06-18 Thread lili via Digitalmars-d-learn
On Tuesday, 18 June 2019 at 17:25:51 UTC, Johannes Loher wrote: The result of heapify is a BinaryHeap, which is a range. writeln basically prints ranges by iterating over them and printing each element (except for the types which are special cased, such as dynamic arrays etc.). However, ranges

Re: Why after writeln the binaryHeap become empty?

2019-06-18 Thread Jonathan M Davis via Digitalmars-d-learn
mpty()); > ~~~ > dmd v2.086.0 run all assert passed. Why? Looking at std/container/binaryheap.d, heapify returns the type BinaryHeap which provides the API for an input range but no toString. As such, writeln likely uses the range API to read the elements and print them. And that's going to po

Re: Why after writeln the binaryHeap become empty?

2019-06-18 Thread Johannes Loher via Digitalmars-d-learn
sert passed. Why? The result of heapify is a BinaryHeap, which is a range. writeln basically prints ranges by iterating over them and printing each element (except for the types which are special cased, such as dynamic arrays etc.). However, ranges are consumed by iterating over them, whi

Why after writeln the binaryHeap become empty?

2019-06-18 Thread lili via Digitalmars-d-learn
Hi Guys: see this code ~~~ int[] ar = [1,2,3,4,52,34,22]; auto h = heapify(ar); assert(h.length() == ar.length); writeln("h:",h); assert(h.empty()); ~~~ dmd v2.086.0 run all assert passed. Why?

Re: BinaryHeap as member

2017-11-14 Thread balddenimhero via Digitalmars-d-learn
On Tuesday, 14 November 2017 at 04:13:16 UTC, Era Scarecrow wrote: On Monday, 13 November 2017 at 16:26:20 UTC, balddenimhero wrote: In the course of writing a minimal example I removed more than necessary in the previous pastebin (the passed IntOrder has not even been used). Thus here is the

Re: BinaryHeap as member

2017-11-13 Thread Era Scarecrow via Digitalmars-d-learn
foreach(i, ref v; arr) { a ~= E(order[i%$], ); } h = BinaryHeap!(E[])(a); } E[] a; BinaryHeap!(E[]) h; // alias h this; static struct E { int weight; T* v; //alias v this; int opCmp(E a) const { return a.weight-weight; } } } [/code] Output: Weigh

Re: BinaryHeap as member

2017-11-13 Thread balddenimhero via Digitalmars-d-learn
, with the `h` member having type `BinaryHeap(int[], "a<b")` and the constructor trying to assign a `BinaryHeap!(int[], f)` the types do not quite match (line 18). Maybe I'm missing something obvious or approaching this in the wrong way. Do you have any suggestions on how to achieve the d

Re: BinaryHeap as member

2017-11-13 Thread balddenimhero via Digitalmars-d-learn
?compiler=dmd The `Foo` class is supposed to wrap a binary heap (to be used as priority queue) and order its contents according to `o.isLess`. However I do not manage to initialise the according member variable. Obviously, with the `h` member having type `BinaryHeap(int[], &quo

BinaryHeap as member

2017-11-13 Thread balddenimhero via Digitalmars-d-learn
(to be used as priority queue) and order its contents according to `o.isLess`. However I do not manage to initialise the according member variable. Obviously, with the `h` member having type `BinaryHeap(int[], "a<b")` and the constructor trying to assign a `BinaryHeap!(int[], f)`

Re: Why does BinaryHeap sometime cause compile-error in foeach?

2017-09-30 Thread Shigeki Karita via Digitalmars-d-learn
Oh, struct/class semantics really confuses me!

Re: Why does BinaryHeap sometime cause compile-error in foeach?

2017-09-30 Thread user1234 via Digitalmars-d-learn
On Saturday, 30 September 2017 at 09:27:23 UTC, Shigeki Karita wrote: https://dpaste.dzfl.pl/cd605899d050 why this code cannot convert to foreach (over Structs and Classes with Ranges). auto h = new BinaryHeap!(int[])(new int[0]); typeof(h).stringof.writeln; static assert

Why does BinaryHeap sometime cause compile-error in foeach?

2017-09-30 Thread Shigeki Karita via Digitalmars-d-learn
https://dpaste.dzfl.pl/cd605899d050 why this code cannot convert to foreach (over Structs and Classes with Ranges). auto h = new BinaryHeap!(int[])(new int[0]); typeof(h).stringof.writeln; static assert(isInputRange!(typeof(h))); h.insert(3); h.insert(1); h.insert(2

Re: BinaryHeap crashes upon insertion if heapified with an array of length 1?

2017-04-10 Thread TheGag96 via Digitalmars-d-learn
On Sunday, 9 April 2017 at 13:31:05 UTC, Michael Coulombe wrote: This is a bug in the insert method. I created a bug report for you and submitted a pull request for a fix: https://issues.dlang.org/show_bug.cgi?id=17314 Ah, so it is. Thanks!!

Re: BinaryHeap crashes upon insertion if heapified with an array of length 1?

2017-04-09 Thread Michael Coulombe via Digitalmars-d-learn
On Sunday, 9 April 2017 at 00:36:00 UTC, TheGag96 wrote: I'm trying to use a binary heap initialized with one element. However, this always seems to cause a range violation for some reason. This small example will do it: import std.stdio, std.container; void main() { auto pq =

BinaryHeap crashes upon insertion if heapified with an array of length 1?

2017-04-08 Thread TheGag96 via Digitalmars-d-learn
I'm trying to use a binary heap initialized with one element. However, this always seems to cause a range violation for some reason. This small example will do it: import std.stdio, std.container; void main() { auto pq = heapify([5]); pq.insert(8); } ...And it produces this error:

What's wrong with this BinaryHeap declaration line? Assertion failure in std.container.array.d

2015-09-23 Thread Enjoys Math via Digitalmars-d-learn
What I HAD TO do to get it to compile: programResultsQ = heapify!(compareResults, Array!(Results!(O,I)))(Array!(Results!(O,I))([Results!(O,I)()]), 1); programResultsQ.popFront(); What running it says: AssertionFailure at line 381 of std.container.array.d, which looks like: /**

Re: What's wrong with this BinaryHeap declaration line? Assertion failure in std.container.array.d

2015-09-23 Thread Enjoys Math via Digitalmars-d-learn
ideas. How can I improve this declaration? Using Phobos sometimes is such a mystery. I mean initialization... Here's the corresponding declaration: alias ProgramResultsQueue(O,I) = BinaryHeap!(Array!(Results!(O,I)), compareResults); /* module scope */ ProgramResultsQueue!(O,I

Can't seem to alias BinaryHeap and use heapify

2015-09-21 Thread Enjoys Math via Digitalmars-d-learn
I've got: alias ProgramResultsQueue(O,I) = BinaryHeap!(Array!(Results!(O,I)), compareResults); outside the class ProgramOptimizer. Inside the class I have: ProgramResultsQueue!(O,I) programResultsQ = heapify!(compareResults, Array!(Results!(O,I)))(Array!(Results!(O,I)), 0); at class

Re: Can't seem to alias BinaryHeap and use heapify

2015-09-21 Thread Ali Çehreli via Digitalmars-d-learn
,I) = BinaryHeap!(Array!(Results!(O,I)), compareResults); class ProgramOptimizer(O, I) { ProgramResultsQueue!(O,I) programResultsQ = heapify!(compareResults, Array!(Results!(O,I)))(Array!(Results!(O,I)), 0); } void main

Extracting Sorted Storage from BinaryHeap

2015-03-29 Thread Nordlöw
What's the most efficient way to extract a the storage from a BinaryHeap and then sort it? Is there a better way other than binaryHeap.release.sort than makes use of the heap property? For example while (!binaryHeap.empty) { sortedStorage ~= binaryHeap.front

Re: Extracting Sorted Storage from BinaryHeap

2015-03-29 Thread Ivan Kazmenko via Digitalmars-d-learn
On Sunday, 29 March 2015 at 20:05:22 UTC, Nordlöw wrote: What's the most efficient way to extract a the storage from a BinaryHeap and then sort it? Is there a better way other than binaryHeap.release.sort than makes use of the heap property? For example while (!binaryHeap.empty

Problem Instantiating a BinaryHeap with a Comparison Function the needs this

2015-02-19 Thread Nordlöw
I can understand how to correctly define an instance of BinaryHeap in my class DijkstraWalker at https://github.com/nordlow/justd/blob/master/knet/traversal.d#L264 because the comparsion function can't ge access to the class member distMap I get the error need 'this' for 'distMap' of type

Re: Problem Instantiating a BinaryHeap with a Comparison Function the needs this

2015-02-19 Thread Gary Willoughby via Digitalmars-d-learn
On Thursday, 19 February 2015 at 11:56:19 UTC, Nordlöw wrote: I can understand how to correctly define an instance of BinaryHeap in my class DijkstraWalker at https://github.com/nordlow/justd/blob/master/knet/traversal.d#L264 because the comparsion function can't ge access to the class

Re: Problem Instantiating a BinaryHeap with a Comparison Function the needs this

2015-02-19 Thread Tobias Pankrath via Digitalmars-d-learn
On Thursday, 19 February 2015 at 11:56:19 UTC, Nordlöw wrote: Please provide reduced examples. This fails: class C { int[] a; alias BH = BinaryHeap!(int[], (x, y) = (x+a y)); } This works: class C { int[] a; void foo() { alias BH = BinaryHeap!(int[], (x, y) = (x

Re: Problem Instantiating a BinaryHeap with a Comparison Function the needs this

2015-02-19 Thread Nordlöw
On Thursday, 19 February 2015 at 14:12:51 UTC, Tobias Pankrath wrote: On Thursday, 19 February 2015 at 11:56:19 UTC, Nordlöw wrote: Please provide reduced examples. This fails: class C { int[] a; alias BH = BinaryHeap!(int[], (x, y) = (x+a y)); } This works: class C { int

change value of item in BinaryHeap

2014-04-27 Thread Øivind
I am trying to figure out how to change the value of an element in a BinaryHeap (from std.container) and have it repositioned such that the heap is still valid. Can anyone help me with this? The approach I would have taken (I think) is to remove the elements that get new values from the heap

Re: BinaryHeap

2013-11-27 Thread Agustin
On Thursday, 7 November 2013 at 14:31:27 UTC, Agustin wrote: On Thursday, 7 November 2013 at 12:29:44 UTC, bearophile wrote: Agustin: no property 'popFront' for type 'BinaryHeap!(uint[])' Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye

Re: BinaryHeap

2013-11-07 Thread bearophile
Agustin: I'm trying to use BinaryHeap and i found out that i cannot use foreach(). My question is, there is any other way to do it?, can i iterate a BinaryHeap? Please show the code :-) Perhaps you need to look at the head, pop the head item, look at the head, etc. Bye, bearophile

Re: BinaryHeap

2013-11-07 Thread Agustin
On Thursday, 7 November 2013 at 09:00:11 UTC, bearophile wrote: Agustin: I'm trying to use BinaryHeap and i found out that i cannot use foreach(). My question is, there is any other way to do it?, can i iterate a BinaryHeap? Please show the code :-) Perhaps you need to look at the head

Re: BinaryHeap

2013-11-07 Thread Agustin
On Thursday, 7 November 2013 at 12:14:22 UTC, Agustin wrote: On Thursday, 7 November 2013 at 09:00:11 UTC, bearophile wrote: Agustin: I'm trying to use BinaryHeap and i found out that i cannot use foreach(). My question is, there is any other way to do it?, can i iterate a BinaryHeap

Re: BinaryHeap

2013-11-07 Thread bearophile
Agustin: no property 'popFront' for type 'BinaryHeap!(uint[])' Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye, bearophile

Re: BinaryHeap

2013-11-07 Thread Agustin
On Thursday, 7 November 2013 at 12:29:44 UTC, bearophile wrote: Agustin: no property 'popFront' for type 'BinaryHeap!(uint[])' Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye, bearophile Saddly i had to do this auto clone

Re: BinaryHeap

2013-11-07 Thread Agustin
On Thursday, 7 November 2013 at 12:45:11 UTC, Agustin wrote: On Thursday, 7 November 2013 at 12:29:44 UTC, bearophile wrote: Agustin: no property 'popFront' for type 'BinaryHeap!(uint[])' Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye

Re: BinaryHeap

2013-11-07 Thread Agustin
On Thursday, 7 November 2013 at 12:29:44 UTC, bearophile wrote: Agustin: no property 'popFront' for type 'BinaryHeap!(uint[])' Try to use front and removeFront (I don't know why there is removeFront instead of popFront). Bye, bearophile I had to implement a custom IterableBinaryHeap

BinaryHeap

2013-11-06 Thread Agustin
I'm trying to use BinaryHeap and i found out that i cannot use foreach(). My question is, there is any other way to do it?, can i iterate a BinaryHeap?

Why this simple binaryHeap program does not compile?

2011-09-22 Thread Cheng Wei
import std.container; class T { int i; } void main() { auto array = Array!(T); auto heap = BinaryHeap!(Array!(T), a.i b.i)(array); } After compiling: dmd2/linux/bin32/../../src/phobos/std/container.d(1612): Error: template std.conv.emplace(T) if (!is(T == class)) does not match

Re: Why this simple binaryHeap program does not compile?

2011-09-22 Thread Ellery Newcomer
On 09/22/2011 06:10 AM, Cheng Wei wrote: Is this a bug or I use the binary heap wrongly? Thanks a lot! Looks like a bug in Array. emplace doesn't accept a pointer to a chunk for class types. Report that puppy!

Re: Why this simple binaryHeap program does not compile?

2011-09-22 Thread David Nadlinger
On 9/22/11 7:20 PM, Ellery Newcomer wrote: Looks like a bug in Array. emplace doesn't accept a pointer to a chunk for class types. Report that puppy! See https://github.com/D-Programming-Language/phobos/commit/65a0c2158b1d2ea8e9d3094746739da636266089. David

How do you use BinaryHeap with Array (or just make a growable heap)?

2011-03-28 Thread Magnus Lie Hetland
I need a (growable) binary heap, and I'm trying to avoid writing one myself (which isn't too hard, of course :) ... but for some reason I can't figure out how to use Phobos to do it. I've seen stated (e.g., by Andrei and in the docs) that the standard way is combining BinaryHeap with an Array

Re: How do you use BinaryHeap with Array (or just make a growable heap)?

2011-03-28 Thread Magnus Lie Hetland
of dynamic arrays). I've never used BinaryHeap, but glancing at it, my guess would be that the correct solution (if you want to use Array with it) is to create an Array and then pass its range to heapify: Array!uint a; //... put stuff in a. auto heap = heapify(a[]); I'm not sure if that's growable

Re: Growable BinaryHeap: use w/Array?

2011-03-07 Thread Magnus Lie Hetland
On 2011-03-06 14:58:10 +0100, Magnus Lie Hetland said: [corrected the example below, replacing int with string] that works just fine. However, if I try alias Tuple!(real,string) Entry; Array!Entry Q; then I get the following errors: container.d(1549): Error: this for _data needs to

Growable BinaryHeap: use w/Array?

2011-03-06 Thread Magnus Lie Hetland
Just wondering: If I want a growable binary heap (I'd be open to other priority queue structures, for that matter ;), is the standard way in D (w/Phobos) to combine std.container.BinaryHeap with std.container.Array? Any reason why BinaryHeap can't deal with ordinary dynamic array appending

Re: Growable BinaryHeap: use w/Array?

2011-03-06 Thread Magnus Lie Hetland
... when I use priority queues, I'm usually not interested in just having a set of priorities -- the payload data is what it's all about. So I thought I'd just use an Array of Tuples, managed by BinaryHeap (possibly with a custom comparison, to avoid comparing the payloads). But perhaps that's

Re: Growable BinaryHeap: use w/Array?

2011-03-06 Thread David Nadlinger
On 3/6/11 2:58 PM, Magnus Lie Hetland wrote: alias Tuple!(real,int) Entry; Array!Entry Q; […] alias Tuple!(real,int) Entry; Array!Entry Q; Is it just me, or is there really no difference between the two snippets? ;) David

Re: Growable BinaryHeap: use w/Array?

2011-03-06 Thread Magnus Lie Hetland
On 2011-03-06 15:00:29 +0100, David Nadlinger said: On 3/6/11 2:58 PM, Magnus Lie Hetland wrote: alias Tuple!(real,int) Entry; Array!Entry Q; [...] alias Tuple!(real,int) Entry; Array!Entry Q; Is it just me, or is there really no difference between the two snippets? ;) $(WITTY_REPLY) ;-)

BinaryHeap usage

2010-12-14 Thread Matthias Walter
Hi all, suppose I have an array of comparable Foo structs which I want to access in a sorted order (e.g. a priority queue) using a BinaryHeap object (I know that for just sorting, the BinHeap is not the right tools), but I do not want to change the order of the objects in the original array. I

Re: BinaryHeap usage

2010-12-14 Thread bearophile
Matthias Walter: Any further ideas for this problem, or did I cover everything already? For a C programmer the solution with pointers seems more natural, for a Pascal-family programmer the solution with indexes seems more natural, a bit safer (and it is probably just as fast). A third