sequence.cpp

Go to the documentation of this file.
00001 #ifndef SEQUENCE_IMPLEMENTATION
00002 #define SEQUENCE_IMPLEMENTATION
00003 
00004 /*****************************************************************************\
00005 *                                                                             *
00006 *  Name   : sequence                                                          *
00007 *  Author : Chris Koeritz                                                     *
00008 *                                                                             *
00009 *******************************************************************************
00010 * Copyright (c) 1989-$now By Author.  This program is free software; you can  *
00011 * redistribute it and/or modify it under the terms of the GNU General Public  *
00012 * License as published by the Free Software Foundation; either version 2 of   *
00013 * the License or (at your option) any later version.  This is online at:      *
00014 *     http://www.fsf.org/copyleft/gpl.html                                    *
00015 * Please send any updates to: fred@gruntose.com                               *
00016 \*****************************************************************************/
00017 
00018 #include "array.cpp"
00019 #include "function.h"
00020 #include "guards.h"
00021 #include "sequence.h"
00022 
00023 template <class contents>
00024 int sequence<contents>::comparator(const sequence &s2) const
00025 {
00026   if (!this->length() && !s2.length()) return 0;
00027   for (int i = 0; i < minimum(this->length(), s2.length()); i++) {
00028     if (this->get(i) < s2.get(i)) return -1;
00029     else if (s2.get(i) < this->get(i)) return 1;
00030   }
00031   if (this->length() < s2.length()) return -1;
00032   else if (this->length() > s2.length()) return 1;
00033   else return 0;
00034 }
00035 
00036 template <class contents>
00037 int sequence<contents>::find(const contents &to_find, int &position,
00038     outcome &outcome) const
00039 {
00040   outcome = common::OUT_OF_RANGE;
00041   bounds_return(position, 0, this->last(), 0);
00042   for (int i = position; i < this->length(); i++)
00043     if (this->get(i) == to_find) {
00044       position = i;
00045       outcome = common::OKAY;
00046       return i;
00047     }
00048   outcome = common::NOT_FOUND;
00049   return 0;
00050 }
00051 
00052 template <class contents>
00053 int sequence<contents>::find(const sequence &to_find, int &position,
00054     outcome &outcome) const
00055 {
00056   outcome = common::OUT_OF_RANGE;
00057   bounds_return(position, 0, this->length() - to_find.length(), 0);
00058 //hmmm: there is a better way to do this loop in terms of the number of
00059 //      comparisons performed!  knuth morris pratt algorithm?
00060   for (int i = position; i < this->length() - to_find.length() + 1; i++) {
00061     sequence<contents> chopped = this->subarray(i, i + to_find.last());
00062     if (chopped == to_find) {
00063       position = i;
00064       outcome = common::OKAY;
00065       return i;
00066     }
00067   }
00068   outcome = common::NOT_FOUND;
00069   return 0;
00070 }
00071 
00072 #endif
00073 

Generated on Fri Sep 5 04:28:37 2008 for HOOPLE Libraries by  doxygen 1.5.1