MCPcopy Create free account
hub / github.com/MariaDB/server / Sort

Method Sort

storage/connect/array.cpp:696–785  ·  view source on GitHub ↗

/ Sort and eliminate distinct values from an array. */ Note: this is done by making a sorted index on distinct values. */ Returns false if Ok or true in case of error. */ /

Source from the content-addressed store, hash-verified

694/* Returns false if Ok or true in case of error. */
695/***********************************************************************/
696bool ARRAY::Sort(PGLOBAL g)
697 {
698 int i, j, k;
699
700 // This is to avoid multiply allocating for correlated subqueries
701 if (Nval > Xsize) {
702 if (Xsize >= 0) {
703 // Was already allocated
704 PlgDBfree(Index);
705 PlgDBfree(Offset);
706 } // endif Xsize
707
708 // Prepare non conservative sort with offet values
709 Index.Size = Nval * sizeof(int);
710
711 if (!PlgDBalloc(g, NULL, Index))
712 goto error;
713
714 Offset.Size = (Nval + 1) * sizeof(int);
715
716 if (!PlgDBalloc(g, NULL, Offset))
717 goto error;
718
719 Xsize = Nval;
720 } // endif Nval
721
722 // Call the sort program, it returns the number of distinct values
723 Ndif = Qsort(g, Nval);
724
725 if (Ndif < 0)
726 goto error;
727
728 // Use the sort index to reorder the data in storage so it will
729 // be physically sorted and Index can be removed.
730 for (i = 0; i < Nval; i++) {
731 if (Pex[i] == i || Pex[i] == Nval)
732 // Already placed or already moved
733 continue;
734
735 Save(i);
736
737 for (j = i;; j = k) {
738 k = Pex[j];
739 Pex[j] = Nval; // Mark position as set
740
741 if (k == i) {
742 Restore(j);
743 break; // end of loop
744 } else
745 Move(j, k);
746
747 } // endfor j
748
749 } // endfor i
750
751 // Reduce the size of the To_Val array if Ndif < Nval
752 if (Ndif < Nval) {
753 for (i = 1; i < Ndif; i++)

Callers 1

ConvertMethod · 0.80

Calls 7

PlgDBfreeFunction · 0.85
PlgDBallocFunction · 0.85
ReAllocateMethod · 0.80
FreeMethod · 0.80
SaveMethod · 0.45
RestoreMethod · 0.45
MoveMethod · 0.45

Tested by

no test coverage detected