c4_JoinViewer::c4_JoinViewer(c4_Sequence &seq_, const c4_View &keys_, const c4_View &view_, bool outer_): _parent(&seq_), _argView(view_.SortOn(keys_)) { // why not in GetTemplate, since we don't need to know this... _template = _parent.Clone(); for (int l = 0; l < _argView.NumProperties(); ++l) _template.AddProperty(_argView.NthProperty(l)); c4_View sorted = _parent.SortOn(keys_).Project(keys_); c4_View temp = _argView.Project(keys_); _base.SetSize(0, 5); _offset.SetSize(0, 5); int j = 0, n = 0; for (int i = 0; i < sorted.GetSize(); ++i) { int orig = _parent.GetIndexOf(sorted[i]); d4_assert(orig >= 0); if (i > 0 && sorted[i] == sorted[i - 1]) { // if last key was same, repeat the same join int last = _offset.GetSize() - n; for (int k = 0; k < n; ++k) { _base.Add(orig); _offset.Add(_offset.GetAt(last + k)); } } else { // no, this is a new combination bool match = false; // advance until the temp view entry is >= this sorted entry while (j < temp.GetSize()) if (sorted[i] <= temp[j]) { match = sorted[i] == temp[j]; break; } else ++j; n = 0; if (match) { do { _base.Add(orig); _offset.Add(j); ++n; } while (++j < temp.GetSize() && temp[j] == temp[j - 1]); } else if (outer_) { // no match, add an entry anyway if this is an outer join _base.Add(orig); _offset.Add(~(t4_i32)0); // special null entry ++n; } } } }
v1.Add(p1[345] + p2["four"]); v1.Add(p1[234] + p2["five"]); v1.Add(p1[123] + p2["six"]); c4_View v2 = v1.Sort(); A(v2.GetSize() == 6); A(p1(v2[0]) == 111); A(p1(v2[1]) == 123); A(p1(v2[2]) == 222); A(p1(v2[3]) == 234); A(p1(v2[4]) == 333); A(p1(v2[5]) == 345); A(v2.Search(p1[123]) == 1); A(v2.Search(p1[100]) == 0); A(v2.Search(p1[200]) == 2); A(v2.Search(p1[400]) == 6); c4_View v3 = v1.SortOn(p2); A(v3.GetSize() == 6); A(p1(v3[0]) == 234); A(p1(v3[1]) == 345); A(p1(v3[2]) == 111); A(p1(v3[3]) == 123); A(p1(v3[4]) == 333); A(p1(v3[5]) == 222); A(v3.Search(p2["six"]) == 3); A(v3.Search(p2["aha"]) == 0); A(v3.Search(p2["gee"]) == 2); A(v3.Search(p2["wow"]) == 6); c4_View v4 = v1.SortOnReverse(p2, p2); A(v4.GetSize() == 6); A(p1(v4[0]) == 222); A(p1(v4[1]) == 333);