% wCount = bagEvents(eventList, episode)
%
% eventList : List of events in order (no occurrence times).
% episode : List of the event types in the episode, no copies.
% wCount : Number of found episodes in the event list.
function wCount = bagEvents(eventList, episode)
   wCount = 0;
   start = 1;
   epLength = length(episode);
   occs = zeros(1, epLength);
   type = 0;
   
   for i = 1:eventList,
      % pointerTo is a question-mark function,
      % find gives a default implementation
      type = pointerTo(eventList(i), episode);
      if(type > 0)
         timeToLastOcc = i - occs(type);
         timeToStart = i - start;
         if( timeToStart < timeToLastOcc )
            if(timeToStart == epLength-1)
               wCount = wCount + 1;
               start = start + 1;
            end
         else
            start = start + 1;
         end
         occs(type) = i;
      else
         start = i+1;
      end
   end
end
