473,788 Members | 2,895 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stl container for interval data type

Hi,

I need to implement a container for integer intervals, something that
looks like (1,5), (10,20), (4,7), etc. The container has to be of the
following properties: search for an arbitratry interval for
intersection in O(log n) or O(1) if possible, insert and delete in
O(log n).
Does this sound simple? Is there any reference implementation that I
can look at?

-Alex

Jul 2 '06 #1
3 6519
al**********@gm ail.com wrote:
I need to implement a container for integer intervals, something that
looks like (1,5), (10,20), (4,7), etc. The container has to be of the
following properties: search for an arbitratry interval for
intersection in O(log n) or O(1) if possible, insert and delete in
O(log n).
Does this sound simple? Is there any reference implementation that I
can look at?
It sounds vaguely like homework, but not necessarily, so...

....actually, would be a great interview question, too. But anyway...

....I don't think any of the standard library containers are a
particularly good fit for this problem. However, I can see
representing this quite nicely as a binary tree. I actually
implemented something very similar a few months ago. Basically think
of the tree as a bitset, where if a bit is set it means the
corresponding integer is in the interval. When a whole subtree is the
same, you don't actually need nodes for it -- just interpret null
children as being all the same as their parent. The neat part (though
a bit tricky to implement) is that you can flip a whole subtree by
toggling a single bit. The reason it's tricky is worrying about all
those possibly-flipped bits when interpreting the contents of the tree.
I've done it, it's workable, it's not the only approach but it's an
interesting one. I suspect that most practical solutions to this
problem will revolve in some way around a binary tree.

Luke

Jul 2 '06 #2
<al**********@g mail.comwrote:
I need to implement a container for integer intervals, something that
looks like (1,5), (10,20), (4,7), etc. The container has to be of the
following properties: search for an arbitratry interval for
intersection in O(log n) or O(1) if possible, insert and delete in
O(log n).
Does this sound simple? Is there any reference implementation that I
can look at?

I'm not sure what you mean by "interval for intersection"; can you
elaborate?

I do have a couple of ideas for you, though:
// ======= APPROACH #1: =============== =============== =========

// Make a list of ordered pairs of integers:
typedef std::pair<int, intInterval;
std::list<Inter val MyIntervals;

// Add elements to list:
MyIntervals.pus h_back(Interval (1,3));
MyIntervals.pus h_back(Interval (7,32));
MyIntervals.pus h_back(Interval (13, -74));

// Remove all instances of (7,32) from list:
MyIntervals.rem ove(Interval(7, 32));

I can't swear as to the exact speed of this, but it's fast.
Insertions and deletions will be quite fast.

Drawbacks: it allows intervals such as (13,-74) which are mal-formed
according to usual mathematical convention. You'd have to manually
disallow adding such intervals to the container if you don't want
that. There may also be an issue trying to do MyIntervals.sor t()
if a suitable version of "<" is not available; you'd have to look
into that.

Advantages: ultra-simple; ready to go out of the box with no
modification; fast insertion, deletion.

Research "std::list" and "std::pair" for more info.
// ======= Approach #2: =============== =============== ===========

If you want to prohibit duplicate intervals,
use a std::set instead of a std::list, like so:

typedef std::pair<int, intInterval;
std::set<Interv al MyIntervals;

Advantages: Uniqueness of elements (if that's what you want);
blazing-fast lookup (uses binary trees).

Disadvantages: Might be a bit trickier to work with than lists
or other "ordinary" containers, do to the uniqueness requirement.

Research "std::set" for more info.
// ======= Approach #3: =============== =============== ==========

Same as above, but using std::multiset instead of std::set.
This allows multiple copies of an interval such as (5,7),
if you want to allow that. Research "std::multi set" for more
info.
--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 4 '06 #3
al**********@gm ail.com wrote:
Hi,

I need to implement a container for integer intervals, something that
looks like (1,5), (10,20), (4,7), etc. The container has to be of the
following properties: search for an arbitratry interval for
intersection in O(log n) or O(1) if possible, insert and delete in
O(log n).
Does this sound simple? Is there any reference implementation that I
can look at?

-Alex
Most implementations of STL map use an RB, (Red Black), tree which have
an insertion and deletion operation worse case of O(log n) and a search
operation with a worse case of O(h) where h is the height of the tree.
Unless there's some other trick you can use on the data I doubt you'll
find a better solution. Hopefully you'll find this to be the best
solution, being easy with STL map and hence not requiring a lot of coding.

JB
Jul 4 '06 #4

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

Similar topics

0
3948
by: Johnson, Shaunn | last post by:
Howdy: Running PostgreSQL 7.2.1 on RedHat Linux 7.2. How can I convert data in a table that has been created with the INTERVAL data type into a numeric format? Say, I have a table with this type of data:
5
724
by: bg | last post by:
Hi! How do I check if "date" exists before using that code? I've built a RSSreader and sometimes there's a date in it and sometimes not. How can I check if it exists to avoid crash (DataBinder.Eval: 'System.Data.DataRowView' does not contain a property with the name date) <asp:DataGrid id="RssNewsGrid"
2
1825
by: pruebauno | last post by:
I am currently working on a tricky problem at work. I googled around a bit, but "time intervals" did not come up with anything useful. Although I have some rough idea of how I could solve it, I still would value some input. I have information of (It has only couple dozen entries.) ServiceNum, DollarCost and a input database table in the form (several GBytes): ClientNumber (CN), BeginDate(BD), EndDate(ED),
3
1721
by: Valeriu Catina | last post by:
Hi, I have an array class (the blitz library actually) which looks like this: template<typename P_numtype, int N_rank> class Array : public MemoryBlockReference<P_numtype> , public ETBase<Array<P_numtype,N_rank> > { // ... code };
0
1326
by: John Davis | last post by:
Hi all, hope you are well. I would like to know if I can access the strongly typed properties of my datatable when binding through the ObjectDataSource object. I am currently creating some hyperlinks in a repeater bound to an objectdatasource with the following code: <asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl='<%# string.Format("~/Products.aspx?page=0&categoryId={0}",
14
3654
by: markww | last post by:
Hi, I want to use the vector container class to store pixel data. Currently I have some memory allocated using c++'s new operator. I allocate the memory differently based on if the pixel type is unsigned char or unsigned short like this: int nPixelType = ?; // unsigned short, or unsigned char? BYTE *pByte = NULL; switch (nPixelType) {
0
1398
by: John Davis | last post by:
Hi all, hope you are well.(not sure if this posted the first time) I would like to know if I can access the strongly typed properties of my datatable when binding through the ObjectDataSource object. I am currently creating some hyperlinks in a repeater bound to an objectdatasource with the following code: <asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl='<%# string.Format("~/Products.aspx?page=0&categoryId={0}",
10
1788
by: Adrian | last post by:
Below is an example of the problem I am having. I don't understand how I can get the compiler to see deriv &operator=(const T &rhs). I am sure this is a common problem - any suggestions? #include <iostream> #include <vector> struct base
11
1688
by: food4uk | last post by:
Dear all : I am not good at programming, please give a hand. My data structure is very similar as an array. I actually can use the std::vector as container to organize my data objects. However, the behaviours of std::vector::iterator can not meet my requirements. I need to redefine the vector::iterator's behavious such as ++. It means "next position in the object sequence" in STL but now I redefine it to mean "next position within a 2-d...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10366
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...
0
9967
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
8993
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
7517
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
5399
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...
0
5536
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
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.