6

Similar to this question Pivot Table in c#, I'm looking to find an implementation of a pivot table in c++. Due to the project requirements speed is fairly critical and the rest of the performance critical part project is written in c++ so an implementation in c++ or callable from c++ would be highly desirable. Does anyone know of implementations of a pivot table similar to the one found in Excel or open office?

I'd rather not have to code such a thing from scratch, but if I was to do this how should I go about it? What algorithms and data structures would be good to be aware of? Any links to an algorithm would be greatly appreciated.

Community
  • 1
  • 1
shuttle87
  • 15,466
  • 11
  • 77
  • 106

3 Answers3

3

I am sure you are not asking full feature of pivot table in Excel. I think you want simple statistics table based on discrete explanatory variables and given statistics. If you do, I think this is the case that writing from scratch might be faster than looking at other implementations.

Just update std::map (or similar data structure) of key representing combination of explanatory variables and value of given statistics when program reading each data point.

After done with the reading, it's just matter of organizing output table with the map which might be trivial depending on your goal.

I believe most of C# examples in that question you linked do this approach anyway.

Tae-Sung Shin
  • 20,215
  • 33
  • 138
  • 240
2

I'm not aware of an existing implementation that would suit your needs, so, assuming you were to write one...

I'd suggest using SQLite to store your data and use SQL to compute aggregates (Note: SQL won't do median, I suggest an abstraction at some stage to allow such behavior), The benefit of using SQLite is that it's pretty flexible and extremely robust, plus it lets you take advantage of their hard work in terms of storing and manipulating data. Wrapping the interface you expect from your pivot table around this concept seems like a good way to start, and save you quite a lot of time.

You could then combine this with a model-view-controller architecture for UI components, I anticipate that would work like a charm. I'm a very satisfied user of Qt, so in this regard I'd suggest using Qt's QTableView in combination with QStandardItemModel (if I can get away with it) or QAbstractItemModel (if I have to). Not sure if you wanted this suggestion, but it's there if you want it :).

Hope that gives you a starting point, any questions or additions, don't hesitate to ask.

Liam M
  • 5,306
  • 4
  • 39
  • 55
0

I think the reason your question didn't get much attention is that it's not clear what your input data is, nor what options for pivot table you want to support.

A pivot table is in it's basic form, running through the data, aggregating operations into buckets. For example, you want to see how many items you shipped each week from each warehouse for the last few weeks:

You would create a multi-dimensional array of buckets (rows are weeks, columns are warehouses), and run through the data, deciding which bucket that data belongs to, adding the amount in the record you're looking at, and moving to the next record.

Eve Freeman
  • 32,467
  • 4
  • 86
  • 101