function [prob atlas cseg] = flush(atlas, prob, cseg)
%FLUSH   Flush point list.
%
% Merge last successfully located chart into atlas. Use superclass flush
% function to write point list to screen and disk. Terminate when number of
% successfully completed continuation steps equals PtMX or when the atlas
% boundary is empty.

% This file is part of the atlas_kd toolbox, copyright (C) Michael
% Henderson, Frank Schilder, Harry Dankowicz, Erika Fotsch, of the package
% COCO (http://sourceforge.net/projects/cocotools).

if cseg.Status==cseg.CurveSegmentOK
  [atlas cseg] = atlas.merge(cseg);
end
if isempty(atlas.boundary)
  chart = cseg.ptlist{end};
  chart.pt_type    = 'EP';
  chart.ep_flag    = 1;
  cseg.ptlist{end} = chart;
end
[prob atlas cseg] = atlas.flush@AtlasBase(prob, cseg);
if any(cseg.Status==[cseg.CurveSegmentOK cseg.BoundaryPoint])
  if isempty(atlas.boundary) || (atlas.next_pt>atlas.cont.PtMX)
    cseg.Status = cseg.BoundaryPoint;
  else
    cseg.Status = cseg.CurveSegmentOK;
  end
end

end
