473,738 Members | 5,934 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

here is code for the "Soft Heap"

Here is some code for the Soft Heap, taken directly from the original
paper on the subject with a little help from manish_gupta.
Unfortunately, it's about half as fast as a basic binary heap simply
because of memory allocation. In fact, my profiler says that 90% of
the time in this sucker is memory allocation. Can anybody devise a way
to cut down on the number of "news" in this? I need to insert a few
million elements and remove a tenth of that. I don't really want to do
several million memory allocations. Thanks for your time.

using System;

namespace HeapTests
{
public class ILCell<Twhere T : IComparable<T>, IInfinitable
{
public ILCell<TNext = null;
public T Key;
}

public class Node<Twhere T : IComparable<T>, IInfinitable
{
public T Key;
public int Rank = 0;
public Node<TNext = null, Child = null;
public ILCell<TIL = null, ILTail = null;
}

public class Head<Twhere T : IComparable<T>, IInfinitable
{
public Node<TQueue = null;
public Head<TNext = null, Previous = null, Suffix = null;
public int Rank = int.MaxValue;
}

public interface IInfinitable
{
bool Infinite { get; set; }
}

public class SoftHeap<Twhere T : IComparable<T>, IInfinitable
{
private readonly Head<T_header = new Head<T>(), _tail = new
Head<T>();
private readonly int _r;
public SoftHeap(int allowCorruptOne In)
{
_r = 2 + 2 * (int)Math.Ceili ng(Math.Log(all owCorruptOneIn) );
_header.Next = _tail;
_tail.Previous = _header;
}

private int _count = 0;
public int Count { get { return _count; } }

public void Insert(T key)
{
ILCell<Tl = new ILCell<T>();
l.Key = key;
Node<Tq = new Node<T>();
q.Key = key;
q.IL = l;
q.ILTail = l;
Meld(q);
_count++;
}

private void Meld(Node<Tq)
{
Head<Th, prevhead, tohead = _header.Next;
Node<Ttop;
while (q.Rank tohead.Rank)
tohead = tohead.Next;
prevhead = tohead.Previous ;
while (q.Rank == tohead.Rank)
{
Node<Tbottom;
if (tohead.Queue.K ey.CompareTo(q. Key) 0)
{
top = q;
bottom = tohead.Queue;
}
else
{
top = tohead.Queue;
bottom = q;
}
q = new Node<T>();
q.Key = top.Key;
q.Rank = top.Rank + 1;
q.Child = bottom;
q.Next = top;
q.IL = top.IL;
q.ILTail = top.ILTail;
tohead = tohead.Next;
}
if (prevhead == tohead.Previous )
h = new Head<T>();
else
h = prevhead.Next;
h.Queue = q;
h.Rank = q.Rank;
h.Previous = prevhead;
h.Next = tohead;
prevhead.Next = h;
tohead.Previous = h;
FixMinList(h);
}
private void FixMinList(Head <Th)
{
Head<Ttmpmin;
if (h.Next == _tail) tmpmin = h;
else tmpmin = h.Next.Suffix;
while (h != _header)
{
if (h.Queue.Key.Co mpareTo(tmpmin. Queue.Key) < 0)
{
tmpmin = h;
}
h.Suffix = tmpmin;
h = h.Previous;
}
}

public T DeleteMin()
{
if (_count == 0)
return default(T);
Node<Ttmp;
Head<Th = _header.Next.Su ffix;
if (h == null)
return default(T);
while (h.Queue.IL == null)
{
tmp = h.Queue;
int childcount = 0;
while (tmp.Next != null)
{
tmp = tmp.Next;
childcount++;
}
if (childcount < h.Rank/2)
{
h.Previous.Next = h.Next;
h.Next.Previous = h.Previous;
FixMinList(h.Pr evious);
tmp = h.Queue;
while (tmp.Next != null)
{
Meld(tmp.Child) ;
tmp = tmp.Next;
}
}
else
{
h.Queue = Sift(h.Queue);
if (h.Queue.Key.In finite)
{
h.Previous.Next = h.Next;
h.Next.Previous = h.Previous;
h = h.Previous;
}
FixMinList(h);
}
h = _header.Next.Su ffix;
if(h == null || h.Queue == null)
return default(T);
}
T min = h.Queue.IL.Key;
h.Queue.IL = h.Queue.IL.Next ;
if (h.Queue.IL == null)
h.Queue.ILTail = null;
_count--;
return min;
}

private Node<TSift(Node <Tv)
{
Node<Ttmp;
v.IL = null;
v.ILTail = null;
if(v.Next == null && v.Child == null)
{
v.Key.Infinite = true;
return v;
}
v.Next = Sift(v.Next);

if (v.Next.Key.Com pareTo(v.Child. Key) 0)
{
tmp = v.Child;
v.Child = v.Next;
v.Next = tmp;
}

v.IL = v.Next.IL;
v.ILTail = v.Next.ILTail;
v.Key = v.Next.Key;

if (v.Rank _r && (v.Rank % 2 == 1 || v.Child.Rank < v.Rank - 1))
{
v.Next = Sift(v.Next);
if (v.Next.Key.Com pareTo(v.Child. Key) 0)
{
tmp = v.Child;
v.Child = v.Next;
v.Next = tmp;
}
if (!v.Next.Key.In finite && v.Next.IL != null)
{
v.Next.ILTail.N ext = v.IL;
v.IL = v.Next.IL;
if (v.ILTail == null)
v.ILTail = v.Next.ILTail;
v.Key = v.Next.Key;
}
}
if (v.Child.Key.In finite)
{
if (v.Next.Key.Inf inite)
{
v.Child = null;
v.Next = null;
}
else
{
v.Child = v.Next.Child;
v.Next = v.Next.Next;
}
}
return v;
}
}
}

Oct 11 '07 #1
0 1837

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
3242
by: hokiegal99 | last post by:
How would I determine if a filename is greater than a certain number of characters and then truncate it to that number? For example a file named XXXXXXXXX.txt would become XXXXXX fname = files if fname > 6 print fname Thanks!!!
87
5234
by: ziliath | last post by:
I recently tried out the Google "top coder" contest, as a C++ coder. I noticed immediately that they expected me to know STL. To which I say, what the fuck?! I may be missing something, but at what point when learning C++ was I supposed to have picked up STL? I ask because at every job I've had, and every job interview for that matter, whenever STL comes up it's not I, but a manager who says effectively yuck, we don't use that crap.
8
3193
by: Mark | last post by:
My PHP script builds a table that is too wide to fit on the paper. Two of the columns contain strings that are more lengthy than data in the other columns. I can get the table to fit by letting the browser break the lengthy strings across two or more lines, thus allowing the column to get skinnier. I do this by inserting spaces in various places in the string. But this is not a desirable solution as it would mislead the user into...
0
4412
by: Peter | last post by:
When I issue call sqlj.install_jar('file:///f:/jars/mail.jar','MAIL'); I get the messages SQL4301N Java or .NET interpreter startup or communication failed, reason
4
5532
by: JvdWal | last post by:
Hi, I've been trying the PDF & Mail Class Library from ACG Soft in combination with Northwind-2k3 style of db. Can someone help me with the error which is popping up: (which I cant get solved..) I'm trying to send a report in PDF-format with a unique QuotationID to one specific customer by Email. >> Button in a form >> quotation-report and a "filter and printfilter"- query..
15
6725
by: Joe Van Dyk | last post by:
Can someone explain what a heap and what a stack is? And why I should care? I used to know, but then I forgot. And I can't seem to find it in the C++ FAQ. I keep reading how allocating from the heap is slow or something, and I have no idea why that would be. Joe
53
26386
by: fdmfdmfdm | last post by:
This is an interview question and I gave out my answer here, could you please check for me? Q. What are the memory allocation for static variable in a function, an automatic variable and global variable? My answer: static variable in function and global variable are allocated in head, and automatic variable is allocated in stack. Right?
6
3242
by: haneeshkb | last post by:
I am getting this problem when I tried to build my borland c++ project. (compling and make doesn't giving any problem ) Fatal: Fatal: Assertion failed: !"How can you expand a vapor heap?" at "LMEM.C" Can anybody help me on this ?
350
11813
by: Lloyd Bonafide | last post by:
I followed a link to James Kanze's web site in another thread and was surprised to read this comment by a link to a GC: "I can't imagine writing C++ without it" How many of you c.l.c++'ers use one, and in what percentage of your projects is one used? I have never used one in personal or professional C++ programming. Am I a holdover to days gone by?
0
9476
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.